From c84c5e6439199d58ac62a144590c335b182c12d1 Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Fri, 10 Mar 2023 16:09:30 +0100 Subject: [PATCH] rustdoc: Don't crash on `crate` references in blocks This is a regression from #94857. --- compiler/rustc_resolve/src/lib.rs | 12 ++---------- tests/rustdoc-ui/crate-reference-in-block-module.rs | 5 +++++ .../crate-reference-in-block-module.stderr | 0 3 files changed, 7 insertions(+), 10 deletions(-) create mode 100644 tests/rustdoc-ui/crate-reference-in-block-module.rs create mode 100644 tests/rustdoc-ui/crate-reference-in-block-module.stderr diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 5eba208e3ed..f6888e55ad4 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1849,20 +1849,12 @@ fn resolve_rustdoc_path( &mut self, path_str: &str, ns: Namespace, - mut parent_scope: ParentScope<'a>, + parent_scope: ParentScope<'a>, ) -> Option { let mut segments = Vec::from_iter(path_str.split("::").map(Ident::from_str).map(Segment::from_ident)); 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 `parent_scope`'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; - parent_scope.module = - self.expect_module(parent_scope.module.def_id().krate.as_def_id()); - } else if segment.ident.name == kw::Empty { + if segment.ident.name == kw::Empty { segment.ident.name = kw::PathRoot; } } diff --git a/tests/rustdoc-ui/crate-reference-in-block-module.rs b/tests/rustdoc-ui/crate-reference-in-block-module.rs new file mode 100644 index 00000000000..aede030e072 --- /dev/null +++ b/tests/rustdoc-ui/crate-reference-in-block-module.rs @@ -0,0 +1,5 @@ +// check-pass +fn main() { + /// [](crate) + struct X; +} diff --git a/tests/rustdoc-ui/crate-reference-in-block-module.stderr b/tests/rustdoc-ui/crate-reference-in-block-module.stderr new file mode 100644 index 00000000000..e69de29bb2d