-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Do not suggest adding semicolon/changing delimiters for macros in item position that originates in macros #97377
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// force-host | ||
// no-prefer-dynamic | ||
|
||
#![crate_type = "proc-macro"] | ||
|
||
extern crate proc_macro; | ||
|
||
use proc_macro::TokenStream; | ||
|
||
fn compile_error() -> TokenStream { | ||
r#"compile_error!("")"#.parse().unwrap() | ||
} | ||
|
||
#[proc_macro_derive(MyTrait)] | ||
pub fn derive(input: TokenStream) -> TokenStream { | ||
compile_error() | ||
} | ||
#[proc_macro_attribute] | ||
pub fn attribute_macro(_attr: TokenStream, mut input: TokenStream) -> TokenStream { | ||
input.extend(compile_error()); | ||
input | ||
} | ||
#[proc_macro] | ||
pub fn fn_macro(_item: TokenStream) -> TokenStream { | ||
compile_error() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// aux-build: issue-91800-macro.rs | ||
|
||
#[macro_use] | ||
extern crate issue_91800_macro; | ||
|
||
#[derive(MyTrait)] | ||
//~^ ERROR macros that expand to items must be delimited with braces or followed by a semicolon | ||
//~| ERROR proc-macro derive produced unparseable tokens | ||
#[attribute_macro] | ||
//~^ ERROR macros that expand to items must be delimited with braces or followed by a semicolon | ||
struct MyStruct; | ||
|
||
fn_macro! {} | ||
//~^ ERROR macros that expand to items must be delimited with braces or followed by a semicolon | ||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
error: macros that expand to items must be delimited with braces or followed by a semicolon | ||
--> $DIR/issue-91800.rs:6:10 | ||
| | ||
LL | #[derive(MyTrait)] | ||
| ^^^^^^^ | ||
| | ||
= note: this error originates in the derive macro `MyTrait` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: proc-macro derive produced unparseable tokens | ||
--> $DIR/issue-91800.rs:6:10 | ||
| | ||
LL | #[derive(MyTrait)] | ||
| ^^^^^^^ | ||
|
||
error: | ||
estebank marked this conversation as resolved.
Show resolved
Hide resolved
|
||
--> $DIR/issue-91800.rs:6:10 | ||
| | ||
LL | #[derive(MyTrait)] | ||
| ^^^^^^^ | ||
| | ||
= note: this error originates in the derive macro `MyTrait` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: macros that expand to items must be delimited with braces or followed by a semicolon | ||
--> $DIR/issue-91800.rs:9:1 | ||
| | ||
LL | #[attribute_macro] | ||
| ^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this error originates in the attribute macro `attribute_macro` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: | ||
--> $DIR/issue-91800.rs:9:1 | ||
| | ||
LL | #[attribute_macro] | ||
| ^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this error originates in the attribute macro `attribute_macro` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: macros that expand to items must be delimited with braces or followed by a semicolon | ||
--> $DIR/issue-91800.rs:13:1 | ||
| | ||
LL | fn_macro! {} | ||
| ^^^^^^^^^^^^ | ||
| | ||
= note: this error originates in the macro `fn_macro` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: | ||
--> $DIR/issue-91800.rs:13:1 | ||
| | ||
LL | fn_macro! {} | ||
| ^^^^^^^^^^^^ | ||
| | ||
= note: this error originates in the macro `fn_macro` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: aborting due to 7 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What happens if you instead use the following? Is the FIXME still applicable then? I suspect it might not be.
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.
That doesn't work.
can_be_used_for_suggestions()
only checks for derive macros where the span they emit is different from the span of themselves. This will both provide incorrect suggestions for those macros and still fail as with the FIXME.rust/compiler/rustc_span/src/lib.rs
Lines 575 to 583 in 49c82f3
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.
What about using
in_derive_expansion
and only gate on those, then?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've thought about that, but attribute macros should not suggest help either; and probably neither should declarative macros from separate crates.
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.
Could you then checks for
!matches!(kind, ExprKind::Macro(MacroKind::Derive | MacroKind::Attr, _))
and usingtcx.sess().source_map().lookup_char_pos(span.lo()).file
on both the suggestion span and the callsite span to see if they belong to the same file?Also, an additional test for those cases so that we see when the suggestion should appear in the same file might be useful. That might be easy to do for a macro by example. You might need to expand the test to also have an "external crate" example to check for that case too.
Would you have time to get these things done in this PR?
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.
Soon. But should it be the same file or same crate?
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.
And what about function-like proc macros? How can I differentiate those?
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.
Ah, you're right. @petrochenkov what would the best way of checking that a span doesn't correspond to a proc-macro of any type nor a macro by example from a foreign crate, while parsing an item?
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.
@petrochenkov friendly ping, if you have advice re: ^
I'm ok with landing with
if !span.from_expansion() {
, at least for now. Would you mind rebasing @ChayimFriedman2?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.
@estebank ^^^ Done.