Skip to content

Commit 212aa3e

Browse files
committed
Auto merge of #69330 - Centril:literally-melting-ice, r=eddyb
`lit_to_const`: gracefully bubble up type errors. Fixes #69310 which was injected by #68118. r? @pnkfelix @varkor @eddyb cc @Skinny121
2 parents 01a8b5f + 748dd45 commit 212aa3e

File tree

7 files changed

+56
-37
lines changed

7 files changed

+56
-37
lines changed

src/librustc/mir/interpret/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ pub struct LitToConstInput<'tcx> {
148148
/// Error type for `tcx.lit_to_const`.
149149
#[derive(Copy, Clone, Debug, Eq, PartialEq, HashStable)]
150150
pub enum LitToConstError {
151+
/// The literal's inferred type did not match the expected `ty` in the input.
152+
/// This is used for graceful error handling (`delay_span_bug`) in
153+
/// type checking (`AstConv::ast_const_to_const`).
154+
TypeError,
151155
UnparseableFloat,
152156
Reported,
153157
}

src/librustc_mir_build/hair/constant.rs

+22-37
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc::mir::interpret::{
22
truncate, Allocation, ConstValue, LitToConstError, LitToConstInput, Scalar,
33
};
4-
use rustc::ty::{self, layout::Size, ParamEnv, TyCtxt};
4+
use rustc::ty::{self, layout::Size, ParamEnv, TyCtxt, TyS};
55
use rustc_span::symbol::Symbol;
66
use syntax::ast;
77

@@ -20,50 +20,35 @@ crate fn lit_to_const<'tcx>(
2020
Ok(ConstValue::Scalar(Scalar::from_uint(result, width)))
2121
};
2222

