Skip to content
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

rustdoc: Create an attribute version of --extern-html-root-url #96533

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,7 @@ impl CheckAttrVisitor<'_> {
| sym::html_root_url
| sym::html_no_source
| sym::test
| sym::extern_html_root_url
if !self.check_attr_crate_level(attr, meta, hir_id) =>
{
is_valid = false;
Expand Down Expand Up @@ -1052,6 +1053,7 @@ impl CheckAttrVisitor<'_> {
sym::alias
| sym::cfg
| sym::cfg_hide
| sym::extern_html_root_url
| sym::hidden
| sym::html_favicon_url
| sym::html_logo_url
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ symbols! {
extern_absolute_paths,
extern_crate_item_prelude,
extern_crate_self,
extern_html_root_url,
extern_in_paths,
extern_prelude,
extern_types,
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ pub(crate) struct RenderOptions {
pub(crate) extension_css: Option<PathBuf>,
/// A map of crate names to the URL to use instead of querying the crate's `html_root_url`.
pub(crate) extern_html_root_urls: BTreeMap<String, String>,
/// Whether to give precedence to `html_root_url` or `--exten-html-root-url`.
/// Whether to give precedence to `html_root_url` or `--extern-html-root-url`.
/// By default it is `html_root_url`.
pub(crate) extern_html_root_takes_precedence: bool,
/// A map of the default settings (values are as for DOM storage API). Keys should lack the
/// `rustdoc-` prefix.
Expand Down
39 changes: 39 additions & 0 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use rustc_span::symbol::sym;
use rustc_span::{source_map, Span, Symbol};

use std::cell::RefCell;
use std::collections::btree_map::Entry;
use std::lazy::SyncLazy;
use std::mem;
use std::rc::Rc;
Expand Down Expand Up @@ -456,6 +457,44 @@ pub(crate) fn run_global_ctxt(
if attr.is_word() && name == sym::document_private_items {
ctxt.render_options.document_private = true;
}

if name == sym::extern_html_root_url {
if let Some(attr) = attr.meta_item_list() {
for sub_attr in attr {
let name = sub_attr.name_or_empty().as_str().to_owned();
match sub_attr.value_str() {
Some(value) => {
let value = value.as_str().to_owned();
if value.is_empty() {
tcx.sess.span_err(sub_attr.span(), "URL cannot be empty");
continue;
}
match ctxt.render_options.extern_html_root_urls.entry(name) {
Entry::Occupied(_) => {
// do nothing since command line `--extern_html_root_url`
// takes precedence.
}
Entry::Vacant(v) => {
v.insert(value);
}
}
}
None => {
tcx.sess.span_err(
sub_attr.span(),
"extern_html_root_url() only accepts `crate_name = \"url\"`",
);
}
}
}
} else {
diag.span_err(
attr.span(),
"extern_html_root_url only accepts this form: \
`extern_html_root_url(crate = \"url\")`",
);
}
}
}

info!("Executing passes");
Expand Down
18 changes: 18 additions & 0 deletions src/test/rustdoc-ui/extern_html_root_url.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![crate_type = "lib"]

#![doc(extern_html_root_url)]
//~^ ERROR
#![doc(extern_html_root_url = "a")]
//~^ ERROR
#![doc(extern_html_root_url(a))]
//~^ ERROR
#![doc(extern_html_root_url(a()))]
//~^ ERROR
#![doc(extern_html_root_url(a = 1))]
//~^ ERROR
#![doc(extern_html_root_url(a = ""))]
//~^ ERROR
#![doc(extern_html_root_url(a = "1"))] // This one is supposed to work.
#![doc(extern_html_root_url(b = "2", c = "3"))] // This one is supposed to work.

pub fn foo() {}
38 changes: 38 additions & 0 deletions src/test/rustdoc-ui/extern_html_root_url.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
error: extern_html_root_url only accepts this form: `extern_html_root_url(crate = "url")`
--> $DIR/extern_html_root_url.rs:3:8
|
LL | #![doc(extern_html_root_url)]
| ^^^^^^^^^^^^^^^^^^^^

error: extern_html_root_url only accepts this form: `extern_html_root_url(crate = "url")`
--> $DIR/extern_html_root_url.rs:5:8
|
LL | #![doc(extern_html_root_url = "a")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: extern_html_root_url() only accepts `crate_name = "url"`
--> $DIR/extern_html_root_url.rs:7:29
|
LL | #![doc(extern_html_root_url(a))]
| ^

error: extern_html_root_url() only accepts `crate_name = "url"`
--> $DIR/extern_html_root_url.rs:9:29
|
LL | #![doc(extern_html_root_url(a()))]
| ^^^

error: extern_html_root_url() only accepts `crate_name = "url"`
--> $DIR/extern_html_root_url.rs:11:29
|
LL | #![doc(extern_html_root_url(a = 1))]
| ^^^^^

error: URL cannot be empty
--> $DIR/extern_html_root_url.rs:13:29
|
LL | #![doc(extern_html_root_url(a = ""))]
| ^^^^^^

error: aborting due to 6 previous errors

6 changes: 6 additions & 0 deletions src/test/rustdoc/extern_html_root_url.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![crate_name = "foo"]
#![doc(extern_html_root_url(std = "https://doc.rust-lang.org/nightly"))]

// @has 'foo/index.html'
// @has - '//a[@class="mod"]/@href' 'https://doc.rust-lang.org/nightly/std/index.html'
pub use std as other;