From b5acf9501adc6926cb4496a1e981266f4c98ff25 Mon Sep 17 00:00:00 2001 From: CoCo-Japan-pan <115922543+CoCo-Japan-pan@users.noreply.github.com> Date: Tue, 2 Jun 2026 18:15:46 +0900 Subject: [PATCH 1/2] Render `impl` restriction --- src/librustdoc/html/render/print_item.rs | 25 +++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 49a8c077188dd..8385e691ed385 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -628,7 +628,11 @@ fn item_trait(cx: &Context<'_>, it: &clean::Item, t: &clean::Trait) -> impl fmt: let count_types = required_types.len() + provided_types.len(); let count_consts = required_consts.len() + provided_consts.len(); let count_methods = required_methods.len() + provided_methods.len(); - let must_implement_one_of_functions = &tcx.trait_def(t.def_id).must_implement_one_of; + let &rustc_middle::ty::TraitDef { + must_implement_one_of: ref must_implement_one_of_functions, + impl_restriction, + .. + } = tcx.trait_def(t.def_id); // Output the trait definition wrap_item(w, |mut w| { @@ -782,6 +786,25 @@ fn item_trait(cx: &Context<'_>, it: &clean::Item, t: &clean::Trait) -> impl fmt: // Trait documentation write!(w, "{}", document(cx, it, None, HeadingOffset::H2))?; + if let rustc_middle::ty::trait_def::ImplRestrictionKind::Restricted(def_id, _) = + impl_restriction + { + let v1; + let v2; + write!( + w, + "
This trait cannot be implemented outside {}.
", + if cx.cache().document_private { + v1 = + rustc_middle::ty::print::with_resolve_crate_name!(tcx.def_path_str(def_id)); + v1.as_str() + } else { + v2 = tcx.crate_name(def_id.krate); + v2.as_str() + }, + )?; + } + fn trait_item(cx: &Context<'_>, m: &clean::Item, t: &clean::Item) -> impl fmt::Display { fmt::from_fn(|w| { let name = m.name.unwrap(); From 5358c2fdbb2f8f1ecdb6bf4e375b692da680d161 Mon Sep 17 00:00:00 2001 From: CoCo-Japan-pan <115922543+CoCo-Japan-pan@users.noreply.github.com> Date: Tue, 2 Jun 2026 19:10:08 +0900 Subject: [PATCH 2/2] Add tests for `impl` restriction rendering --- .../impl/impl-restriction-document-private.rs | 15 +++++++++++++++ tests/rustdoc-html/impl/impl-restriction.rs | 14 ++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 tests/rustdoc-html/impl/impl-restriction-document-private.rs create mode 100644 tests/rustdoc-html/impl/impl-restriction.rs diff --git a/tests/rustdoc-html/impl/impl-restriction-document-private.rs b/tests/rustdoc-html/impl/impl-restriction-document-private.rs new file mode 100644 index 0000000000000..6ea55eff54cf6 --- /dev/null +++ b/tests/rustdoc-html/impl/impl-restriction-document-private.rs @@ -0,0 +1,15 @@ +//@ compile-flags: --document-private-items +#![crate_name = "c"] +#![feature(impl_restriction)] + +//@ matches c/trait.Foo.html '//*[@class="stab impl_restriction"]' \ +// 'This trait cannot be implemented outside c.$' +//@ has c/trait.Foo.html '//*[@class="stab impl_restriction"]//code' 'c' +pub impl(crate) trait Foo {} + +pub mod inner { + //@ matches c/inner/trait.Bar.html '//*[@class="stab impl_restriction"]' \ + // 'This trait cannot be implemented outside c::inner.$' + //@ has c/inner/trait.Bar.html '//*[@class="stab impl_restriction"]//code' 'c::inner' + pub impl(self) trait Bar {} +} diff --git a/tests/rustdoc-html/impl/impl-restriction.rs b/tests/rustdoc-html/impl/impl-restriction.rs new file mode 100644 index 0000000000000..11df67425f9b9 --- /dev/null +++ b/tests/rustdoc-html/impl/impl-restriction.rs @@ -0,0 +1,14 @@ +#![crate_name = "c"] +#![feature(impl_restriction)] + +//@ matches c/trait.Foo.html '//*[@class="stab impl_restriction"]' \ +// 'This trait cannot be implemented outside c.$' +//@ has c/trait.Foo.html '//*[@class="stab impl_restriction"]//code' 'c' +pub impl(crate) trait Foo {} + +pub mod inner { + //@ matches c/inner/trait.Bar.html '//*[@class="stab impl_restriction"]' \ + // 'This trait cannot be implemented outside c.$' + //@ has c/inner/trait.Bar.html '//*[@class="stab impl_restriction"]//code' 'c' + pub impl(self) trait Bar {} +}