Skip to content

Rollup of 4 pull requests #66499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
75ab3a8
Stabilize rustdoc theme options
GuillaumeGomez Oct 1, 2018
272799f
Add documentation for stabilized flags
GuillaumeGomez May 23, 2019
ca64df5
Warn instead of failing for themes
GuillaumeGomez May 23, 2019
10e6ca6
Apply review comments
GuillaumeGomez May 24, 2019
8df4ce2
add test for rustdoc's --themes flag
QuietMisdreavus May 24, 2019
2aa57e9
Prevent invalid html characters in themes name
GuillaumeGomez Jun 30, 2019
5b2862c
Add sentence to tell other options are ignored when running check-theme
GuillaumeGomez Jun 30, 2019
2f535a4
Rename theme-checker option to check-theme
GuillaumeGomez Jun 30, 2019
29494a8
Fix typos
GuillaumeGomez Jul 26, 2019
b65a369
Improve documentation, add checks for themes option arguments, make s…
GuillaumeGomez Aug 21, 2019
c45a553
rename check-theme option into check-themes
GuillaumeGomez Sep 14, 2019
747dcaf
remove unstable docs
GuillaumeGomez Sep 14, 2019
94b21eb
Rename rustdoc options --themes and --check-themes to --theme and --c…
GuillaumeGomez Sep 22, 2019
c614cad
Apply review comments
GuillaumeGomez Oct 10, 2019
82a7b32
remove -Z option from rustdoc theme checker tool
GuillaumeGomez Oct 12, 2019
1ebb9ec
improve error messages and documentation
GuillaumeGomez Oct 17, 2019
1907589
rustdoc: Stabilize `edition` annotation.
ehuss Nov 9, 2019
71cf364
Fix the source code highlighting on source comments
dns2utf8 Nov 15, 2019
1bbb816
Prevent jumps when selecting one or many lines
dns2utf8 Nov 15, 2019
4e1eeb9
Add explanation message for E0641
clemkoh Nov 16, 2019
dc137dd
Update ui tests
clemkoh Nov 16, 2019
e0d2d9d
Rollup merge of #54733 - GuillaumeGomez:stabilize-rustdoc-theme, r=Dy…
GuillaumeGomez Nov 17, 2019
18668f9
Rollup merge of #66238 - ehuss:stabilize-rustdoc-edition, r=Guillaume…
GuillaumeGomez Nov 17, 2019
62ebeb8
Rollup merge of #66430 - dns2utf8:fix_code_selection_click_handler, r…
GuillaumeGomez Nov 17, 2019
e4b2089
Rollup merge of #66461 - clemencetbk:master, r=GuillaumeGomez
GuillaumeGomez Nov 17, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/doc/rustdoc/src/command-line-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,38 @@ $ rustdoc src/lib.rs --edition 2018
$ rustdoc --test src/lib.rs --edition 2018
```

This flag allows rustdoc to treat your rust code as the given edition. It will compile doctests with
This flag allows `rustdoc` to treat your rust code as the given edition. It will compile doctests with
the given edition as well. As with `rustc`, the default edition that `rustdoc` will use is `2015`
(the first edition).

## `--theme`: add a theme to the documentation output

Using this flag looks like this:

```bash
$ rustdoc src/lib.rs --theme /path/to/your/custom-theme.css
```

`rustdoc`'s default output includes two themes: `light` (the default) and
`dark`. This flag allows you to add custom themes to the output. Giving a CSS
file to this flag adds it to your documentation as an additional theme choice.
The theme's name is determined by its filename; a theme file named
`custom-theme.css` will add a theme named `custom-theme` to the documentation.

## `--check-theme`: verify custom themes against the default theme

Using this flag looks like this:

```bash
$ rustdoc --check-theme /path/to/your/custom-theme.css
```

While `rustdoc`'s HTML output is more-or-less consistent between versions, there
is no guarantee that a theme file will have the same effect. The `--theme` flag
will still allow you to add the theme to your documentation, but to ensure that
your theme works as expected, you can use this flag to verify that it implements
the same CSS rules as the official `light` theme.

`--check-theme` is a separate mode in `rustdoc`. When `rustdoc` sees the
`--check-theme` flag, it discards all other flags and only performs the CSS rule
comparison operation.
24 changes: 0 additions & 24 deletions src/doc/rustdoc/src/unstable-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,30 +294,6 @@ some consideration for their stability, and names that end in a number). Giving
`rustdoc` will disable this sorting and instead make it print the items in the order they appear in
the source.

### `--themes`: provide additional themes

Using this flag looks like this:

```bash
$ rustdoc src/lib.rs -Z unstable-options --themes theme.css
```

Giving this flag to `rustdoc` will make it copy your theme into the generated crate docs and enable
it in the theme selector. Note that `rustdoc` will reject your theme file if it doesn't style
everything the "light" theme does. See `--theme-checker` below for details.

### `--theme-checker`: verify theme CSS for validity

Using this flag looks like this:

```bash
$ rustdoc -Z unstable-options --theme-checker theme.css
```

Before including your theme in crate docs, `rustdoc` will compare all the CSS rules it contains
against the "light" theme included by default. Using this flag will allow you to see which rules are
missing if `rustdoc` rejects your theme.

### `--resource-suffix`: modifying the name of CSS/JavaScript in crate docs

Using this flag looks like this:
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_error_codes/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ E0635: include_str!("./error_codes/E0635.md"),
E0636: include_str!("./error_codes/E0636.md"),
E0638: include_str!("./error_codes/E0638.md"),
E0639: include_str!("./error_codes/E0639.md"),
E0641: include_str!("./error_codes/E0641.md"),
E0642: include_str!("./error_codes/E0642.md"),
E0643: include_str!("./error_codes/E0643.md"),
E0644: include_str!("./error_codes/E0644.md"),
Expand Down Expand Up @@ -584,7 +585,6 @@ E0744: include_str!("./error_codes/E0744.md"),
E0634, // type has conflicting packed representaton hints
E0637, // "'_" is not a valid lifetime bound
E0640, // infer outlives requirements
E0641, // cannot cast to/from a pointer with an unknown kind
// E0645, // trait aliases not finished
E0657, // `impl Trait` can only capture lifetimes bound at the fn level
E0667, // `impl Trait` in projections
Expand Down
19 changes: 19 additions & 0 deletions src/librustc_error_codes/error_codes/E0641.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Attempted to cast to/from a pointer with an unknown kind.

Erroneous code examples:

```compile_fail,E0641
let b = 0 as *const _; // error
```

Must give information for type of pointer that is being cast from/to if the
type cannot be inferred.

```
// Creating a pointer from reference: type can be inferred
let a = &(String::from("Hello world!")) as *const _; // Ok

