@@ -14,9 +14,33 @@ pub struct Rename {
1414 pub rename : String ,
1515}
1616
17- #[ derive( Debug , Deserialize ) ]
17+ #[ derive( Debug , Serialize ) ]
18+ pub struct DisallowedPath < const REPLACEMENT_ALLOWED : bool = true> {
19+ path : String ,
20+ reason : Option < String > ,
21+ replacement : Option < String > ,
22+ }
23+
24+ impl < ' de , const REPLACEMENT_ALLOWED : bool > Deserialize < ' de > for DisallowedPath < REPLACEMENT_ALLOWED > {
25+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
26+ where
27+ D : Deserializer < ' de > ,
28+ {
29+ let enum_ = DisallowedPathEnum :: deserialize ( deserializer) ?;
30+ if !REPLACEMENT_ALLOWED && enum_. replacement ( ) . is_some ( ) {
31+ return Err ( de:: Error :: custom ( "replacement not allowed for this configuration" ) ) ;
32+ }
33+ Ok ( Self {
34+ path : enum_. path ( ) . to_owned ( ) ,
35+ reason : enum_. reason ( ) . map ( ToOwned :: to_owned) ,
36+ replacement : enum_. replacement ( ) . map ( ToOwned :: to_owned) ,
37+ } )
38+ }
39+ }
40+
41+ #[ derive( Debug , Deserialize , Serialize ) ]
1842#[ serde( untagged) ]
19- pub enum DisallowedPath {
43+ pub enum DisallowedPathEnum {
2044 Simple ( String ) ,
2145 WithReason {
2246 path : String ,
@@ -25,27 +49,33 @@ pub enum DisallowedPath {
2549 } ,
2650}
2751
28- impl DisallowedPath {
52+ impl < const REPLACEMENT_ALLOWED : bool > DisallowedPath < REPLACEMENT_ALLOWED > {
2953 pub fn path ( & self ) -> & str {
30- let ( Self :: Simple ( path) | Self :: WithReason { path, .. } ) = self ;
31-
32- path
54+ & self . path
3355 }
3456
35- pub fn diag_amendment ( & self , span : Span ) -> impl FnOnce ( & mut Diag < ' _ , ( ) > ) + use < ' _ > {
57+ pub fn diag_amendment ( & self , span : Span ) -> impl FnOnce ( & mut Diag < ' _ , ( ) > ) + use < ' _ , REPLACEMENT_ALLOWED > {
3658 move |diag| {
37- if let Some ( replacement) = self . replacement ( ) {
59+ if let Some ( replacement) = & self . replacement {
3860 diag. span_suggestion (
3961 span,
40- self . reason ( ) . map_or_else ( || String :: from ( "use" ) , ToOwned :: to_owned ) ,
62+ self . reason . as_ref ( ) . map_or_else ( || String :: from ( "use" ) , Clone :: clone ) ,
4163 replacement,
4264 Applicability :: MachineApplicable ,
4365 ) ;
44- } else if let Some ( reason) = self . reason ( ) {
45- diag. note ( reason. to_owned ( ) ) ;
66+ } else if let Some ( reason) = & self . reason {
67+ diag. note ( reason. clone ( ) ) ;
4668 }
4769 }
4870 }
71+ }
72+
73+ impl DisallowedPathEnum {
74+ pub fn path ( & self ) -> & str {
75+ let ( Self :: Simple ( path) | Self :: WithReason { path, .. } ) = self ;
76+
77+ path
78+ }
4979
5080 fn reason ( & self ) -> Option < & str > {
5181 match & self {
@@ -63,10 +93,10 @@ impl DisallowedPath {
6393}
6494
6595/// Creates a map of disallowed items to the reason they were disallowed.
66- pub fn create_disallowed_map (
96+ pub fn create_disallowed_map < const REPLACEMENT_ALLOWED : bool > (
6797 tcx : TyCtxt < ' _ > ,
68- disallowed : & ' static [ DisallowedPath ] ,
69- ) -> DefIdMap < ( & ' static str , & ' static DisallowedPath ) > {
98+ disallowed : & ' static [ DisallowedPath < REPLACEMENT_ALLOWED > ] ,
99+ ) -> DefIdMap < ( & ' static str , & ' static DisallowedPath < REPLACEMENT_ALLOWED > ) > {
70100 disallowed
71101 . iter ( )
72102 . map ( |x| ( x. path ( ) , x. path ( ) . split ( "::" ) . collect :: < Vec < _ > > ( ) , x) )
@@ -466,7 +496,6 @@ macro_rules! unimplemented_serialize {
466496}
467497
468498unimplemented_serialize ! {
469- DisallowedPath ,
470499 Rename ,
471500 MacroMatcher ,
472501}
0 commit comments