@@ -25,6 +25,7 @@ use tracing::{debug, instrument};
25
25
use super :: method:: MethodCallee ;
26
26
use super :: method:: probe:: ProbeScope ;
27
27
use super :: { Expectation , FnCtxt , TupleArgumentsFlag } ;
28
+ use crate :: method:: TreatNotYetDefinedOpaques ;
28
29
use crate :: { errors, fluent_generated} ;
29
30
30
31
/// Checks that it is legal to call methods of the trait corresponding
@@ -78,7 +79,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
78
79
_ => self . check_expr ( callee_expr) ,
79
80
} ;
80
81
81
- let expr_ty = self . structurally_resolve_type ( call_expr. span , original_callee_ty) ;
82
+ let expr_ty = self . try_structurally_resolve_type ( call_expr. span , original_callee_ty) ;
82
83
83
84
let mut autoderef = self . autoderef ( callee_expr. span , expr_ty) ;
84
85
let mut result = None ;
@@ -200,7 +201,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
200
201
arg_exprs : & ' tcx [ hir:: Expr < ' tcx > ] ,
201
202
autoderef : & Autoderef < ' a , ' tcx > ,
202
203
) -> Option < CallStep < ' tcx > > {
203
- let adjusted_ty = self . structurally_resolve_type ( autoderef. span ( ) , autoderef. final_ty ( ) ) ;
204
+ let adjusted_ty =
205
+ self . try_structurally_resolve_type ( autoderef. span ( ) , autoderef. final_ty ( ) ) ;
204
206
205
207
// If the callee is a function pointer or a closure, then we're all set.
206
208
match * adjusted_ty. kind ( ) {
@@ -297,6 +299,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
297
299
return None ;
298
300
}
299
301
302
+ ty:: Infer ( ty:: TyVar ( vid) ) => {
303
+ // If we end up with an inference variable which is not the hidden type of
304
+ // an opaque, emit an error.
305
+ if !self . has_opaques_with_sub_unified_hidden_type ( vid) {
306
+ self . type_must_be_known_at_this_point ( autoderef. span ( ) , adjusted_ty) ;
307
+ return None ;
308
+ }
309
+ }
310
+
300
311
ty:: Error ( _) => {
301
312
return None ;
302
313
}
@@ -367,35 +378,39 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
367
378
Ty :: new_tup_from_iter ( self . tcx , arg_exprs. iter ( ) . map ( |e| self . next_ty_var ( e. span ) ) )
368
379
} ) ;
369
380
370
- if let Some ( ok) = self . lookup_method_for_operator (
381
+ // We use `TreatNotYetDefinedOpaques::AsRigid` here so that if the `adjusted_ty`
382
+ // is `Box<impl FnOnce()>` we choose `FnOnce` instead of `Fn`.
383
+ let Some ( ok) = self . lookup_method_for_operator (
371
384
self . misc ( call_expr. span ) ,
372
385
method_name,
373
386
trait_def_id,
374
387
adjusted_ty,
375
388
opt_input_type,
376
- ) {
377
- let method = self . register_infer_ok_obligations ( ok) ;
378
- let mut autoref = None ;
379
- if borrow {
380
- // Check for &self vs &mut self in the method signature. Since this is either
381
- // the Fn or FnMut trait, it should be one of those.
382
- let ty:: Ref ( _, _, mutbl) = method. sig . inputs ( ) [ 0 ] . kind ( ) else {
383
- bug ! ( "Expected `FnMut`/`Fn` to take receiver by-ref/by-mut" )
384
- } ;
385
-
386
- // For initial two-phase borrow
387
- // deployment, conservatively omit
388
- // overloaded function call ops.
389
- let mutbl = AutoBorrowMutability :: new ( * mutbl, AllowTwoPhase :: No ) ;
390
-
391
- autoref = Some ( Adjustment {
392
- kind : Adjust :: Borrow ( AutoBorrow :: Ref ( mutbl) ) ,
393
- target : method. sig . inputs ( ) [ 0 ] ,
394
- } ) ;
395
- }
389
+ TreatNotYetDefinedOpaques :: AsRigid ,
390
+ ) else {
391
+ continue ;
392
+ } ;
393
+ let method = self . register_infer_ok_obligations ( ok) ;
394
+ let mut autoref = None ;
395
+ if borrow {
396
+ // Check for &self vs &mut self in the method signature. Since this is either
397
+ // the Fn or FnMut trait, it should be one of those.
398
+ let ty:: Ref ( _, _, mutbl) = * method. sig . inputs ( ) [ 0 ] . kind ( ) else {
399
+ bug ! ( "Expected `FnMut`/`Fn` to take receiver by-ref/by-mut" )
400
+ } ;
396
401
397
- return Some ( ( autoref, method) ) ;
402
+ // For initial two-phase borrow
403
+ // deployment, conservatively omit
404
+ // overloaded function call ops.
405
+ let mutbl = AutoBorrowMutability :: new ( mutbl, AllowTwoPhase :: No ) ;
406
+
407
+ autoref = Some ( Adjustment {
408
+ kind : Adjust :: Borrow ( AutoBorrow :: Ref ( mutbl) ) ,
409
+ target : method. sig . inputs ( ) [ 0 ] ,
410
+ } ) ;
398
411
}
412
+
413
+ return Some ( ( autoref, method) ) ;
399
414
}
400
415
401
416
None
0 commit comments