Skip to content

Commit 9b87d22

Browse files
committed
Don't abort on unevaluated constants without at least tryting to eval them
1 parent 437f017 commit 9b87d22

File tree

33 files changed

+80
-64
lines changed

33 files changed

+80
-64
lines changed

src/librustc/middle/mem_categorization.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
896896

897897
// Always promote `[T; 0]` (even when e.g., borrowed mutably).
898898
let promotable = match expr_ty.sty {
899-
ty::Array(_, len) if len.assert_usize(self.tcx) == Some(0) => true,
899+
ty::Array(_, len) if len.try_eval_usize(self.tcx) == Some(0) => true,
900900
_ => promotable,
901901
};
902902

src/librustc/mir/tcx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<'tcx> PlaceTy<'tcx> {
9090
ProjectionElem::Subslice { from, to } => {
9191
PlaceTy::from_ty(match self.ty.sty {
9292
ty::Array(inner, size) => {
93-
let size = size.unwrap_usize(tcx);
93+
let size = size.eval_usize(tcx);
9494
let len = size - (from as u64) - (to as u64);
9595
tcx.mk_array(inner, len)
9696
}

src/librustc/traits/error_reporting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
417417
Some(format!("[{}]", self.tcx.type_of(def.did).to_string())),
418418
));
419419
let tcx = self.tcx;
420-
if let Some(len) = len.assert_usize(tcx) {
420+
if let Some(len) = len.try_eval_usize(tcx) {
421421
flags.push((
422422
sym::_Self,
423423
Some(format!("[{}; {}]", self.tcx.type_of(def.did).to_string(), len)),

src/librustc/ty/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl<'tcx> ty::TyS<'tcx> {
194194
ty::Foreign(def_id) => format!("extern type `{}`", tcx.def_path_str(def_id)).into(),
195195
ty::Array(_, n) => {
196196
let n = tcx.lift_to_global(&n).unwrap();
197-
match n.assert_usize(tcx) {
197+
match n.try_eval_usize(tcx) {
198198
Some(n) => format!("array of {} elements", n).into(),
199199
None => "array".into(),
200200
}

src/librustc/ty/inhabitedness/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl<'tcx> TyS<'tcx> {
190190
}))
191191
}
192192

193-
Array(ty, len) => match len.assert_usize(tcx) {
193+
Array(ty, len) => match len.try_eval_usize(tcx) {
194194
// If the array is definitely non-empty, it's uninhabited if
195195
// the type of its elements is uninhabited.
196196
Some(n) if n != 0 => ty.uninhabited_from(tcx),

src/librustc/ty/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
594594
}
595595
}
596596

597-
let count = count.assert_usize(tcx).ok_or(LayoutError::Unknown(ty))?;
597+
let count = count.try_eval_usize(tcx).ok_or(LayoutError::Unknown(ty))?;
598598
let element = self.layout_of(element)?;
599599
let size = element.size.checked_mul(count, dl)
600600
.ok_or(LayoutError::SizeOverflow(ty))?;

src/librustc/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2368,7 +2368,7 @@ impl<'tcx> AdtDef {
23682368
match tcx.const_eval(param_env.and(cid)) {
23692369
Ok(val) => {
23702370
// FIXME: Find the right type and use it instead of `val.ty` here
2371-
if let Some(b) = val.assert_bits(tcx.global_tcx(), val.ty) {
2371+
if let Some(b) = val.try_eval_bits(tcx.global_tcx(), val.ty) {
23722372
trace!("discriminants: {} ({:?})", b, repr_type);
23732373
Some(Discr {
23742374
val: b,

src/librustc/ty/print/obsolete.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl DefPathBasedNames<'tcx> {
8989
ty::Array(inner_type, len) => {
9090
output.push('[');
9191
self.push_type_name(inner_type, output, debug);
92-
write!(output, "; {}", len.unwrap_usize(self.tcx)).unwrap();
92+
write!(output, "; {}", len.eval_usize(self.tcx)).unwrap();
9393
output.push(']');
9494
}
9595
ty::Slice(inner_type) => {

src/librustc/ty/print/pretty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ pub trait PrettyPrinter<'tcx>:
696696
},
697697
ty::Array(ty, sz) => {
698698
p!(write("["), print(ty), write("; "));
699-
if let Some(n) = sz.assert_usize(self.tcx()) {
699+
if let Some(n) = sz.try_eval_usize(self.tcx()) {
700700
p!(write("{}", n));
701701
} else {
702702
p!(write("_"));
@@ -915,7 +915,7 @@ pub trait PrettyPrinter<'tcx>:
915915
if let ty::Ref(_, ref_ty, _) = ct.ty.sty {
916916
let byte_str = match (ct.val, &ref_ty.sty) {
917917
(ConstValue::Scalar(Scalar::Ptr(ptr)), ty::Array(t, n)) if *t == u8 => {
918-
let n = n.unwrap_usize(self.tcx());
918+
let n = n.eval_usize(self.tcx());
919919
Some(self.tcx()
920920
.alloc_map.lock()
921921
.unwrap_memory(ptr.alloc_id)

src/librustc/ty/relate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ pub fn super_relate_tys<R: TypeRelation<'tcx>>(
466466
Err(err) => {
467467
// Check whether the lengths are both concrete/known values,
468468
// but are unequal, for better diagnostics.
469-
match (sz_a.assert_usize(tcx), sz_b.assert_usize(tcx)) {
469+
match (sz_a.try_eval_usize(tcx), sz_b.try_eval_usize(tcx)) {
470470
(Some(sz_a_val), Some(sz_b_val)) => {
471471
Err(TypeError::FixedArraySize(
472472
expected_found(relation, &sz_a_val, &sz_b_val)

0 commit comments

Comments
 (0)