23-
let lit = match *lit {
24-
ast::LitKind::Str(ref s, _) => {
23+
let lit = match (lit, &ty.kind) {
24+
(ast::LitKind::Str(s, _), ty::Ref(_, TyS { kind: ty::Str, .. }, _)) => {
2525
let s = s.as_str();
2626
let allocation = Allocation::from_byte_aligned_bytes(s.as_bytes());
2727
let allocation = tcx.intern_const_alloc(allocation);
2828
ConstValue::Slice { data: allocation, start: 0, end: s.len() }
2929
}
30-
ast::LitKind::ByteStr(ref data) => {
31-
if let ty::Ref(_, ref_ty, _) = ty.kind {
32-
match ref_ty.kind {
33-
ty::Slice(_) => {
34-
let allocation = Allocation::from_byte_aligned_bytes(data as &Vec<u8>);
35-
let allocation = tcx.intern_const_alloc(allocation);
36-
ConstValue::Slice { data: allocation, start: 0, end: data.len() }
37-
}
38-
ty::Array(_, _) => {
39-
let id = tcx.allocate_bytes(data);
40-
ConstValue::Scalar(Scalar::Ptr(id.into()))
41-
}
42-
_ => {
43-
bug!("bytestring should have type of either &[u8] or &[u8; _], not {}", ty)
44-
}
45-
}
46-
} else {
47-
bug!("bytestring should have type of either &[u8] or &[u8; _], not {}", ty)
48-
}
30+
(ast::LitKind::ByteStr(data), ty::Ref(_, TyS { kind: ty::Slice(_), .. }, _)) => {
31+
let allocation = Allocation::from_byte_aligned_bytes(data as &Vec<u8>);
32+
let allocation = tcx.intern_const_alloc(allocation);
33+
ConstValue::Slice { data: allocation, start: 0, end: data.len() }
34+
}
35+
(ast::LitKind::ByteStr(data), ty::Ref(_, TyS { kind: ty::Array(_, _), .. }, _)) => {
36+
let id = tcx.allocate_bytes(data);
37+
ConstValue::Scalar(Scalar::Ptr(id.into()))
38+
}
39+
(ast::LitKind::Byte(n), ty::Uint(ast::UintTy::U8)) => {
40+
ConstValue::Scalar(Scalar::from_uint(*n, Size::from_bytes(1)))
4941
}
50-
ast::LitKind::Byte(n) => ConstValue::Scalar(Scalar::from_uint(n, Size::from_bytes(1))),
51-
ast::LitKind::Int(n, _) if neg => {
52-
let n = n as i128;
53-
let n = n.overflowing_neg().0;
54-
trunc(n as u128)?
42+
(ast::LitKind::Int(n, _), ty::Uint(_)) | (ast::LitKind::Int(n, _), ty::Int(_)) => {
43+
trunc(if neg { (*n as i128).overflowing_neg().0 as u128 } else { *n })?
5544
}
56-
ast::LitKind::Int(n, _) => trunc(n)?,
57-
ast::LitKind::Float(n, _) => {
58-
let fty = match ty.kind {
59-
ty::Float(fty) => fty,
60-
_ => bug!(),
61-
};
62-
parse_float(n, fty, neg).map_err(|_| LitToConstError::UnparseableFloat)?
45+
(ast::LitKind::Float(n, _), ty::Float(fty)) => {
46+
parse_float(*n, *fty, neg).map_err(|_| LitToConstError::UnparseableFloat)?
6347
}
64-
ast::LitKind::Bool(b) => ConstValue::Scalar(Scalar::from_bool(b)),
65-
ast::LitKind::Char(c) => ConstValue::Scalar(Scalar::from_char(c)),
66-
ast::LitKind::Err(_) => return Err(LitToConstError::Reported),
48+
(ast::LitKind::Bool(b), ty::Bool) => ConstValue::Scalar(Scalar::from_bool(*b)),
49+
(ast::LitKind::Char(c), ty::Char) => ConstValue::Scalar(Scalar::from_char(*c)),
50+
(ast::LitKind::Err(_), _) => return Err(LitToConstError::Reported),
51+
_ => return Err(LitToConstError::TypeError),
6752
};
6853
Ok(ty::Const::from_value(tcx, lit, ty))
6954
}

src/librustc_mir_build/hair/cx/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ impl<'a, 'tcx> Cx<'a, 'tcx> {
148148
// create a dummy value and continue compiling
149149
Const::from_bits(self.tcx, 0, self.param_env.and(ty))
150150
}
151+
Err(LitToConstError::TypeError) => bug!("const_eval_literal: had type error"),
151152
}
152153
}
153154

src/librustc_mir_build/hair/pattern/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
846846
PatKind::Wild
847847
}
848848
Err(LitToConstError::Reported) => PatKind::Wild,
849+
Err(LitToConstError::TypeError) => bug!("lower_lit: had type error"),
849850
}
850851
}
851852
}

src/librustc_typeck/astconv.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2742,6 +2742,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
27422742
// mir.
27432743
if let Ok(c) = tcx.at(expr.span).lit_to_const(lit_input) {
27442744
return c;
2745+
} else {
2746+
tcx.sess.delay_span_bug(expr.span, "ast_const_to_const: couldn't lit_to_const");
27452747
}
27462748
}
27472749

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This is a regression test for #69310, which was injected by #68118.
2+
// The issue here was that as a performance optimization,
3+
// we call the query `lit_to_const(input);`.
4+
// However, the literal `input.lit` would not be of the type expected by `input.ty`.
5+
// As a result, we immediately called `bug!(...)` instead of bubbling up the problem
6+
// so that it could be handled by the caller of `lit_to_const` (`ast_const_to_const`).
7+
8+
fn main() {}
9+
10+
const A: [(); 0.1] = [()]; //~ ERROR mismatched types
11+
const B: [(); b"a"] = [()]; //~ ERROR mismatched types
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-69310-array-size-lit-wrong-ty.rs:10:15
3+
|
4+
LL | const A: [(); 0.1] = [()];
5+
| ^^^ expected `usize`, found floating-point number
6+
7+
error[E0308]: mismatched types
8+
--> $DIR/issue-69310-array-size-lit-wrong-ty.rs:11:15
9+
|
10+
LL | const B: [(); b"a"] = [()];
11+
| ^^^^ expected `usize`, found `&[u8; 1]`
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)