Skip to content

Commit 69122f1

Browse files
authored
Rollup merge of #124318 - bvanjoi:fix-123911, r=petrochenkov
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`
2 parents 150633e + f70f900 commit 69122f1

10 files changed

+70
-48
lines changed

Diff for: compiler/rustc_parse/src/parser/path.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl<'a> Parser<'a> {
160160
style: PathStyle,
161161
ty_generics: Option<&Generics>,
162162
) -> PResult<'a, Path> {
163-
let reject_generics_if_mod_style = |parser: &Parser<'_>, path: &Path| {
163+
let reject_generics_if_mod_style = |parser: &Parser<'_>, path: Path| {
164164
// Ensure generic arguments don't end up in attribute paths, such as:
165165
//
166166
// macro_rules! m {
@@ -178,21 +178,26 @@ impl<'a> Parser<'a> {
178178
.map(|arg| arg.span())
179179
.collect::<Vec<_>>();
180180
parser.dcx().emit_err(errors::GenericsInPath { span });
181+
// Ignore these arguments to prevent unexpected behaviors.
182+
let segments = path
183+
.segments
184+
.iter()
185+
.map(|segment| PathSegment { ident: segment.ident, id: segment.id, args: None })
186+
.collect();
187+
Path { segments, ..path }
188+
} else {
189+
path
181190
}
182191
};
183192

184-
maybe_whole!(self, NtPath, |path| {
185-
reject_generics_if_mod_style(self, &path);
186-
path.into_inner()
187-
});
193+
maybe_whole!(self, NtPath, |path| reject_generics_if_mod_style(self, path.into_inner()));
188194

189195
if let token::Interpolated(nt) = &self.token.kind {
190196
if let token::NtTy(ty) = &nt.0 {
191197
if let ast::TyKind::Path(None, path) = &ty.kind {
192198
let path = path.clone();
193199
self.bump();
194-
reject_generics_if_mod_style(self, &path);
195-
return Ok(path);
200+
return Ok(reject_generics_if_mod_style(self, path));
196201
}
197202
}
198203
}

Diff for: tests/crashes/123911.rs

-16
This file was deleted.

Diff for: tests/crashes/123912.rs

-15
This file was deleted.

Diff for: tests/crashes/97006.rs renamed to tests/ui/macros/genercs-in-path-with-prettry-hir.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
//@ known-bug: #97006
21
//@ compile-flags: -Zunpretty=hir
32

4-
#![allow(unused)]
3+
// issue#97006
54

65
macro_rules! m {
76
($attr_path: path) => {
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: unexpected generic arguments in path
2+
--> $DIR/genercs-in-path-with-prettry-hir.rs:12:10
3+
|
4+
LL | m!(inline<u8>);
5+
| ^^^^
6+
7+
error: aborting due to 1 previous error
8+
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#[prelude_import]
2+
use ::std::prelude::rust_2015::*;
3+
#[macro_use]
4+
extern crate std;
5+
//@ compile-flags: -Zunpretty=hir
6+
7+
// issue#97006
8+
9+
macro_rules! m { ($attr_path: path) => { #[$attr_path] fn f() {} } }
10+
#[
11+
12+
inline]
13+
fn f() { }
14+
15+
fn main() { }
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// issue#123911
2+
// issue#123912
3+
4+
macro_rules! m {
5+
($p: path) => {
6+
#[$p]
7+
struct S;
8+
};
9+
}
10+
11+
macro_rules! p {
12+
() => {};
13+
}
14+
15+
m!(generic<p!()>);
16+
//~^ ERROR: unexpected generic arguments in path
17+
//~| ERROR: cannot find attribute `generic` in this scope
18+
19+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: unexpected generic arguments in path
2+
--> $DIR/macro-expand-within-generics-in-path.rs:15:11
3+
|
4+
LL | m!(generic<p!()>);
5+
| ^^^^^^
6+
7+
error: cannot find attribute `generic` in this scope
8+
--> $DIR/macro-expand-within-generics-in-path.rs:15:4
9+
|
10+
LL | m!(generic<p!()>);
11+
| ^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+

Diff for: tests/ui/span/macro-ty-params.rs

-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ fn main() {
1111
foo::<>!(); //~ ERROR generic arguments in macro path
1212
m!(Default<>);
1313
//~^ ERROR unexpected generic arguments in path
14-
//~^^ ERROR generic arguments in macro path
1514
}

Diff for: tests/ui/span/macro-ty-params.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,5 @@ error: unexpected generic arguments in path
1616
LL | m!(Default<>);
1717
| ^^
1818

19-
error: generic arguments in macro path
20-
--> $DIR/macro-ty-params.rs:12:15
21-
|
22-
LL | m!(Default<>);
23-
| ^^
24-
25-
error: aborting due to 4 previous errors
19+
error: aborting due to 3 previous errors
2620

0 commit comments

Comments
 (0)