@@ -1086,8 +1086,14 @@ impl<'tcx> ty::FallibleTypeFolder<TyCtxt<'tcx>> for RemapHiddenTyRegions<'tcx> {
10861086 _ => return Ok ( region) ,
10871087 }
10881088
1089- let e = if let Some ( region) = self . map . get ( & region) {
1090- if let ty:: ReEarlyParam ( e) = region. kind ( ) { e } else { bug ! ( ) }
1089+ let e = if let Some ( id_region) = self . map . get ( & region) {
1090+ if let ty:: ReEarlyParam ( e) = id_region. kind ( ) {
1091+ e
1092+ } else {
1093+ bug ! (
1094+ "expected to map region {region} to early-bound identity region, but got {id_region}"
1095+ ) ;
1096+ }
10911097 } else {
10921098 let guar = match region. kind ( ) {
10931099 ty:: ReEarlyParam ( ty:: EarlyParamRegion { def_id, .. } )
@@ -1710,92 +1716,87 @@ fn compare_synthetic_generics<'tcx>(
17101716 trait_m. name
17111717 ) ;
17121718 err. span_label ( trait_span, "declaration in trait here" ) ;
1713- match ( impl_synthetic, trait_synthetic ) {
1719+ if impl_synthetic {
17141720 // The case where the impl method uses `impl Trait` but the trait method uses
17151721 // explicit generics
1716- ( true , false ) => {
1717- err. span_label ( impl_span, "expected generic parameter, found `impl Trait`" ) ;
1718- let _: Option < _ > = try {
1719- // try taking the name from the trait impl
1720- // FIXME: this is obviously suboptimal since the name can already be used
1721- // as another generic argument
1722- let new_name = tcx. opt_item_name ( trait_def_id) ?;
1723- let trait_m = trait_m. def_id . as_local ( ) ?;
1724- let trait_m = tcx. hir ( ) . expect_trait_item ( trait_m) ;
1725-
1726- let impl_m = impl_m. def_id . as_local ( ) ?;
1727- let impl_m = tcx. hir ( ) . expect_impl_item ( impl_m) ;
1728-
1729- // in case there are no generics, take the spot between the function name
1730- // and the opening paren of the argument list
1731- let new_generics_span = tcx. def_ident_span ( impl_def_id) ?. shrink_to_hi ( ) ;
1732- // in case there are generics, just replace them
1733- let generics_span =
1734- impl_m. generics . span . substitute_dummy ( new_generics_span) ;
1735- // replace with the generics from the trait
1736- let new_generics =
1737- tcx. sess . source_map ( ) . span_to_snippet ( trait_m. generics . span ) . ok ( ) ?;
1738-
1739- err. multipart_suggestion (
1740- "try changing the `impl Trait` argument to a generic parameter" ,
1741- vec ! [
1742- // replace `impl Trait` with `T`
1743- ( impl_span, new_name. to_string( ) ) ,
1744- // replace impl method generics with trait method generics
1745- // This isn't quite right, as users might have changed the names
1746- // of the generics, but it works for the common case
1747- ( generics_span, new_generics) ,
1748- ] ,
1749- Applicability :: MaybeIncorrect ,
1750- ) ;
1751- } ;
1752- }
1722+ err. span_label ( impl_span, "expected generic parameter, found `impl Trait`" ) ;
1723+ let _: Option < _ > = try {
1724+ // try taking the name from the trait impl
1725+ // FIXME: this is obviously suboptimal since the name can already be used
1726+ // as another generic argument
1727+ let new_name = tcx. opt_item_name ( trait_def_id) ?;
1728+ let trait_m = trait_m. def_id . as_local ( ) ?;
1729+ let trait_m = tcx. hir ( ) . expect_trait_item ( trait_m) ;
1730+
1731+ let impl_m = impl_m. def_id . as_local ( ) ?;
1732+ let impl_m = tcx. hir ( ) . expect_impl_item ( impl_m) ;
1733+
1734+ // in case there are no generics, take the spot between the function name
1735+ // and the opening paren of the argument list
1736+ let new_generics_span = tcx. def_ident_span ( impl_def_id) ?. shrink_to_hi ( ) ;
1737+ // in case there are generics, just replace them
1738+ let generics_span = impl_m. generics . span . substitute_dummy ( new_generics_span) ;
1739+ // replace with the generics from the trait
1740+ let new_generics =
1741+ tcx. sess . source_map ( ) . span_to_snippet ( trait_m. generics . span ) . ok ( ) ?;
1742+
1743+ err. multipart_suggestion (
1744+ "try changing the `impl Trait` argument to a generic parameter" ,
1745+ vec ! [
1746+ // replace `impl Trait` with `T`
1747+ ( impl_span, new_name. to_string( ) ) ,
1748+ // replace impl method generics with trait method generics
1749+ // This isn't quite right, as users might have changed the names
1750+ // of the generics, but it works for the common case
1751+ ( generics_span, new_generics) ,
1752+ ] ,
1753+ Applicability :: MaybeIncorrect ,
1754+ ) ;
1755+ } ;
1756+ } else {
17531757 // The case where the trait method uses `impl Trait`, but the impl method uses
17541758 // explicit generics.
1755- ( false , true ) => {
1756- err. span_label ( impl_span, "expected `impl Trait`, found generic parameter" ) ;
1757- let _: Option < _ > = try {
1758- let impl_m = impl_m. def_id . as_local ( ) ?;
1759- let impl_m = tcx. hir ( ) . expect_impl_item ( impl_m) ;
1760- let ( sig, _) = impl_m. expect_fn ( ) ;
1761- let input_tys = sig. decl . inputs ;
1762-
1763- struct Visitor ( Option < Span > , hir:: def_id:: LocalDefId ) ;
1764- impl < ' v > intravisit:: Visitor < ' v > for Visitor {
1765- fn visit_ty ( & mut self , ty : & ' v hir:: Ty < ' v > ) {
1766- intravisit:: walk_ty ( self , ty) ;
1767- if let hir:: TyKind :: Path ( hir:: QPath :: Resolved ( None , path) ) = ty. kind
1768- && let Res :: Def ( DefKind :: TyParam , def_id) = path. res
1769- && def_id == self . 1 . to_def_id ( )
1770- {
1771- self . 0 = Some ( ty. span ) ;
1772- }
1759+ err. span_label ( impl_span, "expected `impl Trait`, found generic parameter" ) ;
1760+ let _: Option < _ > = try {
1761+ let impl_m = impl_m. def_id . as_local ( ) ?;
1762+ let impl_m = tcx. hir ( ) . expect_impl_item ( impl_m) ;
1763+ let ( sig, _) = impl_m. expect_fn ( ) ;
1764+ let input_tys = sig. decl . inputs ;
1765+
1766+ struct Visitor ( Option < Span > , hir:: def_id:: LocalDefId ) ;
1767+ impl < ' v > intravisit:: Visitor < ' v > for Visitor {
1768+ fn visit_ty ( & mut self , ty : & ' v hir:: Ty < ' v > ) {
1769+ intravisit:: walk_ty ( self , ty) ;
1770+ if let hir:: TyKind :: Path ( hir:: QPath :: Resolved ( None , path) ) = ty. kind
1771+ && let Res :: Def ( DefKind :: TyParam , def_id) = path. res
1772+ && def_id == self . 1 . to_def_id ( )
1773+ {
1774+ self . 0 = Some ( ty. span ) ;
17731775 }
17741776 }
1777+ }
17751778
1776- let mut visitor = Visitor ( None , impl_def_id) ;
1777- for ty in input_tys {
1778- intravisit:: Visitor :: visit_ty ( & mut visitor, ty) ;
1779- }
1780- let span = visitor. 0 ?;
1781-
1782- let bounds = impl_m. generics . bounds_for_param ( impl_def_id) . next ( ) ?. bounds ;
1783- let bounds = bounds. first ( ) ?. span ( ) . to ( bounds. last ( ) ?. span ( ) ) ;
1784- let bounds = tcx. sess . source_map ( ) . span_to_snippet ( bounds) . ok ( ) ?;
1785-
1786- err. multipart_suggestion (
1787- "try removing the generic parameter and using `impl Trait` instead" ,
1788- vec ! [
1789- // delete generic parameters
1790- ( impl_m. generics. span, String :: new( ) ) ,
1791- // replace param usage with `impl Trait`
1792- ( span, format!( "impl {bounds}" ) ) ,
1793- ] ,
1794- Applicability :: MaybeIncorrect ,
1795- ) ;
1796- } ;
1797- }
1798- _ => unreachable ! ( ) ,
1779+ let mut visitor = Visitor ( None , impl_def_id) ;
1780+ for ty in input_tys {
1781+ intravisit:: Visitor :: visit_ty ( & mut visitor, ty) ;
1782+ }
1783+ let span = visitor. 0 ?;
1784+
1785+ let bounds = impl_m. generics . bounds_for_param ( impl_def_id) . next ( ) ?. bounds ;
1786+ let bounds = bounds. first ( ) ?. span ( ) . to ( bounds. last ( ) ?. span ( ) ) ;
1787+ let bounds = tcx. sess . source_map ( ) . span_to_snippet ( bounds) . ok ( ) ?;
1788+
1789+ err. multipart_suggestion (
1790+ "try removing the generic parameter and using `impl Trait` instead" ,
1791+ vec ! [
1792+ // delete generic parameters
1793+ ( impl_m. generics. span, String :: new( ) ) ,
1794+ // replace param usage with `impl Trait`
1795+ ( span, format!( "impl {bounds}" ) ) ,
1796+ ] ,
1797+ Applicability :: MaybeIncorrect ,
1798+ ) ;
1799+ } ;
17991800 }
18001801 error_found = Some ( err. emit_unless ( delay) ) ;
18011802 }
@@ -1859,7 +1860,9 @@ fn compare_generic_param_kinds<'tcx>(
18591860 // this is exhaustive so that anyone adding new generic param kinds knows
18601861 // to make sure this error is reported for them.
18611862 ( Const { .. } , Const { .. } ) | ( Type { .. } , Type { .. } ) => false ,
1862- ( Lifetime { .. } , _) | ( _, Lifetime { .. } ) => unreachable ! ( ) ,
1863+ ( Lifetime { .. } , _) | ( _, Lifetime { .. } ) => {
1864+ bug ! ( "lifetime params are expected to be filtered by `ty_const_params_of`" )
1865+ }
18631866 } {
18641867 let param_impl_span = tcx. def_span ( param_impl. def_id ) ;
18651868 let param_trait_span = tcx. def_span ( param_trait. def_id ) ;
@@ -1883,7 +1886,10 @@ fn compare_generic_param_kinds<'tcx>(
18831886 )
18841887 }
18851888 Type { .. } => format ! ( "{prefix} type parameter" ) ,
1886- Lifetime { .. } => unreachable ! ( ) ,
1889+ Lifetime { .. } => span_bug ! (
1890+ tcx. def_span( param. def_id) ,
1891+ "lifetime params are expected to be filtered by `ty_const_params_of`"
1892+ ) ,
18871893 } ;
18881894
18891895 let trait_header_span = tcx. def_ident_span ( tcx. parent ( trait_item. def_id ) ) . unwrap ( ) ;
@@ -2187,7 +2193,10 @@ pub(super) fn check_type_bounds<'tcx>(
21872193 ..
21882194 } ) => ty. span ,
21892195 hir:: Node :: ImplItem ( hir:: ImplItem { kind : hir:: ImplItemKind :: Type ( ty) , .. } ) => ty. span ,
2190- _ => bug ! ( ) ,
2196+ item => span_bug ! (
2197+ tcx. def_span( impl_ty_def_id) ,
2198+ "cannot call `check_type_bounds` on item: {item:?}" ,
2199+ ) ,
21912200 }
21922201 } ;
21932202 let assumed_wf_types = ocx. assumed_wf_types_and_report_errors ( param_env, impl_ty_def_id) ?;
0 commit comments