@@ -40,18 +40,28 @@ fn type_op_ascribe_user_type<'tcx>(
40
40
canonicalized : Canonical < ' tcx , ParamEnvAnd < ' tcx , AscribeUserType < ' tcx > > > ,
41
41
) -> Result < & ' tcx Canonical < ' tcx , QueryResponse < ' tcx , ( ) > > , NoSolution > {
42
42
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
+ }
49
46
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
+ ) ;
52
61
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 ( ( ) )
55
65
}
56
66
57
67
struct AscribeUserTypeCx < ' me , ' tcx > {
@@ -85,10 +95,15 @@ impl AscribeUserTypeCx<'me, 'tcx> {
85
95
Ok ( ( ) )
86
96
}
87
97
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
+ } ;
89
104
self . fulfill_cx . register_predicate_obligation (
90
105
self . infcx ,
91
- Obligation :: new ( ObligationCause :: dummy ( ) , self . param_env , predicate) ,
106
+ Obligation :: new ( cause , self . param_env , predicate) ,
92
107
) ;
93
108
}
94
109
@@ -108,6 +123,7 @@ impl AscribeUserTypeCx<'me, 'tcx> {
108
123
mir_ty : Ty < ' tcx > ,
109
124
def_id : DefId ,
110
125
user_substs : UserSubsts < ' tcx > ,
126
+ span : Option < Span > ,
111
127
) -> Result < ( ) , NoSolution > {
112
128
let UserSubsts { user_self_ty, substs } = user_substs;
113
129
let tcx = self . tcx ( ) ;
@@ -129,7 +145,7 @@ impl AscribeUserTypeCx<'me, 'tcx> {
129
145
debug ! ( ?instantiated_predicates. predicates) ;
130
146
for instantiated_predicate in instantiated_predicates. predicates {
131
147
let instantiated_predicate = self . normalize ( instantiated_predicate) ;
132
- self . prove_predicate ( instantiated_predicate) ;
148
+ self . prove_predicate ( instantiated_predicate, span ) ;
133
149
}
134
150
135
151
if let Some ( UserSelfTy { impl_def_id, self_ty } ) = user_self_ty {
@@ -141,6 +157,7 @@ impl AscribeUserTypeCx<'me, 'tcx> {
141
157
142
158
self . prove_predicate (
143
159
ty:: PredicateKind :: WellFormed ( impl_self_ty. into ( ) ) . to_predicate ( self . tcx ( ) ) ,
160
+ span,
144
161
) ;
145
162
}
146
163
@@ -155,7 +172,10 @@ impl AscribeUserTypeCx<'me, 'tcx> {
155
172
// them? This would only be relevant if some input
156
173
// type were ill-formed but did not appear in `ty`,
157
174
// 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
+ ) ;
159
179
Ok ( ( ) )
160
180
}
161
181
}
0 commit comments