1
1
use rustc_ast as ast;
2
- use rustc_ast:: { FieldDef , Item , ItemKind , VariantData } ;
2
+ use rustc_ast:: { ItemKind , VariantData } ;
3
3
use rustc_expand:: base:: { Annotatable , ExtCtxt } ;
4
- use rustc_span:: { Ident , Span , kw , sym } ;
4
+ use rustc_span:: { kw , sym , Ident , Span } ;
5
5
use thin_vec:: thin_vec;
6
6
7
7
use crate :: deriving:: generic:: ty:: { Bounds , Path , PathKind , Ty } ;
8
8
use crate :: deriving:: generic:: {
9
- BlockOrExpr , FieldlessVariantsStrategy , MethodDef , SubstructureFields , TraitDef ,
10
- combine_substructure ,
9
+ combine_substructure , BlockOrExpr , FieldlessVariantsStrategy , MethodDef , SubstructureFields ,
10
+ TraitDef ,
11
11
} ;
12
12
use crate :: deriving:: pathvec_std;
13
13
use crate :: errors;
@@ -22,12 +22,23 @@ pub(crate) fn expand_deriving_from(
22
22
push : & mut dyn FnMut ( Annotatable ) ,
23
23
is_const : bool ,
24
24
) {
25
- let mut visitor = ExtractNonSingleFieldStruct { cx, field : None } ;
26
- item. visit_with ( & mut visitor) ;
25
+ let field = match & item {
26
+ Annotatable :: Item ( item) => match & item. kind {
27
+ ItemKind :: Struct ( _, _, data) => match data. fields ( ) {
28
+ [ field] => Some ( field. clone ( ) ) ,
29
+ _ => None ,
30
+ } ,
31
+ _ => None ,
32
+ } ,
33
+ _ => None ,
34
+ } ;
27
35
28
36
// Make sure that the derive is only invoked on single-field [tuple] structs.
29
37
// From this point below, we know that there is exactly one field.
30
- let Some ( field) = visitor. field else { return } ;
38
+ let Some ( field) = field else {
39
+ cx. dcx ( ) . emit_err ( errors:: DeriveFromWrongTarget { span } ) ;
40
+ return ;
41
+ } ;
31
42
32
43
let path = Path :: new_ (
33
44
pathvec_std ! ( convert:: From ) ,
@@ -98,23 +109,3 @@ pub(crate) fn expand_deriving_from(
98
109
99
110
from_trait_def. expand ( cx, mitem, item, push) ;
100
111
}
101
-
102
- struct ExtractNonSingleFieldStruct < ' a , ' b > {
103
- cx : & ' a ExtCtxt < ' b > ,
104
- field : Option < FieldDef > ,
105
- }
106
-
107
- impl < ' a , ' b > rustc_ast:: visit:: Visitor < ' a > for ExtractNonSingleFieldStruct < ' a , ' b > {
108
- fn visit_item ( & mut self , item : & ' a Item ) -> Self :: Result {
109
- match & item. kind {
110
- ItemKind :: Struct ( _, _, data) => match data. fields ( ) {
111
- [ field] => self . field = Some ( field. clone ( ) ) ,
112
- _ => { }
113
- } ,
114
- _ => { }
115
- } ;
116
- if self . field . is_none ( ) {
117
- self . cx . dcx ( ) . emit_err ( errors:: DeriveFromWrongTarget { span : item. span } ) ;
118
- }
119
- }
120
- }
0 commit comments