@@ -107,6 +107,12 @@ pub enum TypeNs {
107
107
// ModuleId(ModuleId)
108
108
}
109
109
110
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
111
+ pub enum ModuleOrTypeNs {
112
+ ModuleNs ( ModuleId ) ,
113
+ TypeNs ( TypeNs ) ,
114
+ }
115
+
110
116
#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
111
117
pub enum ResolveValueResult {
112
118
ValueNs ( ValueNs , Option < ImportOrGlob > ) ,
@@ -163,22 +169,33 @@ impl Resolver {
163
169
self . resolve_module_path ( db, path, BuiltinShadowMode :: Module )
164
170
}
165
171
166
- pub fn resolve_path_in_type_ns (
167
- & self ,
168
- db : & dyn DefDatabase ,
169
- path : & Path ,
170
- ) -> Option < ( TypeNs , Option < usize > , Option < ImportOrExternCrate > ) > {
172
+ pub fn resolve_path_in_type_ns < ' a > (
173
+ & ' a self ,
174
+ db : & ' a dyn DefDatabase ,
175
+ path : & ' a Path ,
176
+ ) -> impl Iterator < Item = ( ModuleOrTypeNs , Option < usize > , Option < ImportOrExternCrate > ) > + ' a
177
+ {
171
178
self . resolve_path_in_type_ns_with_prefix_info ( db, path) . map (
172
- |( resolution, remaining_segments, import, _) | ( resolution, remaining_segments, import) ,
179
+ move |( resolution, remaining_segments, import, _) | {
180
+ ( resolution, remaining_segments, import)
181
+ } ,
173
182
)
174
183
}
175
184
176
- pub fn resolve_path_in_type_ns_with_prefix_info (
177
- & self ,
178
- db : & dyn DefDatabase ,
179
- path : & Path ,
180
- ) -> Option < ( TypeNs , Option < usize > , Option < ImportOrExternCrate > , ResolvePathResultPrefixInfo ) >
181
- {
185
+ pub fn resolve_path_in_type_ns_with_prefix_info < ' a > (
186
+ & ' a self ,
187
+ db : & ' a dyn DefDatabase ,
188
+ path : & ' a Path ,
189
+ ) -> Box <
190
+ dyn Iterator <
191
+ Item = (
192
+ ModuleOrTypeNs ,
193
+ Option < usize > ,
194
+ Option < ImportOrExternCrate > ,
195
+ ResolvePathResultPrefixInfo ,
196
+ ) ,
197
+ > + ' a ,
198
+ > {
182
199
let path = match path {
183
200
Path :: BarePath ( mod_path) => mod_path,
184
201
Path :: Normal ( it) => & it. mod_path ,
@@ -192,75 +209,87 @@ impl Resolver {
192
209
LangItemTarget :: Trait ( it) => TypeNs :: TraitId ( it) ,
193
210
LangItemTarget :: Function ( _)
194
211
| LangItemTarget :: ImplDef ( _)
195
- | LangItemTarget :: Static ( _) => return None ,
212
+ | LangItemTarget :: Static ( _) => return Box :: new ( iter :: empty ( ) ) ,
196
213
} ;
197
- return Some ( (
198
- type_ns,
214
+ return Box :: new ( iter :: once ( (
215
+ ModuleOrTypeNs :: TypeNs ( type_ns) ,
199
216
seg. as_ref ( ) . map ( |_| 1 ) ,
200
217
None ,
201
218
ResolvePathResultPrefixInfo :: default ( ) ,
202
- ) ) ;
219
+ ) ) ) ;
203
220
}
204
221
} ;
205
- let first_name = path. segments ( ) . first ( ) ? ;
222
+ let Some ( first_name) = path. segments ( ) . first ( ) else { return Box :: new ( iter :: empty ( ) ) } ;
206
223
let skip_to_mod = path. kind != PathKind :: Plain ;
207
224
if skip_to_mod {
208
- return self . module_scope . resolve_path_in_type_ns ( db, path) ;
225
+ return Box :: new ( self . module_scope . resolve_path_in_type_ns ( db, path) . into_iter ( ) ) ;
209
226
}
210
227
211
228
let remaining_idx = || {
212
229
if path. segments ( ) . len ( ) == 1 { None } else { Some ( 1 ) }
213
230
} ;
214
231
215
- for scope in self . scopes ( ) {
216
- match scope {
217
- Scope :: ExprScope ( _) | Scope :: MacroDefScope ( _) => continue ,
232
+ let ns = self
233
+ . scopes ( )
234
+ . filter_map ( move |scope| match scope {
235
+ Scope :: ExprScope ( _) | Scope :: MacroDefScope ( _) => None ,
218
236
Scope :: GenericParams { params, def } => {
219
237
if let Some ( id) = params. find_type_by_name ( first_name, * def) {
220
238
return Some ( (
221
- TypeNs :: GenericParam ( id) ,
239
+ ModuleOrTypeNs :: TypeNs ( TypeNs :: GenericParam ( id) ) ,
222
240
remaining_idx ( ) ,
223
241
None ,
224
242
ResolvePathResultPrefixInfo :: default ( ) ,
225
243
) ) ;
226
244
}
245
+ None
227
246
}
228
247
& Scope :: ImplDefScope ( impl_) => {
229
248
if * first_name == sym:: Self_ . clone ( ) {
230
249
return Some ( (
231
- TypeNs :: SelfType ( impl_) ,
250
+ ModuleOrTypeNs :: TypeNs ( TypeNs :: SelfType ( impl_) ) ,
232
251
remaining_idx ( ) ,
233
252
None ,
234
253
ResolvePathResultPrefixInfo :: default ( ) ,
235
254
) ) ;
236
255
}
256
+ None
237
257
}
238
258
& Scope :: AdtScope ( adt) => {
239
259
if * first_name == sym:: Self_ . clone ( ) {
240
260
return Some ( (
241
- TypeNs :: AdtSelfType ( adt) ,
261
+ ModuleOrTypeNs :: TypeNs ( TypeNs :: AdtSelfType ( adt) ) ,
242
262
remaining_idx ( ) ,
243
263
None ,
244
264
ResolvePathResultPrefixInfo :: default ( ) ,
245
265
) ) ;
246
266
}
267
+ None
247
268
}
248
269
Scope :: BlockScope ( m) => {
249
270
if let Some ( res) = m. resolve_path_in_type_ns ( db, path) {
250
271
return Some ( res) ;
251
272
}
273
+ None
252
274
}
253
- }
254
- }
255
- self . module_scope . resolve_path_in_type_ns ( db, path)
275
+ } )
276
+ . chain ( self . module_scope . resolve_path_in_type_ns ( db, path) ) ;
277
+
278
+ Box :: new ( ns)
256
279
}
257
280
258
281
pub fn resolve_path_in_type_ns_fully (
259
282
& self ,
260
283
db : & dyn DefDatabase ,
261
284
path : & Path ,
262
285
) -> Option < TypeNs > {
263
- let ( res, unresolved, _) = self . resolve_path_in_type_ns ( db, path) ?;
286
+ let ( res, unresolved) = self
287
+ . resolve_path_in_type_ns ( db, path)
288
+ . filter_map ( |( res, unresolved, _) | match res {
289
+ ModuleOrTypeNs :: TypeNs ( it) => Some ( ( it, unresolved) ) ,
290
+ ModuleOrTypeNs :: ModuleNs ( _) => None ,
291
+ } )
292
+ . next ( ) ?;
264
293
if unresolved. is_some ( ) {
265
294
return None ;
266
295
}
@@ -1158,16 +1187,20 @@ impl ModuleItemMap {
1158
1187
& self ,
1159
1188
db : & dyn DefDatabase ,
1160
1189
path : & ModPath ,
1161
- ) -> Option < ( TypeNs , Option < usize > , Option < ImportOrExternCrate > , ResolvePathResultPrefixInfo ) >
1162
- {
1190
+ ) -> Option < (
1191
+ ModuleOrTypeNs ,
1192
+ Option < usize > ,
1193
+ Option < ImportOrExternCrate > ,
1194
+ ResolvePathResultPrefixInfo ,
1195
+ ) > {
1163
1196
let ( module_def, idx, prefix_info) = self . def_map . resolve_path_locally (
1164
1197
& self . local_def_map ,
1165
1198
db,
1166
1199
self . module_id ,
1167
1200
path,
1168
1201
BuiltinShadowMode :: Other ,
1169
1202
) ;
1170
- let ( res, import) = to_type_ns ( module_def) ?;
1203
+ let ( res, import) = to_module_or_type_ns ( module_def) ?;
1171
1204
Some ( ( res, idx, import, prefix_info) )
1172
1205
}
1173
1206
}
@@ -1192,23 +1225,24 @@ fn to_value_ns(per_ns: PerNs) -> Option<(ValueNs, Option<ImportOrGlob>)> {
1192
1225
Some ( ( res, import) )
1193
1226
}
1194
1227
1195
- fn to_type_ns ( per_ns : PerNs ) -> Option < ( TypeNs , Option < ImportOrExternCrate > ) > {
1228
+ fn to_module_or_type_ns ( per_ns : PerNs ) -> Option < ( ModuleOrTypeNs , Option < ImportOrExternCrate > ) > {
1196
1229
let def = per_ns. take_types_full ( ) ?;
1197
1230
let res = match def. def {
1198
- ModuleDefId :: AdtId ( it) => TypeNs :: AdtId ( it) ,
1199
- ModuleDefId :: EnumVariantId ( it) => TypeNs :: EnumVariantId ( it) ,
1231
+ ModuleDefId :: AdtId ( it) => ModuleOrTypeNs :: TypeNs ( TypeNs :: AdtId ( it) ) ,
1232
+ ModuleDefId :: EnumVariantId ( it) => ModuleOrTypeNs :: TypeNs ( TypeNs :: EnumVariantId ( it) ) ,
1233
+
1234
+ ModuleDefId :: TypeAliasId ( it) => ModuleOrTypeNs :: TypeNs ( TypeNs :: TypeAliasId ( it) ) ,
1235
+ ModuleDefId :: BuiltinType ( it) => ModuleOrTypeNs :: TypeNs ( TypeNs :: BuiltinType ( it) ) ,
1200
1236
1201
- ModuleDefId :: TypeAliasId ( it) => TypeNs :: TypeAliasId ( it) ,
1202
- ModuleDefId :: BuiltinType ( it) => TypeNs :: BuiltinType ( it) ,
1237
+ ModuleDefId :: TraitId ( it) => ModuleOrTypeNs :: TypeNs ( TypeNs :: TraitId ( it) ) ,
1238
+ ModuleDefId :: TraitAliasId ( it) => ModuleOrTypeNs :: TypeNs ( TypeNs :: TraitAliasId ( it) ) ,
1203
1239
1204
- ModuleDefId :: TraitId ( it) => TypeNs :: TraitId ( it) ,
1205
- ModuleDefId :: TraitAliasId ( it) => TypeNs :: TraitAliasId ( it) ,
1240
+ ModuleDefId :: ModuleId ( it) => ModuleOrTypeNs :: ModuleNs ( it) ,
1206
1241
1207
1242
ModuleDefId :: FunctionId ( _)
1208
1243
| ModuleDefId :: ConstId ( _)
1209
1244
| ModuleDefId :: MacroId ( _)
1210
- | ModuleDefId :: StaticId ( _)
1211
- | ModuleDefId :: ModuleId ( _) => return None ,
1245
+ | ModuleDefId :: StaticId ( _) => return None ,
1212
1246
} ;
1213
1247
Some ( ( res, def. import ) )
1214
1248
}
0 commit comments