let b = 0 as *const i32; // Ok

let c: *const i32 = 0 as *const _; // Ok
```
31 changes: 22 additions & 9 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::BTreeMap;
use std::ffi::OsStr;
use std::fmt;
use std::path::PathBuf;

Expand Down Expand Up @@ -281,12 +282,12 @@ impl Options {
// check for deprecated options
check_deprecated_options(&matches, &diag);

let to_check = matches.opt_strs("theme-checker");
let to_check = matches.opt_strs("check-theme");
if !to_check.is_empty() {
let paths = theme::load_css_paths(static_files::themes::LIGHT.as_bytes());
let mut errors = 0;

println!("rustdoc: [theme-checker] Starting tests!");
println!("rustdoc: [check-theme] Starting tests! (Ignoring all other arguments)");
for theme_file in to_check.iter() {
print!(" - Checking \"{}\"...", theme_file);
let (success, differences) = theme::test_theme_against(theme_file, &paths, &diag);
Expand Down Expand Up @@ -357,23 +358,35 @@ impl Options {
}

let mut themes = Vec::new();
if matches.opt_present("themes") {
if matches.opt_present("theme") {
let paths = theme::load_css_paths(static_files::themes::LIGHT.as_bytes());

for (theme_file, theme_s) in matches.opt_strs("themes")
for (theme_file, theme_s) in matches.opt_strs("theme")
.iter()
.map(|s| (PathBuf::from(&s), s.to_owned())) {
if !theme_file.is_file() {
diag.struct_err("option --themes arguments must all be files").emit();
diag.struct_err(&format!("invalid argument: \"{}\"", theme_s))
.help("arguments to --theme must be files")
.emit();
return Err(1);
}
let (success, ret) = theme::test_theme_against(&theme_file, &paths, &diag);
if !success || !ret.is_empty() {
diag.struct_err(&format!("invalid theme: \"{}\"", theme_s))
.help("check what's wrong with the --theme-checker option")
if theme_file.extension() != Some(OsStr::new("css")) {
diag.struct_err(&format!("invalid argument: \"{}\"", theme_s))
.emit();
return Err(1);
}
let (success, ret) = theme::test_theme_against(&theme_file, &paths, &diag);
if !success {
diag.struct_err(&format!("error loading theme file: \"{}\"", theme_s)).emit();
return Err(1);
} else if !ret.is_empty() {
diag.struct_warn(&format!("theme file \"{}\" is missing CSS rules from the \
default theme", theme_s))
.warn("the theme may appear incorrect when loaded")
.help(&format!("to see what rules are missing, call `rustdoc \
--check-theme \"{}\"`", theme_s))
.emit();
}
themes.push(theme_file);
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/librustdoc/html/layout.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::PathBuf;

