-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Fix argument removal suggestion around macros #112500
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
Conversation
(rustbot has picked a reviewer for you, use r? to override) |
| ^^^^^ ----- help: remove the extra argument | ||
| | | ||
| unexpected argument of type `{integer}` | ||
| ^^^^^ - unexpected argument of type `{integer}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be able to suggest empty()
here. Should normalize_span
return an Result<Span, Span>
, Ok if successfully normalized, Err otherwise, instead of merging both cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK there is no way to go from the span of a macro argument
foo!(~, 1);
^ here
to the span of the metavariable
empty(1, $y);
^^ here
But it should be possible to suggest removing the other argument that is inside the macro, I'll look into that.
EDIT: What's actually happening here is the span of the argument gets "extended" to the empty span before the comma, which results in an empty span similar to the one from the bug report. This empty span then gets extended to just before the )
, so we end up with:
empty(1, $y);
│└─┬┘
│ │
│ help: remove the extra argument
unexpected argument of type `{integer}`
Note that the span doesn't include the 1
, this just isn't very visible in the stderr output.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggesting to remove the argument in the macro may be incorrect for this test if the macro definition is correct and the invocation is wrong: https://github.com/rust-lang/rust/blob/d1e79b1c74c0b68ef200b317de5aedb8790dc004/tests/ui/macros/issue-26094.rs#L1-L12
This especially applies to macros defined by dependencies.
Since the error already says unexpected argument of type `whatever`
and that should be enough to figure out a solution, I think it's probably better to just leave the suggestion off.
That's a surprising behaviour. I'm not sure the change to @petrochenkov has more knowledge than I do on macros. |
r? compiler |
Macro expansion isn't my strong suit. @cjgillot are you comfortable enough to take review here, or should I reroll? |
r? petrochenkov |
|
and use it for function argument diagnostics
@bors r+ |
☀️ Test successful - checks-actions |
1 similar comment
☀️ Test successful - checks-actions |
Finished benchmarking commit (c94cb83): comparison URL. Overall result: ❌ regressions - no action needed@rustbot label: -perf-regression Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)This benchmark run did not return any relevant results for this metric. CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 635.223s -> 634.344s (-0.14%) |
Fixes #112437.
Fixes #113866.
Helps with #114255.
The issue was that
span.find_ancestor_inside(outer)
could previously return a span with a different expansion context fromouter
.This happens for example for the built-in macro
panic!
, which expands to another macro call ofpanic_2021!
orpanic_2015!
. Because the call site ofpanic_20xx!
has not associated source code, its span currently points to the call site ofpanic!
instead.Something similar also happens items that get desugared in AST->HIR lowering. For example,
for
loops get two spans: One "inner" span that has the.desugaring_kind()
kind set toDesugaringKind::ForLoop
and one "outer" span that does not. Similar to the macro situation, both of these spans point to the same source code, but have different expansion contexts.This causes problems, because joining two spans with different expansion contexts will usually1 not actually join them together to avoid creating "spaghetti" spans that go from the macro definition to the macro call. For example, in the following snippet
full_span
might not actually contain theadjusted_start
andadjusted_end
. This caused the broken suggestion / debug ICE in the linked issues.To fix the issue, this PR introduces a new method,
find_ancestor_inside_same_ctxt
, which combines the functionality offind_ancestor_inside
andfind_ancestor_in_same_ctxt
: It finds an ancestor span that is contained within the parent and has the same syntax context, and is therefore safe to extend. This new method should probably be used everywhere, where the returned span is extended, but for now it is just used for the argument removal suggestion.Additionally, this PR fixes a second issue where the function call itself is inside a macro but the arguments come from outside the macro. The test is added in the first commit to include stderr diff, so this is best reviewed commit by commit.
Footnotes
If one expansion context is the root context and the other is not. ↩