-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Unify TokResult
and ResultAnyMacro
#36669
Conversation
fn make_items(mut self: Box<Self>) -> Option<SmallVector<P<ast::Item>>> { | ||
if self.parser.sess.span_diagnostic.has_errors() { | ||
return Some(SmallVector::zero()); | ||
} |
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.
@nrc is there a reason we treat items specially here by bailing out early if there have been errors?
This PR currently doesn't bail out early if there have been errors, but if there's a reason for it I could bail out on all expansions or just on items like now.
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.
I think we should probably do this for all cases - this would happen if we can't parse what the proc macro has produced - in that case, there doesn't seem any point in including the expanded code only to get the same errors again (it is probably only in items since that is the only case where the parser errored out later, or possibly just because that is the only case I tested :-( )
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.
Hmm, I thought at this point we haven't parsed what the proc macro has produced yet.
Wouldn't we be bailing out here if there have been any errors for any reason -- even totally related to the macro in question?
if allow_semi && self.token == token::Semi { | ||
// We allow semicolons at the end of expressions -- e.g. the semicolon in | ||
// `macro_rules! m { () => { panic!(); } }` isn't parsed by `.parse_expr()`, | ||
// but `m!()` is allowed in expression positions (c.f. issue #34706). |
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.
I don't understand the motivation for this -- might be a good idea not to allow in macros 2.0.
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.
I think you get unexpected behaviour without this - something to do with expressions in statement context and having the same behaviour for macros as other expressions. Oh and allowing the same macro to be used in both expression and statement position (and possibly the weirdness about whether a macro is a block or not).
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.
If a macro author wants their macro to be usable in an expression context, couldn't they just not include the trailing semicolon? Afaik, the trailing semicolon isn't needed for the macro to be usable in a statement context.
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.
Also, ignoring the trailing semicolon creates unexpected (to me) behavior today -- for example, in this gist I would expect the expansion to have the unit type instead of i32
.
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.
(compare to this gist, which compiles)
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.
I amended this PR so that ProcMacro
and AttrProcMacro
do not allow trailing semicolons in expression positions.
@@ -6172,6 +6174,45 @@ impl<'a> Parser<'a> { | |||
} | |||
} | |||
|
|||
pub fn parse_expansion(&mut self, kind: ExpansionKind) -> PResult<'a, Expansion> { |
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.
I wonder if this might be better in ext somewhere rather than in the Parser. IIRC the parser is fairly ignorant of macro stuff at the moment but is kind of huge and hard to understand, I think it might serve us well to minimise adding macro stuff. What do you think?
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.
I agree that we should minimize adding stuff to the parser in general, but I think this is a simple and small enough function with similar enough structure to the other parse_*
functions to warrant being in parser.rs
.
It's not that important though -- I'd be OK moving it to expand.rs
.
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.
I'm kind of wary of this because it introduces new types to the parser module and it doesn't seem to need to be a method - it doesn't use any of Parser's private API afaict, so seems like it would be fine in expand.rs
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.
Makes sense -- I amended this PR to move parse_expansion
to expand.rs
.
☔ The latest upstream changes (presumably #36616) made this pull request unmergeable. Please resolve the merge conflicts. |
8efc852
to
53c5cb6
Compare
53c5cb6
to
b90cedd
Compare
@bors: r+ |
📌 Commit df0e4bf has been approved by |
Unify `TokResult` and `ResultAnyMacro` Fixes rust-lang#36641. r? @nrc
Unify `TokResult` and `ResultAnyMacro` Fixes rust-lang#36641. r? @nrc
Fixes #36641.
r? @nrc