@@ -82,38 +82,40 @@ pub enum NameClass {
8282 Definition ( Definition ) ,
8383 /// `None` in `if let None = Some(82) {}`
8484 ConstReference ( Definition ) ,
85+ FieldShorthand {
86+ local : Local ,
87+ field : Definition ,
88+ } ,
8589}
8690
8791impl NameClass {
8892 pub fn into_definition ( self ) -> Option < Definition > {
8993 match self {
9094 NameClass :: Definition ( it) => Some ( it) ,
9195 NameClass :: ConstReference ( _) => None ,
96+ NameClass :: FieldShorthand { local : _, field } => Some ( field) ,
9297 }
9398 }
9499
95100 pub fn definition ( self ) -> Definition {
96101 match self {
97102 NameClass :: Definition ( it) | NameClass :: ConstReference ( it) => it,
103+ NameClass :: FieldShorthand { local, field : _ } => Definition :: Local ( local) ,
98104 }
99105 }
100106}
101107
102108pub fn classify_name ( sema : & Semantics < RootDatabase > , name : & ast:: Name ) -> Option < NameClass > {
103109 let _p = profile ( "classify_name" ) ;
104110
105- if let Some ( bind_pat) = name. syntax ( ) . parent ( ) . and_then ( ast:: BindPat :: cast) {
111+ let parent = name. syntax ( ) . parent ( ) ?;
112+
113+ if let Some ( bind_pat) = ast:: BindPat :: cast ( parent. clone ( ) ) {
106114 if let Some ( def) = sema. resolve_bind_pat_to_const ( & bind_pat) {
107115 return Some ( NameClass :: ConstReference ( Definition :: ModuleDef ( def) ) ) ;
108116 }
109117 }
110118
111- classify_name_inner ( sema, name) . map ( NameClass :: Definition )
112- }
113-
114- fn classify_name_inner ( sema : & Semantics < RootDatabase > , name : & ast:: Name ) -> Option < Definition > {
115- let parent = name. syntax ( ) . parent ( ) ?;
116-
117119 match_ast ! {
118120 match parent {
119121 ast:: Alias ( it) => {
@@ -123,69 +125,71 @@ fn classify_name_inner(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Opti
123125 let name_ref = path_segment. name_ref( ) ?;
124126 let name_ref_class = classify_name_ref( sema, & name_ref) ?;
125127
126- Some ( name_ref_class. definition( ) )
128+ Some ( NameClass :: Definition ( name_ref_class. definition( ) ) )
127129 } ,
128130 ast:: BindPat ( it) => {
131+ let local = sema. to_def( & it) ?;
132+
129133 if let Some ( record_field_pat) = it. syntax( ) . parent( ) . and_then( ast:: RecordFieldPat :: cast) {
130- return Some ( Definition :: Field (
131- sema. resolve_record_field_pat( & record_field_pat) ?
132- ) ) ;
134+ if let Some ( field) = sema. resolve_record_field_pat( & record_field_pat) {
135+ let field = Definition :: Field ( field) ;
136+ return Some ( NameClass :: FieldShorthand { local, field } ) ;
137+ }
133138 }
134139
135- let local = sema. to_def( & it) ?;
136- Some ( Definition :: Local ( local) )
140+ Some ( NameClass :: Definition ( Definition :: Local ( local) ) )
137141 } ,
138142 ast:: RecordFieldDef ( it) => {
139143 let field: hir:: Field = sema. to_def( & it) ?;
140- Some ( Definition :: Field ( field) )
144+ Some ( NameClass :: Definition ( Definition :: Field ( field) ) )
141145 } ,
142146 ast:: Module ( it) => {
143147 let def = sema. to_def( & it) ?;
144- Some ( Definition :: ModuleDef ( def. into( ) ) )
148+ Some ( NameClass :: Definition ( Definition :: ModuleDef ( def. into( ) ) ) )
145149 } ,
146150 ast:: StructDef ( it) => {
147151 let def: hir:: Struct = sema. to_def( & it) ?;
148- Some ( Definition :: ModuleDef ( def. into( ) ) )
152+ Some ( NameClass :: Definition ( Definition :: ModuleDef ( def. into( ) ) ) )
149153 } ,
150154 ast:: UnionDef ( it) => {
151155 let def: hir:: Union = sema. to_def( & it) ?;
152- Some ( Definition :: ModuleDef ( def. into( ) ) )
156+ Some ( NameClass :: Definition ( Definition :: ModuleDef ( def. into( ) ) ) )
153157 } ,
154158 ast:: EnumDef ( it) => {
155159 let def: hir:: Enum = sema. to_def( & it) ?;
156- Some ( Definition :: ModuleDef ( def. into( ) ) )
160+ Some ( NameClass :: Definition ( Definition :: ModuleDef ( def. into( ) ) ) )
157161 } ,
158162 ast:: TraitDef ( it) => {
159163 let def: hir:: Trait = sema. to_def( & it) ?;
160- Some ( Definition :: ModuleDef ( def. into( ) ) )
164+ Some ( NameClass :: Definition ( Definition :: ModuleDef ( def. into( ) ) ) )
161165 } ,
162166 ast:: StaticDef ( it) => {
163167 let def: hir:: Static = sema. to_def( & it) ?;
164- Some ( Definition :: ModuleDef ( def. into( ) ) )
168+ Some ( NameClass :: Definition ( Definition :: ModuleDef ( def. into( ) ) ) )
165169 } ,
166170 ast:: EnumVariant ( it) => {
167171 let def: hir:: EnumVariant = sema. to_def( & it) ?;
168- Some ( Definition :: ModuleDef ( def. into( ) ) )
172+ Some ( NameClass :: Definition ( Definition :: ModuleDef ( def. into( ) ) ) )
169173 } ,
170174 ast:: FnDef ( it) => {
171175 let def: hir:: Function = sema. to_def( & it) ?;
172- Some ( Definition :: ModuleDef ( def. into( ) ) )
176+ Some ( NameClass :: Definition ( Definition :: ModuleDef ( def. into( ) ) ) )
173177 } ,
174178 ast:: ConstDef ( it) => {
175179 let def: hir:: Const = sema. to_def( & it) ?;
176- Some ( Definition :: ModuleDef ( def. into( ) ) )
180+ Some ( NameClass :: Definition ( Definition :: ModuleDef ( def. into( ) ) ) )
177181 } ,
178182 ast:: TypeAliasDef ( it) => {
179183 let def: hir:: TypeAlias = sema. to_def( & it) ?;
180- Some ( Definition :: ModuleDef ( def. into( ) ) )
184+ Some ( NameClass :: Definition ( Definition :: ModuleDef ( def. into( ) ) ) )
181185 } ,
182186 ast:: MacroCall ( it) => {
183187 let def = sema. to_def( & it) ?;
184- Some ( Definition :: Macro ( def) )
188+ Some ( NameClass :: Definition ( Definition :: Macro ( def) ) )
185189 } ,
186190 ast:: TypeParam ( it) => {
187191 let def = sema. to_def( & it) ?;
188- Some ( Definition :: TypeParam ( def) )
192+ Some ( NameClass :: Definition ( Definition :: TypeParam ( def) ) )
189193 } ,
190194 _ => None ,
191195 }
0 commit comments