Skip to content

[rustdoc] Detect and resolve ambiguities in fn parameters type names #122872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ use std::borrow::Cow;
use std::cell::Cell;
use std::fmt::{self, Display, Write};
use std::iter::{self, once};
use std::rc::Rc;

use rustc_ast as ast;
use rustc_attr::{ConstStability, StabilityLevel};
use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir as hir;
use rustc_hir::def::DefKind;
Expand Down Expand Up @@ -1418,6 +1420,53 @@ impl clean::FnDecl {
})
}

fn ambiguities<'a>(
types: impl Iterator<Item = &'a clean::Type>,
) -> FxHashMap<DefId, Rc<Cell<bool>>> {
fn inner(
ty: &clean::Type,
res_map: &mut FxHashMap<DefId, Rc<Cell<bool>>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to have Rc<Cell<bool>> instead of just a bool?

conflict_map: &mut FxHashMap<Symbol, Rc<Cell<bool>>>,
) {
match ty {
clean::Type::Path { path } => {
res_map.entry(path.def_id()).or_insert_with(|| {
let last = path.last();
conflict_map
.entry(last)
.and_modify(|b| {
b.replace(true);
})
.or_insert_with(|| Rc::new(Cell::new(false)))
.clone()
});
}
clean::Type::Tuple(types) => {
for ty in types {
inner(ty, res_map, conflict_map)
}
}
clean::Type::Slice(ty)
| clean::Type::Array(ty, _)
| clean::Type::RawPointer(_, ty)
| clean::Type::BorrowedRef { type_: ty, .. } => inner(ty, res_map, conflict_map),
clean::Type::QPath(_)
| clean::Type::Infer
| clean::Type::ImplTrait(_)
| clean::Type::BareFunction(_)
| clean::Type::Primitive(_)
| clean::Type::Generic(_)
| clean::Type::DynTrait(_, _) => (),
}
}
let mut res_map = FxHashMap::default();
let mut conflict_map = FxHashMap::default();
for ty in types {
inner(ty, &mut res_map, &mut conflict_map)
}
res_map
}

fn inner_full_print(
&self,
// For None, the declaration will not be line-wrapped. For Some(n),
Expand All @@ -1434,6 +1483,7 @@ impl clean::FnDecl {
{
write!(f, "\n{}", Indent(n + 4))?;
}
let ambiguities = Self::ambiguities(self.inputs.values.iter().map(|inpt| &inpt.type_));
for (i, input) in self.inputs.values.iter().enumerate() {
if i > 0 {
match line_wrapping_indent {
Expand Down Expand Up @@ -1471,7 +1521,14 @@ impl clean::FnDecl {
write!(f, "const ")?;
}
write!(f, "{}: ", input.name)?;
input.type_.print(cx).fmt(f)?;

let use_absolute = input
.type_
.def_id(cx.cache())
.and_then(|did| ambiguities.get(&did))
.map(|rcb| rcb.get())
.unwrap_or(false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.map(|rcb| rcb.get())
.unwrap_or(false);
.is_some_and(|rcb| rcb.get());

fmt_type(&input.type_, f, use_absolute, cx)?;
}
}

Expand Down
13 changes: 13 additions & 0 deletions tests/rustdoc/fn_param_ambiguities.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pub mod X {
pub enum A {}
pub enum B {}
pub enum C {}
}

pub mod Y {
pub enum A {}
pub enum B {}
}

// @has fn_param_ambiguities/fn.f.html //pre 'pub fn f(xa: fn_param_ambiguities::X::A, ya: fn_param_ambiguities::Y::A, yb: B, xc: C)'
pub fn f(xa: X::A, ya: Y::A, yb : Y::B, xc: X::C) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add struct and other types with impls and do the same for their associated items (like constants, methods and types).

Loading