@@ -762,19 +762,19 @@ fn check_opaque_types<'fcx, 'tcx>(
762762 substituted_predicates
763763}
764764
765+ const HELP_FOR_SELF_TYPE : & str =
766+ "consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, \
767+ `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one \
768+ of the previous types except `Self`)";
769+
765770fn check_method_receiver < ' fcx , ' tcx > (
766771 fcx : & FnCtxt < ' fcx , ' tcx > ,
767772 method_sig : & hir:: MethodSig ,
768773 method : & ty:: AssocItem ,
769774 self_ty : Ty < ' tcx > ,
770775) {
771- const HELP_FOR_SELF_TYPE : & str =
772- "consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, \
773- `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one \
774- of the previous types except `Self`)";
775776 // Check that the method has a valid receiver type, given the type `Self`.
776- debug ! ( "check_method_receiver({:?}, self_ty={:?})" ,
777- method, self_ty) ;
777+ debug ! ( "check_method_receiver({:?}, self_ty={:?})" , method, self_ty) ;
778778
779779 if !method. method_has_self_argument {
780780 return ;
@@ -805,12 +805,7 @@ fn check_method_receiver<'fcx, 'tcx>(
805805 if fcx. tcx . features ( ) . arbitrary_self_types {
806806 if !receiver_is_valid ( fcx, span, receiver_ty, self_ty, true ) {
807807 // Report error; `arbitrary_self_types` was enabled.
808- fcx. tcx . sess . diagnostic ( ) . mut_span_err (
809- span, & format ! ( "invalid method receiver type: {:?}" , receiver_ty)
810- ) . note ( "type of `self` must be `Self` or a type that dereferences to it" )
811- . help ( HELP_FOR_SELF_TYPE )
812- . code ( DiagnosticId :: Error ( "E0307" . into ( ) ) )
813- . emit ( ) ;
808+ e0307 ( fcx, span, receiver_ty) ;
814809 }
815810 } else {
816811 if !receiver_is_valid ( fcx, span, receiver_ty, self_ty, false ) {
@@ -830,17 +825,22 @@ fn check_method_receiver<'fcx, 'tcx>(
830825 . emit ( ) ;
831826 } else {
832827 // Report error; would not have worked with `arbitrary_self_types`.
833- fcx. tcx . sess . diagnostic ( ) . mut_span_err (
834- span, & format ! ( "invalid method receiver type: {:?}" , receiver_ty)
835- ) . note ( "type must be `Self` or a type that dereferences to it" )
836- . help ( HELP_FOR_SELF_TYPE )
837- . code ( DiagnosticId :: Error ( "E0307" . into ( ) ) )
838- . emit ( ) ;
828+ e0307 ( fcx, span, receiver_ty) ;
839829 }
840830 }
841831 }
842832}
843833
834+ fn e0307 ( fcx : & FnCtxt < ' fcx , ' tcx > , span : Span , receiver_ty : Ty < ' _ > ) {
835+ fcx. tcx . sess . diagnostic ( ) . mut_span_err (
836+ span,
837+ & format ! ( "invalid `self` parameter type: {:?}" , receiver_ty)
838+ ) . note ( "type of `self` must be `Self` or a type that dereferences to it" )
839+ . help ( HELP_FOR_SELF_TYPE )
840+ . code ( DiagnosticId :: Error ( "E0307" . into ( ) ) )
841+ . emit ( ) ;
842+ }
843+
844844/// Returns whether `receiver_ty` would be considered a valid receiver type for `self_ty`. If
845845/// `arbitrary_self_types` is enabled, `receiver_ty` must transitively deref to `self_ty`, possibly
846846/// through a `*const/mut T` raw pointer. If the feature is not enabled, the requirements are more
0 commit comments