Skip to content

Commit b0e559a

Browse files
authored
Rollup merge of #143796 - JonathanBrouwer:fix-builtin-attribute-prefix, r=jdonszelmann
Fix ICE for parsed attributes with longer path not handled by CheckAttribute Fixes #137590 Fixes #143789 r? ```@jdonszelmann```
2 parents 8719acd + 2f05fa6 commit b0e559a

File tree

6 files changed

+40
-1
lines changed

6 files changed

+40
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4349,6 +4349,7 @@ dependencies = [
43494349
"rustc_ast_lowering",
43504350
"rustc_ast_pretty",
43514351
"rustc_attr_data_structures",
4352+
"rustc_attr_parsing",
43524353
"rustc_data_structures",
43534354
"rustc_errors",
43544355
"rustc_expand",

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,11 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
735735
attributes
736736
}
737737

738+
/// Returns whether there is a parser for an attribute with this name
739+
pub fn is_parsed_attribute(path: &[Symbol]) -> bool {
740+
Late::parsers().0.contains_key(path)
741+
}
742+
738743
fn lower_attr_args(&self, args: &ast::AttrArgs, lower_span: impl Fn(Span) -> Span) -> AttrArgs {
739744
match args {
740745
ast::AttrArgs::Empty => AttrArgs::Empty,

compiler/rustc_passes/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ rustc_ast = { path = "../rustc_ast" }
1010
rustc_ast_lowering = { path = "../rustc_ast_lowering" }
1111
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1212
rustc_attr_data_structures = { path = "../rustc_attr_data_structures" }
13+
rustc_attr_parsing = { path = "../rustc_attr_parsing" }
1314
rustc_data_structures = { path = "../rustc_data_structures" }
1415
rustc_errors = { path = "../rustc_errors" }
1516
rustc_expand = { path = "../rustc_expand" }

compiler/rustc_passes/src/check_attr.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
88
use std::cell::Cell;
99
use std::collections::hash_map::Entry;
10+
use std::slice;
1011

1112
use rustc_abi::{Align, ExternAbi, Size};
1213
use rustc_ast::{AttrStyle, LitKind, MetaItemInner, MetaItemKind, ast};
1314
use rustc_attr_data_structures::{AttributeKind, InlineAttr, ReprAttr, find_attr};
15+
use rustc_attr_parsing::{AttributeParser, Late};
1416
use rustc_data_structures::fx::FxHashMap;
1517
use rustc_errors::{Applicability, DiagCtxtHandle, IntoDiagArg, MultiSpan, StashKey};
1618
use rustc_feature::{AttributeDuplicates, AttributeType, BUILTIN_ATTRIBUTE_MAP, BuiltinAttribute};
@@ -384,11 +386,18 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
384386
| sym::custom_mir,
385387
..
386388
] => {}
387-
[name, ..] => {
389+
[name, rest@..] => {
388390
match BUILTIN_ATTRIBUTE_MAP.get(name) {
389391
// checked below
390392
Some(BuiltinAttribute { type_: AttributeType::CrateLevel, .. }) => {}
391393
Some(_) => {
394+
if rest.len() > 0 && AttributeParser::<Late>::is_parsed_attribute(slice::from_ref(name)) {
395+
// Check if we tried to use a builtin attribute as an attribute namespace, like `#[must_use::skip]`.
396+
// This check is here to solve https://github.com/rust-lang/rust/issues/137590
397+
// An error is already produced for this case elsewhere
398+
continue
399+
}
400+
392401
// FIXME: differentiate between unstable and internal attributes just
393402
// like we do with features instead of just accepting `rustc_`
394403
// attributes by name. That should allow trimming the above list, too.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Regression test for https://github.com/rust-lang/rust/issues/143789
2+
#[must_use::skip]
3+
//~^ ERROR failed to resolve: use of unresolved module or unlinked crate `must_use`
4+
fn main() { }
5+
6+
// Regression test for https://github.com/rust-lang/rust/issues/137590
7+
struct S(#[stable::skip] u8, u16, u32);
8+
//~^ ERROR failed to resolve: use of unresolved module or unlinked crate `stable`
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `stable`
2+
--> $DIR/builtin-attribute-prefix.rs:7:12
3+
|
4+
LL | struct S(#[stable::skip] u8, u16, u32);
5+
| ^^^^^^ use of unresolved module or unlinked crate `stable`
6+
7+
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `must_use`
8+
--> $DIR/builtin-attribute-prefix.rs:2:3
9+
|
10+
LL | #[must_use::skip]
11+
| ^^^^^^^^ use of unresolved module or unlinked crate `must_use`
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0433`.

0 commit comments

Comments
 (0)