@@ -81,11 +81,25 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
8181 let rcvr_substs = self . fresh_receiver_substs ( self_ty, & pick) ;
8282 let all_substs = self . instantiate_method_substs ( & pick, segment, rcvr_substs) ;
8383
84- debug ! ( "all_substs={ :?}" , all_substs) ;
84+ debug ! ( "rcvr_substs={rcvr_substs :?}, all_substs={all_substs:?}" ) ;
8585
8686 // Create the final signature for the method, replacing late-bound regions.
8787 let ( method_sig, method_predicates) = self . instantiate_method_sig ( & pick, all_substs) ;
8888
89+ // If there is a `Self: Sized` bound and `Self` is a trait object, it is possible that
90+ // something which derefs to `Self` actually implements the trait and the caller
91+ // wanted to make a static dispatch on it but forgot to import the trait.
92+ // See test `src/test/ui/issue-35976.rs`.
93+ //
94+ // In that case, we'll error anyway, but we'll also re-run the search with all traits
95+ // in scope, and if we find another method which can be used, we'll output an
96+ // appropriate hint suggesting to import the trait.
97+ let filler_substs = rcvr_substs
98+ . extend_to ( self . tcx , pick. item . def_id , |def, _| self . tcx . mk_param_from_def ( def) ) ;
99+ let illegal_sized_bound = self . predicates_require_illegal_sized_bound (
100+ & self . tcx . predicates_of ( pick. item . def_id ) . instantiate ( self . tcx , filler_substs) ,
101+ ) ;
102+
89103 // Unify the (adjusted) self type with what the method expects.
90104 //
91105 // SUBTLE: if we want good error messages, because of "guessing" while matching
@@ -106,16 +120,6 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
106120 // Make sure nobody calls `drop()` explicitly.
107121 self . enforce_illegal_method_limitations ( & pick) ;
108122
109- // If there is a `Self: Sized` bound and `Self` is a trait object, it is possible that
110- // something which derefs to `Self` actually implements the trait and the caller
111- // wanted to make a static dispatch on it but forgot to import the trait.
112- // See test `src/test/ui/issue-35976.rs`.
113- //
114- // In that case, we'll error anyway, but we'll also re-run the search with all traits
115- // in scope, and if we find another method which can be used, we'll output an
116- // appropriate hint suggesting to import the trait.
117- let illegal_sized_bound = self . predicates_require_illegal_sized_bound ( & method_predicates) ;
118-
119123 // Add any trait/regions obligations specified on the method's type parameters.
120124 // We won't add these if we encountered an illegal sized bound, so that we can use
121125 // a custom error in that case.
0 commit comments