Skip to content

Commit b1d67b2

Browse files
Rollup merge of rust-lang#139391 - TaKO8Ki:check-if-merged-attrs-list-is-empty, r=jdonszelmann
Check if merged attributes list is empty in expr Fixes rust-lang#139373 In the example code, an [`UnrecognizedReprHint`](https://github.com/rust-lang/rust/blob/6b5ccfc87f59ab017032e430d4d358b4989735da/compiler/rustc_attr_parsing/src/attributes/repr.rs#L155) error is output, and the list of merged attributes becomes empty. This causes a [panic](https://github.com/rust-lang/rust/blob/6b5ccfc87f59ab017032e430d4d358b4989735da/compiler/rustc_ast_lowering/src/lib.rs#L618) to occur. So, it's necessary to check if merged attributes list is empty as other functions do. ref: https://github.com/rust-lang/rust/blob/6b5ccfc87f59ab017032e430d4d358b4989735da/compiler/rustc_ast_lowering/src/lib.rs#L896
2 parents ed81e34 + 6b5ccfc commit b1d67b2

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

Diff for: compiler/rustc_ast_lowering/src/expr.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
7474
// Merge attributes into the inner expression.
7575
if !e.attrs.is_empty() {
7676
let old_attrs = self.attrs.get(&ex.hir_id.local_id).copied().unwrap_or(&[]);
77-
self.attrs.insert(
78-
ex.hir_id.local_id,
79-
&*self.arena.alloc_from_iter(
80-
self.lower_attrs_vec(&e.attrs, e.span)
81-
.into_iter()
82-
.chain(old_attrs.iter().cloned()),
83-
),
77+
let attrs = &*self.arena.alloc_from_iter(
78+
self.lower_attrs_vec(&e.attrs, e.span)
79+
.into_iter()
80+
.chain(old_attrs.iter().cloned()),
8481
);
82+
if attrs.is_empty() {
83+
return ex;
84+
}
85+
86+
self.attrs.insert(ex.hir_id.local_id, attrs);
8587
}
8688
return ex;
8789
}

Diff for: tests/ui/attributes/invalid-reprs.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
let y = #[repr(uwu(4))]
3+
//~^ ERROR attributes on expressions are experimental
4+
//~| ERROR unrecognized representation hint
5+
(&id(5)); //~ ERROR: cannot find function `id` in this scope
6+
}

Diff for: tests/ui/attributes/invalid-reprs.stderr

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0658]: attributes on expressions are experimental
2+
--> $DIR/invalid-reprs.rs:2:13
3+
|
4+
LL | let y = #[repr(uwu(4))]
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information
8+
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error[E0425]: cannot find function `id` in this scope
12+
--> $DIR/invalid-reprs.rs:5:7
13+
|
14+
LL | (&id(5));
15+
| ^^ not found in this scope
16+
|
17+
help: consider importing this function
18+
|
19+
LL + use std::process::id;
20+
|
21+
22+
error[E0552]: unrecognized representation hint
23+
--> $DIR/invalid-reprs.rs:2:20
24+
|
25+
LL | let y = #[repr(uwu(4))]
26+
| ^^^^^^
27+
|
28+
= help: valid reprs are `Rust` (default), `C`, `align`, `packed`, `transparent`, `simd`, `i8`, `u8`, `i16`, `u16`, `i32`, `u32`, `i64`, `u64`, `i128`, `u128`, `isize`, `usize`
29+
30+
error: aborting due to 3 previous errors
31+
32+
Some errors have detailed explanations: E0425, E0552, E0658.
33+
For more information about an error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)