Skip to content

Commit 17f493f

Browse files
committed
Auto merge of #42394 - ollie27:rustdoc_deref_box, r=QuietMisdreavus
rustdoc: Hide `self: Box<Self>` in list of deref methods These methods can never be called through deref so there is no point including them. For example you can't call [`into_boxed_bytes`](https://doc.rust-lang.org/nightly/std/string/struct.String.html#method.into_boxed_bytes) or [`into_string`](https://doc.rust-lang.org/nightly/std/string/struct.String.html#method.into_string) on `String`.
2 parents e862695 + 88c791b commit 17f493f

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

src/librustdoc/clean/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ impl<'a, 'tcx> Clean<Crate> for visit_ast::RustdocVisitor<'a, 'tcx> {
124124
let mut r = cx.renderinfo.borrow_mut();
125125
r.deref_trait_did = cx.tcx.lang_items.deref_trait();
126126
r.deref_mut_trait_did = cx.tcx.lang_items.deref_mut_trait();
127+
r.owned_box_did = cx.tcx.lang_items.owned_box();
127128
}
128129

129130
let mut externs = Vec::new();

src/librustdoc/html/render.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ pub struct Cache {
262262
stripped_mod: bool,
263263
deref_trait_did: Option<DefId>,
264264
deref_mut_trait_did: Option<DefId>,
265+
owned_box_did: Option<DefId>,
265266

266267
// In rare case where a structure is defined in one module but implemented
267268
// in another, if the implementing module is parsed before defining module,
@@ -280,6 +281,7 @@ pub struct RenderInfo {
280281
pub external_typarams: FxHashMap<DefId, String>,
281282
pub deref_trait_did: Option<DefId>,
282283
pub deref_mut_trait_did: Option<DefId>,
284+
pub owned_box_did: Option<DefId>,
283285
}
284286

285287
/// Helper struct to render all source code to HTML pages
@@ -507,6 +509,7 @@ pub fn run(mut krate: clean::Crate,
507509
external_typarams,
508510
deref_trait_did,
509511
deref_mut_trait_did,
512+
owned_box_did,
510513
} = renderinfo;
511514

512515
let external_paths = external_paths.into_iter()
@@ -530,6 +533,7 @@ pub fn run(mut krate: clean::Crate,
530533
traits: mem::replace(&mut krate.external_traits, FxHashMap()),
531534
deref_trait_did: deref_trait_did,
532535
deref_mut_trait_did: deref_mut_trait_did,
536+
owned_box_did: owned_box_did,
533537
typarams: external_typarams,
534538
};
535539

@@ -2933,17 +2937,18 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
29332937
};
29342938

29352939
if let Some(self_ty) = self_type_opt {
2936-
let by_mut_ref = match self_ty {
2937-
SelfTy::SelfBorrowed(_lifetime, mutability) => {
2938-
mutability == Mutability::Mutable
2939-
},
2940+
let (by_mut_ref, by_box) = match self_ty {
2941+
SelfTy::SelfBorrowed(_, mutability) |
29402942
SelfTy::SelfExplicit(clean::BorrowedRef { mutability, .. }) => {
2941-
mutability == Mutability::Mutable
2943+
(mutability == Mutability::Mutable, false)
2944+
},
2945+
SelfTy::SelfExplicit(clean::ResolvedPath { did, .. }) => {
2946+
(false, Some(did) == cache().owned_box_did)
29422947
},
2943-
_ => false,
2948+
_ => (false, false),
29442949
};
29452950

2946-
deref_mut_ || !by_mut_ref
2951+
(deref_mut_ || !by_mut_ref) && !by_box
29472952
} else {
29482953
false
29492954
}

src/test/rustdoc/issue-35169-2.rs

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ impl Foo {
1919
pub fn by_explicit_ref(self: &Foo) {}
2020
pub fn by_mut_ref(&mut self) {}
2121
pub fn by_explicit_mut_ref(self: &mut Foo) {}
22+
pub fn by_explicit_box(self: Box<Foo>) {}
23+
pub fn by_explicit_self_box(self: Box<Self>) {}
2224
pub fn static_foo() {}
2325
}
2426

@@ -41,5 +43,9 @@ impl DerefMut for Bar {
4143
// @has - '//*[@id="method.by_mut_ref"]' 'fn by_mut_ref(&mut self)'
4244
// @has - '//*[@id="by_explicit_mut_ref.v"]' 'fn by_explicit_mut_ref(self: &mut Foo)'
4345
// @has - '//*[@id="method.by_explicit_mut_ref"]' 'fn by_explicit_mut_ref(self: &mut Foo)'
46+
// @!has - '//*[@id="by_explicit_box.v"]' 'fn by_explicit_box(self: Box<Foo>)'
47+
// @!has - '//*[@id="method.by_explicit_box"]' 'fn by_explicit_box(self: Box<Foo>)'
48+
// @!has - '//*[@id="by_explicit_self_box.v"]' 'fn by_explicit_self_box(self: Box<Self>)'
49+
// @!has - '//*[@id="method.by_explicit_self_box"]' 'fn by_explicit_self_box(self: Box<Self>)'
4450
// @!has - '//*[@id="static_foo.v"]' 'fn static_foo()'
4551
// @!has - '//*[@id="method.static_foo"]' 'fn static_foo()'

src/test/rustdoc/issue-35169.rs

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ impl Foo {
1818
pub fn by_explicit_ref(self: &Foo) {}
1919
pub fn by_mut_ref(&mut self) {}
2020
pub fn by_explicit_mut_ref(self: &mut Foo) {}
21+
pub fn by_explicit_box(self: Box<Foo>) {}
22+
pub fn by_explicit_self_box(self: Box<Self>) {}
2123
pub fn static_foo() {}
2224
}
2325

@@ -36,5 +38,9 @@ impl Deref for Bar {
3638
// @!has - '//*[@id="method.by_mut_ref"]' 'fn by_mut_ref(&mut self)'
3739
// @!has - '//*[@id="by_explicit_mut_ref.v"]' 'fn by_explicit_mut_ref(self: &mut Foo)'
3840
// @!has - '//*[@id="method.by_explicit_mut_ref"]' 'fn by_explicit_mut_ref(self: &mut Foo)'
41+
// @!has - '//*[@id="by_explicit_box.v"]' 'fn by_explicit_box(self: Box<Foo>)'
42+
// @!has - '//*[@id="method.by_explicit_box"]' 'fn by_explicit_box(self: Box<Foo>)'
43+
// @!has - '//*[@id="by_explicit_self_box.v"]' 'fn by_explicit_self_box(self: Box<Self>)'
44+
// @!has - '//*[@id="method.by_explicit_self_box"]' 'fn by_explicit_self_box(self: Box<Self>)'
3945
// @!has - '//*[@id="static_foo.v"]' 'fn static_foo()'
4046
// @!has - '//*[@id="method.static_foo"]' 'fn static_foo()'

0 commit comments

Comments
 (0)