Skip to content

Commit 6c53a0c

Browse files
authored
Rollup merge of #73476 - JakobDegen:should_panic_rustdoc, r=GuillaumeGomez
Added tooltip for should_panic code examples This change adds a tooltip to the documentation for `should_panic` examples. It currently displays identically to `compile_fail` examples, save for the changed text. It may be helpful to change the color that this displays in to make it visually more clear what is going on, but I'm unsure if additional colors wouldn't just be distracting. I brought this [up on internals](https://internals.rust-lang.org/t/indicating-that-an-example-is-should-panic-in-docs/12544) a few days ago, and there seemed to be a mild positive response to it.
2 parents f15b346 + 721facf commit 6c53a0c

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

src/librustdoc/html/markdown.rs

+8
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
192192
fn next(&mut self) -> Option<Self::Item> {
193193
let event = self.inner.next();
194194
let compile_fail;
195+
let should_panic;
195196
let ignore;
196197
let edition;
197198
if let Some(Event::Start(Tag::CodeBlock(kind))) = event {
@@ -205,6 +206,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
205206
return Some(Event::Start(Tag::CodeBlock(kind)));
206207
}
207208
compile_fail = parse_result.compile_fail;
209+
should_panic = parse_result.should_panic;
208210
ignore = parse_result.ignore;
209211
edition = parse_result.edition;
210212
} else {
@@ -280,6 +282,8 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
280282
Some(("This example is not tested".to_owned(), "ignore"))
281283
} else if compile_fail {
282284
Some(("This example deliberately fails to compile".to_owned(), "compile_fail"))
285+
} else if should_panic {
286+
Some(("This example panics".to_owned(), "should_panic"))
283287
} else if explicit_edition {
284288
Some((format!("This code runs with edition {}", edition), "edition"))
285289
} else {
@@ -295,6 +299,8 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
295299
" ignore"
296300
} else if compile_fail {
297301
" compile_fail"
302+
} else if should_panic {
303+
" should_panic"
298304
} else if explicit_edition {
299305
" edition "
300306
} else {
@@ -314,6 +320,8 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
314320
" ignore"
315321
} else if compile_fail {
316322
" compile_fail"
323+
} else if should_panic {
324+
" should_panic"
317325
} else if explicit_edition {
318326
" edition "
319327
} else {

src/librustdoc/html/static/rustdoc.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ h3 > .collapse-toggle, h4 > .collapse-toggle {
10891089
border-style: solid;
10901090
}
10911091

1092-
.tooltip.compile_fail, .tooltip.ignore {
1092+
.tooltip.compile_fail, .tooltip.should_panic, .tooltip.ignore {
10931093
font-weight: bold;
10941094
font-size: 20px;
10951095
}

src/librustdoc/html/static/themes/dark.css

+16
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,14 @@ pre.compile_fail:hover, .information:hover + pre.compile_fail {
283283
border-left: 2px solid #f00;
284284
}
285285

286+
pre.should_panic {
287+
border-left: 2px solid rgba(255,0,0,.8);
288+
}
289+
290+
pre.should_panic:hover, .information:hover + pre.should_panic {
291+
border-left: 2px solid #f00;
292+
}
293+
286294
pre.ignore {
287295
border-left: 2px solid rgba(255,142,0,.6);
288296
}
@@ -299,6 +307,14 @@ pre.ignore:hover, .information:hover + pre.ignore {
299307
color: #f00;
300308
}
301309

310+
.tooltip.should_panic {
311+
color: rgba(255,0,0,.8);
312+
}
313+
314+
.information > .should_panic:hover {
315+
color: #f00;
316+
}
317+
302318
.tooltip.ignore {
303319
color: rgba(255,142,0,.6);
304320
}

src/librustdoc/html/static/themes/light.css

+16
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,14 @@ pre.compile_fail:hover, .information:hover + pre.compile_fail {
278278
border-left: 2px solid #f00;
279279
}
280280

281+
pre.should_panic {
282+
border-left: 2px solid rgba(255,0,0,.5);
283+
}
284+
285+
pre.should_panic:hover, .information:hover + pre.should_panic {
286+
border-left: 2px solid #f00;
287+
}
288+
281289
pre.ignore {
282290
border-left: 2px solid rgba(255,142,0,.6);
283291
}
@@ -294,6 +302,14 @@ pre.ignore:hover, .information:hover + pre.ignore {
294302
color: #f00;
295303
}
296304

305+
.tooltip.should_panic {
306+
color: rgba(255,0,0,.5);
307+
}
308+
309+
.information > .should_panic:hover {
310+
color: #f00;
311+
}
312+
297313
.tooltip.ignore {
298314
color: rgba(255,142,0,.6);
299315
}

src/test/rustdoc/codeblock-title.rs

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
// @has foo/fn.bar.html '//*[@class="tooltip compile_fail"]/span' "This example deliberately fails to compile"
66
// @has foo/fn.bar.html '//*[@class="tooltip ignore"]/span' "This example is not tested"
7+
// @has foo/fn.bar.html '//*[@class="tooltip should_panic"]/span' "This example panics"
78

89
/// foo
910
///
@@ -15,6 +16,10 @@
1516
/// goo();
1617
/// ```
1718
///
19+
/// ```should_panic
20+
/// hoo();
21+
/// ```
22+
///
1823
/// ```
1924
/// let x = 0;
2025
/// ```

0 commit comments

Comments
 (0)