Skip to content

Commit f714613

Browse files
Bail in combine if consts have different types
1 parent 9574f39 commit f714613

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

Diff for: compiler/rustc_infer/src/infer/combine.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use rustc_hir::def_id::DefId;
3434
use rustc_middle::infer::canonical::OriginalQueryValues;
3535
use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue};
3636
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
37-
use rustc_middle::traits::query::NoSolution;
3837
use rustc_middle::traits::ObligationCause;
3938
use rustc_middle::ty::error::{ExpectedFound, TypeError};
4039
use rustc_middle::ty::relate::{self, Relate, RelateResult, TypeRelation};
@@ -161,9 +160,9 @@ impl<'tcx> InferCtxt<'tcx> {
161160
//
162161
// This probe is probably not strictly necessary but it seems better to be safe and not accidentally find
163162
// ourselves with a check to find bugs being required for code to compile because it made inference progress.
164-
self.probe(|_| {
163+
let compatible_types = self.probe(|_| {
165164
if a.ty() == b.ty() {
166-
return;
165+
return Ok(());
167166
}
168167

169168
// 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> {
173172
(relation.param_env(), a.ty(), b.ty()),
174173
&mut OriginalQueryValues::default(),
175174
);
176-
177-
if let Err(NoSolution) = self.tcx.check_tys_might_be_eq(canonical) {
175+
self.tcx.check_tys_might_be_eq(canonical).map_err(|_| {
178176
self.tcx.sess.delay_span_bug(
179177
DUMMY_SP,
180178
&format!("cannot relate consts of different types (a={:?}, b={:?})", a, b,),
181-
);
182-
}
179+
)
180+
})
183181
});
184182

183+
// If the consts have differing types, just bail with a const error with
184+
// the expected const's type. Specifically, we don't want const infer vars
185+
// to do any type shapeshifting before and after resolution.
186+
if let Err(guar) = compatible_types {
187+
return Ok(self.tcx.const_error_with_guaranteed(
188+
if relation.a_is_expected() { a.ty() } else { b.ty() },
189+
guar,
190+
));
191+
}
192+
185193
match (a.kind(), b.kind()) {
186194
(
187195
ty::ConstKind::Infer(InferConst::Var(a_vid)),

0 commit comments

Comments
 (0)