Skip to content

Commit 6391165

Browse files
authored
Rollup merge of #114704 - bvanjoi:fix-114636, r=compiler-errors
parser: not insert dummy field in struct Fixes #114636 This PR eliminates the dummy field, initially introduced in #113999, thereby enabling unrestricted use of `ident.unwrap()`. A side effect of this action is that we can only report the error of the first macro invocation field within the struct node. An alternative solution might be giving a virtual name to the macro, but it appears more complex.(#114636 (comment)). Furthermore, if you think #114636 (comment) is a better solution, feel free to close this PR.
2 parents dafea5f + 3ed435f commit 6391165

File tree

3 files changed

+47
-51
lines changed

3 files changed

+47
-51
lines changed

compiler/rustc_parse/src/parser/item.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -1851,21 +1851,11 @@ impl<'a> Parser<'a> {
18511851
attrs: AttrVec,
18521852
) -> PResult<'a, FieldDef> {
18531853
let name = self.parse_field_ident(adt_ty, lo)?;
1854-
// Parse the macro invocation and recover
18551854
if self.token.kind == token::Not {
18561855
if let Err(mut err) = self.unexpected::<FieldDef>() {
1857-
err.subdiagnostic(MacroExpandsToAdtField { adt_ty }).emit();
1858-
self.bump();
1859-
self.parse_delim_args()?;
1860-
return Ok(FieldDef {
1861-
span: DUMMY_SP,
1862-
ident: None,
1863-
vis,
1864-
id: DUMMY_NODE_ID,
1865-
ty: self.mk_ty(DUMMY_SP, TyKind::Err),
1866-
attrs,
1867-
is_placeholder: false,
1868-
});
1856+
// Encounter the macro invocation
1857+
err.subdiagnostic(MacroExpandsToAdtField { adt_ty });
1858+
return Err(err);
18691859
}
18701860
}
18711861
self.expect_field_ty_separator()?;

tests/ui/parser/macro/macro-expand-to-field.rs

+20-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// compile-flags: --crate-type=lib
22

