Repository navigation
Conversation
|
r? @davidtwco rustbot has assigned @davidtwco. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
|
||
| fn main() { | ||
| let a = a!(); | ||
| a!() |
There was a problem hiding this comment.
The trailing a!() is sort of unnecessary but my first fix didn't handle this case properly, so I added it to the same test. I can remove this or move it to a separate test if necessary
| ); | ||
|
|
||
| if parser.token == token::Semi { | ||
| if !is_trailing_mac { |
There was a problem hiding this comment.
rust/compiler/rustc_expand/src/expand.rs
Lines 2149 to 2153 in d1a0517
if there is nest macro, the trailing_mac may be wrong. for example, inner will inherit the outer's is_trailing_mac(true), and make it add ;, it may be a regression. We should make is_trailing_mac correct for nested macro when constructing expansion data.🤔
macro_rules! inner {
() => {;};
}
macro_rules! outer {
() => {{ let _ = inner!(); }};
}
fn main() {
outer!()
}
There was a problem hiding this comment.
Right, this also fails for the other diagnostic that is_trailing_mac is responsible for, like in
macro_rules! inner {
() => { true; };
}
macro_rules! outer {
() => {{ let _ = inner!(); }};
}
fn main() {
outer!()
}which will result in the wrong note
note: macro invocations at the end of a block are treated as expressions
note: to ignore the value produced by the macro, add a semicolon after the invocation of `inner`
|
Reminder, once the PR becomes ready for a review, use |
| self.cx.current_expansion.is_trailing_mac &= match &fragment { | ||
| AstFragment::Expr(expr) | AstFragment::OptExpr(Some(expr)) => { | ||
| matches!(expr.kind, ExprKind::MacCall(..)) | ||
| } | ||
| _ => false, | ||
| }; |
There was a problem hiding this comment.
I don't know if this is the right way to fix this. The reason we can't just set is_trailing_mac to false is that in some cases, we have to inherit the is_trailing_mac from the parent so that a directly nested macro like
macro_rules! inner {
() => { true; };
}
macro_rules! outer {
() => { inner!() };
}
fn foo() -> bool {
outer!()
}is still treated as a trailing macro.
(I added a test for this, although its results are the same on main and on this branch, see tests/ui/lint/semicolon-in-expressions-from-macros/semicolon-in-expression-from-macros-nested-2.rs)
However, in the current logic, is_trailing_mac was basically always true for nested macros where the outer one is trailing, so that wasn't right either.
This fix is sort of the dumbest logic that solves the problem ...
7b3dd04 to
48b7c79
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
I dont have the right to review. r? compiler |
48b7c79 to
a8cce9c
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
Oh sorry, didn't realize. I rebased this after the changes in #161035 |
This comment has been minimized.
This comment has been minimized.
This was previously `true` for any macro called from within a trailing
outer macro. This fix checks explicitly if the outer macro expands
directly to the inner one, i.e. `macro_rules! outer => { { inner!() } }`
in which case the `is_trailing_mac` state is preserved, otherwise it is
reset.
a8cce9c to
cb9ac7f
Compare
Closes #164087