@@ -11,7 +11,7 @@ pub use self::suggest::SelfSource;
11
11
pub use self :: MethodError :: * ;
12
12
13
13
use crate :: errors:: OpMethodGenericParams ;
14
- use crate :: { Expectation , FnCtxt } ;
14
+ use crate :: FnCtxt ;
15
15
use rustc_data_structures:: sync:: Lrc ;
16
16
use rustc_errors:: { Applicability , Diagnostic } ;
17
17
use rustc_hir as hir;
@@ -264,7 +264,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
264
264
265
265
pub ( super ) fn obligation_for_method (
266
266
& self ,
267
- span : Span ,
267
+ cause : ObligationCause < ' tcx > ,
268
268
trait_def_id : DefId ,
269
269
self_ty : Ty < ' tcx > ,
270
270
opt_input_types : Option < & [ Ty < ' tcx > ] > ,
@@ -282,70 +282,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
282
282
}
283
283
}
284
284
}
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)
326
286
} ) ;
327
287
328
288
let trait_ref = self . tcx . mk_trait_ref ( trait_def_id, substs) ;
329
289
330
290
// Construct an obligation
331
291
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
-
334
292
(
335
293
traits:: Obligation :: new (
336
294
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,
347
296
self . param_env ,
348
- poly_trait_ref,
297
+ poly_trait_ref. without_const ( ) ,
349
298
) ,
350
299
substs,
351
300
)
@@ -356,40 +305,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
356
305
/// In particular, it doesn't really do any probing: it simply constructs
357
306
/// an obligation for a particular trait with the given self type and checks
358
307
/// whether that trait is implemented.
359
- #[ instrument( level = "debug" , skip( self , span ) ) ]
308
+ #[ instrument( level = "debug" , skip( self ) ) ]
360
309
pub ( super ) fn lookup_method_in_trait (
361
310
& self ,
362
- span : Span ,
311
+ cause : ObligationCause < ' tcx > ,
363
312
m_name : Ident ,
364
313
trait_def_id : DefId ,
365
314
self_ty : Ty < ' tcx > ,
366
315
opt_input_types : Option < & [ Ty < ' tcx > ] > ,
367
316
) -> Option < InferOk < ' tcx , MethodCallee < ' tcx > > > {
368
317
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)
385
320
}
386
321
387
322
// FIXME(#18741): it seems likely that we can consolidate some of this
388
323
// code with the other method-lookup code. In particular, the second half
389
324
// of this method is basically the same as confirmation.
390
325
fn construct_obligation_for_trait (
391
326
& self ,
392
- span : Span ,
393
327
m_name : Ident ,
394
328
trait_def_id : DefId ,
395
329
obligation : traits:: PredicateObligation < ' tcx > ,
@@ -409,7 +343,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
409
343
let tcx = self . tcx ;
410
344
let Some ( method_item) = self . associated_value ( trait_def_id, m_name) else {
411
345
tcx. sess . delay_span_bug (
412
- span,
346
+ obligation . cause . span ,
413
347
"operator trait does not have corresponding operator method" ,
414
348
) ;
415
349
return None ;
@@ -435,7 +369,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
435
369
// with bound regions.
436
370
let fn_sig = tcx. bound_fn_sig ( def_id) ;
437
371
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) ;
439
374
440
375
let InferOk { value, obligations : o } =
441
376
self . at ( & obligation. cause , self . param_env ) . normalize ( fn_sig) ;
0 commit comments