@@ -3,7 +3,7 @@ use rustc_hir::def_id::DefId;
3
3
use rustc_infer:: infer:: at:: ToTrace ;
4
4
use rustc_infer:: infer:: canonical:: { Canonical , QueryResponse } ;
5
5
use rustc_infer:: infer:: { DefiningAnchor , InferCtxt , TyCtxtInferExt } ;
6
- use rustc_infer:: traits:: TraitEngineExt as _;
6
+ use rustc_infer:: traits:: { ObligationCauseCode , TraitEngineExt as _} ;
7
7
use rustc_middle:: ty:: query:: Providers ;
8
8
use rustc_middle:: ty:: subst:: { GenericArg , Subst , UserSelfTy , UserSubsts } ;
9
9
use rustc_middle:: ty:: {
@@ -22,6 +22,7 @@ use rustc_trait_selection::traits::query::type_op::subtype::Subtype;
22
22
use rustc_trait_selection:: traits:: query:: { Fallible , NoSolution } ;
23
23
use rustc_trait_selection:: traits:: { Normalized , Obligation , ObligationCause , TraitEngine } ;
24
24
use std:: fmt;
25
+ use std:: iter:: zip;
25
26
26
27
pub ( crate ) fn provide ( p : & mut Providers ) {
27
28
* p = Providers {
@@ -61,14 +62,15 @@ pub fn type_op_ascribe_user_type_with_span<'a, 'tcx: 'a>(
61
62
mir_ty, def_id, user_substs
62
63
) ;
63
64
64
- let mut cx = AscribeUserTypeCx { infcx, param_env, fulfill_cx } ;
65
- cx. relate_mir_and_user_ty ( mir_ty, def_id, user_substs, span ) ?;
65
+ let mut cx = AscribeUserTypeCx { infcx, param_env, span : span . unwrap_or ( DUMMY_SP ) , fulfill_cx } ;
66
+ cx. relate_mir_and_user_ty ( mir_ty, def_id, user_substs) ?;
66
67
Ok ( ( ) )
67
68
}
68
69
69
70
struct AscribeUserTypeCx < ' me , ' tcx > {
70
71
infcx : & ' me InferCtxt < ' me , ' tcx > ,
71
72
param_env : ParamEnv < ' tcx > ,
73
+ span : Span ,
72
74
fulfill_cx : & ' me mut dyn TraitEngine < ' tcx > ,
73
75
}
74
76
@@ -79,7 +81,7 @@ impl<'me, 'tcx> AscribeUserTypeCx<'me, 'tcx> {
79
81
{
80
82
self . infcx
81
83
. partially_normalize_associated_types_in (
82
- ObligationCause :: misc ( DUMMY_SP , hir:: CRATE_HIR_ID ) ,
84
+ ObligationCause :: misc ( self . span , hir:: CRATE_HIR_ID ) ,
83
85
self . param_env ,
84
86
value,
85
87
)
@@ -91,18 +93,13 @@ impl<'me, 'tcx> AscribeUserTypeCx<'me, 'tcx> {
91
93
T : ToTrace < ' tcx > ,
92
94
{
93
95
self . infcx
94
- . at ( & ObligationCause :: dummy ( ) , self . param_env )
96
+ . at ( & ObligationCause :: dummy_with_span ( self . span ) , self . param_env )
95
97
. relate ( a, variance, b) ?
96
98
. into_value_registering_obligations ( self . infcx , self . fulfill_cx ) ;
97
99
Ok ( ( ) )
98
100
}
99
101
100
- fn prove_predicate ( & mut self , predicate : Predicate < ' tcx > , span : Option < Span > ) {
101
- let cause = if let Some ( span) = span {
102
- ObligationCause :: dummy_with_span ( span)
103
- } else {
104
- ObligationCause :: dummy ( )
105
- } ;
102
+ fn prove_predicate ( & mut self , predicate : Predicate < ' tcx > , cause : ObligationCause < ' tcx > ) {
106
103
self . fulfill_cx . register_predicate_obligation (
107
104
self . infcx ,
108
105
Obligation :: new ( cause, self . param_env , predicate) ,
@@ -126,7 +123,6 @@ impl<'me, 'tcx> AscribeUserTypeCx<'me, 'tcx> {
126
123
mir_ty : Ty < ' tcx > ,
127
124
def_id : DefId ,
128
125
user_substs : UserSubsts < ' tcx > ,
129
- span : Option < Span > ,
130
126
) -> Result < ( ) , NoSolution > {
131
127
let UserSubsts { user_self_ty, substs } = user_substs;
132
128
let tcx = self . tcx ( ) ;
@@ -145,10 +141,20 @@ impl<'me, 'tcx> AscribeUserTypeCx<'me, 'tcx> {
145
141
// outlives" error messages.
146
142
let instantiated_predicates =
147
143
self . tcx ( ) . predicates_of ( def_id) . instantiate ( self . tcx ( ) , substs) ;
144
+
145
+ let cause = ObligationCause :: dummy_with_span ( self . span ) ;
146
+
148
147
debug ! ( ?instantiated_predicates) ;
149
- for instantiated_predicate in instantiated_predicates. predicates {
150
- let instantiated_predicate = self . normalize ( instantiated_predicate) ;
151
- self . prove_predicate ( instantiated_predicate, span) ;
148
+ for ( instantiated_predicate, predicate_span) in
149
+ zip ( instantiated_predicates. predicates , instantiated_predicates. spans )
150
+ {
151
+ let span = if self . span == DUMMY_SP { predicate_span } else { self . span } ;
152
+ let cause = ObligationCause :: new (
153
+ span,
154
+ hir:: CRATE_HIR_ID ,
155
+ ObligationCauseCode :: AscribeUserTypeProvePredicate ( predicate_span) ,
156
+ ) ;
157
+ self . prove_predicate ( instantiated_predicate, cause) ;
152
158
}
153
159
154
160
if let Some ( UserSelfTy { impl_def_id, self_ty } ) = user_self_ty {
@@ -161,7 +167,7 @@ impl<'me, 'tcx> AscribeUserTypeCx<'me, 'tcx> {
161
167
self . prove_predicate (
162
168
ty:: Binder :: dummy ( ty:: PredicateKind :: WellFormed ( impl_self_ty. into ( ) ) )
163
169
. to_predicate ( self . tcx ( ) ) ,
164
- span ,
170
+ cause . clone ( ) ,
165
171
) ;
166
172
}
167
173
@@ -178,7 +184,7 @@ impl<'me, 'tcx> AscribeUserTypeCx<'me, 'tcx> {
178
184
// which...could happen with normalization...
179
185
self . prove_predicate (
180
186
ty:: Binder :: dummy ( ty:: PredicateKind :: WellFormed ( ty. into ( ) ) ) . to_predicate ( self . tcx ( ) ) ,
181
- span ,
187
+ cause ,
182
188
) ;
183
189
Ok ( ( ) )
184
190
}
0 commit comments