@@ -3,7 +3,7 @@ use rustc_hir::def_id::DefId;
33use rustc_infer:: infer:: at:: ToTrace ;
44use rustc_infer:: infer:: canonical:: { Canonical , QueryResponse } ;
55use rustc_infer:: infer:: { DefiningAnchor , InferCtxt , TyCtxtInferExt } ;
6- use rustc_infer:: traits:: TraitEngineExt as _;
6+ use rustc_infer:: traits:: { ObligationCauseCode , TraitEngineExt as _} ;
77use rustc_middle:: ty:: query:: Providers ;
88use rustc_middle:: ty:: subst:: { GenericArg , Subst , UserSelfTy , UserSubsts } ;
99use rustc_middle:: ty:: {
@@ -22,6 +22,7 @@ use rustc_trait_selection::traits::query::type_op::subtype::Subtype;
2222use rustc_trait_selection:: traits:: query:: { Fallible , NoSolution } ;
2323use rustc_trait_selection:: traits:: { Normalized , Obligation , ObligationCause , TraitEngine } ;
2424use std:: fmt;
25+ use std:: iter:: zip;
2526
2627pub ( crate ) fn provide ( p : & mut Providers ) {
2728 * p = Providers {
@@ -61,14 +62,15 @@ pub fn type_op_ascribe_user_type_with_span<'a, 'tcx: 'a>(
6162 mir_ty, def_id, user_substs
6263 ) ;
6364
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) ?;
6667 Ok ( ( ) )
6768}
6869
6970struct AscribeUserTypeCx < ' me , ' tcx > {
7071 infcx : & ' me InferCtxt < ' me , ' tcx > ,
7172 param_env : ParamEnv < ' tcx > ,
73+ span : Span ,
7274 fulfill_cx : & ' me mut dyn TraitEngine < ' tcx > ,
7375}
7476
@@ -79,7 +81,7 @@ impl<'me, 'tcx> AscribeUserTypeCx<'me, 'tcx> {
7981 {
8082 self . infcx
8183 . partially_normalize_associated_types_in (
82- ObligationCause :: misc ( DUMMY_SP , hir:: CRATE_HIR_ID ) ,
84+ ObligationCause :: misc ( self . span , hir:: CRATE_HIR_ID ) ,
8385 self . param_env ,
8486 value,
8587 )
@@ -91,18 +93,13 @@ impl<'me, 'tcx> AscribeUserTypeCx<'me, 'tcx> {
9193 T : ToTrace < ' tcx > ,
9294 {
9395 self . infcx
94- . at ( & ObligationCause :: dummy ( ) , self . param_env )
96+ . at ( & ObligationCause :: dummy_with_span ( self . span ) , self . param_env )
9597 . relate ( a, variance, b) ?
9698 . into_value_registering_obligations ( self . infcx , self . fulfill_cx ) ;
9799 Ok ( ( ) )
98100 }
99101
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 > ) {
106103 self . fulfill_cx . register_predicate_obligation (
107104 self . infcx ,
108105 Obligation :: new ( cause, self . param_env , predicate) ,
@@ -126,7 +123,6 @@ impl<'me, 'tcx> AscribeUserTypeCx<'me, 'tcx> {
126123 mir_ty : Ty < ' tcx > ,
127124 def_id : DefId ,
128125 user_substs : UserSubsts < ' tcx > ,
129- span : Option < Span > ,
130126 ) -> Result < ( ) , NoSolution > {
131127 let UserSubsts { user_self_ty, substs } = user_substs;
132128 let tcx = self . tcx ( ) ;
@@ -145,10 +141,20 @@ impl<'me, 'tcx> AscribeUserTypeCx<'me, 'tcx> {
145141 // outlives" error messages.
146142 let instantiated_predicates =
147143 self . tcx ( ) . predicates_of ( def_id) . instantiate ( self . tcx ( ) , substs) ;
144+
145+ let cause = ObligationCause :: dummy_with_span ( self . span ) ;
146+
148147 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) ;
152158 }
153159
154160 if let Some ( UserSelfTy { impl_def_id, self_ty } ) = user_self_ty {
@@ -161,7 +167,7 @@ impl<'me, 'tcx> AscribeUserTypeCx<'me, 'tcx> {
161167 self . prove_predicate (
162168 ty:: Binder :: dummy ( ty:: PredicateKind :: WellFormed ( impl_self_ty. into ( ) ) )
163169 . to_predicate ( self . tcx ( ) ) ,
164- span ,
170+ cause . clone ( ) ,
165171 ) ;
166172 }
167173
@@ -178,7 +184,7 @@ impl<'me, 'tcx> AscribeUserTypeCx<'me, 'tcx> {
178184 // which...could happen with normalization...
179185 self . prove_predicate (
180186 ty:: Binder :: dummy ( ty:: PredicateKind :: WellFormed ( ty. into ( ) ) ) . to_predicate ( self . tcx ( ) ) ,
181- span ,
187+ cause ,
182188 ) ;
183189 Ok ( ( ) )
184190 }
0 commit comments