@@ -40,18 +40,28 @@ fn type_op_ascribe_user_type<'tcx>(
4040 canonicalized : Canonical < ' tcx , ParamEnvAnd < ' tcx , AscribeUserType < ' tcx > > > ,
4141) -> Result < & ' tcx Canonical < ' tcx , QueryResponse < ' tcx , ( ) > > , NoSolution > {
4242 tcx. infer_ctxt ( ) . enter_canonical_trait_query ( & canonicalized, |infcx, fulfill_cx, key| {
43- let ( param_env, AscribeUserType { mir_ty, def_id, user_substs } ) = key. into_parts ( ) ;
44-
45- debug ! (
46- "type_op_ascribe_user_type: mir_ty={:?} def_id={:?} user_substs={:?}" ,
47- mir_ty, def_id, user_substs
48- ) ;
43+ type_op_ascribe_user_type_with_span ( infcx, fulfill_cx, key, None )
44+ } )
45+ }
4946
50- let mut cx = AscribeUserTypeCx { infcx, param_env, fulfill_cx } ;
51- cx. relate_mir_and_user_ty ( mir_ty, def_id, user_substs) ?;
47+ /// The core of the `type_op_ascribe_user_type` query: for diagnostics purposes in NLL HRTB errors,
48+ /// this query can be re-run to better track the span of the obligation cause, and improve the error
49+ /// message. Do not call directly unless you're in that very specific context.
50+ pub fn type_op_ascribe_user_type_with_span < ' a , ' tcx : ' a > (
51+ infcx : & ' a InferCtxt < ' a , ' tcx > ,
52+ fulfill_cx : & ' a mut dyn TraitEngine < ' tcx > ,
53+ key : ParamEnvAnd < ' tcx , AscribeUserType < ' tcx > > ,
54+ span : Option < Span > ,
55+ ) -> Result < ( ) , NoSolution > {
56+ let ( param_env, AscribeUserType { mir_ty, def_id, user_substs } ) = key. into_parts ( ) ;
57+ debug ! (
58+ "type_op_ascribe_user_type: mir_ty={:?} def_id={:?} user_substs={:?}" ,
59+ mir_ty, def_id, user_substs
60+ ) ;
5261
53- Ok ( ( ) )
54- } )
62+ let mut cx = AscribeUserTypeCx { infcx, param_env, fulfill_cx } ;
63+ cx. relate_mir_and_user_ty ( mir_ty, def_id, user_substs, span) ?;
64+ Ok ( ( ) )
5565}
5666
5767struct AscribeUserTypeCx < ' me , ' tcx > {
@@ -85,10 +95,15 @@ impl AscribeUserTypeCx<'me, 'tcx> {
8595 Ok ( ( ) )
8696 }
8797
88- fn prove_predicate ( & mut self , predicate : Predicate < ' tcx > ) {
98+ fn prove_predicate ( & mut self , predicate : Predicate < ' tcx > , span : Option < Span > ) {
99+ let cause = if let Some ( span) = span {
100+ ObligationCause :: dummy_with_span ( span)
101+ } else {
102+ ObligationCause :: dummy ( )
103+ } ;
89104 self . fulfill_cx . register_predicate_obligation (
90105 self . infcx ,
91- Obligation :: new ( ObligationCause :: dummy ( ) , self . param_env , predicate) ,
106+ Obligation :: new ( cause , self . param_env , predicate) ,
92107 ) ;
93108 }
94109
@@ -108,6 +123,7 @@ impl AscribeUserTypeCx<'me, 'tcx> {
108123 mir_ty : Ty < ' tcx > ,
109124 def_id : DefId ,
110125 user_substs : UserSubsts < ' tcx > ,
126+ span : Option < Span > ,
111127 ) -> Result < ( ) , NoSolution > {
112128 let UserSubsts { user_self_ty, substs } = user_substs;
113129 let tcx = self . tcx ( ) ;
@@ -129,7 +145,7 @@ impl AscribeUserTypeCx<'me, 'tcx> {
129145 debug ! ( ?instantiated_predicates. predicates) ;
130146 for instantiated_predicate in instantiated_predicates. predicates {
131147 let instantiated_predicate = self . normalize ( instantiated_predicate) ;
132- self . prove_predicate ( instantiated_predicate) ;
148+ self . prove_predicate ( instantiated_predicate, span ) ;
133149 }
134150
135151 if let Some ( UserSelfTy { impl_def_id, self_ty } ) = user_self_ty {
@@ -141,6 +157,7 @@ impl AscribeUserTypeCx<'me, 'tcx> {
141157
142158 self . prove_predicate (
143159 ty:: PredicateKind :: WellFormed ( impl_self_ty. into ( ) ) . to_predicate ( self . tcx ( ) ) ,
160+ span,
144161 ) ;
145162 }
146163
@@ -155,7 +172,10 @@ impl AscribeUserTypeCx<'me, 'tcx> {
155172 // them? This would only be relevant if some input
156173 // type were ill-formed but did not appear in `ty`,
157174 // which...could happen with normalization...
158- self . prove_predicate ( ty:: PredicateKind :: WellFormed ( ty. into ( ) ) . to_predicate ( self . tcx ( ) ) ) ;
175+ self . prove_predicate (
176+ ty:: PredicateKind :: WellFormed ( ty. into ( ) ) . to_predicate ( self . tcx ( ) ) ,
177+ span,
178+ ) ;
159179 Ok ( ( ) )
160180 }
161181}
0 commit comments