@@ -11,7 +11,7 @@ pub use self::suggest::SelfSource;
1111pub use self :: MethodError :: * ;
1212
1313use crate :: errors:: OpMethodGenericParams ;
14- use crate :: { Expectation , FnCtxt } ;
14+ use crate :: FnCtxt ;
1515use rustc_data_structures:: sync:: Lrc ;
1616use rustc_errors:: { Applicability , Diagnostic } ;
1717use rustc_hir as hir;
@@ -264,7 +264,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
264264
265265 pub ( super ) fn obligation_for_method (
266266 & self ,
267- span : Span ,
267+ cause : ObligationCause < ' tcx > ,
268268 trait_def_id : DefId ,
269269 self_ty : Ty < ' tcx > ,
270270 opt_input_types : Option < & [ Ty < ' tcx > ] > ,
@@ -282,70 +282,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
282282 }
283283 }
284284 }
285- self . var_for_def ( span, param)
286- } ) ;
287-
288- let trait_ref = self . tcx . mk_trait_ref ( trait_def_id, substs) ;
289-
290- // Construct an obligation
291- let poly_trait_ref = ty:: Binder :: dummy ( trait_ref) ;
292- (
293- traits:: Obligation :: misc (
294- self . tcx ,
295- span,
296- self . body_id ,
297- self . param_env ,
298- poly_trait_ref. without_const ( ) ,
299- ) ,
300- substs,
301- )
302- }
303-
304- pub ( super ) fn obligation_for_op_method (
305- & self ,
306- span : Span ,
307- trait_def_id : DefId ,
308- self_ty : Ty < ' tcx > ,
309- opt_rhs : Option < ( & ' tcx hir:: Expr < ' tcx > , Ty < ' tcx > ) > ,
310- expected : Expectation < ' tcx > ,
311- ) -> ( traits:: Obligation < ' tcx , ty:: Predicate < ' tcx > > , & ' tcx ty:: List < ty:: subst:: GenericArg < ' tcx > > )
312- {
313- // Construct a trait-reference `self_ty : Trait<input_tys>`
314- let substs = InternalSubsts :: for_item ( self . tcx , trait_def_id, |param, _| {
315- match param. kind {
316- GenericParamDefKind :: Lifetime | GenericParamDefKind :: Const { .. } => { }
317- GenericParamDefKind :: Type { .. } => {
318- if param. index == 0 {
319- return self_ty. into ( ) ;
320- } else if let Some ( ( _, input_type) ) = opt_rhs {
321- return input_type. into ( ) ;
322- }
323- }
324- }
325- self . var_for_def ( span, param)
285+ self . var_for_def ( cause. span , param)
326286 } ) ;
327287
328288 let trait_ref = self . tcx . mk_trait_ref ( trait_def_id, substs) ;
329289
330290 // Construct an obligation
331291 let poly_trait_ref = ty:: Binder :: dummy ( trait_ref) ;
332- let output_ty = expected. only_has_type ( self ) . and_then ( |ty| ( !ty. needs_infer ( ) ) . then ( || ty) ) ;
333-
334292 (
335293 traits:: Obligation :: new (
336294 self . tcx ,
337- traits:: ObligationCause :: new (
338- span,
339- self . body_id ,
340- traits:: BinOp {
341- rhs_span : opt_rhs. map ( |( expr, _) | expr. span ) ,
342- is_lit : opt_rhs
343- . map_or ( false , |( expr, _) | matches ! ( expr. kind, hir:: ExprKind :: Lit ( _) ) ) ,
344- output_ty,
345- } ,
346- ) ,
295+ cause,
347296 self . param_env ,
348- poly_trait_ref,
297+ poly_trait_ref. without_const ( ) ,
349298 ) ,
350299 substs,
351300 )
@@ -356,40 +305,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
356305 /// In particular, it doesn't really do any probing: it simply constructs
357306 /// an obligation for a particular trait with the given self type and checks
358307 /// whether that trait is implemented.
359- #[ instrument( level = "debug" , skip( self , span ) ) ]
308+ #[ instrument( level = "debug" , skip( self ) ) ]
360309 pub ( super ) fn lookup_method_in_trait (
361310 & self ,
362- span : Span ,
311+ cause : ObligationCause < ' tcx > ,
363312 m_name : Ident ,
364313 trait_def_id : DefId ,
365314 self_ty : Ty < ' tcx > ,
366315 opt_input_types : Option < & [ Ty < ' tcx > ] > ,
367316 ) -> Option < InferOk < ' tcx , MethodCallee < ' tcx > > > {
368317 let ( obligation, substs) =
369- self . obligation_for_method ( span, trait_def_id, self_ty, opt_input_types) ;
370- self . construct_obligation_for_trait ( span, m_name, trait_def_id, obligation, substs)
371- }
372-
373- pub ( super ) fn lookup_op_method_in_trait (
374- & self ,
375- span : Span ,
376- m_name : Ident ,
377- trait_def_id : DefId ,
378- self_ty : Ty < ' tcx > ,
379- opt_rhs : Option < ( & ' tcx hir:: Expr < ' tcx > , Ty < ' tcx > ) > ,
380- expected : Expectation < ' tcx > ,
381- ) -> Option < InferOk < ' tcx , MethodCallee < ' tcx > > > {
382- let ( obligation, substs) =
383- self . obligation_for_op_method ( span, trait_def_id, self_ty, opt_rhs, expected) ;
384- self . construct_obligation_for_trait ( span, m_name, trait_def_id, obligation, substs)
318+ self . obligation_for_method ( cause, trait_def_id, self_ty, opt_input_types) ;
319+ self . construct_obligation_for_trait ( m_name, trait_def_id, obligation, substs)
385320 }
386321
387322 // FIXME(#18741): it seems likely that we can consolidate some of this
388323 // code with the other method-lookup code. In particular, the second half
389324 // of this method is basically the same as confirmation.
390325 fn construct_obligation_for_trait (
391326 & self ,
392- span : Span ,
393327 m_name : Ident ,
394328 trait_def_id : DefId ,
395329 obligation : traits:: PredicateObligation < ' tcx > ,
@@ -409,7 +343,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
409343 let tcx = self . tcx ;
410344 let Some ( method_item) = self . associated_value ( trait_def_id, m_name) else {
411345 tcx. sess . delay_span_bug (
412- span,
346+ obligation . cause . span ,
413347 "operator trait does not have corresponding operator method" ,
414348 ) ;
415349 return None ;
@@ -435,7 +369,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
435369 // with bound regions.
436370 let fn_sig = tcx. bound_fn_sig ( def_id) ;
437371 let fn_sig = fn_sig. subst ( self . tcx , substs) ;
438- let fn_sig = self . replace_bound_vars_with_fresh_vars ( span, infer:: FnCall , fn_sig) ;
372+ let fn_sig =
373+ self . replace_bound_vars_with_fresh_vars ( obligation. cause . span , infer:: FnCall , fn_sig) ;
439374
440375 let InferOk { value, obligations : o } =
441376 self . at ( & obligation. cause , self . param_env ) . normalize ( fn_sig) ;
0 commit comments