Skip to content
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

parser: not insert dummy field in struct #114704

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 3 additions & 13 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1851,21 +1851,11 @@ impl<'a> Parser<'a> {
attrs: AttrVec,
) -> PResult<'a, FieldDef> {
let name = self.parse_field_ident(adt_ty, lo)?;
// Parse the macro invocation and recover
if self.token.kind == token::Not {
if let Err(mut err) = self.unexpected::<FieldDef>() {
err.subdiagnostic(MacroExpandsToAdtField { adt_ty }).emit();
self.bump();
self.parse_delim_args()?;
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,
});
// Encounter the macro invocation
err.subdiagnostic(MacroExpandsToAdtField { adt_ty });
return Err(err);
}
}
self.expect_field_ty_separator()?;
Expand Down
31 changes: 20 additions & 11 deletions tests/ui/parser/macro/macro-expand-to-field.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// compile-flags: --crate-type=lib

// https://github.com/rust-lang/rust/issues/113766

macro_rules! field {
($name:ident:$type:ty) => {
$name:$type
Expand All @@ -13,15 +15,14 @@ macro_rules! variant {
}

struct Struct {
//~^ NOTE while parsing this struct
field!(bar:u128),
//~^ NOTE macros cannot expand to struct fields
//~| ERROR unexpected token: `!`
//~| NOTE unexpected token after this
a: u32,
b: u32,
field!(recovers:()), //~ NOTE macros cannot expand to struct fields
//~^ ERROR unexpected token: `!`
//~^^ NOTE unexpected token after this
field!(recovers:()),
}

enum EnumVariant {
Expand All @@ -35,7 +36,7 @@ enum EnumVariant {
//~^ NOTE macros cannot expand to enum variants
//~| ERROR unexpected token: `!`
//~| NOTE unexpected token after this
Data {
Data { //~ NOTE while parsing this struct
field!(x:u32),
//~^ NOTE macros cannot expand to struct fields
//~| ERROR unexpected token: `!`
Expand All @@ -44,27 +45,35 @@ enum EnumVariant {
}

enum EnumVariantField {
Named {
Named { //~ NOTE while parsing this struct
field!(oopsies:()),
//~^ NOTE macros cannot expand to struct fields
//~| ERROR unexpected token: `!`
//~| unexpected token after this
field!(oopsies2:()),
//~^ NOTE macros cannot expand to struct fields
//~| ERROR unexpected token: `!`
//~| unexpected token after this
},
}

union Union {
//~^ NOTE while parsing this union
A: u32,
field!(oopsies:()),
//~^ NOTE macros cannot expand to union fields
//~| ERROR unexpected token: `!`
//~| unexpected token after this
//~| NOTE unexpected token after this
B: u32,
field!(recovers:()),
//~^ NOTE macros cannot expand to union fields
}

// 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: `!`
//~| unexpected token after this
//~| NOTE unexpected token after this
}

fn main() {}
51 changes: 24 additions & 27 deletions tests/ui/parser/macro/macro-expand-to-field.stderr
Original file line number Diff line number Diff line change
@@ -1,74 +1,71 @@
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),
| ^ unexpected token after this
|
= note: macros cannot expand to struct fields

error: unexpected token: `!`
--> $DIR/macro-expand-to-field.rs:22:10
|
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
--> $DIR/macro-expand-to-field.rs:29:12
|
LL | variant!(whoops),
| ^ unexpected token after this
|
= note: macros cannot expand to enum variants

error: unexpected token: `!`
--> $DIR/macro-expand-to-field.rs:34:12
--> $DIR/macro-expand-to-field.rs:35:12
|
LL | variant!(recovers),
| ^ unexpected token after this
|
= note: macros cannot expand to enum variants

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),
| ^ unexpected token after this
|
= note: macros cannot expand to struct fields

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:()),
| ^ unexpected token after this
|
= note: macros cannot expand to struct fields

error: unexpected token: `!`
--> $DIR/macro-expand-to-field.rs:52:14
|
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
--> $DIR/macro-expand-to-field.rs:60:10
|
LL | union Union {
| ----- while parsing this union
...
LL | field!(oopsies:()),
| ^ unexpected token after this
|
= note: macros cannot expand to union fields

error: unexpected token: `!`
--> $DIR/macro-expand-to-field.rs:66:10
--> $DIR/macro-expand-to-field.rs:73:16
|
LL | field!(recovers:()),
| ^ unexpected token after this
LL | pub struct Lazy {
| ---- 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