lit_to_const: gracefully bubble up type errors.
This commit is contained in:
parent
bfb96048b5
commit
748dd455ad
|
@ -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,
|
||||
}
|
||||
|
|
|
@ -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<u8>);
|
||||
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<u8>);
|
||||
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))
|
||||
}
|
||||
|
|
|
@ -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"),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2740,6 +2740,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");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
|
@ -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`.
|
Loading…
Reference in New Issue