Skip to content

Commit

Permalink
Use delay_span_bug instead of panic in layout_scalar_valid_range
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tmiasko committed Mar 16, 2021
1 parent 195ad48 commit 335427a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
15 changes: 9 additions & 6 deletions compiler/rustc_middle/src/ty/context.rs
Expand Up @@ -1091,13 +1091,16 @@ impl<'tcx> TyCtxt<'tcx> {
None => return Bound::Unbounded,
};
debug!("layout_scalar_valid_range: attr={:?}", attr);
for meta in attr.meta_item_list().expect("rustc_layout_scalar_valid_range takes args") {
match meta.literal().expect("attribute takes lit").kind {
ast::LitKind::Int(a, _) => return Bound::Included(a),
_ => span_bug!(attr.span, "rustc_layout_scalar_valid_range expects int arg"),
}
if let Some(
&[ast::NestedMetaItem::Literal(ast::Lit { kind: ast::LitKind::Int(a, _), .. })],
) = attr.meta_item_list().as_deref()
{
Bound::Included(a)
} else {
self.sess
.delay_span_bug(attr.span, "invalid rustc_layout_scalar_valid_range attribute");
Bound::Unbounded
}
span_bug!(attr.span, "no arguments to `rustc_layout_scalar_valid_range` attribute");
};
(
get(sym::rustc_layout_scalar_valid_range_start),
Expand Down
Expand Up @@ -15,6 +15,13 @@ enum E {
Y = 14,
}

#[rustc_layout_scalar_valid_range_start(rustc_layout_scalar_valid_range_start)] //~ ERROR
struct NonZero<T>(T);

fn not_field() -> impl Send {
NonZero(false)
}

fn main() {
let _ = A(0);
let _ = B(0);
Expand Down
Expand Up @@ -27,5 +27,11 @@ LL | | Y = 14,
LL | | }
| |_- not a struct

error: aborting due to 4 previous errors
error: expected exactly one integer literal argument
--> $DIR/invalid_rustc_layout_scalar_valid_range.rs:18:1
|
LL | #[rustc_layout_scalar_valid_range_start(rustc_layout_scalar_valid_range_start)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 5 previous errors

0 comments on commit 335427a

Please sign in to comment.