diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs index b97ec8c613d91..3636a3579781b 100644 --- a/compiler/rustc_parse/src/parser/path.rs +++ b/compiler/rustc_parse/src/parser/path.rs @@ -160,7 +160,7 @@ impl<'a> Parser<'a> { style: PathStyle, ty_generics: Option<&Generics>, ) -> PResult<'a, Path> { - let reject_generics_if_mod_style = |parser: &Parser<'_>, path: &Path| { + let reject_generics_if_mod_style = |parser: &Parser<'_>, path: Path| { // Ensure generic arguments don't end up in attribute paths, such as: // // macro_rules! m { @@ -178,21 +178,26 @@ impl<'a> Parser<'a> { .map(|arg| arg.span()) .collect::>(); parser.dcx().emit_err(errors::GenericsInPath { span }); + // Ignore these arguments to prevent unexpected behaviors. + let segments = path + .segments + .iter() + .map(|segment| PathSegment { ident: segment.ident, id: segment.id, args: None }) + .collect(); + Path { segments, ..path } + } else { + path } }; - maybe_whole!(self, NtPath, |path| { - reject_generics_if_mod_style(self, &path); - path.into_inner() - }); + maybe_whole!(self, NtPath, |path| reject_generics_if_mod_style(self, path.into_inner())); if let token::Interpolated(nt) = &self.token.kind { if let token::NtTy(ty) = &nt.0 { if let ast::TyKind::Path(None, path) = &ty.kind { let path = path.clone(); self.bump(); - reject_generics_if_mod_style(self, &path); - return Ok(path); + return Ok(reject_generics_if_mod_style(self, path)); } } } diff --git a/tests/crashes/123911.rs b/tests/crashes/123911.rs deleted file mode 100644 index 1a4d6a7951281..0000000000000 --- a/tests/crashes/123911.rs +++ /dev/null @@ -1,16 +0,0 @@ -//@ known-bug: #123911 - -macro_rules! m { - ($attr_path: path) => { - #[$attr_path] - fn f() {} - } -} - -m!(inline<{ - let a = CharCharFloat { a: 1 }; - let b = rustrt::rust_dbg_abi_4(a); - println!("a: {}", b.a); -}>); - -fn main() {} diff --git a/tests/crashes/123912.rs b/tests/crashes/123912.rs deleted file mode 100644 index 35216caabcdbf..0000000000000 --- a/tests/crashes/123912.rs +++ /dev/null @@ -1,15 +0,0 @@ -//@ known-bug: #123912 - -macro_rules! m { - ($attr_path: path) => { - #[$attr_path] - fn f() {} - } -} - -m!(inline<{ - let a = CharCharFloat { a: 1 }; - println!("a: {}", a); -}>); - -fn main() {} diff --git a/tests/crashes/97006.rs b/tests/ui/macros/genercs-in-path-with-prettry-hir.rs similarity index 83% rename from tests/crashes/97006.rs rename to tests/ui/macros/genercs-in-path-with-prettry-hir.rs index c8dfa52ebee07..84370fcebbcbc 100644 --- a/tests/crashes/97006.rs +++ b/tests/ui/macros/genercs-in-path-with-prettry-hir.rs @@ -1,7 +1,6 @@ -//@ known-bug: #97006 //@ compile-flags: -Zunpretty=hir -#![allow(unused)] +// issue#97006 macro_rules! m { ($attr_path: path) => { diff --git a/tests/ui/macros/genercs-in-path-with-prettry-hir.stderr b/tests/ui/macros/genercs-in-path-with-prettry-hir.stderr new file mode 100644 index 0000000000000..8fcc7c6fbff06 --- /dev/null +++ b/tests/ui/macros/genercs-in-path-with-prettry-hir.stderr @@ -0,0 +1,8 @@ +error: unexpected generic arguments in path + --> $DIR/genercs-in-path-with-prettry-hir.rs:12:10 + | +LL | m!(inline); + | ^^^^ + +error: aborting due to 1 previous error + diff --git a/tests/ui/macros/genercs-in-path-with-prettry-hir.stdout b/tests/ui/macros/genercs-in-path-with-prettry-hir.stdout new file mode 100644 index 0000000000000..e9ee59abfae8d --- /dev/null +++ b/tests/ui/macros/genercs-in-path-with-prettry-hir.stdout @@ -0,0 +1,15 @@ +#[prelude_import] +use ::std::prelude::rust_2015::*; +#[macro_use] +extern crate std; +//@ compile-flags: -Zunpretty=hir + +// issue#97006 + +macro_rules! m { ($attr_path: path) => { #[$attr_path] fn f() {} } } +#[ + +inline] +fn f() { } + +fn main() { } diff --git a/tests/ui/macros/macro-expand-within-generics-in-path.rs b/tests/ui/macros/macro-expand-within-generics-in-path.rs new file mode 100644 index 0000000000000..017d5152221bf --- /dev/null +++ b/tests/ui/macros/macro-expand-within-generics-in-path.rs @@ -0,0 +1,19 @@ +// issue#123911 +// issue#123912 + +macro_rules! m { + ($p: path) => { + #[$p] + struct S; + }; +} + +macro_rules! p { + () => {}; +} + +m!(generic); +//~^ ERROR: unexpected generic arguments in path +//~| ERROR: cannot find attribute `generic` in this scope + +fn main() {} diff --git a/tests/ui/macros/macro-expand-within-generics-in-path.stderr b/tests/ui/macros/macro-expand-within-generics-in-path.stderr new file mode 100644 index 0000000000000..72026c41050a2 --- /dev/null +++ b/tests/ui/macros/macro-expand-within-generics-in-path.stderr @@ -0,0 +1,14 @@ +error: unexpected generic arguments in path + --> $DIR/macro-expand-within-generics-in-path.rs:15:11 + | +LL | m!(generic); + | ^^^^^^ + +error: cannot find attribute `generic` in this scope + --> $DIR/macro-expand-within-generics-in-path.rs:15:4 + | +LL | m!(generic); + | ^^^^^^^ + +error: aborting due to 2 previous errors + diff --git a/tests/ui/span/macro-ty-params.rs b/tests/ui/span/macro-ty-params.rs index cf28b0255d17d..c7ce9b9faed09 100644 --- a/tests/ui/span/macro-ty-params.rs +++ b/tests/ui/span/macro-ty-params.rs @@ -11,5 +11,4 @@ fn main() { foo::<>!(); //~ ERROR generic arguments in macro path m!(Default<>); //~^ ERROR unexpected generic arguments in path - //~^^ ERROR generic arguments in macro path } diff --git a/tests/ui/span/macro-ty-params.stderr b/tests/ui/span/macro-ty-params.stderr index 7023ef8cd1c33..138cd2598a1c5 100644 --- a/tests/ui/span/macro-ty-params.stderr +++ b/tests/ui/span/macro-ty-params.stderr @@ -16,11 +16,5 @@ error: unexpected generic arguments in path LL | m!(Default<>); | ^^ -error: generic arguments in macro path - --> $DIR/macro-ty-params.rs:12:15 - | -LL | m!(Default<>); - | ^^ - -error: aborting due to 4 previous errors +error: aborting due to 3 previous errors