use crate::externalfiles::ExternalHtml;
use crate::html::escape::Escape;
use crate::html::render::ensure_trailing_slash;
use crate::html::format::{Buffer, Print};

Expand Down Expand Up @@ -166,10 +167,11 @@ pub fn render<T: Print, S: Print>(
themes = themes.iter()
.filter_map(|t| t.file_stem())
.filter_map(|t| t.to_str())
.map(|t| format!(r#"<link rel="stylesheet" type="text/css" href="{}{}{}.css">"#,
static_root_path,
t,
page.resource_suffix))
.map(|t| format!(r#"<link rel="stylesheet" type="text/css" href="{}.css">"#,
Escape(&format!("{}{}{}",
static_root_path,
t,
page.resource_suffix))))
.collect::<String>(),
suffix=page.resource_suffix,
static_extra_scripts=page.static_extra_scripts.iter().map(|e| {
Expand Down
4 changes: 1 addition & 3 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,9 +680,7 @@ impl LangString {
seen_rust_tags = !seen_other_tags || seen_rust_tags;
data.no_run = true;
}
x if allow_error_code_check && x.starts_with("edition") => {
// allow_error_code_check is true if we're on nightly, which
// is needed for edition support
x if x.starts_with("edition") => {
data.edition = x[7..].parse::<Edition>().ok();
}
x if allow_error_code_check && x.starts_with("E") && x.len() == 5 => {
Expand Down
9 changes: 3 additions & 6 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,19 +633,16 @@ function handleThemeButtonsBlur(e) {{

themePicker.onclick = switchThemeButtonState;
themePicker.onblur = handleThemeButtonsBlur;
[{}].forEach(function(item) {{
{}.forEach(function(item) {{
var but = document.createElement('button');
but.innerHTML = item;
but.textContent = item;
but.onclick = function(el) {{
switchTheme(currentTheme, mainTheme, item, true);
}};
but.onblur = handleThemeButtonsBlur;
themes.appendChild(but);
}});"#,
themes.iter()
.map(|s| format!("\"{}\"", s))
.collect::<Vec<String>>()
.join(","));
as_json(&themes));
write(cx.dst.join(&format!("theme{}.js", cx.shared.resource_suffix)),
theme_js.as_bytes()
)?;
Expand Down
61 changes: 37 additions & 24 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,38 +396,51 @@ function getSearchElement() {

document.onkeypress = handleShortcut;
document.onkeydown = handleShortcut;
document.onclick = function(ev) {
if (hasClass(ev.target, "collapse-toggle")) {
collapseDocs(ev.target, "toggle");
} else if (hasClass(ev.target.parentNode, "collapse-toggle")) {
collapseDocs(ev.target.parentNode, "toggle");
} else if (ev.target.tagName === "SPAN" && hasClass(ev.target.parentNode, "line-numbers")) {
var prev_id = 0;

var set_fragment = function(name) {
if (browserSupportsHistoryApi()) {
history.replaceState(null, null, "#" + name);
highlightSourceLines();
} else {
location.replace("#" + name);
}
};
var handleSourceHighlight = (function() {
var prev_line_id = 0;

var cur_id = parseInt(ev.target.id, 10);
var set_fragment = function(name) {
var x = window.scrollX,
y = window.scrollY;
if (browserSupportsHistoryApi()) {
history.replaceState(null, null, "#" + name);
highlightSourceLines();
} else {
location.replace("#" + name);
}
// Prevent jumps when selecting one or many lines
window.scrollTo(x, y);
};

return function(ev) {
var cur_line_id = parseInt(ev.target.id, 10);
ev.preventDefault();

if (ev.shiftKey && prev_id) {
if (prev_id > cur_id) {
var tmp = prev_id;
prev_id = cur_id;
cur_id = tmp;
if (ev.shiftKey && prev_line_id) {
// Swap selection if needed
if (prev_line_id > cur_line_id) {
var tmp = prev_line_id;
prev_line_id = cur_line_id;
cur_line_id = tmp;
}

set_fragment(prev_id + "-" + cur_id);
set_fragment(prev_line_id + "-" + cur_line_id);
} else {
prev_id = cur_id;
prev_line_id = cur_line_id;

set_fragment(cur_id);
set_fragment(cur_line_id);
}
}
})();

document.onclick = function(ev) {
if (hasClass(ev.target, "collapse-toggle")) {
collapseDocs(ev.target, "toggle");
} else if (hasClass(ev.target.parentNode, "collapse-toggle")) {
collapseDocs(ev.target.parentNode, "toggle");
} else if (ev.target.tagName === "SPAN" && hasClass(ev.target.parentNode, "line-numbers")) {
handleSourceHighlight(ev);
} else if (hasClass(getHelpElement(), "hidden") === false) {
var help = getHelpElement();
var is_inside_help_popup = ev.target !== help && help.contains(ev.target);
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/static_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub static RUST_FAVICON: &'static [u8] = include_bytes!("static/favicon.ico");
/// The built-in themes given to every documentation site.
pub mod themes {
/// The "light" theme, selected by default when no setting is available. Used as the basis for
/// the `--theme-checker` functionality.
/// the `--check-theme` functionality.
pub static LIGHT: &'static str = include_str!("static/themes/light.css");

/// The "dark" theme.
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,13 @@ fn opts() -> Vec<RustcOptGroup> {
o.optflag("", "sort-modules-by-appearance", "sort modules by where they appear in the \
program, rather than alphabetically")
}),
unstable("themes", |o| {
o.optmulti("", "themes",
stable("theme", |o| {
o.optmulti("", "theme",
"additional themes which will be added to the generated docs",
"FILES")
}),
unstable("theme-checker", |o| {
o.optmulti("", "theme-checker",
stable("check-theme", |o| {
o.optmulti("", "check-theme",
"check if given theme is valid",
"FILES")
}),
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ pub fn test_theme_against<P: AsRef<Path>>(
diag: &Handler,
) -> (bool, Vec<String>) {
let data = try_something!(fs::read(f), diag, (false, vec![]));

let paths = load_css_paths(&data);
let mut ret = vec![];
get_differences(against, &paths, &mut ret);
Expand Down
10 changes: 10 additions & 0 deletions src/test/run-make-fulldeps/rustdoc-themes/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-include ../tools.mk

# Test that rustdoc will properly load in a theme file and display it in the theme selector.

OUTPUT_DIR := "$(TMPDIR)/rustdoc-themes"

all:
cp $(S)/src/librustdoc/html/static/themes/light.css $(TMPDIR)/test.css
$(RUSTDOC) -o $(OUTPUT_DIR) foo.rs --theme $(TMPDIR)/test.css
$(HTMLDOCCK) $(OUTPUT_DIR) foo.rs
4 changes: 4 additions & 0 deletions src/test/run-make-fulldeps/rustdoc-themes/foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @has test.css
// @has foo/struct.Foo.html
// @has - '//link[@rel="stylesheet"]/@href' '../test.css'
pub struct Foo;
2 changes: 1 addition & 1 deletion src/test/rustdoc/edition-flag.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// compile-flags:--test -Z unstable-options
// compile-flags:--test
// edition:2018

/// ```rust
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/issues/issue-45730.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ LL | let x = 0 as *const i32 as *const _ as *mut _;

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0641`.
1 change: 1 addition & 0 deletions src/test/ui/order-dependent-cast-inference.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ LL | let mut y = 0 as *const _;

error: aborting due to previous error

For more information about this error, try `rustc --explain E0641`.
6 changes: 4 additions & 2 deletions src/tools/rustdoc-themes/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ fn main() {
eprintln!("No theme found in \"{}\"...", themes_folder);
exit(1);
}
let arg_name = "--check-theme".to_owned();
let status = Command::new(rustdoc_bin)
.args(&["-Z", "unstable-options", "--theme-checker"])
.args(&themes)
.args(&themes.iter()
.flat_map(|t| vec![&arg_name, t].into_iter())
.collect::<Vec<_>>())
.status()
.expect("failed to execute child");
if !status.success() {
Expand Down