Skip to content

Commit fdb1d6e

Browse files
committed
rustdoc: don't give depreciation notes special handling in future edition
1 parent 66428d9 commit fdb1d6e

5 files changed

Lines changed: 58 additions & 9 deletions

File tree

src/librustdoc/html/markdown.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ pub(crate) struct MarkdownWithToc<'a> {
111111
}
112112
/// A tuple struct like `Markdown` that renders the markdown escaping HTML tags
113113
/// and includes no paragraph tags.
114-
pub(crate) struct MarkdownItemInfo<'a>(pub(crate) &'a str, pub(crate) &'a mut IdMap);
114+
pub(crate) struct MarkdownItemInfo<'a>(
115+
pub(crate) &'a str,
116+
pub(crate) &'a mut IdMap,
117+
pub(crate) Edition,
118+
);
115119
/// A tuple struct like `Markdown` that renders only the first paragraph.
116120
pub(crate) struct MarkdownSummaryLine<'a>(pub &'a str, pub &'a [RenderedLink]);
117121

@@ -1461,7 +1465,8 @@ impl MarkdownWithToc<'_> {
14611465

14621466
impl MarkdownItemInfo<'_> {
14631467
pub(crate) fn write_into(self, mut f: impl fmt::Write) -> fmt::Result {
1464-
let MarkdownItemInfo(md, ids) = self;
1468+
let MarkdownItemInfo(md, ids, edition) = self;
1469+
let legacy_wrap = edition != Edition::EditionFuture;
14651470

14661471
// This is actually common enough to special-case
14671472
if md.is_empty() {
@@ -1479,10 +1484,23 @@ impl MarkdownItemInfo<'_> {
14791484
let p = HeadingLinks::new(p, None, ids, HeadingOffset::H1);
14801485
let p = footnotes::Footnotes::new(p, existing_footnotes);
14811486
let p = TableWrapper::new(p.map(|(ev, _)| ev));
1487+
// in legacy wrap mode, strip <p> elements to avoid them inserting newlines
14821488
let p = p.filter(|event| {
1483-
!matches!(event, Event::Start(Tag::Paragraph) | Event::End(TagEnd::Paragraph))
1489+
!legacy_wrap
1490+
|| !matches!(
1491+
event,
1492+
Event::Start(Tag::Paragraph) | Event::End(TagEnd::Paragraph)
1493+
)
14841494
});
1485-
html::write_html_fmt(&mut f, p)
1495+
if legacy_wrap {
1496+
f.write_str("<span class=\"legacy-wrap\">")?;
1497+
}
1498+
html::write_html_fmt(&mut f, p)?;
1499+
if legacy_wrap {
1500+
f.write_str("</span>")?;
1501+
}
1502+
1503+
Ok(())
14861504
})
14871505
}
14881506
}

src/librustdoc/html/markdown/tests.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,31 @@ fn test_plain_text_summary() {
467467
}
468468

469469
#[test]
470-
fn test_markdown_html_escape() {
470+
fn test_markdown_html_escape_legacy() {
471471
fn t(input: &str, expect: &str) {
472472
let mut idmap = IdMap::new();
473473
let mut output = String::new();
474-
MarkdownItemInfo(input, &mut idmap).write_into(&mut output).unwrap();
475-
assert_eq!(output, expect, "original: {}", input);
474+
MarkdownItemInfo(input, &mut idmap, Edition::Edition2015).write_into(&mut output).unwrap();
475+
assert_eq!(
476+
output,
477+
format!("<span class=\"legacy-wrap\">{}</span>", expect),
478+
"original: {}",
479+
input
480+
);
481+
}
482+
483+
t("`Struct<'a, T>`", "<code>Struct&lt;'a, T&gt;</code>");
484+
t("Struct<'a, T>", "Struct&lt;’a, T&gt;");
485+
t("Struct<br>", "Struct&lt;br&gt;");
486+
}
487+
488+
#[test]
489+
fn test_markdown_html_escape_new() {
490+
fn t(input: &str, expect: &str) {
491+
let mut idmap = IdMap::new();
492+
let mut output = String::new();
493+
MarkdownItemInfo(input, &mut idmap, Edition::Edition2015).write_into(&mut output).unwrap();
494+
assert_eq!(output, format!("<p>{}</p>", expect), "original: {}", input);
476495
}
477496

478497
t("`Struct<'a, T>`", "<code>Struct&lt;'a, T&gt;</code>");

src/librustdoc/html/render/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,7 @@ fn short_item_info(
855855
parent: Option<&clean::Item>,
856856
) -> Vec<ShortItemInfo> {
857857
let mut extra_info = vec![];
858+
let edition = item.span(cx.tcx()).expect("unable to get edition of span").inner().edition();
858859

859860
if let Some(depr @ Deprecation { note, since, suggestion: _ }) = item.deprecation(cx.tcx()) {
860861
// We display deprecation messages for #[deprecated], but only display
@@ -877,7 +878,7 @@ fn short_item_info(
877878
if let Some(note) = note {
878879
let note = note.as_str();
879880
let mut id_map = cx.id_map.borrow_mut();
880-
let html = MarkdownItemInfo(note, &mut id_map);
881+
let html = MarkdownItemInfo(note, &mut id_map, edition);
881882
message.push_str(": ");
882883
html.write_into(&mut message).unwrap();
883884
}

src/librustdoc/html/static/css/rustdoc.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1577,7 +1577,6 @@ so that we can apply CSS-filters to change the arrow color in themes */
15771577
color: var(--main-color);
15781578
background-color: var(--stab-background-color);
15791579
width: fit-content;
1580-
white-space: pre-wrap;
15811580
border-radius: 3px;
15821581
display: inline;
15831582
vertical-align: baseline;
@@ -1588,6 +1587,10 @@ so that we can apply CSS-filters to change the arrow color in themes */
15881587
color: var(--stab-code-color);
15891588
}
15901589

1590+
.stab span.legacy-wrap {
1591+
white-space: pre-wrap;
1592+
}
1593+
15911594
.stab .emoji, .item-info .stab::before {
15921595
font-size: 1.25rem;
15931596
}

tests/rustdoc/deprecated.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//@ compile-flags: -Zunstable-options
2+
//@ edition: future
3+
14
//@ has deprecated/index.html '//dt/span[@class="stab deprecated"]' 'Deprecated'
25
//@ has - '//dd' 'Deprecated docs'
36

@@ -30,3 +33,8 @@ pub struct W;
3033
// 'Deprecated: shorthand reason: code$'
3134
#[deprecated = "shorthand reason: `code`"]
3235
pub struct X;
36+
37+
//@ matches deprecated/struct.Y.html '//*[@class="stab deprecated"]//p[1]' 'multiple'
38+
//@ matches deprecated/struct.Y.html '//*[@class="stab deprecated"]//p[2]' 'paragraphs'
39+
#[deprecated = "multiple\n\nparagraphs"]
40+
pub struct Y;

0 commit comments

Comments
 (0)