Skip to content

Commit 5f6cb2c

Browse files
authored
Rollup merge of #91111 - cjgillot:hir-no-lower-attrs, r=michaelwoerister
Do not visit attributes in `ItemLowerer`. By default, AST visitors visit expressions that appear in key-value attributes. Those expressions should not be lowered to HIR, as they do not correspond to actually compiled code. Since an attribute cannot produce meaningful HIR, just skip them altogether. Fixes #81886 Fixes #90873 r? `@michaelwoerister`
2 parents 6970cf5 + 7f5d3ff commit 5f6cb2c

File tree

6 files changed

+69
-19
lines changed

6 files changed

+69
-19
lines changed

compiler/rustc_ast_lowering/src/item.rs

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ impl ItemLowerer<'_, '_, '_> {
4040
}
4141

4242
impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
43+
fn visit_attribute(&mut self, _: &'a Attribute) {
44+
// We do not want to lower expressions that appear in attributes,
45+
// as they are not accessible to the rest of the HIR.
46+
}
47+
4348
fn visit_item(&mut self, item: &'a Item) {
4449
let hir_id = self.lctx.with_hir_id_owner(item.id, |lctx| {
4550
let node = lctx.without_in_scope_lifetime_defs(|lctx| lctx.lower_item(item));

compiler/rustc_resolve/src/late.rs

+4
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ struct LateResolutionVisitor<'a, 'b, 'ast> {
431431

432432
/// Walks the whole crate in DFS order, visiting each item, resolving names as it goes.
433433
impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
434+
fn visit_attribute(&mut self, _: &'ast Attribute) {
435+
// We do not want to resolve expressions that appear in attributes,
436+
// as they do not correspond to actual code.
437+
}
434438
fn visit_item(&mut self, item: &'ast Item) {
435439
let prev = replace(&mut self.diagnostic_metadata.current_item, Some(item));
436440
// Always report errors in items we just entered.

src/test/ui/attributes/issue-90873.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![u=||{static d=||1;}]
2+
//~^ unexpected token
3+
//~| cannot find attribute `u` in this scope
4+
//~| `main` function not found in crate `issue_90873`
5+
//~| missing type for `static` item
6+
7+
#![a={impl std::ops::Neg for i8 {}}]
8+
//~^ ERROR unexpected token
9+
//~| ERROR cannot find attribute `a` in this scope
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
error: unexpected token: `||
2+
{
3+
static d: _ = || 1;
4+
}`
5+
--> $DIR/issue-90873.rs:1:6
6+
|
7+
LL | #![u=||{static d=||1;}]
8+
| ^^^^^^^^^^^^^^^^^
9+
10+
error: unexpected token: `{
11+
impl std::ops::Neg for i8 { }
12+
}`
13+
--> $DIR/issue-90873.rs:7:6
14+
|
15+
LL | #![a={impl std::ops::Neg for i8 {}}]
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17+
18+
error: cannot find attribute `u` in this scope
19+
--> $DIR/issue-90873.rs:1:4
20+
|
21+
LL | #![u=||{static d=||1;}]
22+
| ^
23+
24+
error: cannot find attribute `a` in this scope
25+
--> $DIR/issue-90873.rs:7:4
26+
|
27+
LL | #![a={impl std::ops::Neg for i8 {}}]
28+
| ^
29+
30+
error[E0601]: `main` function not found in crate `issue_90873`
31+
--> $DIR/issue-90873.rs:1:1
32+
|
33+
LL | / #![u=||{static d=||1;}]
34+
LL | |
35+
LL | |
36+
LL | |
37+
LL | |
38+
LL | |
39+
LL | | #![a={impl std::ops::Neg for i8 {}}]
40+
| |____________________________________^ consider adding a `main` function to `$DIR/issue-90873.rs`
41+
42+
error: missing type for `static` item
43+
--> $DIR/issue-90873.rs:1:16
44+
|
45+
LL | #![u=||{static d=||1;}]
46+
| ^ help: provide a type for the item: `d: <type>`
47+
48+
error: aborting due to 6 previous errors
49+
50+
For more information about this error, try `rustc --explain E0601`.

src/test/ui/consts/issue-90878-2.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#![l=|x|[b;x ]] //~ ERROR unexpected token: `|x| [b; x]`
22
//~^ ERROR cannot find attribute `l` in this scope
3-
//~^^ ERROR attempt to use a non-constant value in a constant [E0435]
4-
//~^^^ ERROR cannot find value `b` in this scope [E0425]
53

64
// notice the space at the start,
75
// we can't attach any attributes to this file because it needs to be at the start

src/test/ui/consts/issue-90878-2.stderr

+1-17
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,5 @@ error: cannot find attribute `l` in this scope
1010
LL | #![l=|x|[b;x ]]
1111
| ^
1212

13-
error[E0435]: attempt to use a non-constant value in a constant
14-
--> $DIR/issue-90878-2.rs:1:13
15-
|
16-
LL | #![l=|x|[b;x ]]
17-
| - ^
18-
| |
19-
| this would need to be a `const`
20-
21-
error[E0425]: cannot find value `b` in this scope
22-
--> $DIR/issue-90878-2.rs:1:11
23-
|
24-
LL | #![l=|x|[b;x ]]
25-
| ^ help: a local variable with a similar name exists: `x`
26-
27-
error: aborting due to 4 previous errors
13+
error: aborting due to 2 previous errors
2814

29-
Some errors have detailed explanations: E0425, E0435.
30-
For more information about an error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)