Skip to content

Commit 335427a

Browse files
committed
Use delay_span_bug instead of panic in layout_scalar_valid_range
83054 introduced validation of scalar range attributes, but panicking code that uses the attribute remained reachable. Use `delay_span_bug` instead to avoid the ICE.
1 parent 195ad48 commit 335427a

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

compiler/rustc_middle/src/ty/context.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -1091,13 +1091,16 @@ impl<'tcx> TyCtxt<'tcx> {
10911091
None => return Bound::Unbounded,
10921092
};
10931093
debug!("layout_scalar_valid_range: attr={:?}", attr);
1094-
for meta in attr.meta_item_list().expect("rustc_layout_scalar_valid_range takes args") {
1095-
match meta.literal().expect("attribute takes lit").kind {
1096-
ast::LitKind::Int(a, _) => return Bound::Included(a),
1097-
_ => span_bug!(attr.span, "rustc_layout_scalar_valid_range expects int arg"),
1098-
}
1094+
if let Some(
1095+
&[ast::NestedMetaItem::Literal(ast::Lit { kind: ast::LitKind::Int(a, _), .. })],
1096+
) = attr.meta_item_list().as_deref()
1097+
{
1098+
Bound::Included(a)
1099+
} else {
1100+
self.sess
1101+
.delay_span_bug(attr.span, "invalid rustc_layout_scalar_valid_range attribute");
1102+
Bound::Unbounded
10991103
}
1100-
span_bug!(attr.span, "no arguments to `rustc_layout_scalar_valid_range` attribute");
11011104
};
11021105
(
11031106
get(sym::rustc_layout_scalar_valid_range_start),

src/test/ui/invalid/invalid_rustc_layout_scalar_valid_range.rs

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ enum E {
1515
Y = 14,
1616
}
1717

18+
#[rustc_layout_scalar_valid_range_start(rustc_layout_scalar_valid_range_start)] //~ ERROR
19+
struct NonZero<T>(T);
20+
21+
fn not_field() -> impl Send {
22+
NonZero(false)
23+
}
24+
1825
fn main() {
1926
let _ = A(0);
2027
let _ = B(0);

src/test/ui/invalid/invalid_rustc_layout_scalar_valid_range.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,11 @@ LL | | Y = 14,
2727
LL | | }
2828
| |_- not a struct
2929

30-
error: aborting due to 4 previous errors
30+
error: expected exactly one integer literal argument
31+
--> $DIR/invalid_rustc_layout_scalar_valid_range.rs:18:1
32+
|
33+
LL | #[rustc_layout_scalar_valid_range_start(rustc_layout_scalar_valid_range_start)]
34+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
35+
36+
error: aborting due to 5 previous errors
3137

0 commit comments

Comments
 (0)