Skip to content

Commit 73f7e42

Browse files
committed
Relate identical parameters in array lengths
1 parent 7212685 commit 73f7e42

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

src/librustc/ty/relate.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::ty::subst::{Kind, UnpackedKind, SubstsRef};
99
use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
1010
use crate::ty::error::{ExpectedFound, TypeError};
1111
use crate::mir::interpret::{GlobalId, ConstValue, Scalar};
12-
use crate::util::common::ErrorReported;
1312
use syntax_pos::DUMMY_SP;
1413
use std::rc::Rc;
1514
use std::iter;
@@ -474,8 +473,9 @@ pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
474473
(&ty::Array(a_t, sz_a), &ty::Array(b_t, sz_b)) =>
475474
{
476475
let t = relation.relate(&a_t, &b_t)?;
477-
let to_u64 = |x: ty::Const<'tcx>| -> Result<u64, ErrorReported> {
478-
match x.val {
476+
477+
let to_u64 = |ct: &'tcx ty::Const<'tcx>| -> Option<u64> {
478+
match ct.val {
479479
// FIXME(const_generics): this doesn't work right now,
480480
// because it tries to relate an `Infer` to a `Param`.
481481
ConstValue::Unevaluated(def_id, substs) => {
@@ -493,36 +493,32 @@ pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
493493
instance,
494494
promoted: None,
495495
};
496-
if let Some(s) = tcx.const_eval(param_env.and(cid))
497-
.ok()
498-
.map(|c| c.unwrap_usize(tcx)) {
499-
return Ok(s)
500-
}
496+
return tcx.const_eval(param_env.and(cid))
497+
.ok()
498+
.map(|c| c.unwrap_usize(tcx));
501499
}
502500
}
503-
tcx.sess.delay_span_bug(tcx.def_span(def_id),
504-
"array length could not be evaluated");
505-
Err(ErrorReported)
501+
None
506502
}
507-
_ => x.assert_usize(tcx).ok_or_else(|| {
508-
tcx.sess.delay_span_bug(DUMMY_SP,
509-
"array length could not be evaluated");
510-
ErrorReported
511-
})
503+
_ => ct.assert_usize(tcx),
512504
}
513505
};
514-
match (to_u64(*sz_a), to_u64(*sz_b)) {
515-
(Ok(sz_a_u64), Ok(sz_b_u64)) => {
506+
match (to_u64(sz_a), to_u64(sz_b)) {
507+
(Some(sz_a_u64), Some(sz_b_u64)) => {
516508
if sz_a_u64 == sz_b_u64 {
517509
Ok(tcx.mk_ty(ty::Array(t, sz_a)))
518510
} else {
519511
Err(TypeError::FixedArraySize(
520512
expected_found(relation, &sz_a_u64, &sz_b_u64)))
521513
}
522514
}
523-
// We reported an error or will ICE, so we can return Error.
524-
(Err(ErrorReported), _) | (_, Err(ErrorReported)) => {
525-
Ok(tcx.types.err)
515+
_ => {
516+
if let Ok(sz) = relation.relate(&sz_a, &sz_b) {
517+
Ok(tcx.mk_ty(ty::Array(t, sz)))
518+
} else {
519+
tcx.sess.delay_span_bug(DUMMY_SP, "array length could not be evaluated");
520+
Ok(tcx.types.err)
521+
}
526522
}
527523
}
528524
}

0 commit comments

Comments
 (0)