Skip to content

Commit ae126ad

Browse files
committed
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.
1 parent 7b3cd07 commit ae126ad

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
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));

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

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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: cannot find attribute `u` in this scope
11+
--> $DIR/issue-90873.rs:1:4
12+
|
13+
LL | #![u=||{static d=||1;}]
14+
| ^
15+
16+
error[E0601]: `main` function not found in crate `issue_90873`
17+
--> $DIR/issue-90873.rs:1:1
18+
|
19+
LL | #![u=||{static d=||1;}]
20+
| ^^^^^^^^^^^^^^^^^^^^^^^ consider adding a `main` function to `$DIR/issue-90873.rs`
21+
22+
error: missing type for `static` item
23+
--> $DIR/issue-90873.rs:1:16
24+
|
25+
LL | #![u=||{static d=||1;}]
26+
| ^ help: provide a type for the item: `d: <type>`
27+
28+
error: aborting due to 4 previous errors
29+
30+
For more information about this error, try `rustc --explain E0601`.

0 commit comments

Comments
 (0)