3+
// https://github.com/rust-lang/rust/issues/113766
4+
35
macro_rules! field {
46
($name:ident:$type:ty) => {
57
$name:$type
@@ -13,15 +15,14 @@ macro_rules! variant {
1315
}
1416

1517
struct Struct {
18+
//~^ NOTE while parsing this struct
1619
field!(bar:u128),
1720
//~^ NOTE macros cannot expand to struct fields
1821
//~| ERROR unexpected token: `!`
1922
//~| NOTE unexpected token after this
2023
a: u32,
2124
b: u32,
22-
field!(recovers:()), //~ NOTE macros cannot expand to struct fields
23-
//~^ ERROR unexpected token: `!`
24-
//~^^ NOTE unexpected token after this
25+
field!(recovers:()),
2526
}
2627

2728
enum EnumVariant {
@@ -35,7 +36,7 @@ enum EnumVariant {
3536
//~^ NOTE macros cannot expand to enum variants
3637
//~| ERROR unexpected token: `!`
3738
//~| NOTE unexpected token after this
38-
Data {
39+
Data { //~ NOTE while parsing this struct
3940
field!(x:u32),
4041
//~^ NOTE macros cannot expand to struct fields
4142
//~| ERROR unexpected token: `!`
@@ -44,27 +45,35 @@ enum EnumVariant {
4445
}
4546

4647
enum EnumVariantField {
47-
Named {
48+
Named { //~ NOTE while parsing this struct
4849
field!(oopsies:()),
4950
//~^ NOTE macros cannot expand to struct fields
5051
//~| ERROR unexpected token: `!`
5152
//~| unexpected token after this
5253
field!(oopsies2:()),
53-
//~^ NOTE macros cannot expand to struct fields
54-
//~| ERROR unexpected token: `!`
55-
//~| unexpected token after this
5654
},
5755
}
5856

5957
union Union {
58+
//~^ NOTE while parsing this union
6059
A: u32,
6160
field!(oopsies:()),
6261
//~^ NOTE macros cannot expand to union fields
6362
//~| ERROR unexpected token: `!`
64-
//~| unexpected token after this
63+
//~| NOTE unexpected token after this
6564
B: u32,
6665
field!(recovers:()),
67-
//~^ NOTE macros cannot expand to union fields
66+
}
67+
68+
// https://github.com/rust-lang/rust/issues/114636
69+
70+
#[derive(Debug)]
71+
pub struct Lazy {
72+
//~^ NOTE while parsing this struct
73+
unreachable!()
74+
//~^ NOTE macros cannot expand to struct fields
6875
//~| ERROR unexpected token: `!`
69-
//~| unexpected token after this
76+
//~| NOTE unexpected token after this
7077
}
78+
79+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,71 @@
11
error: unexpected token: `!`
2-
--> $DIR/macro-expand-to-field.rs:16:10
2+
--> $DIR/macro-expand-to-field.rs:19:10
33
|
4+
LL | struct Struct {
5+
| ------ while parsing this struct
6+
LL |
47
LL | field!(bar:u128),
58
| ^ unexpected token after this
69
|
710
= note: macros cannot expand to struct fields
811

912
error: unexpected token: `!`
10-
--> $DIR/macro-expand-to-field.rs:22:10
11-
|
12-
LL | field!(recovers:()),
13-
| ^ unexpected token after this
14-
|
15-
= note: macros cannot expand to struct fields
16-
17-
error: unexpected token: `!`
18-
--> $DIR/macro-expand-to-field.rs:28:12
13+
--> $DIR/macro-expand-to-field.rs:29:12
1914
|
2015
LL | variant!(whoops),
2116
| ^ unexpected token after this
2217
|
2318
= note: macros cannot expand to enum variants
2419

2520
error: unexpected token: `!`
26-
--> $DIR/macro-expand-to-field.rs:34:12
21+
--> $DIR/macro-expand-to-field.rs:35:12
2722
|
2823
LL | variant!(recovers),
2924
| ^ unexpected token after this
3025
|
3126
= note: macros cannot expand to enum variants
3227

3328
error: unexpected token: `!`
34-
--> $DIR/macro-expand-to-field.rs:39:14
29+
--> $DIR/macro-expand-to-field.rs:40:14
3530
|
31+
LL | Data {
32+
| ---- while parsing this struct
3633
LL | field!(x:u32),
3734
| ^ unexpected token after this
3835
|
3936
= note: macros cannot expand to struct fields
4037

4138
error: unexpected token: `!`
42-
--> $DIR/macro-expand-to-field.rs:48:14
39+
--> $DIR/macro-expand-to-field.rs:49:14
4340
|
41+
LL | Named {
42+
| ----- while parsing this struct
4443
LL | field!(oopsies:()),
4544
| ^ unexpected token after this
4645
|
4746
= note: macros cannot expand to struct fields
4847

4948
error: unexpected token: `!`
50-
--> $DIR/macro-expand-to-field.rs:52:14
51-
|
52-
LL | field!(oopsies2:()),
53-
| ^ unexpected token after this
54-
|
55-
= note: macros cannot expand to struct fields
56-
57-
error: unexpected token: `!`
58-
--> $DIR/macro-expand-to-field.rs:61:10
49+
--> $DIR/macro-expand-to-field.rs:60:10
5950
|
51+
LL | union Union {
52+
| ----- while parsing this union
53+
...
6054
LL | field!(oopsies:()),
6155
| ^ unexpected token after this
6256
|
6357
= note: macros cannot expand to union fields
6458

6559
error: unexpected token: `!`
66-
--> $DIR/macro-expand-to-field.rs:66:10
60+
--> $DIR/macro-expand-to-field.rs:73:16
6761
|
68-
LL | field!(recovers:()),
69-
| ^ unexpected token after this
62+
LL | pub struct Lazy {
63+
| ---- while parsing this struct
64+
LL |
65+
LL | unreachable!()
66+
| ^ unexpected token after this
7067
|
71-
= note: macros cannot expand to union fields
68+
= note: macros cannot expand to struct fields
7269

73-
error: aborting due to 9 previous errors
70+
error: aborting due to 7 previous errors
7471

0 commit comments

Comments
 (0)