Skip to content

Commit 4705df4

Browse files
bors[bot]Veykril
andauthored
Merge #9637
9637: Overhaul doc_links testing infra r=Veykril a=Veykril and fix several issues with current implementation. Fixes #9617 Co-authored-by: Lukas Wirth <[email protected]>
2 parents ea71e57 + 2e39d47 commit 4705df4

File tree

9 files changed

+600
-745
lines changed

9 files changed

+600
-745
lines changed

crates/hir/src/attrs.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,15 @@ fn resolve_doc_path(
114114
let path = ast::Path::parse(link).ok()?;
115115
let modpath = ModPath::from_src(db.upcast(), path, &Hygiene::new_unhygienic()).unwrap();
116116
let resolved = resolver.resolve_module_path_in_items(db.upcast(), &modpath);
117-
if resolved == PerNs::none() {
118-
if let Some(trait_id) = resolver.resolve_module_path_in_trait_items(db.upcast(), &modpath) {
119-
return Some(ModuleDefId::TraitId(trait_id));
120-
};
121-
}
122-
let def = match ns {
123-
Some(Namespace::Types) => resolved.take_types()?,
124-
Some(Namespace::Values) => resolved.take_values()?,
125-
Some(Namespace::Macros) => return None,
126-
None => resolved.iter_items().find_map(|it| it.as_module_def_id())?,
117+
let resolved = if resolved == PerNs::none() {
118+
resolver.resolve_module_path_in_trait_assoc_items(db.upcast(), &modpath)?
119+
} else {
120+
resolved
127121
};
128-
Some(def)
122+
match ns {
123+
Some(Namespace::Types) => resolved.take_types(),
124+
Some(Namespace::Values) => resolved.take_values(),
125+
Some(Namespace::Macros) => None,
126+
None => resolved.iter_items().find_map(|it| it.as_module_def_id()),
127+
}
129128
}

crates/hir/src/lib.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,19 @@ impl ModuleDef {
295295
}
296296

297297
pub fn canonical_path(&self, db: &dyn HirDatabase) -> Option<String> {
298-
let mut segments = vec![self.name(db)?.to_string()];
298+
let mut segments = vec![self.name(db)?];
299299
for m in self.module(db)?.path_to_root(db) {
300-
segments.extend(m.name(db).map(|it| it.to_string()))
300+
segments.extend(m.name(db))
301301
}
302302
segments.reverse();
303-
Some(segments.join("::"))
303+
Some(segments.into_iter().join("::"))
304+
}
305+
306+
pub fn canonical_module_path(
307+
&self,
308+
db: &dyn HirDatabase,
309+
) -> Option<impl Iterator<Item = Module>> {
310+
self.module(db).map(|it| it.path_to_root(db).into_iter().rev())
304311
}
305312

306313
pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {

crates/hir_def/src/resolver.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ use crate::{
2020
path::{ModPath, PathKind},
2121
per_ns::PerNs,
2222
visibility::{RawVisibility, Visibility},
23-
AdtId, AssocContainerId, ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId,
24-
FunctionId, GenericDefId, GenericParamId, HasModule, ImplId, LifetimeParamId, LocalModuleId,
25-
Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId,
26-
VariantId,
23+
AdtId, AssocContainerId, AssocItemId, ConstId, ConstParamId, DefWithBodyId, EnumId,
24+
EnumVariantId, FunctionId, GenericDefId, GenericParamId, HasModule, ImplId, LifetimeParamId,
25+
LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
26+
TypeParamId, VariantId,
2727
};
2828

2929
#[derive(Debug, Clone, Default)]
@@ -144,15 +144,28 @@ impl Resolver {
144144
self.resolve_module_path(db, path, BuiltinShadowMode::Module)
145145
}
146146

147-
pub fn resolve_module_path_in_trait_items(
147+
pub fn resolve_module_path_in_trait_assoc_items(
148148
&self,
149149
db: &dyn DefDatabase,
150150
path: &ModPath,
151-
) -> Option<TraitId> {
151+
) -> Option<PerNs> {
152152
let (item_map, module) = self.module_scope()?;
153-
let (module_res, ..) = item_map.resolve_path(db, module, path, BuiltinShadowMode::Module);
153+
let (module_res, idx) = item_map.resolve_path(db, module, path, BuiltinShadowMode::Module);
154154
match module_res.take_types()? {
155-
ModuleDefId::TraitId(it) => Some(it),
155+
ModuleDefId::TraitId(it) => {
156+
let idx = idx?;
157+
let unresolved = &path.segments()[idx..];
158+
let assoc = match unresolved {
159+
[it] => it,
160+
_ => return None,
161+
};
162+
let &(_, assoc) = db.trait_data(it).items.iter().find(|(n, _)| n == assoc)?;
163+
Some(match assoc {
164+
AssocItemId::FunctionId(it) => PerNs::values(it.into(), Visibility::Public),
165+
AssocItemId::ConstId(it) => PerNs::values(it.into(), Visibility::Public),
166+
AssocItemId::TypeAliasId(it) => PerNs::types(it.into(), Visibility::Public),
167+
})
168+
}
156169
_ => None,
157170
}
158171
}

0 commit comments

Comments
 (0)