@@ -934,12 +934,43 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
934934 ty,
935935 tcx. require_lang_item ( LangItem :: ConstParamTy , Some ( hir_ty. span ) ) ,
936936 ) ;
937- wfcx. register_bound (
938- ObligationCause :: new ( hir_ty. span , param. def_id , ObligationCauseCode :: Misc ) ,
939- wfcx. param_env ,
940- ty,
941- tcx. require_lang_item ( LangItem :: Sized , Some ( hir_ty. span ) ) ,
942- ) ;
937+ if !tcx. features ( ) . unsized_const_parameters {
938+ let cause = ObligationCause :: new (
939+ hir_ty. span ,
940+ param. def_id ,
941+ ObligationCauseCode :: Misc ,
942+ ) ;
943+
944+ wfcx. register_bound (
945+ cause. clone ( ) ,
946+ wfcx. param_env ,
947+ ty,
948+ tcx. require_lang_item ( LangItem :: Sized , Some ( hir_ty. span ) ) ,
949+ ) ;
950+
951+ // FIXME(unsized_const_parameters): Just remove the `ConstParamTy` impl on references
952+ // and make `const N: [u8]` work then we can get rid of this.
953+ if let Ok ( ty) = wfcx. structurally_normalize ( & cause, wfcx. param_env , ty)
954+ && let ty:: Ref ( ..) = ty. kind ( )
955+ {
956+ let mut diag = tcx. dcx ( ) . struct_span_err (
957+ hir_ty. span ,
958+ "references are forbidden as the type of const generic parameters" ,
959+ ) ;
960+ diag. note ( "only types that implement `ConstParamTy` are permitted" ) ;
961+
962+ tcx. disabled_nightly_features (
963+ & mut diag,
964+ Some ( param. hir_id ) ,
965+ [ (
966+ " references to implement the `ConstParamTy` trait" . into ( ) ,
967+ sym:: unsized_const_parameters,
968+ ) ] ,
969+ ) ;
970+
971+ return Err ( diag. emit ( ) ) ;
972+ }
973+ }
943974 Ok ( ( ) )
944975 } )
945976 } else {
@@ -962,6 +993,8 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
962993 diag. note ( "the only supported types are integers, `bool` and `char`" ) ;
963994
964995 let cause = ObligationCause :: misc ( hir_ty. span , param. def_id ) ;
996+ let adt_const_params_feature_string =
997+ " more complex and user defined types" . to_string ( ) ;
965998 let may_suggest_feature = match type_allowed_to_implement_const_param_ty (
966999 tcx,
9671000 tcx. param_env ( param. def_id ) ,
@@ -971,9 +1004,17 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
9711004 // Can never implement `ConstParamTy`, don't suggest anything.
9721005 Err (
9731006 ConstParamTyImplementationError :: NotAnAdtOrBuiltinAllowed
974- | ConstParamTyImplementationError :: InvalidInnerTyOfBuiltinTy ( ..)
975- | ConstParamTyImplementationError :: TypeNotSized ,
976- ) => false ,
1007+ | ConstParamTyImplementationError :: InvalidInnerTyOfBuiltinTy ( ..) ,
1008+ ) => None ,
1009+ Err ( ConstParamTyImplementationError :: UnsizedConstParamsFeatureRequired ) => {
1010+ Some ( vec ! [
1011+ ( adt_const_params_feature_string, sym:: adt_const_params) ,
1012+ (
1013+ " references to implement the `ConstParamTy` trait" . into( ) ,
1014+ sym:: unsized_const_parameters,
1015+ ) ,
1016+ ] )
1017+ }
9771018 // May be able to implement `ConstParamTy`. Only emit the feature help
9781019 // if the type is local, since the user may be able to fix the local type.
9791020 Err ( ConstParamTyImplementationError :: InfrigingFields ( ..) ) => {
@@ -993,20 +1034,16 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
9931034 }
9941035 }
9951036
996- ty_is_local ( ty)
1037+ ty_is_local ( ty) . then_some ( vec ! [ (
1038+ adt_const_params_feature_string,
1039+ sym:: adt_const_params,
1040+ ) ] )
9971041 }
9981042 // Implments `ConstParamTy`, suggest adding the feature to enable.
999- Ok ( ..) => true ,
1043+ Ok ( ..) => Some ( vec ! [ ( adt_const_params_feature_string , sym :: adt_const_params ) ] ) ,
10001044 } ;
1001- if may_suggest_feature {
1002- tcx. disabled_nightly_features (
1003- & mut diag,
1004- Some ( param. hir_id ) ,
1005- [ (
1006- " more complex and user defined types" . to_string ( ) ,
1007- sym:: adt_const_params,
1008- ) ] ,
1009- ) ;
1045+ if let Some ( features) = may_suggest_feature {
1046+ tcx. disabled_nightly_features ( & mut diag, Some ( param. hir_id ) , features) ;
10101047 }
10111048
10121049 Err ( diag. emit ( ) )
0 commit comments