Skip to content

Commit

Permalink
Rollup merge of #124318 - bvanjoi:fix-123911, r=petrochenkov
Browse files Browse the repository at this point in the history
ignore generics args in attribute paths

Fixes #97006
Fixes #123911
Fixes #123912

This patch ensures that we no longer have to handle invalid generic arguments in attribute paths.

r? `@petrochenkov`
  • Loading branch information
jieyouxu committed May 11, 2024
2 parents 150633e + f70f900 commit 69122f1
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 48 deletions.
19 changes: 12 additions & 7 deletions compiler/rustc_parse/src/parser/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -178,21 +178,26 @@ impl<'a> Parser<'a> {
.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));
}
}
}
Expand Down
16 changes: 0 additions & 16 deletions tests/crashes/123911.rs

This file was deleted.

15 changes: 0 additions & 15 deletions tests/crashes/123912.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//@ known-bug: #97006
//@ compile-flags: -Zunpretty=hir

#![allow(unused)]
// issue#97006

macro_rules! m {
($attr_path: path) => {
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/macros/genercs-in-path-with-prettry-hir.stderr
Original file line number Diff line number Diff line change
@@ -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

15 changes: 15 additions & 0 deletions tests/ui/macros/genercs-in-path-with-prettry-hir.stdout
Original file line number Diff line number Diff line change
@@ -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() { }
19 changes: 19 additions & 0 deletions tests/ui/macros/macro-expand-within-generics-in-path.rs
Original file line number Diff line number Diff line change
@@ -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() {}
14 changes: 14 additions & 0 deletions tests/ui/macros/macro-expand-within-generics-in-path.stderr
Original file line number Diff line number Diff line change
@@ -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

1 change: 0 additions & 1 deletion tests/ui/span/macro-ty-params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 1 addition & 7 deletions tests/ui/span/macro-ty-params.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 69122f1

Please sign in to comment.