@@ -54,12 +54,6 @@ enum ErrorKind<'a> {
54
54
AnchorFailure ( AnchorFailure ) ,
55
55
}
56
56
57
- impl < ' a > From < ResolutionFailure < ' a > > for ErrorKind < ' a > {
58
- fn from ( err : ResolutionFailure < ' a > ) -> Self {
59
- ErrorKind :: Resolve ( box err)
60
- }
61
- }
62
-
63
57
#[ derive( Copy , Clone , Debug , Hash ) ]
64
58
enum Res {
65
59
Def ( DefKind , DefId ) ,
@@ -371,7 +365,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
371
365
path_str : & ' path str ,
372
366
item_id : ItemId ,
373
367
module_id : DefId ,
374
- ) -> Result < ( Res , Option < ItemFragment > ) , ErrorKind < ' path > > {
368
+ ) -> Result < ( Res , Option < ItemFragment > ) , ResolutionFailure < ' path > > {
375
369
let tcx = self . cx . tcx ;
376
370
let no_res = || ResolutionFailure :: NotResolved {
377
371
item_id,
@@ -445,25 +439,6 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
445
439
} )
446
440
}
447
441
448
- /// Resolves a string as a macro.
449
- ///
450
- /// FIXME(jynelson): Can this be unified with `resolve()`?
451
- fn resolve_macro (
452
- & self ,
453
- path_str : & ' a str ,
454
- item_id : ItemId ,
455
- module_id : DefId ,
456
- ) -> Result < Res , ResolutionFailure < ' a > > {
457
- self . resolve_path ( path_str, MacroNS , item_id, module_id) . ok_or_else ( || {
458
- ResolutionFailure :: NotResolved {
459
- item_id,
460
- module_id,
461
- partial_res : None ,
462
- unresolved : path_str. into ( ) ,
463
- }
464
- } )
465
- }
466
-
467
442
fn resolve_self_ty ( & self , path_str : & str , ns : Namespace , item_id : ItemId ) -> Option < Res > {
468
443
if ns != TypeNS || path_str != "Self" {
469
444
return None ;
@@ -556,12 +531,12 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
556
531
module_id : DefId ,
557
532
user_fragment : & Option < String > ,
558
533
) -> Result < ( Res , Option < UrlFragment > ) , ErrorKind < ' path > > {
559
- let ( res, rustdoc_fragment) = self . resolve_inner ( path_str, ns, item_id, module_id) ?;
534
+ let ( res, rustdoc_fragment) = self
535
+ . resolve_inner ( path_str, ns, item_id, module_id)
536
+ . map_err ( |err| ErrorKind :: Resolve ( box err) ) ?;
560
537
let chosen_fragment = match ( user_fragment, rustdoc_fragment) {
561
- ( Some ( _) , Some ( r_frag) ) => {
562
- let diag_res = match r_frag {
563
- ItemFragment ( _, did) => Res :: Def ( self . cx . tcx . def_kind ( did) , did) ,
564
- } ;
538
+ ( Some ( _) , Some ( ItemFragment ( _, did) ) ) => {
539
+ let diag_res = Res :: Def ( self . cx . tcx . def_kind ( did) , did) ;
565
540
let failure = AnchorFailure :: RustdocAnchorConflict ( diag_res) ;
566
541
return Err ( ErrorKind :: AnchorFailure ( failure) ) ;
567
542
}
@@ -578,7 +553,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
578
553
ns : Namespace ,
579
554
item_id : ItemId ,
580
555
module_id : DefId ,
581
- ) -> Result < ( Res , Option < ItemFragment > ) , ErrorKind < ' path > > {
556
+ ) -> Result < ( Res , Option < ItemFragment > ) , ResolutionFailure < ' path > > {
582
557
if let Some ( res) = self . resolve_path ( path_str, ns, item_id, module_id) {
583
558
match res {
584
559
// FIXME(#76467): make this fallthrough to lookup the associated
@@ -595,6 +570,13 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
595
570
// Not a trait item; just return what we found.
596
571
_ => return Ok ( ( res, None ) ) ,
597
572
}
573
+ } else if ns == MacroNS {
574
+ return Err ( ResolutionFailure :: NotResolved {
575
+ item_id,
576
+ module_id,
577
+ partial_res : None ,
578
+ unresolved : path_str. into ( ) ,
579
+ } ) ;
598
580
}
599
581
600
582
// Try looking for methods and associated items.
@@ -639,8 +621,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
639
621
module_id,
640
622
partial_res : None ,
641
623
unresolved : path_root. into ( ) ,
642
- }
643
- . into ( ) )
624
+ } )
644
625
}
645
626
} )
646
627
}
@@ -862,26 +843,15 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
862
843
module_id : DefId ,
863
844
extra_fragment : & Option < String > ,
864
845
) -> Option < Res > {
865
- // resolve() can't be used for macro namespace
866
- let result = match ns {
867
- Namespace :: MacroNS => self
868
- . resolve_macro ( path_str, item_id, module_id)
869
- . map ( |res| ( res, None ) )
870
- . map_err ( ErrorKind :: from) ,
871
- Namespace :: TypeNS | Namespace :: ValueNS => {
872
- self . resolve ( path_str, ns, item_id, module_id, extra_fragment)
873
- }
874
- } ;
875
-
876
- let res = match result {
846
+ let res = match self . resolve ( path_str, ns, item_id, module_id, extra_fragment) {
877
847
Ok ( ( res, frag) ) => {
878
848
if let Some ( UrlFragment :: Item ( ItemFragment ( _, id) ) ) = frag {
879
849
Some ( Res :: Def ( self . cx . tcx . def_kind ( id) , id) )
880
850
} else {
881
851
Some ( res)
882
852
}
883
853
}
884
- Err ( ErrorKind :: Resolve ( box kind) ) => kind. full_res ( ) ,
854
+ Err ( ErrorKind :: Resolve ( kind) ) => kind. full_res ( ) ,
885
855
Err ( ErrorKind :: AnchorFailure ( AnchorFailure :: RustdocAnchorConflict ( res) ) ) => Some ( res) ,
886
856
Err ( ErrorKind :: AnchorFailure ( AnchorFailure :: MultipleAnchors ) ) => None ,
887
857
} ;
@@ -1481,80 +1451,57 @@ impl LinkCollector<'_, '_> {
1481
1451
let extra_fragment = & key. extra_fragment ;
1482
1452
1483
1453
match disambiguator. map ( Disambiguator :: ns) {
1484
- Some ( expected_ns @ ( ValueNS | TypeNS ) ) => {
1454
+ Some ( expected_ns) => {
1485
1455
match self . resolve ( path_str, expected_ns, item_id, base_node, extra_fragment) {
1486
1456
Ok ( res) => Some ( res) ,
1487
- Err ( ErrorKind :: Resolve ( box mut kind) ) => {
1457
+ Err ( ErrorKind :: AnchorFailure ( msg) ) => {
1458
+ anchor_failure ( self . cx , diag, msg) ;
1459
+ None
1460
+ }
1461
+ Err ( ErrorKind :: Resolve ( mut err) ) => {
1488
1462
// We only looked in one namespace. Try to give a better error if possible.
1489
- if kind. full_res ( ) . is_none ( ) {
1490
- let other_ns = if expected_ns == ValueNS { TypeNS } else { ValueNS } ;
1491
- // FIXME: really it should be `resolution_failure` that does this, not `resolve_with_disambiguator`
1492
- // See https://github.com/rust-lang/rust/pull/76955#discussion_r493953382 for a good approach
1493
- for new_ns in [ other_ns, MacroNS ] {
1463
+ // FIXME: really it should be `resolution_failure` that does this, not `resolve_with_disambiguator`.
1464
+ // See https://github.com/rust-lang/rust/pull/76955#discussion_r493953382 for a good approach.
1465
+ for other_ns in [ TypeNS , ValueNS , MacroNS ] {
1466
+ if other_ns != expected_ns {
1494
1467
if let Some ( res) = self . check_full_res (
1495
- new_ns ,
1468
+ other_ns ,
1496
1469
path_str,
1497
1470
item_id,
1498
1471
base_node,
1499
1472
extra_fragment,
1500
1473
) {
1501
- kind = ResolutionFailure :: WrongNamespace { res, expected_ns } ;
1474
+ * err = ResolutionFailure :: WrongNamespace { res, expected_ns } ;
1502
1475
break ;
1503
1476
}
1504
1477
}
1505
1478
}
1506
- resolution_failure ( self , diag, path_str, disambiguator, smallvec ! [ kind ] ) ;
1479
+ resolution_failure ( self , diag, path_str, disambiguator, smallvec ! [ * err ] ) ;
1507
1480
// This could just be a normal link or a broken link
1508
1481
// we could potentially check if something is
1509
1482
// "intra-doc-link-like" and warn in that case.
1510
1483
None
1511
1484
}
1512
- Err ( ErrorKind :: AnchorFailure ( msg) ) => {
1513
- anchor_failure ( self . cx , diag, msg) ;
1514
- None
1515
- }
1516
1485
}
1517
1486
}
1518
1487
None => {
1519
1488
// Try everything!
1520
- let candidates = PerNS {
1521
- macro_ns : self
1522
- . resolve_macro ( path_str, item_id, base_node)
1523
- . map ( |res| ( res, extra_fragment. clone ( ) . map ( UrlFragment :: UserWritten ) ) ) ,
1524
- type_ns : match self . resolve (
1525
- path_str,
1526
- TypeNS ,
1527
- item_id,
1528
- base_node,
1529
- extra_fragment,
1530
- ) {
1531
- Ok ( res) => {
1532
- debug ! ( "got res in TypeNS: {:?}" , res) ;
1533
- Ok ( res)
1534
- }
1489
+ let mut candidate =
1490
+ |ns| match self . resolve ( path_str, ns, item_id, base_node, extra_fragment) {
1491
+ Ok ( res) => Some ( Ok ( res) ) ,
1535
1492
Err ( ErrorKind :: AnchorFailure ( msg) ) => {
1536
- anchor_failure ( self . cx , diag, msg) ;
1537
- return None ;
1493
+ anchor_failure ( self . cx , diag. clone ( ) , msg) ;
1494
+ None
1538
1495
}
1539
- Err ( ErrorKind :: Resolve ( box kind) ) => Err ( kind) ,
1540
- } ,
1541
- value_ns : match self . resolve (
1542
- path_str,
1543
- ValueNS ,
1544
- item_id,
1545
- base_node,
1546
- extra_fragment,
1547
- ) {
1548
- Ok ( res) => Ok ( res) ,
1549
- Err ( ErrorKind :: AnchorFailure ( msg) ) => {
1550
- anchor_failure ( self . cx , diag, msg) ;
1551
- return None ;
1552
- }
1553
- Err ( ErrorKind :: Resolve ( box kind) ) => Err ( kind) ,
1554
- }
1555
- . and_then ( |( res, fragment) | {
1556
- // Constructors are picked up in the type namespace.
1496
+ Err ( ErrorKind :: Resolve ( err) ) => Some ( Err ( * err) ) ,
1497
+ } ;
1498
+
1499
+ let candidates = PerNS {
1500
+ macro_ns : candidate ( MacroNS ) ?,
1501
+ type_ns : candidate ( TypeNS ) ?,
1502
+ value_ns : candidate ( ValueNS ) ?. and_then ( |( res, fragment) | {
1557
1503
match res {
1504
+ // Constructors are picked up in the type namespace.
1558
1505
Res :: Def ( DefKind :: Ctor ( ..) , _) => {
1559
1506
Err ( ResolutionFailure :: WrongNamespace { res, expected_ns : TypeNS } )
1560
1507
}
@@ -1604,29 +1551,6 @@ impl LinkCollector<'_, '_> {
1604
1551
None
1605
1552
}
1606
1553
}
1607
- Some ( MacroNS ) => {
1608
- match self . resolve_macro ( path_str, item_id, base_node) {
1609
- Ok ( res) => Some ( ( res, extra_fragment. clone ( ) . map ( UrlFragment :: UserWritten ) ) ) ,
1610
- Err ( mut kind) => {
1611
- // `resolve_macro` only looks in the macro namespace. Try to give a better error if possible.
1612
- for ns in [ TypeNS , ValueNS ] {
1613
- if let Some ( res) = self . check_full_res (
1614
- ns,
1615
- path_str,
1616
- item_id,
1617
- base_node,
1618
- extra_fragment,
1619
- ) {
1620
- kind =
1621
- ResolutionFailure :: WrongNamespace { res, expected_ns : MacroNS } ;
1622
- break ;
1623
- }
1624
- }
1625
- resolution_failure ( self , diag, path_str, disambiguator, smallvec ! [ kind] ) ;
1626
- None
1627
- }
1628
- }
1629
- }
1630
1554
}
1631
1555
}
1632
1556
}
0 commit comments