@@ -1935,8 +1935,58 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
1935
1935
ObligationCauseCode :: ImplicitSizedObligation ( item_def_id, span) => {
1936
1936
let item_name = tcx. def_path_str ( item_def_id) ;
1937
1937
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
+ }
1940
1990
}
1941
1991
ObligationCauseCode :: BindingObligation ( item_def_id, span) => {
1942
1992
let item_name = tcx. def_path_str ( item_def_id) ;
@@ -1953,7 +2003,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
1953
2003
}
1954
2004
}
1955
2005
if span != DUMMY_SP {
1956
- err. span_label ( span, & msg) ;
2006
+ err. span_label ( span, & msg) ;
1957
2007
} else {
1958
2008
err. note ( & msg) ;
1959
2009
}
0 commit comments