Skip to content

Commit

Permalink
Emit error for ambiguities still not resolved at later stage
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Oct 9, 2024
1 parent 4aca4e1 commit 07da6fe
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ pub(crate) struct ResolutionInfo {
extra_fragment: Option<String>,
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub(crate) struct DiagnosticInfo<'a> {
item: &'a Item,
dox: &'a str,
Expand All @@ -259,6 +259,7 @@ pub(crate) struct DiagnosticInfo<'a> {

pub(crate) struct OwnedDiagnosticInfo {
item: Item,
dox: String,
ori_link: String,
link_range: MarkdownLinkRange,
}
Expand All @@ -267,6 +268,7 @@ impl From<DiagnosticInfo<'_>> for OwnedDiagnosticInfo {
fn from(f: DiagnosticInfo<'_>) -> Self {
Self {
item: f.item.clone(),
dox: f.dox.to_string(),
ori_link: f.ori_link.to_string(),
link_range: f.link_range.clone(),
}
Expand All @@ -278,7 +280,7 @@ impl OwnedDiagnosticInfo {
DiagnosticInfo {
item: &self.item,
ori_link: &self.ori_link,
dox: "",
dox: &self.dox,
link_range: self.link_range.clone(),
}
}
Expand Down Expand Up @@ -1156,18 +1158,30 @@ impl LinkCollector<'_, '_> {
// Primitive types are always valid.
Res::Primitive(_) => true,
});
if info.resolved.len() == 1 {
let (res, fragment) = info.resolved.pop().unwrap();
let diag_info = info.diag_info.into_info();
if let Some(link) = self.compute_link(
res,
fragment,
path_str,
info.disambiguator,
diag_info,
&info.link_text,
) {
self.save_link(*item_id, link);
match info.resolved.len() {
1 => {
let (res, fragment) = info.resolved.pop().unwrap();
let diag_info = info.diag_info.into_info();
if let Some(link) = self.compute_link(
res,
fragment,
path_str,
info.disambiguator,
diag_info,
&info.link_text,
) {
self.save_link(*item_id, link);
}
}
0 => {}
_ => {
let diag_info = info.diag_info.into_info();
let candidates = info
.resolved
.iter()
.map(|(res, _)| (*res, res.def_id(self.cx.tcx)))
.collect::<Vec<_>>();
ambiguity_error(self.cx, &diag_info, path_str, &candidates, true);
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/rustdoc-ui/intra-doc/filter-out-private.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// This test ensures that ambiguities resolved at a later stage still emit an error.

#![deny(rustdoc::broken_intra_doc_links)]
#![crate_name = "foo"]

pub struct Thing {}

#[allow(non_snake_case)]
pub fn Thing() {}

/// Do stuff with [`Thing`].
//~^ ERROR `Thing` is both a function and a struct
pub fn repro(_: Thing) {}
22 changes: 22 additions & 0 deletions tests/rustdoc-ui/intra-doc/filter-out-private.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error: `Thing` is both a function and a struct
--> $DIR/filter-out-private.rs:11:21
|
LL | /// Do stuff with [`Thing`].
| ^^^^^ ambiguous link
|
note: the lint level is defined here
--> $DIR/filter-out-private.rs:3:9
|
LL | #![deny(rustdoc::broken_intra_doc_links)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: to link to the function, add parentheses
|
LL | /// Do stuff with [`Thing()`].
| ++
help: to link to the struct, prefix with `struct@`
|
LL | /// Do stuff with [`struct@Thing`].
| +++++++

error: aborting due to 1 previous error

0 comments on commit 07da6fe

Please sign in to comment.