@@ -290,9 +290,7 @@ impl SourceAnalyzer {
290
290
return Some ( PathResolution :: Def ( ModuleDef :: Variant ( variant. into ( ) ) ) ) ;
291
291
}
292
292
prefer_value_ns = true ;
293
- }
294
-
295
- if let Some ( path_pat) = parent ( ) . and_then ( ast:: PathPat :: cast) {
293
+ } else if let Some ( path_pat) = parent ( ) . and_then ( ast:: PathPat :: cast) {
296
294
let pat_id = self . pat_id ( & path_pat. into ( ) ) ?;
297
295
if let Some ( assoc) = self . infer . as_ref ( ) ?. assoc_resolutions_for_pat ( pat_id) {
298
296
return Some ( PathResolution :: AssocItem ( assoc. into ( ) ) ) ;
@@ -302,9 +300,7 @@ impl SourceAnalyzer {
302
300
{
303
301
return Some ( PathResolution :: Def ( ModuleDef :: Variant ( variant. into ( ) ) ) ) ;
304
302
}
305
- }
306
-
307
- if let Some ( rec_lit) = parent ( ) . and_then ( ast:: RecordExpr :: cast) {
303
+ } else if let Some ( rec_lit) = parent ( ) . and_then ( ast:: RecordExpr :: cast) {
308
304
let expr_id = self . expr_id ( db, & rec_lit. into ( ) ) ?;
309
305
if let Some ( VariantId :: EnumVariantId ( variant) ) =
310
306
self . infer . as_ref ( ) ?. variant_resolution_for_expr ( expr_id)
@@ -331,32 +327,34 @@ impl SourceAnalyzer {
331
327
// Case where path is a qualifier of a use tree, e.g. foo::bar::{Baz, Qux} where we are
332
328
// trying to resolve foo::bar.
333
329
if let Some ( use_tree) = parent ( ) . and_then ( ast:: UseTree :: cast) {
334
- if let Some ( qualifier) = use_tree. path ( ) {
335
- if path == & qualifier && use_tree. coloncolon_token ( ) . is_some ( ) {
336
- return resolve_hir_path_qualifier ( db, & self . resolver , & hir_path) ;
337
- }
330
+ if use_tree. coloncolon_token ( ) . is_some ( ) {
331
+ return resolve_hir_path_qualifier ( db, & self . resolver , & hir_path) ;
338
332
}
339
333
}
340
334
341
335
let is_path_of_attr = path
342
- . top_path ( )
343
336
. syntax ( )
344
337
. ancestors ( )
345
- . nth ( 2 ) // Path -> Meta -> Attr
346
- . map_or ( false , |it| ast:: Attr :: can_cast ( it. kind ( ) ) ) ;
338
+ . map ( |it| it. kind ( ) )
339
+ . take_while ( |& kind| ast:: Path :: can_cast ( kind) || ast:: Meta :: can_cast ( kind) )
340
+ . last ( )
341
+ . map_or ( false , ast:: Meta :: can_cast) ;
347
342
348
343
// Case where path is a qualifier of another path, e.g. foo::bar::Baz where we are
349
344
// trying to resolve foo::bar.
350
- if let Some ( outer_path) = path. parent_path ( ) {
351
- if let Some ( qualifier) = outer_path. qualifier ( ) {
352
- if path == & qualifier {
353
- return resolve_hir_path_qualifier ( db, & self . resolver , & hir_path) ;
345
+ if path. parent_path ( ) . is_some ( ) {
346
+ return match resolve_hir_path_qualifier ( db, & self . resolver , & hir_path) {
347
+ None if is_path_of_attr => {
348
+ path. first_segment ( ) . and_then ( |it| it. name_ref ( ) ) . and_then ( |name_ref| {
349
+ ToolModule :: by_name ( & name_ref. text ( ) ) . map ( PathResolution :: ToolModule )
350
+ } )
354
351
}
355
- }
352
+ res => res,
353
+ } ;
356
354
} else if is_path_of_attr {
357
355
// Case where we are resolving the final path segment of a path in an attribute
358
356
// in this case we have to check for inert/builtin attributes and tools and prioritize
359
- // resolution of attributes over other namesapces
357
+ // resolution of attributes over other namespaces
360
358
let name_ref = path. as_single_name_ref ( ) ;
361
359
let builtin =
362
360
name_ref. as_ref ( ) . map ( ast:: NameRef :: text) . as_deref ( ) . and_then ( BuiltinAttr :: by_name) ;
@@ -365,27 +363,17 @@ impl SourceAnalyzer {
365
363
}
366
364
return match resolve_hir_path_as_macro ( db, & self . resolver , & hir_path) {
367
365
res @ Some ( m) if m. is_attr ( ) => res. map ( PathResolution :: Macro ) ,
368
- _ => name_ref. and_then ( |name_ref| {
366
+ // this labels any path that starts with a tool module as the tool itself, this is technically wrong
367
+ // but there is no benefit in differentiating these two cases for the time being
368
+ _ => path. first_segment ( ) . and_then ( |it| it. name_ref ( ) ) . and_then ( |name_ref| {
369
369
ToolModule :: by_name ( & name_ref. text ( ) ) . map ( PathResolution :: ToolModule )
370
370
} ) ,
371
371
} ;
372
372
}
373
-
374
- let res = if parent ( ) . map_or ( false , |it| ast:: Visibility :: can_cast ( it. kind ( ) ) ) {
373
+ if parent ( ) . map_or ( false , |it| ast:: Visibility :: can_cast ( it. kind ( ) ) ) {
375
374
resolve_hir_path_qualifier ( db, & self . resolver , & hir_path)
376
375
} else {
377
376
resolve_hir_path_ ( db, & self . resolver , & hir_path, prefer_value_ns)
378
- } ;
379
- match res {
380
- Some ( _) => res,
381
- // this labels any path that starts with a tool module as the tool itself, this is technically wrong
382
- // but there is no benefit in differentiating these two cases for the time being
383
- None if is_path_of_attr => path
384
- . first_segment ( )
385
- . and_then ( |seg| seg. name_ref ( ) )
386
- . and_then ( |name_ref| ToolModule :: by_name ( & name_ref. text ( ) ) )
387
- . map ( PathResolution :: ToolModule ) ,
388
- None => None ,
389
377
}
390
378
}
391
379
0 commit comments