Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad suggestion dyn pub on proc macro (#61963 regressed) #74043

Open
SNCPlay42 opened this issue Jul 4, 2020 · 5 comments
Open

Bad suggestion dyn pub on proc macro (#61963 regressed) #74043

SNCPlay42 opened this issue Jul 4, 2020 · 5 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-proc-macros Area: Procedural macros A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@SNCPlay42
Copy link
Contributor

SNCPlay42 commented Jul 4, 2020

#72306 causes the compiler to emit (and blessed) this bad suggestion in the test for #61963 (output):

error: trait objects without an explicit `dyn` are deprecated
  --> $DIR/issue-61963.rs:18:1
   |
LL | pub struct Foo {
   | ^^^ help: use `dyn`: `dyn pub`

This is the behaviour the test is supposed to guard against (almost - the original bad suggestion was to insert dyn before the proc macro attribute), so the issue is regressed.

The issue was originally fixed nearly a year ago, so this is a regression from stable.

@jonas-schievink jonas-schievink added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-diagnostics Area: Messages for errors, warnings, and lints regression-from-stable-to-stable Performance or correctness regression from one stable version to another. labels Jul 4, 2020
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Jul 4, 2020
@LeSeulArtichaut
Copy link
Contributor

cc @Aaron1011 @davidtwco

@Aaron1011
Copy link
Member

I don't think this is actually an issue anymore. The original issue was caused by the compiler losing span information, causing tokens emitted by a proc-macro to end up with a span (the proc-macro attribute) that the author did not intend.

However, the test is explicitly setting the span of the token in question:

// Construct a dummy span - `#0 bytes(0..0)` - which is present in the input because
// of the specially crafted generated tokens in the `attribute-crate` proc-macro.
let dummy_span = input.clone().into_iter().nth(0).unwrap().span();

However, the first token in the stream no longer has a dummy span, so this proc-macro ends up explicitly spanning the trait object token with the span of the pub token.

I don't think this is a bug - when a proc-macro explicitly sets the span of a token, I don't think the compiler should be trying to second-guess that decision. There are legitimate use cases that involve causing an error to be displayed to be displayed in a unusual location - e.g. pointing to a field definition for a trait bound that the field type fails to meet.

We might want to alter the test documentation to better explain its current behavior (while still guarding for regressions to the original behavior).

@Aaron1011 Aaron1011 added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-proc-macros Area: Procedural macros labels Jul 4, 2020
@SNCPlay42
Copy link
Contributor Author

So this is different from the original situation in #61963? There was discussion there about whether the problem was a bug in the proc macros or the compiler.

There are legitimate use cases that involve causing an error to be displayed to be displayed in a unusual location - e.g. pointing to a field definition for a trait bound that the field type fails to meet.

This I understand, but should the compiler then emit suggestions that produce invalid syntax? They at least shouldn't be reported as MachineApplicable. This causes cargo fix to try to automatically apply the fix, and then fail to compile the result, which it says "likely indicates a bug in either rustc or cargo itself".

@Aaron1011
Copy link
Member

Aaron1011 commented Jul 5, 2020

So this is different from the original situation in #61963? There was discussion there about whether the problem was a bug in the proc macros or the compiler.

Yes - there is no longer any implicit respanning occuring, which was the root of the original issue.

This I understand, but should the compiler then emit suggestions that produce invalid syntax?

I think this is unsolvable in the general case - the span could point to a token Foo instead of pub, which could either be a trait Foo or a struct Foo.

However, I think it would be fine to add in some special cases (e.g. only suggest dyn <ident>, not dyn <keyword> if people are hitting this in real code.

I think the blame for the bad suggestion lies with the proc macro doing the explicit re-spanning.

@Dylan-DPC-zz Dylan-DPC-zz added P-medium Medium priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Jul 7, 2020
@Dylan-DPC-zz
Copy link

Marking it as p-medium based on the discussion

@estebank estebank added the D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. label Mar 15, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 16, 2023
Do not provide suggestions when the spans come from expanded code that doesn't point at user code

Hide invalid proc-macro suggestions and track spans
coming from proc-macros pointing at attribute.

Effectively, unless the proc-macro keeps user spans,
suggestions will not be produced for the code they
produce.

r? `@ghost`

Fix rust-lang#107113, fix rust-lang#107976, fix rust-lang#107977, fix rust-lang#108748, fix rust-lang#106720, fix rust-lang#90557.

Could potentially address rust-lang#50141, rust-lang#67373, rust-lang#55146, rust-lang#78862, rust-lang#74043, rust-lang#88514, rust-lang#83320, rust-lang#91520, rust-lang#104071. CC rust-lang#50122, rust-lang#76360.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 17, 2023
Do not provide suggestions when the spans come from expanded code that doesn't point at user code

Hide invalid proc-macro suggestions and track spans
coming from proc-macros pointing at attribute.

Effectively, unless the proc-macro keeps user spans,
suggestions will not be produced for the code they
produce.

r? ``@ghost``

Fix rust-lang#107113, fix rust-lang#107976, fix rust-lang#107977, fix rust-lang#108748, fix rust-lang#106720, fix rust-lang#90557.

Could potentially address rust-lang#50141, rust-lang#67373, rust-lang#55146, rust-lang#78862, rust-lang#74043, rust-lang#88514, rust-lang#83320, rust-lang#91520, rust-lang#104071. CC rust-lang#50122, rust-lang#76360.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-proc-macros Area: Procedural macros A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants