@@ -14,41 +14,24 @@ pub struct Rename {
1414 pub rename : String ,
1515}
1616
17- #[ derive( Debug , Serialize ) ]
18- pub struct DisallowedPathWithoutReplacement {
19- path : String ,
20- reason : Option < String > ,
21- }
17+ pub type DisallowedPathWithoutReplacement = DisallowedPath < false > ;
2218
23- #[ derive( Clone , Debug , Serialize ) ]
24- pub struct DisallowedPath {
19+ #[ derive( Debug , Serialize ) ]
20+ pub struct DisallowedPath < const REPLACEMENT_ALLOWED : bool = true > {
2521 path : String ,
2622 reason : Option < String > ,
2723 replacement : Option < String > ,
2824}
2925
30- impl < ' de > Deserialize < ' de > for DisallowedPathWithoutReplacement {
26+ impl < ' de , const REPLACEMENT_ALLOWED : bool > Deserialize < ' de > for DisallowedPath < REPLACEMENT_ALLOWED > {
3127 fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
3228 where
3329 D : Deserializer < ' de > ,
3430 {
3531 let enum_ = DisallowedPathEnum :: deserialize ( deserializer) ?;
36- if enum_. replacement ( ) . is_some ( ) {
32+ if ! REPLACEMENT_ALLOWED && enum_. replacement ( ) . is_some ( ) {
3733 return Err ( de:: Error :: custom ( "replacement not allowed for this configuration" ) ) ;
3834 }
39- Ok ( Self {
40- path : enum_. path ( ) . to_owned ( ) ,
41- reason : enum_. reason ( ) . map ( ToOwned :: to_owned) ,
42- } )
43- }
44- }
45-
46- impl < ' de > Deserialize < ' de > for DisallowedPath {
47- fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
48- where
49- D : Deserializer < ' de > ,
50- {
51- let enum_ = DisallowedPathEnum :: deserialize ( deserializer) ?;
5235 Ok ( Self {
5336 path : enum_. path ( ) . to_owned ( ) ,
5437 reason : enum_. reason ( ) . map ( ToOwned :: to_owned) ,
@@ -68,45 +51,24 @@ pub enum DisallowedPathEnum {
6851 } ,
6952}
7053
71- pub trait AmendDiag {
72- fn path ( & self ) -> & str ;
73- fn reason ( & self ) -> Option < & str > ;
74- fn replacement ( & self ) -> Option < & str > ;
75- fn amend_diag ( & self , span : Span , diag : & mut Diag < ' _ , ( ) > ) {
76- if let Some ( replacement) = & self . replacement ( ) {
77- diag. span_suggestion (
78- span,
79- self . reason ( ) . map_or_else ( || String :: from ( "use" ) , ToOwned :: to_owned) ,
80- replacement,
81- Applicability :: MachineApplicable ,
82- ) ;
83- } else if let Some ( reason) = self . reason ( ) {
84- diag. note ( reason. to_owned ( ) ) ;
85- }
86- }
87- }
88-
89- impl AmendDiag for DisallowedPathWithoutReplacement {
90- fn path ( & self ) -> & str {
54+ impl < const REPLACEMENT_ALLOWED : bool > DisallowedPath < REPLACEMENT_ALLOWED > {
55+ pub fn path ( & self ) -> & str {
9156 & self . path
9257 }
93- fn reason ( & self ) -> Option < & str > {
94- self . reason . as_deref ( )
95- }
96- fn replacement ( & self ) -> Option < & str > {
97- None
98- }
99- }
10058
101- impl AmendDiag for DisallowedPath {
102- fn path ( & self ) -> & str {
103- & self . path
104- }
105- fn reason ( & self ) -> Option < & str > {
106- self . reason . as_deref ( )
107- }
108- fn replacement ( & self ) -> Option < & str > {
109- self . replacement . as_deref ( )
59+ pub fn diag_amendment ( & self , span : Span ) -> impl FnOnce ( & mut Diag < ' _ , ( ) > ) + use < ' _ , REPLACEMENT_ALLOWED > {
60+ move |diag| {
61+ if let Some ( replacement) = & self . replacement {
62+ diag. span_suggestion (
63+ span,
64+ self . reason . as_ref ( ) . map_or_else ( || String :: from ( "use" ) , Clone :: clone) ,
65+ replacement,
66+ Applicability :: MachineApplicable ,
67+ ) ;
68+ } else if let Some ( reason) = & self . reason {
69+ diag. note ( reason. clone ( ) ) ;
70+ }
71+ }
11072 }
11173}
11274
@@ -133,10 +95,10 @@ impl DisallowedPathEnum {
13395}
13496
13597/// Creates a map of disallowed items to the reason they were disallowed.
136- pub fn create_disallowed_map < T : AmendDiag > (
98+ pub fn create_disallowed_map < const REPLACEMENT_ALLOWED : bool > (
13799 tcx : TyCtxt < ' _ > ,
138- disallowed : & ' static [ T ] ,
139- ) -> DefIdMap < ( & ' static str , & ' static T ) > {
100+ disallowed : & ' static [ DisallowedPath < REPLACEMENT_ALLOWED > ] ,
101+ ) -> DefIdMap < ( & ' static str , & ' static DisallowedPath < REPLACEMENT_ALLOWED > ) > {
140102 disallowed
141103 . iter ( )
142104 . map ( |x| ( x. path ( ) , x. path ( ) . split ( "::" ) . collect :: < Vec < _ > > ( ) , x) )
0 commit comments