Skip to content

Commit 848a766

Browse files
committed
Use the scope of the imported variable for resolution, not the current scope
- Accept DefId in resolve_str_path_error This will probably break lots of internal invariants.
1 parent 5271e98 commit 848a766

File tree

3 files changed

+95
-7
lines changed

3 files changed

+95
-7
lines changed

src/librustc_resolve/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2978,7 +2978,7 @@ impl<'a> Resolver<'a> {
29782978
span: Span,
29792979
path_str: &str,
29802980
ns: Namespace,
2981-
module_id: LocalDefId,
2981+
module_id: DefId,
29822982
) -> Result<(ast::Path, Res), ()> {
29832983
let path = if path_str.starts_with("::") {
29842984
ast::Path {
@@ -2998,7 +2998,7 @@ impl<'a> Resolver<'a> {
29982998
.collect(),
29992999
}
30003000
};
3001-
let module = self.get_module(module_id.to_def_id());
3001+
let module = self.get_module(module_id);
30023002
let parent_scope = &ParentScope::module(module);
30033003
let res = self.resolve_ast_path(&path, ns, parent_scope).map_err(|_| ())?;
30043004
Ok((path, res))

src/librustdoc/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
430430
DUMMY_SP,
431431
extern_name,
432432
TypeNS,
433-
LocalDefId { local_def_index: CRATE_DEF_INDEX },
433+
LocalDefId { local_def_index: CRATE_DEF_INDEX }.to_def_id(),
434434
)
435435
.unwrap_or_else(|()| {
436436
panic!("Unable to resolve external crate {}", extern_name)

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
6262
&self,
6363
path_str: &str,
6464
current_item: &Option<String>,
65-
module_id: LocalDefId,
65+
module_id: DefId,
6666
) -> Result<(Res, Option<String>), ErrorKind> {
6767
let cx = self.cx;
6868

@@ -167,15 +167,103 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
167167
disambiguator: Option<&str>,
168168
ns: Namespace,
169169
current_item: &Option<String>,
170-
parent_id: Option<hir::HirId>,
170+
mut parent_id: Option<hir::HirId>,
171171
extra_fragment: &Option<String>,
172172
item_opt: Option<&Item>,
173173
) -> Result<(Res, Option<String>), ErrorKind> {
174+
use rustc_hir::{ItemKind, UseKind};
175+
174176
let cx = self.cx;
175177

178+
// In case this is a re-export, try to resolve the docs relative to the original module.
179+
// Since we don't document `use` statements,
180+
// we don't have to consider the case where an item is documented in both the original module and the current module.
181+
let mut module_id = None;
182+
if let Some(item) = item_opt {
183+
if let ItemEnum::ImportItem(import) = &item.inner {
184+
if let Import::Simple(_, source) = import {
185+
if let Some(def_id) = source.did {
186+
use crate::rustc_middle::ty::DefIdTree;
187+
188+
//let mut current_id = def_id;
189+
if cx.tcx.def_kind(def_id) == DefKind::Mod {
190+
module_id = Some(def_id);
191+
debug!("found parent module {:?} for use statement", def_id);
192+
//break;
193+
} else {
194+
debug!(
195+
"not a module: {:?} (maybe an associated item?)",
196+
cx.tcx.def_kind(def_id)
197+
);
198+
}
199+
200+
/*
201+
// For associated items, the parent module might be multiple nodes above
202+
while let Some(parent) = cx.tcx.parent(current_id) {
203+
if cx.tcx.def_kind(parent) == DefKind::Mod {
204+
parent_id = Some(parent);
205+
debug!("found parent module {:?} for use statement", parent);
206+
break;
207+
}
208+
current_id = parent;
209+
}
210+
*/
211+
} else {
212+
debug!("no def id found");
213+
}
214+
} else {
215+
debug!("glob imports not handled for intra-doc links");
216+
}
217+
}
218+
/*
219+
if let Some(reexport) = item.reexport {
220+
use crate::rustc_middle::ty::DefIdTree;
221+
222+
let mut current_id = reexport;
223+
// For associated items, the parent module might be multiple nodes above
224+
while let Some(parent) = cx.tcx.parent(current_id) {
225+
if cx.tcx.def_kind(parent) == DefKind::Mod {
226+
parent_id = Some(parent);
227+
debug!("found parent module {:?} for use statement", parent);
228+
break;
229+
}
230+
current_id = parent;
231+
}
232+
}
233+
*/
234+
/*
235+
if let ItemKind::Use(path, use_kind) = item.kind {
236+
if use_kind == UseKind::Single {
237+
match path.res {
238+
Res::Def(def_kind, def_id) => {
239+
use crate::rustc_middle::ty::DefIdTree;
240+
241+
let mut current_id = def_id;
242+
// For associated items, the parent module might be multiple nodes above
243+
while let Some(parent) = cx.tcx.parent(current_id) {
244+
if cx.tcx.def_kind(parent) == DefKind::Mod {
245+
parent_id = Some(parent);
246+
debug!("found parent module {:?} for use statement", parent);
247+
break;
248+
}
249+
current_id = parent;
250+
}
251+
}
252+
_ => debug!("use {:?} was not a definition, not treating as cross-crate", item.name),
253+
}
254+
} else {
255+
debug!("don't know how to resolve multiple imports for {:?}, not treating as cross-crate", path);
256+
}
257+
}
258+
*/
259+
}
260+
176261
// In case we're in a module, try to resolve the relative path.
177-
if let Some(module_id) = parent_id.or(self.mod_ids.last().cloned()) {
178-
let module_id = cx.tcx.hir().local_def_id(module_id);
262+
if module_id.is_none() {
263+
let id = parent_id.or(self.mod_ids.last().cloned());
264+
module_id = id.map(|id| cx.tcx.hir().local_def_id(id).to_def_id());
265+
}
266+
if let Some(module_id) = module_id {
179267
let result = cx.enter_resolver(|resolver| {
180268
resolver.resolve_str_path_error(DUMMY_SP, &path_str, ns, module_id)
181269
});

0 commit comments

Comments
 (0)