From 335427a3db8fcdbddece8c7257e0ab40c8230d73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Tue, 16 Mar 2021 00:00:00 +0000 Subject: [PATCH] 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. --- compiler/rustc_middle/src/ty/context.rs | 15 +++++++++------ .../invalid_rustc_layout_scalar_valid_range.rs | 7 +++++++ ...invalid_rustc_layout_scalar_valid_range.stderr | 8 +++++++- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index e1d79248171a8..d5ad459126070 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -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), diff --git a/src/test/ui/invalid/invalid_rustc_layout_scalar_valid_range.rs b/src/test/ui/invalid/invalid_rustc_layout_scalar_valid_range.rs index 25fe4be660b24..06cf8c0f0f6d5 100644 --- a/src/test/ui/invalid/invalid_rustc_layout_scalar_valid_range.rs +++ b/src/test/ui/invalid/invalid_rustc_layout_scalar_valid_range.rs @@ -15,6 +15,13 @@ enum E { Y = 14, } +#[rustc_layout_scalar_valid_range_start(rustc_layout_scalar_valid_range_start)] //~ ERROR +struct NonZero(T); + +fn not_field() -> impl Send { + NonZero(false) +} + fn main() { let _ = A(0); let _ = B(0); diff --git a/src/test/ui/invalid/invalid_rustc_layout_scalar_valid_range.stderr b/src/test/ui/invalid/invalid_rustc_layout_scalar_valid_range.stderr index 7e95fedebdfc6..7879e7358c00a 100644 --- a/src/test/ui/invalid/invalid_rustc_layout_scalar_valid_range.stderr +++ b/src/test/ui/invalid/invalid_rustc_layout_scalar_valid_range.stderr @@ -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