From 287db04636ffefa3fdaa39fe0fdcc3cf75b60444 Mon Sep 17 00:00:00 2001 From: Catherine Date: Mon, 24 Jul 2023 04:55:47 +0000 Subject: [PATCH 1/3] Specify macro is invalid in certain contexts --- compiler/rustc_parse/messages.ftl | 6 +++ compiler/rustc_parse/src/errors.rs | 6 +++ .../rustc_parse/src/parser/diagnostics.rs | 37 +++++++++++-------- compiler/rustc_parse/src/parser/item.rs | 26 +++++++++---- compiler/rustc_parse/src/parser/pat.rs | 12 +++++- .../parser/macro/macro-expand-to-field-2.rs | 16 ++++++++ .../macro/macro-expand-to-field-2.stderr | 18 +++++++++ .../parser/macro/macro-expand-to-field-3.rs | 15 ++++++++ .../macro/macro-expand-to-field-3.stderr | 19 ++++++++++ .../ui/parser/macro/macro-expand-to-field.rs | 26 +++++++++++++ .../parser/macro/macro-expand-to-field.stderr | 29 +++++++++++++++ .../parser/macro/macro-expand-to-match-arm.rs | 15 ++++++++ .../macro/macro-expand-to-match-arm.stderr | 10 +++++ 13 files changed, 209 insertions(+), 26 deletions(-) create mode 100644 tests/ui/parser/macro/macro-expand-to-field-2.rs create mode 100644 tests/ui/parser/macro/macro-expand-to-field-2.stderr create mode 100644 tests/ui/parser/macro/macro-expand-to-field-3.rs create mode 100644 tests/ui/parser/macro/macro-expand-to-field-3.stderr create mode 100644 tests/ui/parser/macro/macro-expand-to-field.rs create mode 100644 tests/ui/parser/macro/macro-expand-to-field.stderr create mode 100644 tests/ui/parser/macro/macro-expand-to-match-arm.rs create mode 100644 tests/ui/parser/macro/macro-expand-to-match-arm.stderr diff --git a/compiler/rustc_parse/messages.ftl b/compiler/rustc_parse/messages.ftl index 9787d98c1a49a..7d71449122f70 100644 --- a/compiler/rustc_parse/messages.ftl +++ b/compiler/rustc_parse/messages.ftl @@ -457,6 +457,12 @@ parse_loop_else = `{$loop_kind}...else` loops are not supported .note = consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run .loop_keyword = `else` is attached to this loop +parse_macro_expands_to_adt_field = macros cannot expand to {$adt_ty} fields + +parse_macro_expands_to_enum_variant = macros cannot expand to enum variants + +parse_macro_expands_to_match_arm = macros cannot expand to match arms + parse_macro_invocation_visibility = can't qualify macro invocation with `pub` .suggestion = remove the visibility .help = try adjusting the macro to put `{$vis}` inside the invocation diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index 96e1c0e3c6d9e..7f209c63f4290 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -1800,6 +1800,12 @@ pub struct UnknownPrefix<'a> { pub sugg: Option, } +#[derive(Subdiagnostic)] +#[note(parse_macro_expands_to_adt_field)] +pub struct MacroExpandsToAdtField<'a> { + pub adt_ty: &'a str, +} + #[derive(Subdiagnostic)] pub enum UnknownPrefixSugg { #[suggestion( diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index c3cf6437afa07..3eed3ed9e1b50 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -2591,6 +2591,7 @@ impl<'a> Parser<'a> { pub(crate) fn maybe_recover_unexpected_comma( &mut self, lo: Span, + is_mac_invoc: bool, rt: CommaRecoveryMode, ) -> PResult<'a, ()> { if self.token != token::Comma { @@ -2611,24 +2612,28 @@ impl<'a> Parser<'a> { let seq_span = lo.to(self.prev_token.span); let mut err = self.struct_span_err(comma_span, "unexpected `,` in pattern"); if let Ok(seq_snippet) = self.span_to_snippet(seq_span) { - err.multipart_suggestion( - format!( - "try adding parentheses to match on a tuple{}", - if let CommaRecoveryMode::LikelyTuple = rt { "" } else { "..." }, - ), - vec![ - (seq_span.shrink_to_lo(), "(".to_string()), - (seq_span.shrink_to_hi(), ")".to_string()), - ], - Applicability::MachineApplicable, - ); - if let CommaRecoveryMode::EitherTupleOrPipe = rt { - err.span_suggestion( - seq_span, - "...or a vertical bar to match on multiple alternatives", - seq_snippet.replace(',', " |"), + if is_mac_invoc { + err.note(fluent::parse_macro_expands_to_match_arm); + } else { + err.multipart_suggestion( + format!( + "try adding parentheses to match on a tuple{}", + if let CommaRecoveryMode::LikelyTuple = rt { "" } else { "..." }, + ), + vec![ + (seq_span.shrink_to_lo(), "(".to_string()), + (seq_span.shrink_to_hi(), ")".to_string()), + ], Applicability::MachineApplicable, ); + if let CommaRecoveryMode::EitherTupleOrPipe = rt { + err.span_suggestion( + seq_span, + "...or a vertical bar to match on multiple alternatives", + seq_snippet.replace(',', " |"), + Applicability::MachineApplicable, + ); + } } } Err(err) diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 1470180dea714..6ec3a1ff1a572 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1,8 +1,8 @@ -use crate::errors; - use super::diagnostics::{dummy_arg, ConsumeClosingDelim}; use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign}; use super::{AttrWrapper, FollowedByType, ForceCollect, Parser, PathStyle, TrailingToken}; +use crate::errors::{self, MacroExpandsToAdtField}; +use crate::fluent_generated as fluent; use ast::StaticItem; use rustc_ast::ast::*; use rustc_ast::ptr::P; @@ -1342,6 +1342,13 @@ impl<'a> Parser<'a> { } let ident = this.parse_field_ident("enum", vlo)?; + if this.token == token::Not { + return this.unexpected().map_err(|mut err| { + err.note(fluent::parse_macro_expands_to_enum_variant); + err + }); + } + let struct_def = if this.check(&token::OpenDelim(Delimiter::Brace)) { // Parse a struct variant. let (fields, recovered) = @@ -1369,7 +1376,7 @@ impl<'a> Parser<'a> { Ok((Some(vr), TrailingToken::MaybeComma)) }, - ).map_err(|mut err|{ + ).map_err(|mut err| { err.help("enum variants can be `Variant`, `Variant = `, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`"); err }) @@ -1691,9 +1698,10 @@ impl<'a> Parser<'a> { Ok(a_var) } - fn expect_field_ty_separator(&mut self) -> PResult<'a, ()> { + fn expect_field_ty_separator(&mut self, adt_ty: &str) -> PResult<'a, ()> { if let Err(mut err) = self.expect(&token::Colon) { let sm = self.sess.source_map(); + let mac_invoc = self.token.kind == token::Not; let eq_typo = self.token.kind == token::Eq && self.look_ahead(1, |t| t.is_path_start()); let semi_typo = self.token.kind == token::Semi && self.look_ahead(1, |t| { @@ -1705,7 +1713,9 @@ impl<'a> Parser<'a> { _ => true, } }); - if eq_typo || semi_typo { + if mac_invoc { + err.subdiagnostic(MacroExpandsToAdtField { adt_ty }).emit(); + } else if eq_typo || semi_typo { self.bump(); // Gracefully handle small typos. err.span_suggestion_short( @@ -1713,8 +1723,8 @@ impl<'a> Parser<'a> { "field names and their types are separated with `:`", ":", Applicability::MachineApplicable, - ); - err.emit(); + ) + .emit(); } else { return Err(err); } @@ -1731,7 +1741,7 @@ impl<'a> Parser<'a> { attrs: AttrVec, ) -> PResult<'a, FieldDef> { let name = self.parse_field_ident(adt_ty, lo)?; - self.expect_field_ty_separator()?; + self.expect_field_ty_separator(adt_ty)?; let ty = self.parse_ty()?; if self.token.kind == token::Colon && self.look_ahead(1, |tok| tok.kind != token::Colon) { self.sess.emit_err(errors::SingleColonStructType { span: self.token.span }); diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index 14891c45d81a2..b477453615d79 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -135,7 +135,11 @@ impl<'a> Parser<'a> { // Parse the first pattern (`p_0`). let mut first_pat = self.parse_pat_no_top_alt(expected)?; if rc == RecoverComma::Yes { - self.maybe_recover_unexpected_comma(first_pat.span, rt)?; + self.maybe_recover_unexpected_comma( + first_pat.span, + matches!(first_pat.kind, PatKind::MacCall(_)), + rt, + )?; } // If the next token is not a `|`, @@ -177,7 +181,11 @@ impl<'a> Parser<'a> { err })?; if rc == RecoverComma::Yes { - self.maybe_recover_unexpected_comma(pat.span, rt)?; + self.maybe_recover_unexpected_comma( + pat.span, + matches!(pat.kind, PatKind::MacCall(_)), + rt, + )?; } pats.push(pat); } diff --git a/tests/ui/parser/macro/macro-expand-to-field-2.rs b/tests/ui/parser/macro/macro-expand-to-field-2.rs new file mode 100644 index 0000000000000..2f806bcea845d --- /dev/null +++ b/tests/ui/parser/macro/macro-expand-to-field-2.rs @@ -0,0 +1,16 @@ +#![no_main] + +macro_rules! field { + ($name:ident:$type:ty) => { + $name:$type + }; +} + +enum EnumVariantField { + Named { //~ NOTE while parsing this struct + field!(oopsies:()), //~ NOTE macros cannot expand to struct fields + //~^ ERROR expected `:`, found `!` + //~^^ ERROR expected `,`, or `}`, found `(` + //~^^^ NOTE expected `:` + }, +} diff --git a/tests/ui/parser/macro/macro-expand-to-field-2.stderr b/tests/ui/parser/macro/macro-expand-to-field-2.stderr new file mode 100644 index 0000000000000..8c3758173fee4 --- /dev/null +++ b/tests/ui/parser/macro/macro-expand-to-field-2.stderr @@ -0,0 +1,18 @@ +error: expected `:`, found `!` + --> $DIR/macro-expand-to-field-2.rs:11:14 + | +LL | field!(oopsies:()), + | ^ expected `:` + | + = note: macros cannot expand to struct fields + +error: expected `,`, or `}`, found `(` + --> $DIR/macro-expand-to-field-2.rs:11:15 + | +LL | Named { + | ----- while parsing this struct +LL | field!(oopsies:()), + | ^ + +error: aborting due to 2 previous errors + diff --git a/tests/ui/parser/macro/macro-expand-to-field-3.rs b/tests/ui/parser/macro/macro-expand-to-field-3.rs new file mode 100644 index 0000000000000..0a8b655e0dc03 --- /dev/null +++ b/tests/ui/parser/macro/macro-expand-to-field-3.rs @@ -0,0 +1,15 @@ +#![no_main] + +macro_rules! field { + ($name:ident:$type:ty) => { + $name:$type + }; +} + +union EnumVariantField { //~ NOTE while parsing this union + A: u32, + field!(oopsies:()), //~ NOTE macros cannot expand to union fields + //~^ ERROR expected `:`, found `!` + //~^^ ERROR expected `,`, or `}`, found `(` + //~^^^ NOTE expected `:` +} diff --git a/tests/ui/parser/macro/macro-expand-to-field-3.stderr b/tests/ui/parser/macro/macro-expand-to-field-3.stderr new file mode 100644 index 0000000000000..360b2bf793ba0 --- /dev/null +++ b/tests/ui/parser/macro/macro-expand-to-field-3.stderr @@ -0,0 +1,19 @@ +error: expected `:`, found `!` + --> $DIR/macro-expand-to-field-3.rs:11:10 + | +LL | field!(oopsies:()), + | ^ expected `:` + | + = note: macros cannot expand to union fields + +error: expected `,`, or `}`, found `(` + --> $DIR/macro-expand-to-field-3.rs:11:11 + | +LL | union EnumVariantField { + | ---------------- while parsing this union +LL | A: u32, +LL | field!(oopsies:()), + | ^ + +error: aborting due to 2 previous errors + diff --git a/tests/ui/parser/macro/macro-expand-to-field.rs b/tests/ui/parser/macro/macro-expand-to-field.rs new file mode 100644 index 0000000000000..38055a3bbb2dd --- /dev/null +++ b/tests/ui/parser/macro/macro-expand-to-field.rs @@ -0,0 +1,26 @@ +#![no_main] + +macro_rules! field { + ($name:ident:$type:ty) => { + $name:$type + }; +} + +macro_rules! variant { + ($name:ident) => { + $name + } +} + +struct Struct { //~ NOTE while parsing this struct + field!(bar:u128), //~ NOTE macros cannot expand to struct fields + //~^ ERROR expected `:`, found `!` + //~^^ NOTE expected `:` + //~^^^ ERROR expected `,`, or `}`, found `(` +} + +enum EnumVariant { //~ NOTE while parsing this enum + variant!(whoops), //~ NOTE macros cannot expand to enum variants + //~^ ERROR unexpected token: `!` + //~^^ NOTE unexpected token after this +} diff --git a/tests/ui/parser/macro/macro-expand-to-field.stderr b/tests/ui/parser/macro/macro-expand-to-field.stderr new file mode 100644 index 0000000000000..3f7fad334ecd2 --- /dev/null +++ b/tests/ui/parser/macro/macro-expand-to-field.stderr @@ -0,0 +1,29 @@ +error: expected `:`, found `!` + --> $DIR/macro-expand-to-field.rs:16:10 + | +LL | field!(bar:u128), + | ^ expected `:` + | + = note: macros cannot expand to struct fields + +error: expected `,`, or `}`, found `(` + --> $DIR/macro-expand-to-field.rs:16:11 + | +LL | struct Struct { + | ------ while parsing this struct +LL | field!(bar:u128), + | ^ + +error: unexpected token: `!` + --> $DIR/macro-expand-to-field.rs:23:12 + | +LL | enum EnumVariant { + | ----------- while parsing this enum +LL | variant!(whoops), + | ^ unexpected token after this + | + = note: macros cannot expand to enum variants + = help: enum variants can be `Variant`, `Variant = `, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }` + +error: aborting due to 3 previous errors + diff --git a/tests/ui/parser/macro/macro-expand-to-match-arm.rs b/tests/ui/parser/macro/macro-expand-to-match-arm.rs new file mode 100644 index 0000000000000..043bf371902c4 --- /dev/null +++ b/tests/ui/parser/macro/macro-expand-to-match-arm.rs @@ -0,0 +1,15 @@ +macro_rules! arm { + ($pattern:pat => $block:block) => { + $pattern => $block + }; +} + +fn main() { + let x = Some(1); + match x { + Some(1) => {}, + arm!(None => {}), //~ NOTE macros cannot expand to match arms + //~^ ERROR unexpected `,` in pattern + _ => {}, + }; +} diff --git a/tests/ui/parser/macro/macro-expand-to-match-arm.stderr b/tests/ui/parser/macro/macro-expand-to-match-arm.stderr new file mode 100644 index 0000000000000..1a5f4696858cf --- /dev/null +++ b/tests/ui/parser/macro/macro-expand-to-match-arm.stderr @@ -0,0 +1,10 @@ +error: unexpected `,` in pattern + --> $DIR/macro-expand-to-match-arm.rs:11:25 + | +LL | arm!(None => {}), + | ^ + | + = note: macros cannot expand to match arms + +error: aborting due to previous error + From dece622ee48d9744d6e64891a734e8fd25eac903 Mon Sep 17 00:00:00 2001 From: Catherine Flores Date: Mon, 24 Jul 2023 17:05:10 +0000 Subject: [PATCH 2/3] Recover from some macros --- compiler/rustc_parse/src/parser/item.rs | 46 ++++++++++---- compiler/rustc_parse/src/parser/pat.rs | 6 +- .../parser/macro/macro-expand-to-field-2.rs | 16 ----- .../macro/macro-expand-to-field-2.stderr | 18 ------ .../parser/macro/macro-expand-to-field-3.rs | 15 ----- .../macro/macro-expand-to-field-3.stderr | 19 ------ .../ui/parser/macro/macro-expand-to-field.rs | 41 ++++++++++-- .../parser/macro/macro-expand-to-field.stderr | 63 +++++++++++++++---- .../parser/macro/macro-expand-to-match-arm.rs | 2 + 9 files changed, 124 insertions(+), 102 deletions(-) delete mode 100644 tests/ui/parser/macro/macro-expand-to-field-2.rs delete mode 100644 tests/ui/parser/macro/macro-expand-to-field-2.stderr delete mode 100644 tests/ui/parser/macro/macro-expand-to-field-3.rs delete mode 100644 tests/ui/parser/macro/macro-expand-to-field-3.stderr diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 6ec3a1ff1a572..7b479067ecdce 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1343,10 +1343,14 @@ impl<'a> Parser<'a> { let ident = this.parse_field_ident("enum", vlo)?; if this.token == token::Not { - return this.unexpected().map_err(|mut err| { - err.note(fluent::parse_macro_expands_to_enum_variant); - err - }); + if let Err(mut err) = this.unexpected::<()>() { + err.note(fluent::parse_macro_expands_to_enum_variant).emit(); + } + + this.bump(); + this.parse_delim_args()?; + + return Ok((None, TrailingToken::MaybeComma)); } let struct_def = if this.check(&token::OpenDelim(Delimiter::Brace)) { @@ -1586,7 +1590,8 @@ impl<'a> Parser<'a> { self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| { let lo = this.token.span; let vis = this.parse_visibility(FollowedByType::No)?; - Ok((this.parse_single_struct_field(adt_ty, lo, vis, attrs)?, TrailingToken::None)) + this.parse_single_struct_field(adt_ty, lo, vis, attrs) + .map(|field| (field, TrailingToken::None)) }) } @@ -1698,10 +1703,9 @@ impl<'a> Parser<'a> { Ok(a_var) } - fn expect_field_ty_separator(&mut self, adt_ty: &str) -> PResult<'a, ()> { + fn expect_field_ty_separator(&mut self) -> PResult<'a, ()> { if let Err(mut err) = self.expect(&token::Colon) { let sm = self.sess.source_map(); - let mac_invoc = self.token.kind == token::Not; let eq_typo = self.token.kind == token::Eq && self.look_ahead(1, |t| t.is_path_start()); let semi_typo = self.token.kind == token::Semi && self.look_ahead(1, |t| { @@ -1713,9 +1717,7 @@ impl<'a> Parser<'a> { _ => true, } }); - if mac_invoc { - err.subdiagnostic(MacroExpandsToAdtField { adt_ty }).emit(); - } else if eq_typo || semi_typo { + if eq_typo || semi_typo { self.bump(); // Gracefully handle small typos. err.span_suggestion_short( @@ -1741,7 +1743,29 @@ impl<'a> Parser<'a> { attrs: AttrVec, ) -> PResult<'a, FieldDef> { let name = self.parse_field_ident(adt_ty, lo)?; - self.expect_field_ty_separator(adt_ty)?; + // Parse the macro invocation and recover + if self.token.kind == token::Not { + if let Err(mut err) = self.unexpected::() { + 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: P(Ty { + id: DUMMY_NODE_ID, + kind: TyKind::Err, + span: DUMMY_SP, + tokens: None, + }), + attrs, + is_placeholder: false, + }); + } + } + self.expect_field_ty_separator()?; let ty = self.parse_ty()?; if self.token.kind == token::Colon && self.look_ahead(1, |tok| tok.kind != token::Colon) { self.sess.emit_err(errors::SingleColonStructType { span: self.token.span }); diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index b477453615d79..58c00ebdea0dd 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -181,11 +181,7 @@ impl<'a> Parser<'a> { err })?; if rc == RecoverComma::Yes { - self.maybe_recover_unexpected_comma( - pat.span, - matches!(pat.kind, PatKind::MacCall(_)), - rt, - )?; + self.maybe_recover_unexpected_comma(pat.span, false, rt)?; } pats.push(pat); } diff --git a/tests/ui/parser/macro/macro-expand-to-field-2.rs b/tests/ui/parser/macro/macro-expand-to-field-2.rs deleted file mode 100644 index 2f806bcea845d..0000000000000 --- a/tests/ui/parser/macro/macro-expand-to-field-2.rs +++ /dev/null @@ -1,16 +0,0 @@ -#![no_main] - -macro_rules! field { - ($name:ident:$type:ty) => { - $name:$type - }; -} - -enum EnumVariantField { - Named { //~ NOTE while parsing this struct - field!(oopsies:()), //~ NOTE macros cannot expand to struct fields - //~^ ERROR expected `:`, found `!` - //~^^ ERROR expected `,`, or `}`, found `(` - //~^^^ NOTE expected `:` - }, -} diff --git a/tests/ui/parser/macro/macro-expand-to-field-2.stderr b/tests/ui/parser/macro/macro-expand-to-field-2.stderr deleted file mode 100644 index 8c3758173fee4..0000000000000 --- a/tests/ui/parser/macro/macro-expand-to-field-2.stderr +++ /dev/null @@ -1,18 +0,0 @@ -error: expected `:`, found `!` - --> $DIR/macro-expand-to-field-2.rs:11:14 - | -LL | field!(oopsies:()), - | ^ expected `:` - | - = note: macros cannot expand to struct fields - -error: expected `,`, or `}`, found `(` - --> $DIR/macro-expand-to-field-2.rs:11:15 - | -LL | Named { - | ----- while parsing this struct -LL | field!(oopsies:()), - | ^ - -error: aborting due to 2 previous errors - diff --git a/tests/ui/parser/macro/macro-expand-to-field-3.rs b/tests/ui/parser/macro/macro-expand-to-field-3.rs deleted file mode 100644 index 0a8b655e0dc03..0000000000000 --- a/tests/ui/parser/macro/macro-expand-to-field-3.rs +++ /dev/null @@ -1,15 +0,0 @@ -#![no_main] - -macro_rules! field { - ($name:ident:$type:ty) => { - $name:$type - }; -} - -union EnumVariantField { //~ NOTE while parsing this union - A: u32, - field!(oopsies:()), //~ NOTE macros cannot expand to union fields - //~^ ERROR expected `:`, found `!` - //~^^ ERROR expected `,`, or `}`, found `(` - //~^^^ NOTE expected `:` -} diff --git a/tests/ui/parser/macro/macro-expand-to-field-3.stderr b/tests/ui/parser/macro/macro-expand-to-field-3.stderr deleted file mode 100644 index 360b2bf793ba0..0000000000000 --- a/tests/ui/parser/macro/macro-expand-to-field-3.stderr +++ /dev/null @@ -1,19 +0,0 @@ -error: expected `:`, found `!` - --> $DIR/macro-expand-to-field-3.rs:11:10 - | -LL | field!(oopsies:()), - | ^ expected `:` - | - = note: macros cannot expand to union fields - -error: expected `,`, or `}`, found `(` - --> $DIR/macro-expand-to-field-3.rs:11:11 - | -LL | union EnumVariantField { - | ---------------- while parsing this union -LL | A: u32, -LL | field!(oopsies:()), - | ^ - -error: aborting due to 2 previous errors - diff --git a/tests/ui/parser/macro/macro-expand-to-field.rs b/tests/ui/parser/macro/macro-expand-to-field.rs index 38055a3bbb2dd..fd1d408e7265b 100644 --- a/tests/ui/parser/macro/macro-expand-to-field.rs +++ b/tests/ui/parser/macro/macro-expand-to-field.rs @@ -12,15 +12,46 @@ macro_rules! variant { } } -struct Struct { //~ NOTE while parsing this struct +struct Struct { field!(bar:u128), //~ NOTE macros cannot expand to struct fields - //~^ ERROR expected `:`, found `!` - //~^^ NOTE expected `:` - //~^^^ ERROR expected `,`, or `}`, found `(` + //~^ 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 } -enum EnumVariant { //~ NOTE while parsing this enum +enum EnumVariant { variant!(whoops), //~ NOTE macros cannot expand to enum variants //~^ ERROR unexpected token: `!` //~^^ NOTE unexpected token after this + U32, + F64, + variant!(recovers), //~ NOTE macros cannot expand to enum variants + //~^ ERROR unexpected token: `!` + //~^^ NOTE unexpected token after this +} + +enum EnumVariantField { + Named { + 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 { + A: u32, + field!(oopsies:()), //~ NOTE macros cannot expand to union fields + //~^ ERROR unexpected token: `!` + //~^^ unexpected token after this + B: u32, + field!(recovers:()), //~ NOTE macros cannot expand to union fields + //~^ ERROR unexpected token: `!` + //~^^ unexpected token after this } diff --git a/tests/ui/parser/macro/macro-expand-to-field.stderr b/tests/ui/parser/macro/macro-expand-to-field.stderr index 3f7fad334ecd2..108b68b481f05 100644 --- a/tests/ui/parser/macro/macro-expand-to-field.stderr +++ b/tests/ui/parser/macro/macro-expand-to-field.stderr @@ -1,29 +1,66 @@ -error: expected `:`, found `!` +error: unexpected token: `!` --> $DIR/macro-expand-to-field.rs:16:10 | LL | field!(bar:u128), - | ^ expected `:` + | ^ unexpected token after this | = note: macros cannot expand to struct fields -error: expected `,`, or `}`, found `(` - --> $DIR/macro-expand-to-field.rs:16:11 +error: unexpected token: `!` + --> $DIR/macro-expand-to-field.rs:21:10 | -LL | struct Struct { - | ------ while parsing this struct -LL | field!(bar:u128), - | ^ +LL | field!(recovers:()), + | ^ unexpected token after this + | + = note: macros cannot expand to struct fields error: unexpected token: `!` - --> $DIR/macro-expand-to-field.rs:23:12 + --> $DIR/macro-expand-to-field.rs:27:12 | -LL | enum EnumVariant { - | ----------- while parsing this enum LL | variant!(whoops), | ^ unexpected token after this | = note: macros cannot expand to enum variants - = help: enum variants can be `Variant`, `Variant = `, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }` -error: aborting due to 3 previous errors +error: unexpected token: `!` + --> $DIR/macro-expand-to-field.rs:32: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 + | +LL | field!(oopsies:()), + | ^ unexpected token after this + | + = note: macros cannot expand to struct fields + +error: unexpected token: `!` + --> $DIR/macro-expand-to-field.rs:42:14 + | +LL | field!(oopsies2:()), + | ^ unexpected token after this + | + = note: macros cannot expand to struct fields + +error: unexpected token: `!` + --> $DIR/macro-expand-to-field.rs:50:10 + | +LL | field!(oopsies:()), + | ^ unexpected token after this + | + = note: macros cannot expand to union fields + +error: unexpected token: `!` + --> $DIR/macro-expand-to-field.rs:54:10 + | +LL | field!(recovers:()), + | ^ unexpected token after this + | + = note: macros cannot expand to union fields + +error: aborting due to 8 previous errors diff --git a/tests/ui/parser/macro/macro-expand-to-match-arm.rs b/tests/ui/parser/macro/macro-expand-to-match-arm.rs index 043bf371902c4..c176e8bbd9dc5 100644 --- a/tests/ui/parser/macro/macro-expand-to-match-arm.rs +++ b/tests/ui/parser/macro/macro-expand-to-match-arm.rs @@ -10,6 +10,8 @@ fn main() { Some(1) => {}, arm!(None => {}), //~ NOTE macros cannot expand to match arms //~^ ERROR unexpected `,` in pattern + // doesn't recover + Some(2) => {}, _ => {}, }; } From bbd69e4a4c27f2407326a71a4eb37de694a72b87 Mon Sep 17 00:00:00 2001 From: Catherine Flores Date: Wed, 2 Aug 2023 23:59:30 +0000 Subject: [PATCH 3/3] Add test for enum with fields --- compiler/rustc_parse/src/parser/item.rs | 7 +-- .../ui/parser/macro/macro-expand-to-field.rs | 57 ++++++++++++------- .../parser/macro/macro-expand-to-field.stderr | 22 ++++--- .../parser/macro/macro-expand-to-match-arm.rs | 5 +- 4 files changed, 54 insertions(+), 37 deletions(-) diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 7b479067ecdce..2f1d377585d78 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1754,12 +1754,7 @@ impl<'a> Parser<'a> { ident: None, vis, id: DUMMY_NODE_ID, - ty: P(Ty { - id: DUMMY_NODE_ID, - kind: TyKind::Err, - span: DUMMY_SP, - tokens: None, - }), + ty: self.mk_ty(DUMMY_SP, TyKind::Err), attrs, is_placeholder: false, }); diff --git a/tests/ui/parser/macro/macro-expand-to-field.rs b/tests/ui/parser/macro/macro-expand-to-field.rs index fd1d408e7265b..155872f7a5d79 100644 --- a/tests/ui/parser/macro/macro-expand-to-field.rs +++ b/tests/ui/parser/macro/macro-expand-to-field.rs @@ -1,4 +1,4 @@ -#![no_main] +// compile-flags: --crate-type=lib macro_rules! field { ($name:ident:$type:ty) => { @@ -13,9 +13,10 @@ macro_rules! variant { } struct Struct { - field!(bar:u128), //~ NOTE macros cannot expand to struct fields - //~^ ERROR unexpected token: `!` - //~^^ NOTE unexpected token after this + 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 @@ -24,34 +25,46 @@ struct Struct { } enum EnumVariant { - variant!(whoops), //~ NOTE macros cannot expand to enum variants - //~^ ERROR unexpected token: `!` - //~^^ NOTE unexpected token after this + variant!(whoops), + //~^ NOTE macros cannot expand to enum variants + //~| ERROR unexpected token: `!` + //~| NOTE unexpected token after this U32, F64, - variant!(recovers), //~ NOTE macros cannot expand to enum variants - //~^ ERROR unexpected token: `!` - //~^^ NOTE unexpected token after this + variant!(recovers), + //~^ NOTE macros cannot expand to enum variants + //~| ERROR unexpected token: `!` + //~| NOTE unexpected token after this + Data { + field!(x:u32), + //~^ NOTE macros cannot expand to struct fields + //~| ERROR unexpected token: `!` + //~| NOTE unexpected token after this + } } enum EnumVariantField { Named { - 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 + 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 { A: u32, - field!(oopsies:()), //~ NOTE macros cannot expand to union fields - //~^ ERROR unexpected token: `!` - //~^^ unexpected token after this + field!(oopsies:()), + //~^ NOTE macros cannot expand to union fields + //~| ERROR unexpected token: `!` + //~| unexpected token after this B: u32, - field!(recovers:()), //~ NOTE macros cannot expand to union fields - //~^ ERROR unexpected token: `!` - //~^^ unexpected token after this + field!(recovers:()), + //~^ NOTE macros cannot expand to union fields + //~| ERROR unexpected token: `!` + //~| unexpected token after this } diff --git a/tests/ui/parser/macro/macro-expand-to-field.stderr b/tests/ui/parser/macro/macro-expand-to-field.stderr index 108b68b481f05..adcd032f5c0df 100644 --- a/tests/ui/parser/macro/macro-expand-to-field.stderr +++ b/tests/ui/parser/macro/macro-expand-to-field.stderr @@ -7,7 +7,7 @@ LL | field!(bar:u128), = note: macros cannot expand to struct fields error: unexpected token: `!` - --> $DIR/macro-expand-to-field.rs:21:10 + --> $DIR/macro-expand-to-field.rs:22:10 | LL | field!(recovers:()), | ^ unexpected token after this @@ -15,7 +15,7 @@ LL | field!(recovers:()), = note: macros cannot expand to struct fields error: unexpected token: `!` - --> $DIR/macro-expand-to-field.rs:27:12 + --> $DIR/macro-expand-to-field.rs:28:12 | LL | variant!(whoops), | ^ unexpected token after this @@ -23,7 +23,7 @@ LL | variant!(whoops), = note: macros cannot expand to enum variants error: unexpected token: `!` - --> $DIR/macro-expand-to-field.rs:32:12 + --> $DIR/macro-expand-to-field.rs:34:12 | LL | variant!(recovers), | ^ unexpected token after this @@ -33,13 +33,21 @@ LL | variant!(recovers), error: unexpected token: `!` --> $DIR/macro-expand-to-field.rs:39:14 | +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 + | LL | field!(oopsies:()), | ^ unexpected token after this | = note: macros cannot expand to struct fields error: unexpected token: `!` - --> $DIR/macro-expand-to-field.rs:42:14 + --> $DIR/macro-expand-to-field.rs:52:14 | LL | field!(oopsies2:()), | ^ unexpected token after this @@ -47,7 +55,7 @@ LL | field!(oopsies2:()), = note: macros cannot expand to struct fields error: unexpected token: `!` - --> $DIR/macro-expand-to-field.rs:50:10 + --> $DIR/macro-expand-to-field.rs:61:10 | LL | field!(oopsies:()), | ^ unexpected token after this @@ -55,12 +63,12 @@ LL | field!(oopsies:()), = note: macros cannot expand to union fields error: unexpected token: `!` - --> $DIR/macro-expand-to-field.rs:54:10 + --> $DIR/macro-expand-to-field.rs:66:10 | LL | field!(recovers:()), | ^ unexpected token after this | = note: macros cannot expand to union fields -error: aborting due to 8 previous errors +error: aborting due to 9 previous errors diff --git a/tests/ui/parser/macro/macro-expand-to-match-arm.rs b/tests/ui/parser/macro/macro-expand-to-match-arm.rs index c176e8bbd9dc5..39d1d065ed986 100644 --- a/tests/ui/parser/macro/macro-expand-to-match-arm.rs +++ b/tests/ui/parser/macro/macro-expand-to-match-arm.rs @@ -8,8 +8,9 @@ fn main() { let x = Some(1); match x { Some(1) => {}, - arm!(None => {}), //~ NOTE macros cannot expand to match arms - //~^ ERROR unexpected `,` in pattern + arm!(None => {}), + //~^ NOTE macros cannot expand to match arms + //~| ERROR unexpected `,` in pattern // doesn't recover Some(2) => {}, _ => {},