diff options
| author | Camille GILLOT <gillot.camille@gmail.com> | 2021-11-21 16:16:02 +0100 |
|---|---|---|
| committer | Camille GILLOT <gillot.camille@gmail.com> | 2021-11-23 22:06:22 +0100 |
| commit | ae126ad282f24437d6f57909a038fbca24c1e6dd (patch) | |
| tree | 20d6b6bd430108a76b4bf5f5bdae9cbb5c14b6d9 | |
| parent | 7b3cd075bbe309031b418650a9c32baf0b4a3276 (diff) | |
| download | rust-ae126ad282f24437d6f57909a038fbca24c1e6dd.tar.gz rust-ae126ad282f24437d6f57909a038fbca24c1e6dd.zip | |
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.
| -rw-r--r-- | compiler/rustc_ast_lowering/src/item.rs | 5 | ||||
| -rw-r--r-- | src/test/ui/attributes/issue-90873.rs | 5 | ||||
| -rw-r--r-- | src/test/ui/attributes/issue-90873.stderr | 30 |
3 files changed, 40 insertions, 0 deletions
diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 6a4571cf6d2..692f3936824 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -40,6 +40,11 @@ impl ItemLowerer<'_, '_, '_> { } impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> { + fn visit_attribute(&mut self, _: &'a Attribute) { + // We do not want to lower expressions that appear in attributes, + // as they are not accessible to the rest of the HIR. + } + fn visit_item(&mut self, item: &'a Item) { let hir_id = self.lctx.with_hir_id_owner(item.id, |lctx| { let node = lctx.without_in_scope_lifetime_defs(|lctx| lctx.lower_item(item)); diff --git a/src/test/ui/attributes/issue-90873.rs b/src/test/ui/attributes/issue-90873.rs new file mode 100644 index 00000000000..79ec59e4dc0 --- /dev/null +++ b/src/test/ui/attributes/issue-90873.rs @@ -0,0 +1,5 @@ +#![u=||{static d=||1;}] +//~^ unexpected token +//~| cannot find attribute `u` in this scope +//~| `main` function not found in crate `issue_90873` +//~| missing type for `static` item diff --git a/src/test/ui/attributes/issue-90873.stderr b/src/test/ui/attributes/issue-90873.stderr new file mode 100644 index 00000000000..b249be8d0e2 --- /dev/null +++ b/src/test/ui/attributes/issue-90873.stderr @@ -0,0 +1,30 @@ +error: unexpected token: `|| + { + static d: _ = || 1; + }` + --> $DIR/issue-90873.rs:1:6 + | +LL | #![u=||{static d=||1;}] + | ^^^^^^^^^^^^^^^^^ + +error: cannot find attribute `u` in this scope + --> $DIR/issue-90873.rs:1:4 + | +LL | #![u=||{static d=||1;}] + | ^ + +error[E0601]: `main` function not found in crate `issue_90873` + --> $DIR/issue-90873.rs:1:1 + | +LL | #![u=||{static d=||1;}] + | ^^^^^^^^^^^^^^^^^^^^^^^ consider adding a `main` function to `$DIR/issue-90873.rs` + +error: missing type for `static` item + --> $DIR/issue-90873.rs:1:16 + | +LL | #![u=||{static d=||1;}] + | ^ help: provide a type for the item: `d: <type>` + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0601`. |
