about summary refs log tree commit diff
path: root/compiler/rustc_resolve
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-04-05 04:39:34 +0000
committerbors <bors@rust-lang.org>2022-04-05 04:39:34 +0000
commit949b98cab8a186b98bf87e64374b8d0848c55271 (patch)
tree53bf1e67c7f27b559b81410fa79086525a0426a0 /compiler/rustc_resolve
parenta22cf2af0510b3ec4cbb19c3de11d3d8291349d9 (diff)
parentf5ee822098580f4e99bc0c710427727fe9df802c (diff)
downloadrust-949b98cab8a186b98bf87e64374b8d0848c55271.tar.gz
rust-949b98cab8a186b98bf87e64374b8d0848c55271.zip
Auto merge of #95337 - petrochenkov:doclink3, r=camelid
rustdoc: Fix resolution of `crate`-relative paths in doc links

Resolve `crate::foo` paths transparently to rustdoc, so their resolution no longer affects diagnostics and modules used for determining traits in scope.

The proper solution is to account for the current `module_id`/`parent_scope` in `fn resolve_crate_root`, but it's a slightly larger compiler changes. This PR moves the code closer to it, but keeps it rustdoc-specific.

Fixes https://github.com/rust-lang/rust/issues/78696
Fixes https://github.com/rust-lang/rust/issues/94924
Diffstat (limited to 'compiler/rustc_resolve')
-rw-r--r--compiler/rustc_resolve/src/lib.rs15
1 files changed, 12 insertions, 3 deletions
diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs
index 1f0a6e5ce97..3e677277826 100644
--- a/compiler/rustc_resolve/src/lib.rs
+++ b/compiler/rustc_resolve/src/lib.rs
@@ -3270,12 +3270,21 @@ impl<'a> Resolver<'a> {
         &mut self,
         path_str: &str,
         ns: Namespace,
-        module_id: DefId,
+        mut module_id: DefId,
     ) -> Option<Res> {
         let mut segments =
             Vec::from_iter(path_str.split("::").map(Ident::from_str).map(Segment::from_ident));
-        if path_str.starts_with("::") {
-            segments[0].ident.name = kw::PathRoot;
+        if let Some(segment) = segments.first_mut() {
+            if segment.ident.name == kw::Crate {
+                // FIXME: `resolve_path` always resolves `crate` to the current crate root, but
+                // rustdoc wants it to resolve to the `module_id`'s crate root. This trick of
+                // replacing `crate` with `self` and changing the current module should achieve
+                // the same effect.
+                segment.ident.name = kw::SelfLower;
+                module_id = module_id.krate.as_def_id();
+            } else if segment.ident.name == kw::Empty {
+                segment.ident.name = kw::PathRoot;
+            }
         }
 
         let module = self.expect_module(module_id);