@@ -762,19 +762,19 @@ fn check_opaque_types<'fcx, 'tcx>(
762
762
substituted_predicates
763
763
}
764
764
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
+
765
770
fn check_method_receiver < ' fcx , ' tcx > (
766
771
fcx : & FnCtxt < ' fcx , ' tcx > ,
767
772
method_sig : & hir:: MethodSig ,
768
773
method : & ty:: AssocItem ,
769
774
self_ty : Ty < ' tcx > ,
770
775
) {
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`)";
775
776
// 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) ;
778
778
779
779
if !method. method_has_self_argument {
780
780
return ;
@@ -805,12 +805,7 @@ fn check_method_receiver<'fcx, 'tcx>(
805
805
if fcx. tcx . features ( ) . arbitrary_self_types {
806
806
if !receiver_is_valid ( fcx, span, receiver_ty, self_ty, true ) {
807
807
// 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) ;
814
809
}
815
810
} else {
816
811
if !receiver_is_valid ( fcx, span, receiver_ty, self_ty, false ) {
@@ -830,17 +825,22 @@ fn check_method_receiver<'fcx, 'tcx>(
830
825
. emit ( ) ;
831
826
} else {
832
827
// 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) ;
839
829
}
840
830
}
841
831
}
842
832
}
843
833
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
+
844
844
/// Returns whether `receiver_ty` would be considered a valid receiver type for `self_ty`. If
845
845
/// `arbitrary_self_types` is enabled, `receiver_ty` must transitively deref to `self_ty`, possibly
846
846
/// through a `*const/mut T` raw pointer. If the feature is not enabled, the requirements are more
0 commit comments