1
Fork 0

Bail in combine if consts have different types

This commit is contained in:
Michael Goulet 2023-03-09 20:52:06 +00:00
parent 9574f39c2d
commit f7146136d7

View file

@ -34,7 +34,6 @@ use rustc_hir::def_id::DefId;
use rustc_middle::infer::canonical::OriginalQueryValues; use rustc_middle::infer::canonical::OriginalQueryValues;
use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue}; use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue};
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind}; use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
use rustc_middle::traits::query::NoSolution;
use rustc_middle::traits::ObligationCause; use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::relate::{self, Relate, RelateResult, TypeRelation}; use rustc_middle::ty::relate::{self, Relate, RelateResult, TypeRelation};
@ -161,9 +160,9 @@ impl<'tcx> InferCtxt<'tcx> {
// //
// This probe is probably not strictly necessary but it seems better to be safe and not accidentally find // This probe is probably not strictly necessary but it seems better to be safe and not accidentally find
// ourselves with a check to find bugs being required for code to compile because it made inference progress. // ourselves with a check to find bugs being required for code to compile because it made inference progress.
self.probe(|_| { let compatible_types = self.probe(|_| {
if a.ty() == b.ty() { if a.ty() == b.ty() {
return; return Ok(());
} }
// We don't have access to trait solving machinery in `rustc_infer` so the logic for determining if the // We don't have access to trait solving machinery in `rustc_infer` so the logic for determining if the
@ -173,15 +172,24 @@ impl<'tcx> InferCtxt<'tcx> {
(relation.param_env(), a.ty(), b.ty()), (relation.param_env(), a.ty(), b.ty()),
&mut OriginalQueryValues::default(), &mut OriginalQueryValues::default(),
); );
self.tcx.check_tys_might_be_eq(canonical).map_err(|_| {
if let Err(NoSolution) = self.tcx.check_tys_might_be_eq(canonical) {
self.tcx.sess.delay_span_bug( self.tcx.sess.delay_span_bug(
DUMMY_SP, DUMMY_SP,
&format!("cannot relate consts of different types (a={:?}, b={:?})", a, b,), &format!("cannot relate consts of different types (a={:?}, b={:?})", a, b,),
); )
} })
}); });
// If the consts have differing types, just bail with a const error with
// the expected const's type. Specifically, we don't want const infer vars
// to do any type shapeshifting before and after resolution.
if let Err(guar) = compatible_types {
return Ok(self.tcx.const_error_with_guaranteed(
if relation.a_is_expected() { a.ty() } else { b.ty() },
guar,
));
}
match (a.kind(), b.kind()) { match (a.kind(), b.kind()) {
( (
ty::ConstKind::Infer(InferConst::Var(a_vid)), ty::ConstKind::Infer(InferConst::Var(a_vid)),