diff --git a/src/librustc/mir/interpret/mod.rs b/src/librustc/mir/interpret/mod.rs index f0879bdd8ae..c62f9a049a0 100644 --- a/src/librustc/mir/interpret/mod.rs +++ b/src/librustc/mir/interpret/mod.rs @@ -148,6 +148,10 @@ pub struct LitToConstInput<'tcx> { /// Error type for `tcx.lit_to_const`. #[derive(Copy, Clone, Debug, Eq, PartialEq, HashStable)] pub enum LitToConstError { + /// The literal's inferred type did not match the expected `ty` in the input. + /// This is used for graceful error handling (`delay_span_bug`) in + /// type checking (`AstConv::ast_const_to_const`). + TypeError, UnparseableFloat, Reported, } diff --git a/src/librustc_mir_build/hair/constant.rs b/src/librustc_mir_build/hair/constant.rs index e594e1eeed0..e9dd7854275 100644 --- a/src/librustc_mir_build/hair/constant.rs +++ b/src/librustc_mir_build/hair/constant.rs @@ -1,7 +1,7 @@ use rustc::mir::interpret::{ truncate, Allocation, ConstValue, LitToConstError, LitToConstInput, Scalar, }; -use rustc::ty::{self, layout::Size, ParamEnv, TyCtxt}; +use rustc::ty::{self, layout::Size, ParamEnv, TyCtxt, TyS}; use rustc_span::symbol::Symbol; use syntax::ast; @@ -20,50 +20,35 @@ crate fn lit_to_const<'tcx>( Ok(ConstValue::Scalar(Scalar::from_uint(result, width))) }; - let lit = match *lit { - ast::LitKind::Str(ref s, _) => { + let lit = match (lit, &ty.kind) { + (ast::LitKind::Str(s, _), ty::Ref(_, TyS { kind: ty::Str, .. }, _)) => { let s = s.as_str(); let allocation = Allocation::from_byte_aligned_bytes(s.as_bytes()); let allocation = tcx.intern_const_alloc(allocation); ConstValue::Slice { data: allocation, start: 0, end: s.len() } } - ast::LitKind::ByteStr(ref data) => { - if let ty::Ref(_, ref_ty, _) = ty.kind { - match ref_ty.kind { - ty::Slice(_) => { - let allocation = Allocation::from_byte_aligned_bytes(data as &Vec); - let allocation = tcx.intern_const_alloc(allocation); - ConstValue::Slice { data: allocation, start: 0, end: data.len() } - } - ty::Array(_, _) => { - let id = tcx.allocate_bytes(data); - ConstValue::Scalar(Scalar::Ptr(id.into())) - } - _ => { - bug!("bytestring should have type of either &[u8] or &[u8; _], not {}", ty) - } - } - } else { - bug!("bytestring should have type of either &[u8] or &[u8; _], not {}", ty) - } + (ast::LitKind::ByteStr(data), ty::Ref(_, TyS { kind: ty::Slice(_), .. }, _)) => { + let allocation = Allocation::from_byte_aligned_bytes(data as &Vec); + let allocation = tcx.intern_const_alloc(allocation); + ConstValue::Slice { data: allocation, start: 0, end: data.len() } } - ast::LitKind::Byte(n) => ConstValue::Scalar(Scalar::from_uint(n, Size::from_bytes(1))), - ast::LitKind::Int(n, _) if neg => { - let n = n as i128; - let n = n.overflowing_neg().0; - trunc(n as u128)? + (ast::LitKind::ByteStr(data), ty::Ref(_, TyS { kind: ty::Array(_, _), .. }, _)) => { + let id = tcx.allocate_bytes(data); + ConstValue::Scalar(Scalar::Ptr(id.into())) } - ast::LitKind::Int(n, _) => trunc(n)?, - ast::LitKind::Float(n, _) => { - let fty = match ty.kind { - ty::Float(fty) => fty, - _ => bug!(), - }; - parse_float(n, fty, neg).map_err(|_| LitToConstError::UnparseableFloat)? + (ast::LitKind::Byte(n), ty::Uint(ast::UintTy::U8)) => { + ConstValue::Scalar(Scalar::from_uint(*n, Size::from_bytes(1))) } - ast::LitKind::Bool(b) => ConstValue::Scalar(Scalar::from_bool(b)), - ast::LitKind::Char(c) => ConstValue::Scalar(Scalar::from_char(c)), - ast::LitKind::Err(_) => return Err(LitToConstError::Reported), + (ast::LitKind::Int(n, _), ty::Uint(_)) | (ast::LitKind::Int(n, _), ty::Int(_)) => { + trunc(if neg { (*n as i128).overflowing_neg().0 as u128 } else { *n })? + } + (ast::LitKind::Float(n, _), ty::Float(fty)) => { + parse_float(*n, *fty, neg).map_err(|_| LitToConstError::UnparseableFloat)? + } + (ast::LitKind::Bool(b), ty::Bool) => ConstValue::Scalar(Scalar::from_bool(*b)), + (ast::LitKind::Char(c), ty::Char) => ConstValue::Scalar(Scalar::from_char(*c)), + (ast::LitKind::Err(_), _) => return Err(LitToConstError::Reported), + _ => return Err(LitToConstError::TypeError), }; Ok(ty::Const::from_value(tcx, lit, ty)) } diff --git a/src/librustc_mir_build/hair/cx/mod.rs b/src/librustc_mir_build/hair/cx/mod.rs index f4f7ab4bba6..f4860733db8 100644 --- a/src/librustc_mir_build/hair/cx/mod.rs +++ b/src/librustc_mir_build/hair/cx/mod.rs @@ -148,6 +148,7 @@ impl<'a, 'tcx> Cx<'a, 'tcx> { // create a dummy value and continue compiling Const::from_bits(self.tcx, 0, self.param_env.and(ty)) } + Err(LitToConstError::TypeError) => bug!("const_eval_literal: had type error"), } } diff --git a/src/librustc_mir_build/hair/pattern/mod.rs b/src/librustc_mir_build/hair/pattern/mod.rs index 91011746469..6979a98e687 100644 --- a/src/librustc_mir_build/hair/pattern/mod.rs +++ b/src/librustc_mir_build/hair/pattern/mod.rs @@ -846,6 +846,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> { PatKind::Wild } Err(LitToConstError::Reported) => PatKind::Wild, + Err(LitToConstError::TypeError) => bug!("lower_lit: had type error"), } } } diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 88e0f8b30d9..78c05a51e4f 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -2742,6 +2742,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { // mir. if let Ok(c) = tcx.at(expr.span).lit_to_const(lit_input) { return c; + } else { + tcx.sess.delay_span_bug(expr.span, "ast_const_to_const: couldn't lit_to_const"); } } diff --git a/src/test/ui/consts/issue-69310-array-size-lit-wrong-ty.rs b/src/test/ui/consts/issue-69310-array-size-lit-wrong-ty.rs new file mode 100644 index 00000000000..98be8c345a9 --- /dev/null +++ b/src/test/ui/consts/issue-69310-array-size-lit-wrong-ty.rs @@ -0,0 +1,11 @@ +// This is a regression test for #69310, which was injected by #68118. +// The issue here was that as a performance optimization, +// we call the query `lit_to_const(input);`. +// However, the literal `input.lit` would not be of the type expected by `input.ty`. +// As a result, we immediately called `bug!(...)` instead of bubbling up the problem +// so that it could be handled by the caller of `lit_to_const` (`ast_const_to_const`). + +fn main() {} + +const A: [(); 0.1] = [()]; //~ ERROR mismatched types +const B: [(); b"a"] = [()]; //~ ERROR mismatched types diff --git a/src/test/ui/consts/issue-69310-array-size-lit-wrong-ty.stderr b/src/test/ui/consts/issue-69310-array-size-lit-wrong-ty.stderr new file mode 100644 index 00000000000..7078b4bd7be --- /dev/null +++ b/src/test/ui/consts/issue-69310-array-size-lit-wrong-ty.stderr @@ -0,0 +1,15 @@ +error[E0308]: mismatched types + --> $DIR/issue-69310-array-size-lit-wrong-ty.rs:10:15 + | +LL | const A: [(); 0.1] = [()]; + | ^^^ expected `usize`, found floating-point number + +error[E0308]: mismatched types + --> $DIR/issue-69310-array-size-lit-wrong-ty.rs:11:15 + | +LL | const B: [(); b"a"] = [()]; + | ^^^^ expected `usize`, found `&[u8; 1]` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`.