@@ -934,12 +934,43 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
934
934
ty,
935
935
tcx. require_lang_item ( LangItem :: ConstParamTy , Some ( hir_ty. span ) ) ,
936
936
) ;
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
+ }
943
974
Ok ( ( ) )
944
975
} )
945
976
} else {
@@ -962,6 +993,8 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
962
993
diag. note ( "the only supported types are integers, `bool` and `char`" ) ;
963
994
964
995
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 ( ) ;
965
998
let may_suggest_feature = match type_allowed_to_implement_const_param_ty (
966
999
tcx,
967
1000
tcx. param_env ( param. def_id ) ,
@@ -971,9 +1004,17 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
971
1004
// Can never implement `ConstParamTy`, don't suggest anything.
972
1005
Err (
973
1006
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
+ }
977
1018
// May be able to implement `ConstParamTy`. Only emit the feature help
978
1019
// if the type is local, since the user may be able to fix the local type.
979
1020
Err ( ConstParamTyImplementationError :: InfrigingFields ( ..) ) => {
@@ -993,20 +1034,16 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
993
1034
}
994
1035
}
995
1036
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
+ ) ] )
997
1041
}
998
1042
// Implments `ConstParamTy`, suggest adding the feature to enable.
999
- Ok ( ..) => true ,
1043
+ Ok ( ..) => Some ( vec ! [ ( adt_const_params_feature_string , sym :: adt_const_params ) ] ) ,
1000
1044
} ;
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) ;
1010
1047
}
1011
1048
1012
1049
Err ( diag. emit ( ) )
0 commit comments