Skip to content

Commit d9baa36

Browse files
committed
Auto merge of rust-lang#91291 - GuillaumeGomez:const-deref-method, r=camelid
Fix const deref methods display Fixes rust-lang#90855 (more information in the issue). r? `@camelid`
2 parents a2b7b78 + 02782bb commit d9baa36

File tree

5 files changed

+122
-35
lines changed

5 files changed

+122
-35
lines changed

src/librustdoc/clean/types.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ impl Item {
376376
self.def_id.as_def_id().and_then(|did| tcx.lookup_stability(did))
377377
}
378378

379-
crate fn const_stability<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option<&'tcx ConstStability> {
380-
self.def_id.as_def_id().and_then(|did| tcx.lookup_const_stability(did))
379+
crate fn const_stability<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option<ConstStability> {
380+
self.def_id.as_def_id().and_then(|did| tcx.lookup_const_stability(did)).map(|cs| *cs)
381381
}
382382

383383
crate fn deprecation(&self, tcx: TyCtxt<'_>) -> Option<Deprecation> {
@@ -602,16 +602,16 @@ impl Item {
602602
})
603603
}
604604

605-
crate fn stable_since(&self, tcx: TyCtxt<'_>) -> Option<SymbolStr> {
605+
crate fn stable_since(&self, tcx: TyCtxt<'_>) -> Option<Symbol> {
606606
match self.stability(tcx)?.level {
607-
StabilityLevel::Stable { since, .. } => Some(since.as_str()),
607+
StabilityLevel::Stable { since, .. } => Some(since),
608608
StabilityLevel::Unstable { .. } => None,
609609
}
610610
}
611611

612-
crate fn const_stable_since(&self, tcx: TyCtxt<'_>) -> Option<SymbolStr> {
612+
crate fn const_stable_since(&self, tcx: TyCtxt<'_>) -> Option<Symbol> {
613613
match self.const_stability(tcx)?.level {
614-
StabilityLevel::Stable { since, .. } => Some(since.as_str()),
614+
StabilityLevel::Stable { since, .. } => Some(since),
615615
StabilityLevel::Unstable { .. } => None,
616616
}
617617
}

src/librustdoc/html/format.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1349,10 +1349,7 @@ impl PrintWithSpace for hir::Mutability {
13491349
}
13501350
}
13511351

1352-
crate fn print_constness_with_space(
1353-
c: &hir::Constness,
1354-
s: Option<&ConstStability>,
1355-
) -> &'static str {
1352+
crate fn print_constness_with_space(c: &hir::Constness, s: Option<ConstStability>) -> &'static str {
13561353
match (c, s) {
13571354
// const stable or when feature(staged_api) is not set
13581355
(

src/librustdoc/html/render/mod.rs

+33-16
Original file line numberDiff line numberDiff line change
@@ -804,17 +804,17 @@ fn assoc_type(
804804

805805
fn render_stability_since_raw(
806806
w: &mut Buffer,
807-
ver: Option<&str>,
808-
const_stability: Option<&ConstStability>,
809-
containing_ver: Option<&str>,
810-
containing_const_ver: Option<&str>,
807+
ver: Option<Symbol>,
808+
const_stability: Option<ConstStability>,
809+
containing_ver: Option<Symbol>,
810+
containing_const_ver: Option<Symbol>,
811811
) {
812812
let ver = ver.filter(|inner| !inner.is_empty());
813813

814814
match (ver, const_stability) {
815815
// stable and const stable
816816
(Some(v), Some(ConstStability { level: StabilityLevel::Stable { since }, .. }))
817-
if Some(since.as_str()).as_deref() != containing_const_ver =>
817+
if Some(since) != containing_const_ver =>
818818
{
819819
write!(
820820
w,
@@ -861,6 +861,7 @@ fn render_assoc_item(
861861
link: AssocItemLink<'_>,
862862
parent: ItemType,
863863
cx: &Context<'_>,
864+
render_mode: RenderMode,
864865
) {
865866
fn method(
866867
w: &mut Buffer,
@@ -871,6 +872,7 @@ fn render_assoc_item(
871872
link: AssocItemLink<'_>,
872873
parent: ItemType,
873874
cx: &Context<'_>,
875+
render_mode: RenderMode,
874876
) {
875877
let name = meth.name.as_ref().unwrap();
876878
let href = match link {
@@ -893,8 +895,14 @@ fn render_assoc_item(
893895
}
894896
};
895897
let vis = meth.visibility.print_with_space(meth.def_id, cx).to_string();
896-
let constness =
897-
print_constness_with_space(&header.constness, meth.const_stability(cx.tcx()));
898+
// FIXME: Once https://github.com/rust-lang/rust/issues/67792 is implemented, we can remove
899+
// this condition.
900+
let constness = match render_mode {
901+
RenderMode::Normal => {
902+
print_constness_with_space(&header.constness, meth.const_stability(cx.tcx()))
903+
}
904+
RenderMode::ForDeref { .. } => "",
905+
};
898906
let asyncness = header.asyncness.print_with_space();
899907
let unsafety = header.unsafety.print_with_space();
900908
let defaultness = print_default_space(meth.is_default());
@@ -945,10 +953,10 @@ fn render_assoc_item(
945953
match *item.kind {
946954
clean::StrippedItem(..) => {}
947955
clean::TyMethodItem(ref m) => {
948-
method(w, item, m.header, &m.generics, &m.decl, link, parent, cx)
956+
method(w, item, m.header, &m.generics, &m.decl, link, parent, cx, render_mode)
949957
}
950958
clean::MethodItem(ref m, _) => {
951-
method(w, item, m.header, &m.generics, &m.decl, link, parent, cx)
959+
method(w, item, m.header, &m.generics, &m.decl, link, parent, cx, render_mode)
952960
}
953961
clean::AssocConstItem(ref ty, ref default) => assoc_const(
954962
w,
@@ -1422,7 +1430,7 @@ fn render_impl(
14221430
"<div id=\"{}\" class=\"{}{} has-srclink\">",
14231431
id, item_type, in_trait_class,
14241432
);
1425-
render_rightside(w, cx, item, containing_item);
1433+
render_rightside(w, cx, item, containing_item, render_mode);
14261434
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
14271435
w.write_str("<h4 class=\"code-header\">");
14281436
render_assoc_item(
@@ -1431,6 +1439,7 @@ fn render_impl(
14311439
link.anchor(source_id.as_ref().unwrap_or(&id)),
14321440
ItemType::Impl,
14331441
cx,
1442+
render_mode,
14341443
);
14351444
w.write_str("</h4>");
14361445
w.write_str("</div>");
@@ -1466,7 +1475,7 @@ fn render_impl(
14661475
"<div id=\"{}\" class=\"{}{} has-srclink\">",
14671476
id, item_type, in_trait_class
14681477
);
1469-
render_rightside(w, cx, item, containing_item);
1478+
render_rightside(w, cx, item, containing_item, render_mode);
14701479
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
14711480
w.write_str("<h4 class=\"code-header\">");
14721481
assoc_const(
@@ -1645,16 +1654,24 @@ fn render_rightside(
16451654
cx: &Context<'_>,
16461655
item: &clean::Item,
16471656
containing_item: &clean::Item,
1657+
render_mode: RenderMode,
16481658
) {
16491659
let tcx = cx.tcx();
16501660

1661+
// FIXME: Once https://github.com/rust-lang/rust/issues/67792 is implemented, we can remove
1662+
// this condition.
1663+
let (const_stability, const_stable_since) = match render_mode {
1664+
RenderMode::Normal => (item.const_stability(tcx), containing_item.const_stable_since(tcx)),
1665+
RenderMode::ForDeref { .. } => (None, None),
1666+
};
1667+
16511668
write!(w, "<div class=\"rightside\">");
16521669
render_stability_since_raw(
16531670
w,
1654-
item.stable_since(tcx).as_deref(),
1655-
item.const_stability(tcx),
1656-
containing_item.stable_since(tcx).as_deref(),
1657-
containing_item.const_stable_since(tcx).as_deref(),
1671+
item.stable_since(tcx),
1672+
const_stability,
1673+
containing_item.stable_since(tcx),
1674+
const_stable_since,
16581675
);
16591676

16601677
write_srclink(cx, item, w);
@@ -1690,7 +1707,7 @@ pub(crate) fn render_impl_summary(
16901707
format!(" data-aliases=\"{}\"", aliases.join(","))
16911708
};
16921709
write!(w, "<div id=\"{}\" class=\"impl has-srclink\"{}>", id, aliases);
1693-
render_rightside(w, cx, &i.impl_item, containing_item);
1710+
render_rightside(w, cx, &i.impl_item, containing_item, RenderMode::Normal);
16941711
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
16951712
write!(w, "<h3 class=\"code-header in-band\">");
16961713

src/librustdoc/html/render/print_item.rs

+44-9
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub(super) fn print_item(
101101
let mut stability_since_raw = Buffer::new();
102102
render_stability_since_raw(
103103
&mut stability_since_raw,
104-
item.stable_since(cx.tcx()).as_deref(),
104+
item.stable_since(cx.tcx()),
105105
item.const_stability(cx.tcx()),
106106
None,
107107
None,
@@ -556,7 +556,14 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
556556
);
557557
}
558558
for t in &types {
559-
render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx);
559+
render_assoc_item(
560+
w,
561+
t,
562+
AssocItemLink::Anchor(None),
563+
ItemType::Trait,
564+
cx,
565+
RenderMode::Normal,
566+
);
560567
w.write_str(";\n");
561568
}
562569
// If there are too many associated constants, hide everything after them
@@ -580,7 +587,14 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
580587
w.write_str("\n");
581588
}
582589
for t in &consts {
583-
render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx);
590+
render_assoc_item(
591+
w,
592+
t,
593+
AssocItemLink::Anchor(None),
594+
ItemType::Trait,
595+
cx,
596+
RenderMode::Normal,
597+
);
584598
w.write_str(";\n");
585599
}
586600
if !toggle && should_hide_fields(count_methods) {
@@ -591,7 +605,14 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
591605
w.write_str("\n");
592606
}
593607
for (pos, m) in required.iter().enumerate() {
594-
render_assoc_item(w, m, AssocItemLink::Anchor(None), ItemType::Trait, cx);
608+
render_assoc_item(
609+
w,
610+
m,
611+
AssocItemLink::Anchor(None),
612+
ItemType::Trait,
613+
cx,
614+
RenderMode::Normal,
615+
);
595616
w.write_str(";\n");
596617

597618
if pos < required.len() - 1 {
@@ -602,7 +623,14 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
602623
w.write_str("\n");
603624
}
604625
for (pos, m) in provided.iter().enumerate() {
605-
render_assoc_item(w, m, AssocItemLink::Anchor(None), ItemType::Trait, cx);
626+
render_assoc_item(
627+
w,
628+
m,
629+
AssocItemLink::Anchor(None),
630+
ItemType::Trait,
631+
cx,
632+
RenderMode::Normal,
633+
);
606634
match *m.kind {
607635
clean::MethodItem(ref inner, _)
608636
if !inner.generics.where_predicates.is_empty() =>
@@ -655,7 +683,14 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
655683
write_srclink(cx, m, w);
656684
write!(w, "</div>");
657685
write!(w, "<h4 class=\"code-header\">");
658-
render_assoc_item(w, m, AssocItemLink::Anchor(Some(&id)), ItemType::Impl, cx);
686+
render_assoc_item(
687+
w,
688+
m,
689+
AssocItemLink::Anchor(Some(&id)),
690+
ItemType::Impl,
691+
cx,
692+
RenderMode::Normal,
693+
);
659694
w.write_str("</h4>");
660695
w.write_str("</div>");
661696
if toggled {
@@ -1427,10 +1462,10 @@ fn render_stability_since(
14271462
) {
14281463
render_stability_since_raw(
14291464
w,
1430-
item.stable_since(tcx).as_deref(),
1465+
item.stable_since(tcx),
14311466
item.const_stability(tcx),
1432-
containing_item.stable_since(tcx).as_deref(),
1433-
containing_item.const_stable_since(tcx).as_deref(),
1467+
containing_item.stable_since(tcx),
1468+
containing_item.const_stable_since(tcx),
14341469
)
14351470
}
14361471

src/test/rustdoc/deref-const-fn.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// This test ensures that the const methods from Deref aren't shown as const.
2+
// For more information, see https://github.com/rust-lang/rust/issues/90855.
3+
4+
#![crate_name = "foo"]
5+
6+
#![feature(staged_api)]
7+
8+
#![stable(feature = "rust1", since = "1.0.0")]
9+
10+
// @has 'foo/struct.Bar.html'
11+
#[stable(feature = "rust1", since = "1.0.0")]
12+
pub struct Bar;
13+
14+
impl Bar {
15+
// @has - '//*[@id="method.len"]' 'pub const fn len(&self) -> usize'
16+
// @has - '//*[@id="method.len"]//span[@class="since"]' '1.0.0 (const: 1.0.0)'
17+
#[stable(feature = "rust1", since = "1.0.0")]
18+
#[rustc_const_stable(feature = "rust1", since = "1.0.0")]
19+
pub const fn len(&self) -> usize { 0 }
20+
}
21+
22+
#[stable(feature = "rust1", since = "1.0.0")]
23+
pub struct Foo {
24+
value: Bar,
25+
}
26+
27+
// @has 'foo/struct.Foo.html'
28+
// @has - '//*[@id="method.len"]' 'pub fn len(&self) -> usize'
29+
// @!has - '//*[@id="method.len"]//span[@class="since"]' '1.0.0'
30+
// @!has - '//*[@id="method.len"]//span[@class="since"]' '(const: 1.0.0)'
31+
#[stable(feature = "rust1", since = "1.0.0")]
32+
impl std::ops::Deref for Foo {
33+
type Target = Bar;
34+
35+
fn deref(&self) -> &Self::Target {
36+
&self.value
37+
}
38+
}

0 commit comments

Comments
 (0)