1
Fork 0

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.(https://github.com/rust-lang/rust/issues/114636#issuecomment-1670228715). Furthermore, if you think https://github.com/rust-lang/rust/issues/114636#issuecomment-1670228715 is a better solution, feel free to close this PR.
This commit is contained in:
Matthias Krüger 2023-08-30 07:18:10 +02:00 committed by GitHub
commit 639116505a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 52 deletions

View file

@ -1851,21 +1851,11 @@ impl<'a> Parser<'a> {
attrs: AttrVec, attrs: AttrVec,
) -> PResult<'a, FieldDef> { ) -> PResult<'a, FieldDef> {
let name = self.parse_field_ident(adt_ty, lo)?; let name = self.parse_field_ident(adt_ty, lo)?;
// Parse the macro invocation and recover
if self.token.kind == token::Not { if self.token.kind == token::Not {
if let Err(mut err) = self.unexpected::<FieldDef>() { if let Err(mut err) = self.unexpected::<FieldDef>() {
err.subdiagnostic(MacroExpandsToAdtField { adt_ty }).emit(); // Encounter the macro invocation
self.bump(); err.subdiagnostic(MacroExpandsToAdtField { adt_ty });
self.parse_delim_args()?; return Err(err);
return Ok(FieldDef {
span: DUMMY_SP,
ident: None,
vis,
id: DUMMY_NODE_ID,
ty: self.mk_ty(DUMMY_SP, TyKind::Err),
attrs,
is_placeholder: false,
});
} }
} }
self.expect_field_ty_separator()?; self.expect_field_ty_separator()?;

View file

@ -1,5 +1,7 @@
// compile-flags: --crate-type=lib // compile-flags: --crate-type=lib
// https://github.com/rust-lang/rust/issues/113766
macro_rules! field { macro_rules! field {
($name:ident:$type:ty) => { ($name:ident:$type:ty) => {
$name:$type $name:$type
@ -13,15 +15,14 @@ macro_rules! variant {
} }
struct Struct { struct Struct {
//~^ NOTE while parsing this struct
field!(bar:u128), field!(bar:u128),
//~^ NOTE macros cannot expand to struct fields //~^ NOTE macros cannot expand to struct fields
//~| ERROR unexpected token: `!` //~| ERROR unexpected token: `!`
//~| NOTE unexpected token after this //~| NOTE unexpected token after this
a: u32, a: u32,
b: u32, b: u32,
field!(recovers:()), //~ NOTE macros cannot expand to struct fields field!(recovers:()),
//~^ ERROR unexpected token: `!`
//~^^ NOTE unexpected token after this
} }
enum EnumVariant { enum EnumVariant {
@ -35,7 +36,7 @@ enum EnumVariant {
//~^ NOTE macros cannot expand to enum variants //~^ NOTE macros cannot expand to enum variants
//~| ERROR unexpected token: `!` //~| ERROR unexpected token: `!`
//~| NOTE unexpected token after this //~| NOTE unexpected token after this
Data { Data { //~ NOTE while parsing this struct
field!(x:u32), field!(x:u32),
//~^ NOTE macros cannot expand to struct fields //~^ NOTE macros cannot expand to struct fields
//~| ERROR unexpected token: `!` //~| ERROR unexpected token: `!`
@ -44,27 +45,35 @@ enum EnumVariant {
} }
enum EnumVariantField { enum EnumVariantField {
Named { Named { //~ NOTE while parsing this struct
field!(oopsies:()), field!(oopsies:()),
//~^ NOTE macros cannot expand to struct fields //~^ NOTE macros cannot expand to struct fields
//~| ERROR unexpected token: `!` //~| ERROR unexpected token: `!`
//~| unexpected token after this //~| unexpected token after this
field!(oopsies2:()), field!(oopsies2:()),
//~^ NOTE macros cannot expand to struct fields
//~| ERROR unexpected token: `!`
//~| unexpected token after this
}, },
} }
union Union { union Union {
//~^ NOTE while parsing this union
A: u32, A: u32,
field!(oopsies:()), field!(oopsies:()),
//~^ NOTE macros cannot expand to union fields //~^ NOTE macros cannot expand to union fields
//~| ERROR unexpected token: `!` //~| ERROR unexpected token: `!`
//~| unexpected token after this //~| NOTE unexpected token after this
B: u32, B: u32,
field!(recovers:()), field!(recovers:()),
//~^ NOTE macros cannot expand to union fields
//~| ERROR unexpected token: `!`
//~| unexpected token after this
} }
// https://github.com/rust-lang/rust/issues/114636
#[derive(Debug)]
pub struct Lazy {
//~^ NOTE while parsing this struct
unreachable!()
//~^ NOTE macros cannot expand to struct fields
//~| ERROR unexpected token: `!`
//~| NOTE unexpected token after this
}
fn main() {}

View file

@ -1,21 +1,16 @@
error: unexpected token: `!` error: unexpected token: `!`
--> $DIR/macro-expand-to-field.rs:16:10 --> $DIR/macro-expand-to-field.rs:19:10
| |
LL | struct Struct {
| ------ while parsing this struct
LL |
LL | field!(bar:u128), LL | field!(bar:u128),
| ^ unexpected token after this | ^ unexpected token after this
| |
= note: macros cannot expand to struct fields = note: macros cannot expand to struct fields
error: unexpected token: `!` error: unexpected token: `!`
--> $DIR/macro-expand-to-field.rs:22:10 --> $DIR/macro-expand-to-field.rs:29:12
|
LL | field!(recovers:()),
| ^ unexpected token after this
|
= note: macros cannot expand to struct fields
error: unexpected token: `!`
--> $DIR/macro-expand-to-field.rs:28:12
| |
LL | variant!(whoops), LL | variant!(whoops),
| ^ unexpected token after this | ^ unexpected token after this
@ -23,7 +18,7 @@ LL | variant!(whoops),
= note: macros cannot expand to enum variants = note: macros cannot expand to enum variants
error: unexpected token: `!` error: unexpected token: `!`
--> $DIR/macro-expand-to-field.rs:34:12 --> $DIR/macro-expand-to-field.rs:35:12
| |
LL | variant!(recovers), LL | variant!(recovers),
| ^ unexpected token after this | ^ unexpected token after this
@ -31,44 +26,46 @@ LL | variant!(recovers),
= note: macros cannot expand to enum variants = note: macros cannot expand to enum variants
error: unexpected token: `!` error: unexpected token: `!`
--> $DIR/macro-expand-to-field.rs:39:14 --> $DIR/macro-expand-to-field.rs:40:14
| |
LL | Data {
| ---- while parsing this struct
LL | field!(x:u32), LL | field!(x:u32),
| ^ unexpected token after this | ^ unexpected token after this
| |
= note: macros cannot expand to struct fields = note: macros cannot expand to struct fields
error: unexpected token: `!` error: unexpected token: `!`
--> $DIR/macro-expand-to-field.rs:48:14 --> $DIR/macro-expand-to-field.rs:49:14
| |
LL | Named {
| ----- while parsing this struct
LL | field!(oopsies:()), LL | field!(oopsies:()),
| ^ unexpected token after this | ^ unexpected token after this
| |
= note: macros cannot expand to struct fields = note: macros cannot expand to struct fields
error: unexpected token: `!` error: unexpected token: `!`
--> $DIR/macro-expand-to-field.rs:52:14 --> $DIR/macro-expand-to-field.rs:60:10
|
LL | field!(oopsies2:()),
| ^ unexpected token after this
|
= note: macros cannot expand to struct fields
error: unexpected token: `!`
--> $DIR/macro-expand-to-field.rs:61:10
| |
LL | union Union {
| ----- while parsing this union
...
LL | field!(oopsies:()), LL | field!(oopsies:()),
| ^ unexpected token after this | ^ unexpected token after this
| |
= note: macros cannot expand to union fields = note: macros cannot expand to union fields
error: unexpected token: `!` error: unexpected token: `!`
--> $DIR/macro-expand-to-field.rs:66:10 --> $DIR/macro-expand-to-field.rs:73:16
| |
LL | field!(recovers:()), LL | pub struct Lazy {
| ^ unexpected token after this | ---- while parsing this struct
LL |
LL | unreachable!()
| ^ unexpected token after this
| |
= note: macros cannot expand to union fields = note: macros cannot expand to struct fields
error: aborting due to 9 previous errors error: aborting due to 7 previous errors