@@ -6,8 +6,9 @@ use crate::astconv::{
6
6
use crate :: errors:: AssocTypeBindingNotAllowed ;
7
7
use crate :: structured_errors:: { StructuredDiagnostic , WrongNumberOfGenericArgs } ;
8
8
use rustc_ast:: ast:: ParamKindOrd ;
9
- use rustc_errors:: { struct_span_err, Applicability , ErrorReported } ;
9
+ use rustc_errors:: { struct_span_err, Applicability , DiagnosticBuilder , ErrorReported } ;
10
10
use rustc_hir as hir;
11
+ use rustc_hir:: def:: { DefKind , Res } ;
11
12
use rustc_hir:: def_id:: DefId ;
12
13
use rustc_hir:: GenericArg ;
13
14
use rustc_middle:: ty:: {
@@ -24,8 +25,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
24
25
tcx : TyCtxt < ' _ > ,
25
26
arg : & GenericArg < ' _ > ,
26
27
param : & GenericParamDef ,
27
- // DefId of the function
28
- //body_def_id: DefId,
29
28
possible_ordering_error : bool ,
30
29
help : Option < & str > ,
31
30
) {
@@ -45,6 +44,19 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
45
44
}
46
45
}
47
46
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
+
48
60
// Specific suggestion set for diagnostics
49
61
match ( arg, & param. kind ) {
50
62
(
@@ -53,40 +65,34 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
53
65
..
54
66
} ) ,
55
67
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) ;
65
78
let param_name = tcx. hir ( ) . ty_param_name ( param_hir_id) ;
66
79
let param_type = tcx. type_of ( param. def_id ) ;
67
80
if param_type. is_suggestable ( ) {
68
81
err. span_suggestion (
69
82
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" ,
71
84
format ! ( "const {}: {}" , param_name, param_type) ,
72
85
Applicability :: MaybeIncorrect ,
73
86
) ;
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
+ } ;
87
88
}
88
89
}
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) ,
90
96
(
91
97
GenericArg :: Type ( hir:: Ty { kind : hir:: TyKind :: Array ( _, len) , .. } ) ,
92
98
GenericParamDefKind :: Const { .. } ,
0 commit comments