@@ -1935,8 +1935,58 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
19351935 ObligationCauseCode :: ImplicitSizedObligation ( item_def_id, span) => {
19361936 let item_name = tcx. def_path_str ( item_def_id) ;
19371937 let mut sp: MultiSpan = span. into ( ) ;
1938- sp. push_span_label ( span, format ! ( "required by this bound in `{}`" , item_name) ) ;
1939- err. span_note ( sp, "type parameters have an implicit `Sized` obligation" ) ;
1938+ match self . tcx . hir ( ) . get_if_local ( item_def_id) {
1939+ Some ( hir:: Node :: TraitItem ( hir:: TraitItem {
1940+ kind : hir:: TraitItemKind :: Type ( bounds, _) ,
1941+ ident,
1942+ ..
1943+ } ) ) => {
1944+ sp. push_span_label (
1945+ span,
1946+ format ! ( "required by associated type `{}`" , item_name) ,
1947+ ) ;
1948+ err. span_note ( sp, "associated types have an implicit `Sized` obligation" ) ;
1949+
1950+ let sized_trait = self . tcx . lang_items ( ) . sized_trait ( ) ;
1951+ if bounds. len ( ) == 0 {
1952+ err. span_suggestion_verbose (
1953+ ident. span . shrink_to_hi ( ) ,
1954+ "consider relaxing the `Sized` obligation" ,
1955+ ": ?Sized" . to_string ( ) ,
1956+ Applicability :: MaybeIncorrect ,
1957+ ) ;
1958+ } else if bounds. iter ( ) . all ( |bound| {
1959+ bound. trait_ref ( ) . and_then ( |tr| tr. trait_def_id ( ) ) != sized_trait
1960+ } ) {
1961+ err. span_suggestion_verbose (
1962+ bounds. iter ( ) . last ( ) . unwrap ( ) . span ( ) . shrink_to_hi ( ) ,
1963+ "consider relaxing the `Sized` obligation" ,
1964+ " + ?Sized" . to_string ( ) ,
1965+ Applicability :: MaybeIncorrect ,
1966+ ) ;
1967+ }
1968+ }
1969+ Some ( hir:: Node :: ImplItem ( hir:: ImplItem {
1970+ kind : hir:: ImplItemKind :: TyAlias ( _) ,
1971+ ..
1972+ } ) ) => {
1973+ let msg = "associated types on `impl` blocks for types, have an implicit \
1974+ mandatory `Sized` obligation; associated types from `trait`s can be \
1975+ relaxed to `?Sized`";
1976+ sp. push_span_label (
1977+ span,
1978+ format ! ( "required by associated type `{}`" , item_name) ,
1979+ ) ;
1980+ err. span_note ( sp, msg) ;
1981+ }
1982+ _ => {
1983+ sp. push_span_label (
1984+ span,
1985+ format ! ( "required by this bound in `{}`" , item_name) ,
1986+ ) ;
1987+ err. span_note ( sp, "type parameters have an implicit `Sized` obligation" ) ;
1988+ }
1989+ }
19401990 }
19411991 ObligationCauseCode :: BindingObligation ( item_def_id, span) => {
19421992 let item_name = tcx. def_path_str ( item_def_id) ;
@@ -1953,7 +2003,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
19532003 }
19542004 }
19552005 if span != DUMMY_SP {
1956- err. span_label ( span, & msg) ;
2006+ err. span_label ( span, & msg) ;
19572007 } else {
19582008 err. note ( & msg) ;
19592009 }
0 commit comments