ignore generics args in attribute paths

This commit is contained in:
bohan 2024-04-24 11:28:45 +08:00
parent cf774742b6
commit f70f900036
10 changed files with 70 additions and 48 deletions

View File

@ -160,7 +160,7 @@ pub(super) fn parse_path_inner(
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 @@ pub(super) fn parse_path_inner(
.map(|arg| arg.span())
.collect::<Vec<_>>();
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));
}
}
}

View File

@ -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() {}

View File

@ -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() {}

View File

@ -1,7 +1,6 @@
//@ known-bug: #97006
//@ compile-flags: -Zunpretty=hir
#![allow(unused)]
// issue#97006
macro_rules! m {
($attr_path: path) => {

View File

@ -0,0 +1,8 @@
error: unexpected generic arguments in path
--> $DIR/genercs-in-path-with-prettry-hir.rs:12:10
|
LL | m!(inline<u8>);
| ^^^^
error: aborting due to 1 previous error

View File

@ -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() { }

View File

@ -0,0 +1,19 @@
// issue#123911
// issue#123912
macro_rules! m {
($p: path) => {
#[$p]
struct S;
};
}
macro_rules! p {
() => {};
}
m!(generic<p!()>);
//~^ ERROR: unexpected generic arguments in path
//~| ERROR: cannot find attribute `generic` in this scope
fn main() {}

View File

@ -0,0 +1,14 @@
error: unexpected generic arguments in path
--> $DIR/macro-expand-within-generics-in-path.rs:15:11
|
LL | m!(generic<p!()>);
| ^^^^^^
error: cannot find attribute `generic` in this scope
--> $DIR/macro-expand-within-generics-in-path.rs:15:4
|
LL | m!(generic<p!()>);
| ^^^^^^^
error: aborting due to 2 previous errors

View File

@ -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
}

View File

@ -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