@@ -6,8 +6,9 @@ use crate::astconv::{
66use crate :: errors:: AssocTypeBindingNotAllowed ;
77use crate :: structured_errors:: { StructuredDiagnostic , WrongNumberOfGenericArgs } ;
88use rustc_ast:: ast:: ParamKindOrd ;
9- use rustc_errors:: { struct_span_err, Applicability , ErrorReported } ;
9+ use rustc_errors:: { struct_span_err, Applicability , DiagnosticBuilder , ErrorReported } ;
1010use rustc_hir as hir;
11+ use rustc_hir:: def:: { DefKind , Res } ;
1112use rustc_hir:: def_id:: DefId ;
1213use rustc_hir:: GenericArg ;
1314use rustc_middle:: ty:: {
@@ -24,8 +25,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2425 tcx : TyCtxt < ' _ > ,
2526 arg : & GenericArg < ' _ > ,
2627 param : & GenericParamDef ,
27- // DefId of the function
28- //body_def_id: DefId,
2928 possible_ordering_error : bool ,
3029 help : Option < & str > ,
3130 ) {
@@ -45,6 +44,19 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
4544 }
4645 }
4746
47+ let add_braces_suggestion = |arg : & GenericArg < ' _ > , err : & mut DiagnosticBuilder < ' _ > | {
48+ let suggestions = vec ! [
49+ ( arg. span( ) . shrink_to_lo( ) , String :: from( "{ " ) ) ,
50+ ( arg. span( ) . shrink_to_hi( ) , String :: from( " }" ) ) ,
51+ ] ;
52+ err. multipart_suggestion (
53+ "if this generic argument was intended as a const parameter, \
54+ surround it with braces",
55+ suggestions,
56+ Applicability :: MaybeIncorrect ,
57+ ) ;
58+ } ;
59+
4860 // Specific suggestion set for diagnostics
4961 match ( arg, & param. kind ) {
5062 (
@@ -53,40 +65,34 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
5365 ..
5466 } ) ,
5567 GenericParamDefKind :: Const ,
56- ) => {
57- use rustc_hir:: def:: { DefKind , Res } ;
58- match path. res {
59- Res :: Err => { }
60- Res :: Def ( DefKind :: TyParam , src_def_id) => ( || {
61- let param_hir_id = match param. def_id . as_local ( ) {
62- Some ( x) => tcx. hir ( ) . local_def_id_to_hir_id ( x) ,
63- None => return ,
64- } ;
68+ ) => match path. res {
69+ Res :: Err => {
70+ add_braces_suggestion ( arg, & mut err) ;
71+ err. set_primary_message (
72+ "unresolved item provided when a constant was expected" ,
73+ ) ;
74+ }
75+ Res :: Def ( DefKind :: TyParam , src_def_id) => {
76+ if let Some ( param_local_id) = param. def_id . as_local ( ) {
77+ let param_hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( param_local_id) ;
6578 let param_name = tcx. hir ( ) . ty_param_name ( param_hir_id) ;
6679 let param_type = tcx. type_of ( param. def_id ) ;
6780 if param_type. is_suggestable ( ) {
6881 err. span_suggestion (
6982 tcx. def_span ( src_def_id) ,
70- & format ! ( "try changing to a const-generic parameter:" ) ,
83+ "consider changing this type paramater to a ` const` -generic" ,
7184 format ! ( "const {}: {}" , param_name, param_type) ,
7285 Applicability :: MaybeIncorrect ,
7386 ) ;
74- }
75- } ) ( ) ,
76- _ => {
77- let suggestions = vec ! [
78- ( arg. span( ) . shrink_to_lo( ) , String :: from( "{ " ) ) ,
79- ( arg. span( ) . shrink_to_hi( ) , String :: from( " }" ) ) ,
80- ] ;
81- err. multipart_suggestion (
82- "if this generic argument was intended as a const parameter, \
83- try surrounding it with braces:",
84- suggestions,
85- Applicability :: MaybeIncorrect ,
86- ) ;
87+ } ;
8788 }
8889 }
89- }
90+ _ => add_braces_suggestion ( arg, & mut err) ,
91+ } ,
92+ (
93+ GenericArg :: Type ( hir:: Ty { kind : hir:: TyKind :: Path ( _) , .. } ) ,
94+ GenericParamDefKind :: Const ,
95+ ) => add_braces_suggestion ( arg, & mut err) ,
9096 (
9197 GenericArg :: Type ( hir:: Ty { kind : hir:: TyKind :: Array ( _, len) , .. } ) ,
9298 GenericParamDefKind :: Const { .. } ,
0 commit comments