Skip to content

Commit 415a3ca

Browse files
Put back is_derive_trait_collision check
1 parent 537fdbd commit 415a3ca

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

+29-2
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,20 @@ fn trait_impls_for<'a>(
806806
iter.collect()
807807
}
808808

809+
/// Check for resolve collisions between a trait and its derive.
810+
///
811+
/// These are common and we should just resolve to the trait in that case.
812+
fn is_derive_trait_collision<T>(ns: &PerNS<Result<Vec<(Res, T)>, ResolutionFailure<'_>>>) -> bool {
813+
if let (&Ok(ref type_ns), &Ok(ref macro_ns)) = (&ns.type_ns, &ns.macro_ns) {
814+
type_ns.iter().any(|(res, _)| matches!(res, Res::Def(DefKind::Trait, _)))
815+
&& macro_ns
816+
.iter()
817+
.any(|(res, _)| matches!(res, Res::Def(DefKind::Macro(MacroKind::Derive), _)))
818+
} else {
819+
false
820+
}
821+
}
822+
809823
impl<'a, 'tcx> DocVisitor for LinkCollector<'a, 'tcx> {
810824
fn visit_item(&mut self, item: &Item) {
811825
self.resolve_links(item);
@@ -1313,9 +1327,22 @@ impl LinkCollector<'_, '_> {
13131327
disambiguator,
13141328
candidates.into_iter().filter_map(|res| res.err()).collect(),
13151329
);
1330+
} else if len == 1 {
1331+
candidates.into_iter().filter_map(|res| res.ok()).flatten().collect::<Vec<_>>()
1332+
} else {
1333+
let has_derive_trait_collision = is_derive_trait_collision(&candidates);
1334+
if len == 2 && has_derive_trait_collision {
1335+
candidates.type_ns.unwrap()
1336+
} else {
1337+
// If we're reporting an ambiguity, don't mention the namespaces that failed
1338+
let mut candidates = candidates.map(|candidate| candidate.ok());
1339+
// If there a collision between a trait and a derive, we ignore the derive.
1340+
if has_derive_trait_collision {
1341+
candidates.macro_ns = None;
1342+
}
1343+
candidates.into_iter().filter_map(|res| res).flatten().collect::<Vec<_>>()
1344+
}
13161345
}
1317-
1318-
candidates.into_iter().filter_map(|res| res.ok()).flatten().collect::<Vec<_>>()
13191346
}
13201347
}
13211348
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This test ensures that this warning doesn't show up:
2+
// warning: `PartialEq` is both a trait and a derive macro
3+
// --> tests/rustdoc-ui/intra-doc/issue-108653-associated-items-10.rs:1:7
4+
// |
5+
// 1 | //! [`PartialEq`]
6+
// | ^^^^^^^^^ ambiguous link
7+
// |
8+
// = note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default
9+
// help: to link to the trait, prefix with `trait@`
10+
// |
11+
// 1 | //! [`trait@PartialEq`]
12+
// | ++++++
13+
// help: to link to the derive macro, prefix with `derive@`
14+
// |
15+
// 1 | //! [`derive@PartialEq`]
16+
// | +++++++
17+
18+
// check-pass
19+
20+
#![deny(rustdoc::broken_intra_doc_links)]
21+
22+
//! [`PartialEq`]

0 commit comments

Comments
 (0)