@@ -324,35 +324,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
324
324
Ok ( pick)
325
325
}
326
326
327
- pub ( super ) fn obligation_for_method (
328
- & self ,
329
- cause : ObligationCause < ' tcx > ,
330
- trait_def_id : DefId ,
331
- self_ty : Ty < ' tcx > ,
332
- opt_input_types : Option < & [ Ty < ' tcx > ] > ,
333
- ) -> ( traits:: PredicateObligation < ' tcx > , ty:: GenericArgsRef < ' tcx > ) {
334
- // Construct a trait-reference `self_ty : Trait<input_tys>`
335
- let args = GenericArgs :: for_item ( self . tcx , trait_def_id, |param, _| {
336
- match param. kind {
337
- GenericParamDefKind :: Lifetime | GenericParamDefKind :: Const { .. } => { }
338
- GenericParamDefKind :: Type { .. } => {
339
- if param. index == 0 {
340
- return self_ty. into ( ) ;
341
- } else if let Some ( input_types) = opt_input_types {
342
- return input_types[ param. index as usize - 1 ] . into ( ) ;
343
- }
344
- }
345
- }
346
- self . var_for_def ( cause. span , param)
347
- } ) ;
348
-
349
- let trait_ref = ty:: TraitRef :: new_from_args ( self . tcx , trait_def_id, args) ;
350
-
351
- // Construct an obligation
352
- let poly_trait_ref = ty:: Binder :: dummy ( trait_ref) ;
353
- ( traits:: Obligation :: new ( self . tcx , cause, self . param_env , poly_trait_ref) , args)
354
- }
355
-
356
327
/// `lookup_method_in_trait` is used for overloaded operators.
357
328
/// It does a very narrow slice of what the normal probe/confirm path does.
358
329
/// In particular, it doesn't really do any probing: it simply constructs
@@ -365,24 +336,34 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
365
336
m_name : Ident ,
366
337
trait_def_id : DefId ,
367
338
self_ty : Ty < ' tcx > ,
368
- opt_input_types : Option < & [ Ty < ' tcx > ] > ,
339
+ opt_rhs_ty : Option < Ty < ' tcx > > ,
369
340
) -> Option < InferOk < ' tcx , MethodCallee < ' tcx > > > {
370
- let ( obligation, args) =
371
- self . obligation_for_method ( cause, trait_def_id, self_ty, opt_input_types) ;
372
- self . construct_obligation_for_trait ( m_name, trait_def_id, obligation, args)
373
- }
341
+ // Construct a trait-reference `self_ty : Trait<input_tys>`
342
+ let args = GenericArgs :: for_item ( self . tcx , trait_def_id, |param, _| match param. kind {
343
+ GenericParamDefKind :: Lifetime | GenericParamDefKind :: Const { .. } => {
344
+ unreachable ! ( "did not expect operator trait to have lifetime/const" )
345
+ }
346
+ GenericParamDefKind :: Type { .. } => {
347
+ if param. index == 0 {
348
+ self_ty. into ( )
349
+ } else if let Some ( rhs_ty) = opt_rhs_ty {
350
+ assert_eq ! ( param. index, 1 , "did not expect >1 param on operator trait" ) ;
351
+ rhs_ty. into ( )
352
+ } else {
353
+ // FIXME: We should stop passing `None` for the failure case
354
+ // when probing for call exprs. I.e. `opt_rhs_ty` should always
355
+ // be set when it needs to be.
356
+ self . var_for_def ( cause. span , param)
357
+ }
358
+ }
359
+ } ) ;
374
360
375
- // FIXME(#18741): it seems likely that we can consolidate some of this
376
- // code with the other method-lookup code. In particular, the second half
377
- // of this method is basically the same as confirmation.
378
- fn construct_obligation_for_trait (
379
- & self ,
380
- m_name : Ident ,
381
- trait_def_id : DefId ,
382
- obligation : traits:: PredicateObligation < ' tcx > ,
383
- args : ty:: GenericArgsRef < ' tcx > ,
384
- ) -> Option < InferOk < ' tcx , MethodCallee < ' tcx > > > {
385
- debug ! ( ?obligation) ;
361
+ let obligation = traits:: Obligation :: new (
362
+ self . tcx ,
363
+ cause,
364
+ self . param_env ,
365
+ ty:: TraitRef :: new_from_args ( self . tcx , trait_def_id, args) ,
366
+ ) ;
386
367
387
368
// Now we want to know if this can be matched
388
369
if !self . predicate_may_hold ( & obligation) {
@@ -406,8 +387,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
406
387
debug ! ( "lookup_in_trait_adjusted: method_item={:?}" , method_item) ;
407
388
let mut obligations = PredicateObligations :: new ( ) ;
408
389
409
- // FIXME(effects): revisit when binops get `#[const_trait]`
410
-
411
390
// Instantiate late-bound regions and instantiate the trait
412
391
// parameters into the method type to get the actual method type.
413
392
//
@@ -418,12 +397,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
418
397
let fn_sig =
419
398
self . instantiate_binder_with_fresh_vars ( obligation. cause . span , infer:: FnCall , fn_sig) ;
420
399
421
- let InferOk { value, obligations : o } =
400
+ let InferOk { value : fn_sig , obligations : o } =
422
401
self . at ( & obligation. cause , self . param_env ) . normalize ( fn_sig) ;
423
- let fn_sig = {
424
- obligations. extend ( o) ;
425
- value
426
- } ;
402
+ obligations. extend ( o) ;
427
403
428
404
// Register obligations for the parameters. This will include the
429
405
// `Self` parameter, which in turn has a bound of the main trait,
@@ -435,13 +411,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
435
411
// any late-bound regions appearing in its bounds.
436
412
let bounds = self . tcx . predicates_of ( def_id) . instantiate ( self . tcx , args) ;
437
413
438
- let InferOk { value, obligations : o } =
414
+ let InferOk { value : bounds , obligations : o } =
439
415
self . at ( & obligation. cause , self . param_env ) . normalize ( bounds) ;
440
- let bounds = {
441
- obligations. extend ( o) ;
442
- value
443
- } ;
444
-
416
+ obligations. extend ( o) ;
445
417
assert ! ( !bounds. has_escaping_bound_vars( ) ) ;
446
418
447
419
let predicates_cause = obligation. cause . clone ( ) ;
@@ -454,7 +426,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
454
426
// Also add an obligation for the method type being well-formed.
455
427
let method_ty = Ty :: new_fn_ptr ( tcx, ty:: Binder :: dummy ( fn_sig) ) ;
456
428
debug ! (
457
- "lookup_in_trait_adjusted : matched method method_ty={:?} obligation={:?}" ,
429
+ "lookup_method_in_trait : matched method method_ty={:?} obligation={:?}" ,
458
430
method_ty, obligation
459
431
) ;
460
432
obligations. push ( traits:: Obligation :: new (
@@ -467,7 +439,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
467
439
) ) ;
468
440
469
441
let callee = MethodCallee { def_id, args, sig : fn_sig } ;
470
-
471
442
debug ! ( "callee = {:?}" , callee) ;
472
443
473
444
Some ( InferOk { obligations, value : callee } )
0 commit comments