Skip to content

Commit e8747d0

Browse files
Add support for doc(extern_html_root_url()) attribute
1 parent 4f372b1 commit e8747d0

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

compiler/rustc_passes/src/check_attr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,7 @@ impl CheckAttrVisitor<'_> {
10201020
| sym::html_root_url
10211021
| sym::html_no_source
10221022
| sym::test
1023+
| sym::extern_html_root_url
10231024
if !self.check_attr_crate_level(attr, meta, hir_id) =>
10241025
{
10251026
is_valid = false;
@@ -1052,6 +1053,7 @@ impl CheckAttrVisitor<'_> {
10521053
sym::alias
10531054
| sym::cfg
10541055
| sym::cfg_hide
1056+
| sym::extern_html_root_url
10551057
| sym::hidden
10561058
| sym::html_favicon_url
10571059
| sym::html_logo_url

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ symbols! {
653653
extern_absolute_paths,
654654
extern_crate_item_prelude,
655655
extern_crate_self,
656+
extern_html_root_url,
656657
extern_in_paths,
657658
extern_prelude,
658659
extern_types,

src/librustdoc/config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ pub(crate) struct RenderOptions {
230230
pub(crate) extension_css: Option<PathBuf>,
231231
/// A map of crate names to the URL to use instead of querying the crate's `html_root_url`.
232232
pub(crate) extern_html_root_urls: BTreeMap<String, String>,
233-
/// Whether to give precedence to `html_root_url` or `--exten-html-root-url`.
233+
/// Whether to give precedence to `html_root_url` or `--extern-html-root-url`.
234+
/// By default it is `html_root_url`.
234235
pub(crate) extern_html_root_takes_precedence: bool,
235236
/// A map of the default settings (values are as for DOM storage API). Keys should lack the
236237
/// `rustdoc-` prefix.

src/librustdoc/core.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc_span::symbol::sym;
2121
use rustc_span::{source_map, Span, Symbol};
2222

2323
use std::cell::RefCell;
24+
use std::collections::btree_map::Entry;
2425
use std::lazy::SyncLazy;
2526
use std::mem;
2627
use std::rc::Rc;
@@ -456,6 +457,44 @@ pub(crate) fn run_global_ctxt(
456457
if attr.is_word() && name == sym::document_private_items {
457458
ctxt.render_options.document_private = true;
458459
}
460+
461+
if name == sym::extern_html_root_url {
462+
if let Some(attr) = attr.meta_item_list() {
463+
for sub_attr in attr {
464+
let name = sub_attr.name_or_empty().as_str().to_owned();
465+
match sub_attr.value_str() {
466+
Some(value) => {
467+
let value = value.as_str().to_owned();
468+
if value.is_empty() {
469+
tcx.sess.span_err(sub_attr.span(), "URL cannot be empty");
470+
continue;
471+
}
472+
match ctxt.render_options.extern_html_root_urls.entry(name) {
473+
Entry::Occupied(_) => {
474+
// do nothing since command line `--extern_html_root_url`
475+
// takes precedence.
476+
}
477+
Entry::Vacant(v) => {
478+
v.insert(value);
479+
}
480+
}
481+
}
482+
None => {
483+
tcx.sess.span_err(
484+
sub_attr.span(),
485+
"extern_html_root_url() only accepts `crate_name = \"url\"`",
486+
);
487+
}
488+
}
489+
}
490+
} else {
491+
diag.span_err(
492+
attr.span(),
493+
"extern_html_root_url only accepts this form: \
494+
`extern_html_root_url(crate = \"url\")`",
495+
);
496+
}
497+
}
459498
}
460499

461500
info!("Executing passes");

0 commit comments

Comments
 (0)