Skip to content

Commit 0ef4098

Browse files
Do not suggest adding semicolon/changing delimiters for macros in item position that originates in macros
1 parent b31f9cc commit 0ef4098

File tree

4 files changed

+123
-21
lines changed

4 files changed

+123
-21
lines changed

compiler/rustc_parse/src/parser/item.rs

+25-21
Original file line numberDiff line numberDiff line change
@@ -1775,30 +1775,34 @@ impl<'a> Parser<'a> {
17751775
span,
17761776
"macros that expand to items must be delimited with braces or followed by a semicolon",
17771777
);
1778-
if self.unclosed_delims.is_empty() {
1779-
let DelimSpan { open, close } = match args {
1780-
MacArgs::Empty | MacArgs::Eq(..) => unreachable!(),
1781-
MacArgs::Delimited(dspan, ..) => *dspan,
1782-
};
1783-
err.multipart_suggestion(
1784-
"change the delimiters to curly braces",
1785-
vec![(open, "{".to_string()), (close, '}'.to_string())],
1786-
Applicability::MaybeIncorrect,
1787-
);
1788-
} else {
1778+
// FIXME: This will make us not emit the help even for declarative
1779+
// macros within the same crate (that we can fix), which is sad.
1780+
if !span.from_expansion() {
1781+
if self.unclosed_delims.is_empty() {
1782+
let DelimSpan { open, close } = match args {
1783+
MacArgs::Empty | MacArgs::Eq(..) => unreachable!(),
1784+
MacArgs::Delimited(dspan, ..) => *dspan,
1785+
};
1786+
err.multipart_suggestion(
1787+
"change the delimiters to curly braces",
1788+
vec![(open, "{".to_string()), (close, '}'.to_string())],
1789+
Applicability::MaybeIncorrect,
1790+
);
1791+
} else {
1792+
err.span_suggestion(
1793+
span,
1794+
"change the delimiters to curly braces",
1795+
" { /* items */ }",
1796+
Applicability::HasPlaceholders,
1797+
);
1798+
}
17891799
err.span_suggestion(
1790-
span,
1791-
"change the delimiters to curly braces",
1792-
" { /* items */ }",
1793-
Applicability::HasPlaceholders,
1800+
span.shrink_to_hi(),
1801+
"add a semicolon",
1802+
';',
1803+
Applicability::MaybeIncorrect,
17941804
);
17951805
}
1796-
err.span_suggestion(
1797-
span.shrink_to_hi(),
1798-
"add a semicolon",
1799-
';',
1800-
Applicability::MaybeIncorrect,
1801-
);
18021806
err.emit();
18031807
}
18041808

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// force-host
2+
// no-prefer-dynamic
3+
4+
#![crate_type = "proc-macro"]
5+
6+
extern crate proc_macro;
7+
8+
use proc_macro::TokenStream;
9+
10+
fn compile_error() -> TokenStream {
11+
r#"compile_error!("")"#.parse().unwrap()
12+
}
13+
14+
#[proc_macro_derive(MyTrait)]
15+
pub fn derive(input: TokenStream) -> TokenStream {
16+
compile_error()
17+
}
18+
#[proc_macro_attribute]
19+
pub fn attribute_macro(_attr: TokenStream, mut input: TokenStream) -> TokenStream {
20+
input.extend(compile_error());
21+
input
22+
}
23+
#[proc_macro]
24+
pub fn fn_macro(_item: TokenStream) -> TokenStream {
25+
compile_error()
26+
}

src/test/ui/proc-macro/issue-91800.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// aux-build: issue-91800-macro.rs
2+
3+
#[macro_use]
4+
extern crate issue_91800_macro;
5+
6+
#[derive(MyTrait)]
7+
//~^ ERROR macros that expand to items must be delimited with braces or followed by a semicolon
8+
//~| ERROR proc-macro derive produced unparseable tokens
9+
#[attribute_macro]
10+
//~^ ERROR macros that expand to items must be delimited with braces or followed by a semicolon
11+
struct MyStruct;
12+
13+
fn_macro! {}
14+
//~^ ERROR macros that expand to items must be delimited with braces or followed by a semicolon
15+
16+
fn main() {}
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
error: macros that expand to items must be delimited with braces or followed by a semicolon
2+
--> $DIR/issue-91800.rs:6:10
3+
|
4+
LL | #[derive(MyTrait)]
5+
| ^^^^^^^
6+
|
7+
= note: this error originates in the derive macro `MyTrait` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error: proc-macro derive produced unparseable tokens
10+
--> $DIR/issue-91800.rs:6:10
11+
|
12+
LL | #[derive(MyTrait)]
13+
| ^^^^^^^
14+
15+
error:
16+
--> $DIR/issue-91800.rs:6:10
17+
|
18+
LL | #[derive(MyTrait)]
19+
| ^^^^^^^
20+
|
21+
= note: this error originates in the derive macro `MyTrait` (in Nightly builds, run with -Z macro-backtrace for more info)
22+
23+
error: macros that expand to items must be delimited with braces or followed by a semicolon
24+
--> $DIR/issue-91800.rs:9:1
25+
|
26+
LL | #[attribute_macro]
27+
| ^^^^^^^^^^^^^^^^^^
28+
|
29+
= note: this error originates in the attribute macro `attribute_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
30+
31+
error:
32+
--> $DIR/issue-91800.rs:9:1
33+
|
34+
LL | #[attribute_macro]
35+
| ^^^^^^^^^^^^^^^^^^
36+
|
37+
= note: this error originates in the attribute macro `attribute_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
38+
39+
error: macros that expand to items must be delimited with braces or followed by a semicolon
40+
--> $DIR/issue-91800.rs:13:1
41+
|
42+
LL | fn_macro! {}
43+
| ^^^^^^^^^^^^
44+
|
45+
= note: this error originates in the macro `fn_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
46+
47+
error:
48+
--> $DIR/issue-91800.rs:13:1
49+
|
50+
LL | fn_macro! {}
51+
| ^^^^^^^^^^^^
52+
|
53+
= note: this error originates in the macro `fn_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
54+
55+
error: aborting due to 7 previous errors
56+

0 commit comments

Comments
 (0)