@@ -34,7 +34,6 @@ use rustc_hir::def_id::DefId;
34
34
use rustc_middle:: infer:: canonical:: OriginalQueryValues ;
35
35
use rustc_middle:: infer:: unify_key:: { ConstVarValue , ConstVariableValue } ;
36
36
use rustc_middle:: infer:: unify_key:: { ConstVariableOrigin , ConstVariableOriginKind } ;
37
- use rustc_middle:: traits:: query:: NoSolution ;
38
37
use rustc_middle:: traits:: ObligationCause ;
39
38
use rustc_middle:: ty:: error:: { ExpectedFound , TypeError } ;
40
39
use rustc_middle:: ty:: relate:: { self , Relate , RelateResult , TypeRelation } ;
@@ -161,9 +160,9 @@ impl<'tcx> InferCtxt<'tcx> {
161
160
//
162
161
// This probe is probably not strictly necessary but it seems better to be safe and not accidentally find
163
162
// 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 ( |_| {
165
164
if a. ty ( ) == b. ty ( ) {
166
- return ;
165
+ return Ok ( ( ) ) ;
167
166
}
168
167
169
168
// 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> {
173
172
( relation. param_env ( ) , a. ty ( ) , b. ty ( ) ) ,
174
173
& mut OriginalQueryValues :: default ( ) ,
175
174
) ;
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 ( |_| {
178
176
self . tcx . sess . delay_span_bug (
179
177
DUMMY_SP ,
180
178
& format ! ( "cannot relate consts of different types (a={:?}, b={:?})" , a, b, ) ,
181
- ) ;
182
- }
179
+ )
180
+ } )
183
181
} ) ;
184
182
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
+
185
193
match ( a. kind ( ) , b. kind ( ) ) {
186
194
(
187
195
ty:: ConstKind :: Infer ( InferConst :: Var ( a_vid) ) ,
0 commit comments