summaryrefslogtreecommitdiffstats
path: root/src/librustdoc
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/librustdoc/Cargo.toml13
-rw-r--r--src/librustdoc/clean/auto_trait.rs35
-rw-r--r--src/librustdoc/clean/blanket_impl.rs209
-rw-r--r--src/librustdoc/clean/inline.rs108
-rw-r--r--src/librustdoc/clean/mod.rs1553
-rw-r--r--src/librustdoc/clean/simplify.rs60
-rw-r--r--src/librustdoc/clean/types.rs190
-rw-r--r--src/librustdoc/clean/utils.rs94
-rw-r--r--src/librustdoc/config.rs96
-rw-r--r--src/librustdoc/core.rs48
-rw-r--r--src/librustdoc/doctest.rs27
-rw-r--r--src/librustdoc/fold.rs4
-rw-r--r--src/librustdoc/formats/cache.rs29
-rw-r--r--src/librustdoc/formats/item_type.rs1
-rw-r--r--src/librustdoc/formats/renderer.rs4
-rw-r--r--src/librustdoc/html/format.rs78
-rw-r--r--src/librustdoc/html/highlight.rs368
-rw-r--r--src/librustdoc/html/highlight/fixtures/decorations.html6
-rw-r--r--src/librustdoc/html/highlight/fixtures/dos_line.html2
-rw-r--r--src/librustdoc/html/highlight/fixtures/highlight.html8
-rw-r--r--src/librustdoc/html/highlight/fixtures/sample.html39
-rw-r--r--src/librustdoc/html/highlight/fixtures/sample.rs1
-rw-r--r--src/librustdoc/html/highlight/fixtures/union.html10
-rw-r--r--src/librustdoc/html/highlight/tests.rs18
-rw-r--r--src/librustdoc/html/markdown.rs66
-rw-r--r--src/librustdoc/html/markdown/tests.rs48
-rw-r--r--src/librustdoc/html/render/context.rs102
-rw-r--r--src/librustdoc/html/render/mod.rs609
-rw-r--r--src/librustdoc/html/render/print_item.rs141
-rw-r--r--src/librustdoc/html/render/search_index.rs13
-rw-r--r--src/librustdoc/html/render/span_map.rs34
-rw-r--r--src/librustdoc/html/render/write_shared.rs85
-rw-r--r--src/librustdoc/html/sources.rs24
-rw-r--r--src/librustdoc/html/static/css/noscript.css6
-rw-r--r--src/librustdoc/html/static/css/rustdoc.css1226
-rw-r--r--src/librustdoc/html/static/css/settings.css22
-rw-r--r--src/librustdoc/html/static/css/themes/ayu.css386
-rw-r--r--src/librustdoc/html/static/css/themes/dark.css318
-rw-r--r--src/librustdoc/html/static/css/themes/light.css313
-rw-r--r--src/librustdoc/html/static/images/down-arrow.svg2
-rw-r--r--src/librustdoc/html/static/js/main.js325
-rw-r--r--src/librustdoc/html/static/js/scrape-examples.js2
-rw-r--r--src/librustdoc/html/static/js/search.js31
-rw-r--r--src/librustdoc/html/static/js/settings.js11
-rw-r--r--src/librustdoc/html/static/js/source-script.js23
-rw-r--r--src/librustdoc/html/static/js/storage.js4
-rw-r--r--src/librustdoc/html/templates/page.html62
-rw-r--r--src/librustdoc/html/templates/print_item.html24
-rw-r--r--src/librustdoc/json/conversions.rs307
-rw-r--r--src/librustdoc/json/import_finder.rs38
-rw-r--r--src/librustdoc/json/mod.rs46
-rw-r--r--src/librustdoc/lib.rs87
-rw-r--r--src/librustdoc/lint.rs11
-rw-r--r--src/librustdoc/markdown.rs7
-rw-r--r--src/librustdoc/passes/bare_urls.rs6
-rw-r--r--src/librustdoc/passes/calculate_doc_coverage.rs18
-rw-r--r--src/librustdoc/passes/check_code_block_syntax.rs112
-rw-r--r--src/librustdoc/passes/check_doc_test_visibility.rs16
-rw-r--r--src/librustdoc/passes/collect_intra_doc_links.rs48
-rw-r--r--src/librustdoc/passes/collect_intra_doc_links/early.rs44
-rw-r--r--src/librustdoc/passes/html_tags.rs153
-rw-r--r--src/librustdoc/passes/propagate_doc_cfg.rs62
-rw-r--r--src/librustdoc/passes/strip_hidden.rs8
-rw-r--r--src/librustdoc/passes/strip_private.rs12
-rw-r--r--src/librustdoc/passes/stripper.rs99
-rw-r--r--src/librustdoc/scrape_examples.rs13
-rw-r--r--src/librustdoc/theme.rs393
-rw-r--r--src/librustdoc/theme/tests.rs120
-rw-r--r--src/librustdoc/visit.rs4
-rw-r--r--src/librustdoc/visit_ast.rs32
-rw-r--r--src/librustdoc/visit_lib.rs32
71 files changed, 4419 insertions, 4127 deletions
diff --git a/src/librustdoc/Cargo.toml b/src/librustdoc/Cargo.toml
index ddaa7438e..7bc35c7d5 100644
--- a/src/librustdoc/Cargo.toml
+++ b/src/librustdoc/Cargo.toml
@@ -10,18 +10,19 @@ path = "lib.rs"
arrayvec = { version = "0.7", default-features = false }
askama = { version = "0.11", default-features = false, features = ["config"] }
atty = "0.2"
+itertools = "0.10.1"
+minifier = "0.2.2"
+once_cell = "1.10.0"
pulldown-cmark = { version = "0.9.2", default-features = false }
-minifier = "0.2.1"
-serde = { version = "1.0", features = ["derive"] }
+regex = "1"
+rustdoc-json-types = { path = "../rustdoc-json-types" }
serde_json = "1.0"
+serde = { version = "1.0", features = ["derive"] }
smallvec = "1.8.1"
tempfile = "3"
-itertools = "0.10.1"
-regex = "1"
-rustdoc-json-types = { path = "../rustdoc-json-types" }
+thin-vec = "0.2.8"
tracing = "0.1"
tracing-tree = "0.2.0"
-once_cell = "1.10.0"
[dependencies.tracing-subscriber]
version = "0.3.3"
diff --git a/src/librustdoc/clean/auto_trait.rs b/src/librustdoc/clean/auto_trait.rs
index af33c1a6a..764a6d3aa 100644
--- a/src/librustdoc/clean/auto_trait.rs
+++ b/src/librustdoc/clean/auto_trait.rs
@@ -123,7 +123,7 @@ where
kind: Box::new(ImplItem(Box::new(Impl {
unsafety: hir::Unsafety::Normal,
generics: new_generics,
- trait_: Some(clean_trait_ref_with_bindings(self.cx, trait_ref, &[])),
+ trait_: Some(clean_trait_ref_with_bindings(self.cx, trait_ref, ThinVec::new())),
for_: clean_middle_ty(ty, self.cx, None),
items: Vec::new(),
polarity,
@@ -148,7 +148,7 @@ where
})
.collect();
// We are only interested in case the type *doesn't* implement the Sized trait.
- if !ty.is_sized(tcx.at(rustc_span::DUMMY_SP), param_env) {
+ if !ty.is_sized(tcx, param_env) {
// In case `#![no_core]` is used, `sized_trait` returns nothing.
if let Some(item) = tcx.lang_items().sized_trait().and_then(|sized_trait_did| {
self.generate_for_trait(ty, sized_trait_did, param_env, item_def_id, &f, true)
@@ -475,8 +475,14 @@ where
let mut ty_to_fn: FxHashMap<Type, (PolyTrait, Option<Type>)> = Default::default();
+ // FIXME: This code shares much of the logic found in `clean_ty_generics` and
+ // `simplify::where_clause`. Consider deduplicating it to avoid diverging
+ // implementations.
+ // Further, the code below does not merge (partially re-sugared) bounds like
+ // `Tr<A = T>` & `Tr<B = U>` and it does not render higher-ranked parameters
+ // originating from equality predicates.
for p in clean_where_predicates {
- let (orig_p, p) = (p, p.clean(self.cx));
+ let (orig_p, p) = (p, clean_predicate(p, self.cx));
if p.is_none() {
continue;
}
@@ -525,8 +531,8 @@ where
GenericBound::TraitBound(ref mut p, _) => {
// Insert regions into the for_generics hash map first, to ensure
// that we don't end up with duplicate bounds (e.g., for<'b, 'b>)
- for_generics.extend(p.generic_params.clone());
- p.generic_params = for_generics.into_iter().collect();
+ for_generics.extend(p.generic_params.drain(..));
+ p.generic_params.extend(for_generics);
self.is_fn_trait(&p.trait_)
}
_ => false,
@@ -549,15 +555,17 @@ where
WherePredicate::RegionPredicate { lifetime, bounds } => {
lifetime_to_bounds.entry(lifetime).or_default().extend(bounds);
}
- WherePredicate::EqPredicate { lhs, rhs } => {
- match lhs {
- Type::QPath { ref assoc, ref self_type, ref trait_, .. } => {
+ WherePredicate::EqPredicate { lhs, rhs, bound_params } => {
+ match *lhs {
+ Type::QPath(box QPathData {
+ ref assoc, ref self_type, ref trait_, ..
+ }) => {
let ty = &*self_type;
let mut new_trait = trait_.clone();
if self.is_fn_trait(trait_) && assoc.name == sym::Output {
ty_to_fn
- .entry(*ty.clone())
+ .entry(ty.clone())
.and_modify(|e| {
*e = (e.0.clone(), Some(rhs.ty().unwrap().clone()))
})
@@ -582,21 +590,22 @@ where
// to 'T: Iterator<Item=u8>'
GenericArgs::AngleBracketed { ref mut bindings, .. } => {
bindings.push(TypeBinding {
- assoc: *assoc.clone(),
- kind: TypeBindingKind::Equality { term: rhs },
+ assoc: assoc.clone(),
+ kind: TypeBindingKind::Equality { term: *rhs },
});
}
GenericArgs::Parenthesized { .. } => {
existing_predicates.push(WherePredicate::EqPredicate {
lhs: lhs.clone(),
rhs,
+ bound_params,
});
continue; // If something other than a Fn ends up
// with parentheses, leave it alone
}
}
- let bounds = ty_to_bounds.entry(*ty.clone()).or_default();
+ let bounds = ty_to_bounds.entry(ty.clone()).or_default();
bounds.insert(GenericBound::TraitBound(
PolyTrait { trait_: new_trait, generic_params: Vec::new() },
@@ -613,7 +622,7 @@ where
));
// Avoid creating any new duplicate bounds later in the outer
// loop
- ty_to_traits.entry(*ty.clone()).or_default().insert(trait_.clone());
+ ty_to_traits.entry(ty.clone()).or_default().insert(trait_.clone());
}
_ => panic!("Unexpected LHS {:?} for {:?}", lhs, item_def_id),
}
diff --git a/src/librustdoc/clean/blanket_impl.rs b/src/librustdoc/clean/blanket_impl.rs
index 01dd95e6e..8b63c3db3 100644
--- a/src/librustdoc/clean/blanket_impl.rs
+++ b/src/librustdoc/clean/blanket_impl.rs
@@ -2,7 +2,6 @@ use crate::rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtE
use rustc_hir as hir;
use rustc_infer::infer::{InferOk, TyCtxtInferExt};
use rustc_infer::traits;
-use rustc_middle::ty::subst::Subst;
use rustc_middle::ty::ToPredicate;
use rustc_span::DUMMY_SP;
@@ -14,122 +13,124 @@ pub(crate) struct BlanketImplFinder<'a, 'tcx> {
impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
pub(crate) fn get_blanket_impls(&mut self, item_def_id: DefId) -> Vec<Item> {
- let param_env = self.cx.tcx.param_env(item_def_id);
- let ty = self.cx.tcx.bound_type_of(item_def_id);
+ let cx = &mut self.cx;
+ let param_env = cx.tcx.param_env(item_def_id);
+ let ty = cx.tcx.bound_type_of(item_def_id);
trace!("get_blanket_impls({:?})", ty);
let mut impls = Vec::new();
- self.cx.with_all_traits(|cx, all_traits| {
- for &trait_def_id in all_traits {
- if !cx.cache.access_levels.is_public(trait_def_id)
- || cx.generated_synthetics.get(&(ty.0, trait_def_id)).is_some()
- {
+ for trait_def_id in cx.tcx.all_traits() {
+ if !cx.cache.effective_visibilities.is_directly_public(trait_def_id)
+ || cx.generated_synthetics.get(&(ty.0, trait_def_id)).is_some()
+ {
+ continue;
+ }
+ // NOTE: doesn't use `for_each_relevant_impl` to avoid looking at anything besides blanket impls
+ let trait_impls = cx.tcx.trait_impls_of(trait_def_id);
+ 'blanket_impls: for &impl_def_id in trait_impls.blanket_impls() {
+ trace!(
+ "get_blanket_impls: Considering impl for trait '{:?}' {:?}",
+ trait_def_id,
+ impl_def_id
+ );
+ let trait_ref = cx.tcx.bound_impl_trait_ref(impl_def_id).unwrap();
+ if !matches!(trait_ref.0.self_ty().kind(), ty::Param(_)) {
continue;
}
- // NOTE: doesn't use `for_each_relevant_impl` to avoid looking at anything besides blanket impls
- let trait_impls = cx.tcx.trait_impls_of(trait_def_id);
- for &impl_def_id in trait_impls.blanket_impls() {
- trace!(
- "get_blanket_impls: Considering impl for trait '{:?}' {:?}",
- trait_def_id,
- impl_def_id
- );
- let trait_ref = cx.tcx.bound_impl_trait_ref(impl_def_id).unwrap();
- let is_param = matches!(trait_ref.0.self_ty().kind(), ty::Param(_));
- let may_apply = is_param && cx.tcx.infer_ctxt().enter(|infcx| {
- let substs = infcx.fresh_substs_for_item(DUMMY_SP, item_def_id);
- let ty = ty.subst(infcx.tcx, substs);
- let param_env = EarlyBinder(param_env).subst(infcx.tcx, substs);
+ let infcx = cx.tcx.infer_ctxt().build();
+ let substs = infcx.fresh_substs_for_item(DUMMY_SP, item_def_id);
+ let impl_ty = ty.subst(infcx.tcx, substs);
+ let param_env = EarlyBinder(param_env).subst(infcx.tcx, substs);
- let impl_substs = infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id);
- let trait_ref = trait_ref.subst(infcx.tcx, impl_substs);
+ let impl_substs = infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id);
+ let impl_trait_ref = trait_ref.subst(infcx.tcx, impl_substs);
- // Require the type the impl is implemented on to match
- // our type, and ignore the impl if there was a mismatch.
- let cause = traits::ObligationCause::dummy();
- let eq_result = infcx.at(&cause, param_env).eq(trait_ref.self_ty(), ty);
- if let Ok(InferOk { value: (), obligations }) = eq_result {
- // FIXME(eddyb) ignoring `obligations` might cause false positives.
- drop(obligations);
+ // Require the type the impl is implemented on to match
+ // our type, and ignore the impl if there was a mismatch.
+ let cause = traits::ObligationCause::dummy();
+ let Ok(eq_result) = infcx.at(&cause, param_env).eq(impl_trait_ref.self_ty(), impl_ty) else {
+ continue
+ };
+ let InferOk { value: (), obligations } = eq_result;
+ // FIXME(eddyb) ignoring `obligations` might cause false positives.
+ drop(obligations);
- trace!(
- "invoking predicate_may_hold: param_env={:?}, trait_ref={:?}, ty={:?}",
- param_env,
- trait_ref,
- ty
- );
- let predicates = cx
- .tcx
- .predicates_of(impl_def_id)
- .instantiate(cx.tcx, impl_substs)
- .predicates
- .into_iter()
- .chain(Some(
- ty::Binder::dummy(trait_ref)
- .to_poly_trait_predicate()
- .map_bound(ty::PredicateKind::Trait)
- .to_predicate(infcx.tcx),
- ));
- for predicate in predicates {
- debug!("testing predicate {:?}", predicate);
- let obligation = traits::Obligation::new(
- traits::ObligationCause::dummy(),
- param_env,
- predicate,
- );
- match infcx.evaluate_obligation(&obligation) {
- Ok(eval_result) if eval_result.may_apply() => {}
- Err(traits::OverflowError::Canonical) => {}
- Err(traits::OverflowError::ErrorReporting) => {}
- _ => {
- return false;
- }
- }
- }
- true
- } else {
- false
- }
- });
- debug!(
- "get_blanket_impls: found applicable impl: {} for trait_ref={:?}, ty={:?}",
- may_apply, trait_ref, ty
+ trace!(
+ "invoking predicate_may_hold: param_env={:?}, impl_trait_ref={:?}, impl_ty={:?}",
+ param_env,
+ impl_trait_ref,
+ impl_ty
+ );
+ let predicates = cx
+ .tcx
+ .predicates_of(impl_def_id)
+ .instantiate(cx.tcx, impl_substs)
+ .predicates
+ .into_iter()
+ .chain(Some(
+ ty::Binder::dummy(impl_trait_ref)
+ .to_poly_trait_predicate()
+ .map_bound(ty::PredicateKind::Trait)
+ .to_predicate(infcx.tcx),
+ ));
+ for predicate in predicates {
+ debug!("testing predicate {:?}", predicate);
+ let obligation = traits::Obligation::new(
+ traits::ObligationCause::dummy(),
+ param_env,
+ predicate,
);
- if !may_apply {
- continue;
+ match infcx.evaluate_obligation(&obligation) {
+ Ok(eval_result) if eval_result.may_apply() => {}
+ Err(traits::OverflowError::Canonical) => {}
+ Err(traits::OverflowError::ErrorReporting) => {}
+ _ => continue 'blanket_impls,
}
+ }
+ debug!(
+ "get_blanket_impls: found applicable impl for trait_ref={:?}, ty={:?}",
+ trait_ref, ty
+ );
- cx.generated_synthetics.insert((ty.0, trait_def_id));
+ cx.generated_synthetics.insert((ty.0, trait_def_id));
- impls.push(Item {
- name: None,
- attrs: Default::default(),
- visibility: Inherited,
- item_id: ItemId::Blanket { impl_id: impl_def_id, for_: item_def_id },
- kind: Box::new(ImplItem(Box::new(Impl {
- unsafety: hir::Unsafety::Normal,
- generics: clean_ty_generics(
- cx,
- cx.tcx.generics_of(impl_def_id),
- cx.tcx.explicit_predicates_of(impl_def_id),
- ),
- // FIXME(eddyb) compute both `trait_` and `for_` from
- // the post-inference `trait_ref`, as it's more accurate.
- trait_: Some(clean_trait_ref_with_bindings(cx, trait_ref.0, &[])),
- for_: clean_middle_ty(ty.0, cx, None),
- items: cx.tcx
- .associated_items(impl_def_id)
- .in_definition_order()
- .map(|x| x.clean(cx))
- .collect::<Vec<_>>(),
- polarity: ty::ImplPolarity::Positive,
- kind: ImplKind::Blanket(Box::new(clean_middle_ty(trait_ref.0.self_ty(), cx, None))),
- }))),
- cfg: None,
- });
- }
+ impls.push(Item {
+ name: None,
+ attrs: Default::default(),
+ visibility: Inherited,
+ item_id: ItemId::Blanket { impl_id: impl_def_id, for_: item_def_id },
+ kind: Box::new(ImplItem(Box::new(Impl {
+ unsafety: hir::Unsafety::Normal,
+ generics: clean_ty_generics(
+ cx,
+ cx.tcx.generics_of(impl_def_id),
+ cx.tcx.explicit_predicates_of(impl_def_id),
+ ),
+ // FIXME(eddyb) compute both `trait_` and `for_` from
+ // the post-inference `trait_ref`, as it's more accurate.
+ trait_: Some(clean_trait_ref_with_bindings(
+ cx,
+ trait_ref.0,
+ ThinVec::new(),
+ )),
+ for_: clean_middle_ty(ty.0, cx, None),
+ items: cx
+ .tcx
+ .associated_items(impl_def_id)
+ .in_definition_order()
+ .map(|x| clean_middle_assoc_item(x, cx))
+ .collect::<Vec<_>>(),
+ polarity: ty::ImplPolarity::Positive,
+ kind: ImplKind::Blanket(Box::new(clean_middle_ty(
+ trait_ref.0.self_ty(),
+ cx,
+ None,
+ ))),
+ }))),
+ cfg: None,
+ });
}
- });
+ }
impls
}
diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs
index 58d0aedb0..4e2031a91 100644
--- a/src/librustdoc/clean/inline.rs
+++ b/src/librustdoc/clean/inline.rs
@@ -3,9 +3,10 @@
use std::iter::once;
use std::sync::Arc;
+use thin_vec::ThinVec;
+
use rustc_ast as ast;
use rustc_data_structures::fx::FxHashSet;
-use rustc_data_structures::thin_vec::ThinVec;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId;
@@ -16,15 +17,14 @@ use rustc_span::hygiene::MacroKind;
use rustc_span::symbol::{kw, sym, Symbol};
use crate::clean::{
- self, clean_fn_decl_from_did_and_sig, clean_middle_field, clean_middle_ty,
- clean_trait_ref_with_bindings, clean_ty, clean_ty_generics, clean_variant_def,
- clean_visibility, utils, Attributes, AttributesExt, Clean, ImplKind, ItemId, Type, Visibility,
+ self, clean_fn_decl_from_did_and_sig, clean_generics, clean_impl_item, clean_middle_assoc_item,
+ clean_middle_field, clean_middle_ty, clean_trait_ref_with_bindings, clean_ty,
+ clean_ty_generics, clean_variant_def, clean_visibility, utils, Attributes, AttributesExt,
+ ImplKind, ItemId, Type, Visibility,
};
use crate::core::DocContext;
use crate::formats::item_type::ItemType;
-type Attrs<'hir> = &'hir [ast::Attribute];
-
/// Attempt to inline a definition into this AST.
///
/// This function will fetch the definition specified, and if it is
@@ -45,7 +45,7 @@ pub(crate) fn try_inline(
import_def_id: Option<DefId>,
res: Res,
name: Symbol,
- attrs: Option<Attrs<'_>>,
+ attrs: Option<&[ast::Attribute]>,
visited: &mut FxHashSet<DefId>,
) -> Option<Vec<clean::Item>> {
let did = res.opt_def_id()?;
@@ -55,13 +55,40 @@ pub(crate) fn try_inline(
let mut ret = Vec::new();
debug!("attrs={:?}", attrs);
- let attrs_clone = attrs;
+
+ let attrs_without_docs = attrs.map(|attrs| {
+ attrs.into_iter().filter(|a| a.doc_str().is_none()).cloned().collect::<Vec<_>>()
+ });
+ // We need this ugly code because:
+ //
+ // ```
+ // attrs_without_docs.map(|a| a.as_slice())
+ // ```
+ //
+ // will fail because it returns a temporary slice and:
+ //
+ // ```
+ // attrs_without_docs.map(|s| {
+ // vec = s.as_slice();
+ // vec
+ // })
+ // ```
+ //
+ // will fail because we're moving an uninitialized variable into a closure.
+ let vec;
+ let attrs_without_docs = match attrs_without_docs {
+ Some(s) => {
+ vec = s;
+ Some(vec.as_slice())
+ }
+ None => None,
+ };
let kind = match res {
Res::Def(DefKind::Trait, did) => {
record_extern_fqn(cx, did, ItemType::Trait);
- build_impls(cx, Some(parent_module), did, attrs, &mut ret);
- clean::TraitItem(build_external_trait(cx, did))
+ build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
+ clean::TraitItem(Box::new(build_external_trait(cx, did)))
}
Res::Def(DefKind::Fn, did) => {
record_extern_fqn(cx, did, ItemType::Function);
@@ -69,27 +96,27 @@ pub(crate) fn try_inline(
}
Res::Def(DefKind::Struct, did) => {
record_extern_fqn(cx, did, ItemType::Struct);
- build_impls(cx, Some(parent_module), did, attrs, &mut ret);
+ build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
clean::StructItem(build_struct(cx, did))
}
Res::Def(DefKind::Union, did) => {
record_extern_fqn(cx, did, ItemType::Union);
- build_impls(cx, Some(parent_module), did, attrs, &mut ret);
+ build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
clean::UnionItem(build_union(cx, did))
}
Res::Def(DefKind::TyAlias, did) => {
record_extern_fqn(cx, did, ItemType::Typedef);
- build_impls(cx, Some(parent_module), did, attrs, &mut ret);
+ build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
clean::TypedefItem(build_type_alias(cx, did))
}
Res::Def(DefKind::Enum, did) => {
record_extern_fqn(cx, did, ItemType::Enum);
- build_impls(cx, Some(parent_module), did, attrs, &mut ret);
+ build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
clean::EnumItem(build_enum(cx, did))
}
Res::Def(DefKind::ForeignTy, did) => {
record_extern_fqn(cx, did, ItemType::ForeignType);
- build_impls(cx, Some(parent_module), did, attrs, &mut ret);
+ build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
clean::ForeignTypeItem
}
// Never inline enum variants but leave them shown as re-exports.
@@ -123,7 +150,7 @@ pub(crate) fn try_inline(
_ => return None,
};
- let (attrs, cfg) = merge_attrs(cx, Some(parent_module), load_attrs(cx, did), attrs_clone);
+ let (attrs, cfg) = merge_attrs(cx, Some(parent_module), load_attrs(cx, did), attrs);
cx.inlined.insert(did.into());
let mut item = clean::Item::from_def_id_and_attrs_and_parts(
did,
@@ -171,7 +198,7 @@ pub(crate) fn try_inline_glob(
}
}
-pub(crate) fn load_attrs<'hir>(cx: &DocContext<'hir>, did: DefId) -> Attrs<'hir> {
+pub(crate) fn load_attrs<'hir>(cx: &DocContext<'hir>, did: DefId) -> &'hir [ast::Attribute] {
cx.tcx.get_attrs_unchecked(did)
}
@@ -217,7 +244,7 @@ pub(crate) fn build_external_trait(cx: &mut DocContext<'_>, did: DefId) -> clean
// which causes methods to have a `pub` prefix, which is invalid since items in traits
// can not have a visibility prefix. Thus we override the visibility here manually.
// See https://github.com/rust-lang/rust/issues/81274
- clean::Item { visibility: Visibility::Inherited, ..item.clean(cx) }
+ clean::Item { visibility: Visibility::Inherited, ..clean_middle_assoc_item(item, cx) }
})
.collect();
@@ -286,7 +313,7 @@ pub(crate) fn build_impls(
cx: &mut DocContext<'_>,
parent_module: Option<DefId>,
did: DefId,
- attrs: Option<Attrs<'_>>,
+ attrs: Option<&[ast::Attribute]>,
ret: &mut Vec<clean::Item>,
) {
let _prof_timer = cx.tcx.sess.prof.generic_activity("build_inherent_impls");
@@ -296,14 +323,29 @@ pub(crate) fn build_impls(
for &did in tcx.inherent_impls(did).iter() {
build_impl(cx, parent_module, did, attrs, ret);
}
+
+ // This pretty much exists expressly for `dyn Error` traits that exist in the `alloc` crate.
+ // See also:
+ //
+ // * https://github.com/rust-lang/rust/issues/103170 — where it didn't used to get documented
+ // * https://github.com/rust-lang/rust/pull/99917 — where the feature got used
+ // * https://github.com/rust-lang/rust/issues/53487 — overall tracking issue for Error
+ if tcx.has_attr(did, sym::rustc_has_incoherent_inherent_impls) {
+ use rustc_middle::ty::fast_reject::SimplifiedTypeGen::*;
+ let type_ =
+ if tcx.is_trait(did) { TraitSimplifiedType(did) } else { AdtSimplifiedType(did) };
+ for &did in tcx.incoherent_impls(type_) {
+ build_impl(cx, parent_module, did, attrs, ret);
+ }
+ }
}
/// `parent_module` refers to the parent of the re-export, not the original item
-fn merge_attrs(
+pub(crate) fn merge_attrs(
cx: &mut DocContext<'_>,
parent_module: Option<DefId>,
- old_attrs: Attrs<'_>,
- new_attrs: Option<Attrs<'_>>,
+ old_attrs: &[ast::Attribute],
+ new_attrs: Option<&[ast::Attribute]>,
) -> (clean::Attributes, Option<Arc<clean::cfg::Cfg>>) {
// NOTE: If we have additional attributes (from a re-export),
// always insert them first. This ensure that re-export
@@ -330,7 +372,7 @@ pub(crate) fn build_impl(
cx: &mut DocContext<'_>,
parent_module: Option<DefId>,
did: DefId,
- attrs: Option<Attrs<'_>>,
+ attrs: Option<&[ast::Attribute]>,
ret: &mut Vec<clean::Item>,
) {
if !cx.inlined.insert(did.into()) {
@@ -347,7 +389,7 @@ pub(crate) fn build_impl(
if !did.is_local() {
if let Some(traitref) = associated_trait {
let did = traitref.def_id;
- if !cx.cache.access_levels.is_public(did) {
+ if !cx.cache.effective_visibilities.is_directly_public(did) {
return;
}
@@ -376,7 +418,7 @@ pub(crate) fn build_impl(
// reachable in rustdoc generated documentation
if !did.is_local() {
if let Some(did) = for_.def_id(&cx.cache) {
- if !cx.cache.access_levels.is_public(did) {
+ if !cx.cache.effective_visibilities.is_directly_public(did) {
return;
}
@@ -410,7 +452,7 @@ pub(crate) fn build_impl(
let assoc_kind = match item.kind {
hir::ImplItemKind::Const(..) => ty::AssocKind::Const,
hir::ImplItemKind::Fn(..) => ty::AssocKind::Fn,
- hir::ImplItemKind::TyAlias(..) => ty::AssocKind::Type,
+ hir::ImplItemKind::Type(..) => ty::AssocKind::Type,
};
let trait_item = tcx
.associated_items(associated_trait.def_id)
@@ -426,9 +468,9 @@ pub(crate) fn build_impl(
true
}
})
- .map(|item| item.clean(cx))
+ .map(|item| clean_impl_item(item, cx))
.collect::<Vec<_>>(),
- impl_.generics.clean(cx),
+ clean_generics(impl_.generics, cx),
),
None => (
tcx.associated_items(did)
@@ -452,7 +494,7 @@ pub(crate) fn build_impl(
item.visibility(tcx).is_public()
}
})
- .map(|item| item.clean(cx))
+ .map(|item| clean_middle_assoc_item(item, cx))
.collect::<Vec<_>>(),
clean::enter_impl_trait(cx, |cx| {
clean_ty_generics(cx, tcx.generics_of(did), predicates)
@@ -460,7 +502,7 @@ pub(crate) fn build_impl(
),
};
let polarity = tcx.impl_polarity(did);
- let trait_ = associated_trait.map(|t| clean_trait_ref_with_bindings(cx, t, &[]));
+ let trait_ = associated_trait.map(|t| clean_trait_ref_with_bindings(cx, t, ThinVec::new()));
if trait_.as_ref().map(|t| t.def_id()) == tcx.lang_items().deref_trait() {
super::build_deref_target_impls(cx, &trait_items, ret);
}
@@ -671,7 +713,7 @@ fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics) -> clean:
g.where_predicates.retain(|pred| match pred {
clean::WherePredicate::BoundPredicate {
- ty: clean::QPath { self_type: box clean::Generic(ref s), trait_, .. },
+ ty: clean::QPath(box clean::QPathData { self_type: clean::Generic(ref s), trait_, .. }),
bounds,
..
} => !(bounds.is_empty() || *s == kw::SelfUpper && trait_.def_id() == trait_did),
@@ -718,10 +760,6 @@ pub(crate) fn record_extern_trait(cx: &mut DocContext<'_>, did: DefId) {
debug!("record_extern_trait: {:?}", did);
let trait_ = build_external_trait(cx, did);
- let trait_ = clean::TraitWithExtraInfo {
- trait_,
- is_notable: clean::utils::has_doc_flag(cx.tcx, did, sym::notable_trait),
- };
cx.external_traits.borrow_mut().insert(did, trait_);
cx.active_extern_traits.remove(&did);
}
diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs
index 929f5f89b..64a18757b 100644
--- a/src/librustdoc/clean/mod.rs
+++ b/src/librustdoc/clean/mod.rs
@@ -17,23 +17,24 @@ use rustc_hir as hir;
use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_hir::PredicateOrigin;
+use rustc_hir_analysis::hir_ty_to_ty;
use rustc_infer::infer::region_constraints::{Constraint, RegionConstraintData};
use rustc_middle::middle::resolve_lifetime as rl;
use rustc_middle::ty::fold::TypeFolder;
-use rustc_middle::ty::subst::{InternalSubsts, Subst};
-use rustc_middle::ty::{self, AdtKind, DefIdTree, EarlyBinder, Lift, Ty, TyCtxt};
+use rustc_middle::ty::InternalSubsts;
+use rustc_middle::ty::{self, AdtKind, DefIdTree, EarlyBinder, Ty, TyCtxt};
use rustc_middle::{bug, span_bug};
use rustc_span::hygiene::{AstPass, MacroKind};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{self, ExpnKind};
-use rustc_typeck::hir_ty_to_ty;
use std::assert_matches::assert_matches;
use std::collections::hash_map::Entry;
use std::collections::BTreeMap;
use std::default::Default;
use std::hash::Hash;
-use std::{mem, vec};
+use std::mem;
+use thin_vec::ThinVec;
use crate::core::{self, DocContext, ImplTraitParam};
use crate::formats::item_type::ItemType;
@@ -44,128 +45,126 @@ use utils::*;
pub(crate) use self::types::*;
pub(crate) use self::utils::{get_auto_trait_and_blanket_impls, krate, register_res};
-pub(crate) trait Clean<'tcx, T> {
- fn clean(&self, cx: &mut DocContext<'tcx>) -> T;
-}
+pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<'tcx>) -> Item {
+ let mut items: Vec<Item> = vec![];
+ let mut inserted = FxHashSet::default();
+ items.extend(doc.foreigns.iter().map(|(item, renamed)| {
+ let item = clean_maybe_renamed_foreign_item(cx, item, *renamed);
+ if let Some(name) = item.name && !item.attrs.lists(sym::doc).has_word(sym::hidden) {
+ inserted.insert((item.type_(), name));
+ }
+ item
+ }));
+ items.extend(doc.mods.iter().filter_map(|x| {
+ if !inserted.insert((ItemType::Module, x.name)) {
+ return None;
+ }
+ let item = clean_doc_module(x, cx);
+ if item.attrs.lists(sym::doc).has_word(sym::hidden) {
+ // Hidden modules are stripped at a later stage.
+ // If a hidden module has the same name as a visible one, we want
+ // to keep both of them around.
+ inserted.remove(&(ItemType::Module, x.name));
+ }
+ Some(item)
+ }));
-impl<'tcx> Clean<'tcx, Item> for DocModule<'tcx> {
- fn clean(&self, cx: &mut DocContext<'tcx>) -> Item {
- let mut items: Vec<Item> = vec![];
- let mut inserted = FxHashSet::default();
- items.extend(self.foreigns.iter().map(|(item, renamed)| {
- let item = clean_maybe_renamed_foreign_item(cx, item, *renamed);
- if let Some(name) = item.name {
+ // Split up imports from all other items.
+ //
+ // This covers the case where somebody does an import which should pull in an item,
+ // but there's already an item with the same namespace and same name. Rust gives
+ // priority to the not-imported one, so we should, too.
+ items.extend(doc.items.iter().flat_map(|(item, renamed)| {
+ // First, lower everything other than imports.
+ if matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) {
+ return Vec::new();
+ }
+ let v = clean_maybe_renamed_item(cx, item, *renamed);
+ for item in &v {
+ if let Some(name) = item.name && !item.attrs.lists(sym::doc).has_word(sym::hidden) {
inserted.insert((item.type_(), name));
}
- item
- }));
- items.extend(self.mods.iter().map(|x| {
- inserted.insert((ItemType::Module, x.name));
- x.clean(cx)
- }));
-
- // Split up imports from all other items.
- //
- // This covers the case where somebody does an import which should pull in an item,
- // but there's already an item with the same namespace and same name. Rust gives
- // priority to the not-imported one, so we should, too.
- items.extend(self.items.iter().flat_map(|(item, renamed)| {
- // First, lower everything other than imports.
- if matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) {
- return Vec::new();
- }
- let v = clean_maybe_renamed_item(cx, item, *renamed);
- for item in &v {
- if let Some(name) = item.name {
- inserted.insert((item.type_(), name));
- }
- }
- v
- }));
- items.extend(self.items.iter().flat_map(|(item, renamed)| {
- // Now we actually lower the imports, skipping everything else.
- if let hir::ItemKind::Use(path, hir::UseKind::Glob) = item.kind {
- let name = renamed.unwrap_or_else(|| cx.tcx.hir().name(item.hir_id()));
- clean_use_statement(item, name, path, hir::UseKind::Glob, cx, &mut inserted)
- } else {
- // skip everything else
- Vec::new()
- }
- }));
-
- // determine if we should display the inner contents or
- // the outer `mod` item for the source code.
-
- let span = Span::new({
- let where_outer = self.where_outer(cx.tcx);
- let sm = cx.sess().source_map();
- let outer = sm.lookup_char_pos(where_outer.lo());
- let inner = sm.lookup_char_pos(self.where_inner.lo());
- if outer.file.start_pos == inner.file.start_pos {
- // mod foo { ... }
- where_outer
- } else {
- // mod foo; (and a separate SourceFile for the contents)
- self.where_inner
- }
- });
+ }
+ v
+ }));
+ items.extend(doc.items.iter().flat_map(|(item, renamed)| {
+ // Now we actually lower the imports, skipping everything else.
+ if let hir::ItemKind::Use(path, hir::UseKind::Glob) = item.kind {
+ let name = renamed.unwrap_or_else(|| cx.tcx.hir().name(item.hir_id()));
+ clean_use_statement(item, name, path, hir::UseKind::Glob, cx, &mut inserted)
+ } else {
+ // skip everything else
+ Vec::new()
+ }
+ }));
+
+ // determine if we should display the inner contents or
+ // the outer `mod` item for the source code.
+
+ let span = Span::new({
+ let where_outer = doc.where_outer(cx.tcx);
+ let sm = cx.sess().source_map();
+ let outer = sm.lookup_char_pos(where_outer.lo());
+ let inner = sm.lookup_char_pos(doc.where_inner.lo());
+ if outer.file.start_pos == inner.file.start_pos {
+ // mod foo { ... }
+ where_outer
+ } else {
+ // mod foo; (and a separate SourceFile for the contents)
+ doc.where_inner
+ }
+ });
- Item::from_hir_id_and_parts(
- self.id,
- Some(self.name),
- ModuleItem(Module { items, span }),
- cx,
- )
- }
+ Item::from_hir_id_and_parts(doc.id, Some(doc.name), ModuleItem(Module { items, span }), cx)
}
-impl<'tcx> Clean<'tcx, Option<GenericBound>> for hir::GenericBound<'tcx> {
- fn clean(&self, cx: &mut DocContext<'tcx>) -> Option<GenericBound> {
- Some(match *self {
- hir::GenericBound::Outlives(lt) => GenericBound::Outlives(clean_lifetime(lt, cx)),
- hir::GenericBound::LangItemTrait(lang_item, span, _, generic_args) => {
- let def_id = cx.tcx.require_lang_item(lang_item, Some(span));
-
- let trait_ref = ty::TraitRef::identity(cx.tcx, def_id).skip_binder();
-
- let generic_args = generic_args.clean(cx);
- let GenericArgs::AngleBracketed { bindings, .. } = generic_args
- else {
- bug!("clean: parenthesized `GenericBound::LangItemTrait`");
- };
+fn clean_generic_bound<'tcx>(
+ bound: &hir::GenericBound<'tcx>,
+ cx: &mut DocContext<'tcx>,
+) -> Option<GenericBound> {
+ Some(match *bound {
+ hir::GenericBound::Outlives(lt) => GenericBound::Outlives(clean_lifetime(lt, cx)),
+ hir::GenericBound::LangItemTrait(lang_item, span, _, generic_args) => {
+ let def_id = cx.tcx.require_lang_item(lang_item, Some(span));
+
+ let trait_ref = ty::TraitRef::identity(cx.tcx, def_id).skip_binder();
+
+ let generic_args = clean_generic_args(generic_args, cx);
+ let GenericArgs::AngleBracketed { bindings, .. } = generic_args
+ else {
+ bug!("clean: parenthesized `GenericBound::LangItemTrait`");
+ };
- let trait_ = clean_trait_ref_with_bindings(cx, trait_ref, &bindings);
- GenericBound::TraitBound(
- PolyTrait { trait_, generic_params: vec![] },
- hir::TraitBoundModifier::None,
- )
+ let trait_ = clean_trait_ref_with_bindings(cx, trait_ref, bindings);
+ GenericBound::TraitBound(
+ PolyTrait { trait_, generic_params: vec![] },
+ hir::TraitBoundModifier::None,
+ )
+ }
+ hir::GenericBound::Trait(ref t, modifier) => {
+ // `T: ~const Destruct` is hidden because `T: Destruct` is a no-op.
+ if modifier == hir::TraitBoundModifier::MaybeConst
+ && cx.tcx.lang_items().destruct_trait() == Some(t.trait_ref.trait_def_id().unwrap())
+ {
+ return None;
}
- hir::GenericBound::Trait(ref t, modifier) => {
- // `T: ~const Destruct` is hidden because `T: Destruct` is a no-op.
- if modifier == hir::TraitBoundModifier::MaybeConst
- && cx.tcx.lang_items().destruct_trait()
- == Some(t.trait_ref.trait_def_id().unwrap())
- {
- return None;
- }
- GenericBound::TraitBound(t.clean(cx), modifier)
- }
- })
- }
+ GenericBound::TraitBound(clean_poly_trait_ref(t, cx), modifier)
+ }
+ })
}
pub(crate) fn clean_trait_ref_with_bindings<'tcx>(
cx: &mut DocContext<'tcx>,
trait_ref: ty::TraitRef<'tcx>,
- bindings: &[TypeBinding],
+ bindings: ThinVec<TypeBinding>,
) -> Path {
let kind = cx.tcx.def_kind(trait_ref.def_id).into();
if !matches!(kind, ItemType::Trait | ItemType::TraitAlias) {
span_bug!(cx.tcx.def_span(trait_ref.def_id), "`TraitRef` had unexpected kind {:?}", kind);
}
inline::record_extern_fqn(cx, trait_ref.def_id, kind);
- let path = external_path(cx, trait_ref.def_id, true, bindings.to_vec(), trait_ref.substs);
+ let path = external_path(cx, trait_ref.def_id, true, bindings, trait_ref.substs);
debug!("ty::TraitRef\n subst: {:?}\n", trait_ref.substs);
@@ -175,10 +174,8 @@ pub(crate) fn clean_trait_ref_with_bindings<'tcx>(
fn clean_poly_trait_ref_with_bindings<'tcx>(
cx: &mut DocContext<'tcx>,
poly_trait_ref: ty::PolyTraitRef<'tcx>,
- bindings: &[TypeBinding],
+ bindings: ThinVec<TypeBinding>,
) -> GenericBound {
- let poly_trait_ref = poly_trait_ref.lift_to_tcx(cx.tcx).unwrap();
-
// collect any late bound regions
let late_bound_regions: Vec<_> = cx
.tcx
@@ -200,16 +197,10 @@ fn clean_poly_trait_ref_with_bindings<'tcx>(
)
}
-impl<'tcx> Clean<'tcx, GenericBound> for ty::PolyTraitRef<'tcx> {
- fn clean(&self, cx: &mut DocContext<'tcx>) -> GenericBound {
- clean_poly_trait_ref_with_bindings(cx, *self, &[])
- }
-}
-
-fn clean_lifetime<'tcx>(lifetime: hir::Lifetime, cx: &mut DocContext<'tcx>) -> Lifetime {
+fn clean_lifetime<'tcx>(lifetime: &hir::Lifetime, cx: &mut DocContext<'tcx>) -> Lifetime {
let def = cx.tcx.named_region(lifetime.hir_id);
if let Some(
- rl::Region::EarlyBound(_, node_id)
+ rl::Region::EarlyBound(node_id)
| rl::Region::LateBound(_, _, node_id)
| rl::Region::Free(_, node_id),
) = def
@@ -257,7 +248,6 @@ pub(crate) fn clean_middle_region<'tcx>(region: ty::Region<'tcx>) -> Option<Life
| ty::ReFree(..)
| ty::ReVar(..)
| ty::RePlaceholder(..)
- | ty::ReEmpty(_)
| ty::ReErased => {
debug!("cannot clean region {:?}", region);
None
@@ -265,66 +255,71 @@ pub(crate) fn clean_middle_region<'tcx>(region: ty::Region<'tcx>) -> Option<Life
}
}
-impl<'tcx> Clean<'tcx, Option<WherePredicate>> for hir::WherePredicate<'tcx> {
- fn clean(&self, cx: &mut DocContext<'tcx>) -> Option<WherePredicate> {
- if !self.in_where_clause() {
- return None;
- }
- Some(match *self {
- hir::WherePredicate::BoundPredicate(ref wbp) => {
- let bound_params = wbp
- .bound_generic_params
- .iter()
- .map(|param| {
- // Higher-ranked params must be lifetimes.
- // Higher-ranked lifetimes can't have bounds.
- assert_matches!(
- param,
- hir::GenericParam { kind: hir::GenericParamKind::Lifetime { .. }, .. }
- );
- Lifetime(param.name.ident().name)
- })
- .collect();
- WherePredicate::BoundPredicate {
- ty: clean_ty(wbp.bounded_ty, cx),
- bounds: wbp.bounds.iter().filter_map(|x| x.clean(cx)).collect(),
- bound_params,
- }
+fn clean_where_predicate<'tcx>(
+ predicate: &hir::WherePredicate<'tcx>,
+ cx: &mut DocContext<'tcx>,
+) -> Option<WherePredicate> {
+ if !predicate.in_where_clause() {
+ return None;
+ }
+ Some(match *predicate {
+ hir::WherePredicate::BoundPredicate(ref wbp) => {
+ let bound_params = wbp
+ .bound_generic_params
+ .iter()
+ .map(|param| {
+ // Higher-ranked params must be lifetimes.
+ // Higher-ranked lifetimes can't have bounds.
+ assert_matches!(
+ param,
+ hir::GenericParam { kind: hir::GenericParamKind::Lifetime { .. }, .. }
+ );
+ Lifetime(param.name.ident().name)
+ })
+ .collect();
+ WherePredicate::BoundPredicate {
+ ty: clean_ty(wbp.bounded_ty, cx),
+ bounds: wbp.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
+ bound_params,
}
+ }
- hir::WherePredicate::RegionPredicate(ref wrp) => WherePredicate::RegionPredicate {
- lifetime: clean_lifetime(wrp.lifetime, cx),
- bounds: wrp.bounds.iter().filter_map(|x| x.clean(cx)).collect(),
- },
+ hir::WherePredicate::RegionPredicate(ref wrp) => WherePredicate::RegionPredicate {
+ lifetime: clean_lifetime(wrp.lifetime, cx),
+ bounds: wrp.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
+ },
- hir::WherePredicate::EqPredicate(ref wrp) => WherePredicate::EqPredicate {
- lhs: clean_ty(wrp.lhs_ty, cx),
- rhs: clean_ty(wrp.rhs_ty, cx).into(),
- },
- })
- }
+ hir::WherePredicate::EqPredicate(ref wrp) => WherePredicate::EqPredicate {
+ lhs: Box::new(clean_ty(wrp.lhs_ty, cx)),
+ rhs: Box::new(clean_ty(wrp.rhs_ty, cx).into()),
+ bound_params: Vec::new(),
+ },
+ })
}
-impl<'tcx> Clean<'tcx, Option<WherePredicate>> for ty::Predicate<'tcx> {
- fn clean(&self, cx: &mut DocContext<'tcx>) -> Option<WherePredicate> {
- let bound_predicate = self.kind();
- match bound_predicate.skip_binder() {
- ty::PredicateKind::Trait(pred) => {
- clean_poly_trait_predicate(bound_predicate.rebind(pred), cx)
- }
- ty::PredicateKind::RegionOutlives(pred) => clean_region_outlives_predicate(pred),
- ty::PredicateKind::TypeOutlives(pred) => clean_type_outlives_predicate(pred, cx),
- ty::PredicateKind::Projection(pred) => Some(clean_projection_predicate(pred, cx)),
- ty::PredicateKind::ConstEvaluatable(..) => None,
- ty::PredicateKind::WellFormed(..) => None,
-
- ty::PredicateKind::Subtype(..)
- | ty::PredicateKind::Coerce(..)
- | ty::PredicateKind::ObjectSafe(..)
- | ty::PredicateKind::ClosureKind(..)
- | ty::PredicateKind::ConstEquate(..)
- | ty::PredicateKind::TypeWellFormedFromEnv(..) => panic!("not user writable"),
+pub(crate) fn clean_predicate<'tcx>(
+ predicate: ty::Predicate<'tcx>,
+ cx: &mut DocContext<'tcx>,
+) -> Option<WherePredicate> {
+ let bound_predicate = predicate.kind();
+ match bound_predicate.skip_binder() {
+ ty::PredicateKind::Trait(pred) => {
+ clean_poly_trait_predicate(bound_predicate.rebind(pred), cx)
+ }
+ ty::PredicateKind::RegionOutlives(pred) => clean_region_outlives_predicate(pred),
+ ty::PredicateKind::TypeOutlives(pred) => clean_type_outlives_predicate(pred, cx),
+ ty::PredicateKind::Projection(pred) => {
+ Some(clean_projection_predicate(bound_predicate.rebind(pred), cx))
}
+ ty::PredicateKind::ConstEvaluatable(..) => None,
+ ty::PredicateKind::WellFormed(..) => None,
+
+ ty::PredicateKind::Subtype(..)
+ | ty::PredicateKind::Coerce(..)
+ | ty::PredicateKind::ObjectSafe(..)
+ | ty::PredicateKind::ClosureKind(..)
+ | ty::PredicateKind::ConstEquate(..)
+ | ty::PredicateKind::TypeWellFormedFromEnv(..) => panic!("not user writable"),
}
}
@@ -342,7 +337,7 @@ fn clean_poly_trait_predicate<'tcx>(
let poly_trait_ref = pred.map_bound(|pred| pred.trait_ref);
Some(WherePredicate::BoundPredicate {
ty: clean_middle_ty(poly_trait_ref.skip_binder().self_ty(), cx, None),
- bounds: vec![poly_trait_ref.clean(cx)],
+ bounds: vec![clean_poly_trait_ref_with_bindings(cx, poly_trait_ref, ThinVec::new())],
bound_params: Vec::new(),
})
}
@@ -352,10 +347,6 @@ fn clean_region_outlives_predicate<'tcx>(
) -> Option<WherePredicate> {
let ty::OutlivesPredicate(a, b) = pred;
- if a.is_empty() && b.is_empty() {
- return None;
- }
-
Some(WherePredicate::RegionPredicate {
lifetime: clean_middle_region(a).expect("failed to clean lifetime"),
bounds: vec![GenericBound::Outlives(
@@ -370,10 +361,6 @@ fn clean_type_outlives_predicate<'tcx>(
) -> Option<WherePredicate> {
let ty::OutlivesPredicate(ty, lt) = pred;
- if lt.is_empty() {
- return None;
- }
-
Some(WherePredicate::BoundPredicate {
ty: clean_middle_ty(ty, cx, None),
bounds: vec![GenericBound::Outlives(
@@ -384,9 +371,9 @@ fn clean_type_outlives_predicate<'tcx>(
}
fn clean_middle_term<'tcx>(term: ty::Term<'tcx>, cx: &mut DocContext<'tcx>) -> Term {
- match term {
- ty::Term::Ty(ty) => Term::Type(clean_middle_ty(ty, cx, None)),
- ty::Term::Const(c) => Term::Constant(clean_middle_const(c, cx)),
+ match term.unpack() {
+ ty::TermKind::Ty(ty) => Term::Type(clean_middle_ty(ty, cx, None)),
+ ty::TermKind::Const(c) => Term::Constant(clean_middle_const(c, cx)),
}
}
@@ -401,13 +388,25 @@ fn clean_hir_term<'tcx>(term: &hir::Term<'tcx>, cx: &mut DocContext<'tcx>) -> Te
}
fn clean_projection_predicate<'tcx>(
- pred: ty::ProjectionPredicate<'tcx>,
+ pred: ty::Binder<'tcx, ty::ProjectionPredicate<'tcx>>,
cx: &mut DocContext<'tcx>,
) -> WherePredicate {
- let ty::ProjectionPredicate { projection_ty, term } = pred;
+ let late_bound_regions = cx
+ .tcx
+ .collect_referenced_late_bound_regions(&pred)
+ .into_iter()
+ .filter_map(|br| match br {
+ ty::BrNamed(_, name) if name != kw::UnderscoreLifetime => Some(Lifetime(name)),
+ _ => None,
+ })
+ .collect();
+
+ let ty::ProjectionPredicate { projection_ty, term } = pred.skip_binder();
+
WherePredicate::EqPredicate {
- lhs: clean_projection(projection_ty, cx, None),
- rhs: clean_middle_term(term, cx),
+ lhs: Box::new(clean_projection(projection_ty, cx, None)),
+ rhs: Box::new(clean_middle_term(term, cx)),
+ bound_params: late_bound_regions,
}
}
@@ -416,8 +415,17 @@ fn clean_projection<'tcx>(
cx: &mut DocContext<'tcx>,
def_id: Option<DefId>,
) -> Type {
- let lifted = ty.lift_to_tcx(cx.tcx).unwrap();
- let trait_ = clean_trait_ref_with_bindings(cx, lifted.trait_ref(cx.tcx), &[]);
+ if cx.tcx.def_kind(ty.item_def_id) == DefKind::ImplTraitPlaceholder {
+ let bounds = cx
+ .tcx
+ .explicit_item_bounds(ty.item_def_id)
+ .iter()
+ .map(|(bound, _)| EarlyBinder(*bound).subst(cx.tcx, ty.substs))
+ .collect::<Vec<_>>();
+ return clean_middle_opaque_bounds(cx, bounds);
+ }
+
+ let trait_ = clean_trait_ref_with_bindings(cx, ty.trait_ref(cx.tcx), ThinVec::new());
let self_type = clean_middle_ty(ty.self_ty(), cx, None);
let self_def_id = if let Some(def_id) = def_id {
cx.tcx.opt_parent(def_id).or(Some(def_id))
@@ -425,12 +433,12 @@ fn clean_projection<'tcx>(
self_type.def_id(&cx.cache)
};
let should_show_cast = compute_should_show_cast(self_def_id, &trait_, &self_type);
- Type::QPath {
- assoc: Box::new(projection_to_path_segment(ty, cx)),
+ Type::QPath(Box::new(QPathData {
+ assoc: projection_to_path_segment(ty, cx),
should_show_cast,
- self_type: Box::new(self_type),
+ self_type,
trait_,
- }
+ }))
}
fn compute_should_show_cast(self_def_id: Option<DefId>, trait_: &Path, self_type: &Type) -> bool {
@@ -509,7 +517,7 @@ fn clean_generic_param<'tcx>(
.filter(|bp| !bp.in_where_clause)
.flat_map(|bp| bp.bounds)
.map(|bound| match bound {
- hir::GenericBound::Outlives(lt) => clean_lifetime(*lt, cx),
+ hir::GenericBound::Outlives(lt) => clean_lifetime(lt, cx),
_ => panic!(),
})
.collect()
@@ -524,7 +532,7 @@ fn clean_generic_param<'tcx>(
.bounds_for_param(did)
.filter(|bp| bp.origin != PredicateOrigin::WhereClause)
.flat_map(|bp| bp.bounds)
- .filter_map(|x| x.clean(cx))
+ .filter_map(|x| clean_generic_bound(x, cx))
.collect()
} else {
Vec::new()
@@ -572,65 +580,68 @@ fn is_elided_lifetime(param: &hir::GenericParam<'_>) -> bool {
matches!(param.kind, hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Elided })
}
-impl<'tcx> Clean<'tcx, Generics> for hir::Generics<'tcx> {
- fn clean(&self, cx: &mut DocContext<'tcx>) -> Generics {
- let impl_trait_params = self
- .params
- .iter()
- .filter(|param| is_impl_trait(param))
- .map(|param| {
- let param = clean_generic_param(cx, Some(self), param);
- match param.kind {
- GenericParamDefKind::Lifetime { .. } => unreachable!(),
- GenericParamDefKind::Type { did, ref bounds, .. } => {
- cx.impl_trait_bounds.insert(did.into(), bounds.clone());
- }
- GenericParamDefKind::Const { .. } => unreachable!(),
+pub(crate) fn clean_generics<'tcx>(
+ gens: &hir::Generics<'tcx>,
+ cx: &mut DocContext<'tcx>,
+) -> Generics {
+ let impl_trait_params = gens
+ .params
+ .iter()
+ .filter(|param| is_impl_trait(param))
+ .map(|param| {
+ let param = clean_generic_param(cx, Some(gens), param);
+ match param.kind {
+ GenericParamDefKind::Lifetime { .. } => unreachable!(),
+ GenericParamDefKind::Type { did, ref bounds, .. } => {
+ cx.impl_trait_bounds.insert(did.into(), bounds.clone());
}
- param
- })
- .collect::<Vec<_>>();
+ GenericParamDefKind::Const { .. } => unreachable!(),
+ }
+ param
+ })
+ .collect::<Vec<_>>();
- let mut params = Vec::with_capacity(self.params.len());
- for p in self.params.iter().filter(|p| !is_impl_trait(p) && !is_elided_lifetime(p)) {
- let p = clean_generic_param(cx, Some(self), p);
- params.push(p);
- }
- params.extend(impl_trait_params);
+ let mut params = Vec::with_capacity(gens.params.len());
+ for p in gens.params.iter().filter(|p| !is_impl_trait(p) && !is_elided_lifetime(p)) {
+ let p = clean_generic_param(cx, Some(gens), p);
+ params.push(p);
+ }
+ params.extend(impl_trait_params);
- let mut generics = Generics {
- params,
- where_predicates: self.predicates.iter().filter_map(|x| x.clean(cx)).collect(),
- };
+ let mut generics = Generics {
+ params,
+ where_predicates: gens
+ .predicates
+ .iter()
+ .filter_map(|x| clean_where_predicate(x, cx))
+ .collect(),
+ };
- // Some duplicates are generated for ?Sized bounds between type params and where
- // predicates. The point in here is to move the bounds definitions from type params
- // to where predicates when such cases occur.
- for where_pred in &mut generics.where_predicates {
- match *where_pred {
- WherePredicate::BoundPredicate {
- ty: Generic(ref name), ref mut bounds, ..
- } => {
- if bounds.is_empty() {
- for param in &mut generics.params {
- match param.kind {
- GenericParamDefKind::Lifetime { .. } => {}
- GenericParamDefKind::Type { bounds: ref mut ty_bounds, .. } => {
- if &param.name == name {
- mem::swap(bounds, ty_bounds);
- break;
- }
+ // Some duplicates are generated for ?Sized bounds between type params and where
+ // predicates. The point in here is to move the bounds definitions from type params
+ // to where predicates when such cases occur.
+ for where_pred in &mut generics.where_predicates {
+ match *where_pred {
+ WherePredicate::BoundPredicate { ty: Generic(ref name), ref mut bounds, .. } => {
+ if bounds.is_empty() {
+ for param in &mut generics.params {
+ match param.kind {
+ GenericParamDefKind::Lifetime { .. } => {}
+ GenericParamDefKind::Type { bounds: ref mut ty_bounds, .. } => {
+ if &param.name == name {
+ mem::swap(bounds, ty_bounds);
+ break;
}
- GenericParamDefKind::Const { .. } => {}
}
+ GenericParamDefKind::Const { .. } => {}
}
}
}
- _ => continue,
}
+ _ => continue,
}
- generics
}
+ generics
}
fn clean_ty_generics<'tcx>(
@@ -643,7 +654,7 @@ fn clean_ty_generics<'tcx>(
let mut impl_trait = BTreeMap::<ImplTraitParam, Vec<GenericBound>>::default();
// Bounds in the type_params and lifetimes fields are repeated in the
- // predicates field (see rustc_typeck::collect::ty_generics), so remove
+ // predicates field (see rustc_hir_analysis::collect::ty_generics), so remove
// them.
let stripped_params = gens
.params
@@ -666,8 +677,9 @@ fn clean_ty_generics<'tcx>(
})
.collect::<Vec<GenericParamDef>>();
- // param index -> [(DefId of trait, associated type name and generics, type)]
- let mut impl_trait_proj = FxHashMap::<u32, Vec<(DefId, PathSegment, Ty<'_>)>>::default();
+ // param index -> [(trait DefId, associated type name & generics, type, higher-ranked params)]
+ let mut impl_trait_proj =
+ FxHashMap::<u32, Vec<(DefId, PathSegment, Ty<'_>, Vec<GenericParamDef>)>>::default();
let where_predicates = preds
.predicates
@@ -701,7 +713,7 @@ fn clean_ty_generics<'tcx>(
if let Some(param_idx) = param_idx {
if let Some(b) = impl_trait.get_mut(&param_idx.into()) {
- let p: WherePredicate = p.clean(cx)?;
+ let p: WherePredicate = clean_predicate(*p, cx)?;
b.extend(
p.get_bounds()
@@ -726,6 +738,14 @@ fn clean_ty_generics<'tcx>(
trait_did,
name,
rhs.ty().unwrap(),
+ p.get_bound_params()
+ .into_iter()
+ .flatten()
+ .map(|param| GenericParamDef {
+ name: param.0,
+ kind: GenericParamDefKind::Lifetime { outlives: Vec::new() },
+ })
+ .collect(),
));
}
@@ -741,15 +761,19 @@ fn clean_ty_generics<'tcx>(
// Move trait bounds to the front.
bounds.sort_by_key(|b| !matches!(b, GenericBound::TraitBound(..)));
- if let crate::core::ImplTraitParam::ParamIndex(idx) = param {
- if let Some(proj) = impl_trait_proj.remove(&idx) {
- for (trait_did, name, rhs) in proj {
- let rhs = clean_middle_ty(rhs, cx, None);
- simplify::merge_bounds(cx, &mut bounds, trait_did, name, &Term::Type(rhs));
- }
+ let crate::core::ImplTraitParam::ParamIndex(idx) = param else { unreachable!() };
+ if let Some(proj) = impl_trait_proj.remove(&idx) {
+ for (trait_did, name, rhs, bound_params) in proj {
+ let rhs = clean_middle_ty(rhs, cx, None);
+ simplify::merge_bounds(
+ cx,
+ &mut bounds,
+ bound_params,
+ trait_did,
+ name,
+ &Term::Type(rhs),
+ );
}
- } else {
- unreachable!();
}
cx.impl_trait_bounds.insert(param, bounds);
@@ -758,33 +782,38 @@ fn clean_ty_generics<'tcx>(
// Now that `cx.impl_trait_bounds` is populated, we can process
// remaining predicates which could contain `impl Trait`.
let mut where_predicates =
- where_predicates.into_iter().flat_map(|p| p.clean(cx)).collect::<Vec<_>>();
+ where_predicates.into_iter().flat_map(|p| clean_predicate(*p, cx)).collect::<Vec<_>>();
- // Type parameters have a Sized bound by default unless removed with
- // ?Sized. Scan through the predicates and mark any type parameter with
- // a Sized bound, removing the bounds as we find them.
+ // In the surface language, all type parameters except `Self` have an
+ // implicit `Sized` bound unless removed with `?Sized`.
+ // However, in the list of where-predicates below, `Sized` appears like a
+ // normal bound: It's either present (the type is sized) or
+ // absent (the type is unsized) but never *maybe* (i.e. `?Sized`).
+ //
+ // This is unsuitable for rendering.
+ // Thus, as a first step remove all `Sized` bounds that should be implicit.
//
- // Note that associated types also have a sized bound by default, but we
+ // Note that associated types also have an implicit `Sized` bound but we
// don't actually know the set of associated types right here so that's
- // handled in cleaning associated types
+ // handled when cleaning associated types.
let mut sized_params = FxHashSet::default();
- where_predicates.retain(|pred| match *pred {
- WherePredicate::BoundPredicate { ty: Generic(ref g), ref bounds, .. } => {
- if bounds.iter().any(|b| b.is_sized_bound(cx)) {
- sized_params.insert(*g);
- false
- } else {
- true
- }
+ where_predicates.retain(|pred| {
+ if let WherePredicate::BoundPredicate { ty: Generic(g), bounds, .. } = pred
+ && *g != kw::SelfUpper
+ && bounds.iter().any(|b| b.is_sized_bound(cx))
+ {
+ sized_params.insert(*g);
+ false
+ } else {
+ true
}
- _ => true,
});
- // Run through the type parameters again and insert a ?Sized
- // unbound for any we didn't find to be Sized.
+ // As a final step, go through the type parameters again and insert a
+ // `?Sized` bound for each one we didn't find to be `Sized`.
for tp in &stripped_params {
- if matches!(tp.kind, types::GenericParamDefKind::Type { .. })
- && !sized_params.contains(&tp.name)
+ if let types::GenericParamDefKind::Type { .. } = tp.kind
+ && !sized_params.contains(&tp.name)
{
where_predicates.push(WherePredicate::BoundPredicate {
ty: Type::Generic(tp.name),
@@ -896,9 +925,12 @@ fn clean_function<'tcx>(
) -> Box<Function> {
let (generics, decl) = enter_impl_trait(cx, |cx| {
// NOTE: generics must be cleaned before args
- let generics = generics.clean(cx);
+ let generics = clean_generics(generics, cx);
let args = clean_args_from_types_and_body_id(cx, sig.decl.inputs, body_id);
- let decl = clean_fn_decl_with_args(cx, sig.decl, args);
+ let mut decl = clean_fn_decl_with_args(cx, sig.decl, args);
+ if sig.header.is_async() {
+ decl.output = decl.sugared_async_return_type();
+ }
(generics, decl)
});
Box::new(Function { decl, generics })
@@ -994,305 +1026,332 @@ fn clean_trait_ref<'tcx>(trait_ref: &hir::TraitRef<'tcx>, cx: &mut DocContext<'t
path
}
-impl<'tcx> Clean<'tcx, PolyTrait> for hir::PolyTraitRef<'tcx> {
- fn clean(&self, cx: &mut DocContext<'tcx>) -> PolyTrait {
- PolyTrait {
- trait_: clean_trait_ref(&self.trait_ref, cx),
- generic_params: self
- .bound_generic_params
- .iter()
- .filter(|p| !is_elided_lifetime(p))
- .map(|x| clean_generic_param(cx, None, x))
- .collect(),
- }
+fn clean_poly_trait_ref<'tcx>(
+ poly_trait_ref: &hir::PolyTraitRef<'tcx>,
+ cx: &mut DocContext<'tcx>,
+) -> PolyTrait {
+ PolyTrait {
+ trait_: clean_trait_ref(&poly_trait_ref.trait_ref, cx),
+ generic_params: poly_trait_ref
+ .bound_generic_params
+ .iter()
+ .filter(|p| !is_elided_lifetime(p))
+ .map(|x| clean_generic_param(cx, None, x))
+ .collect(),
}
}
-impl<'tcx> Clean<'tcx, Item> for hir::TraitItem<'tcx> {
- fn clean(&self, cx: &mut DocContext<'tcx>) -> Item {
- let local_did = self.def_id.to_def_id();
- cx.with_param_env(local_did, |cx| {
- let inner = match self.kind {
- hir::TraitItemKind::Const(ty, Some(default)) => AssocConstItem(
- clean_ty(ty, cx),
- ConstantKind::Local { def_id: local_did, body: default },
- ),
- hir::TraitItemKind::Const(ty, None) => TyAssocConstItem(clean_ty(ty, cx)),
- hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
- let m = clean_function(cx, sig, self.generics, body);
- MethodItem(m, None)
- }
- hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Required(names)) => {
- let (generics, decl) = enter_impl_trait(cx, |cx| {
- // NOTE: generics must be cleaned before args
- let generics = self.generics.clean(cx);
- let args = clean_args_from_types_and_names(cx, sig.decl.inputs, names);
- let decl = clean_fn_decl_with_args(cx, sig.decl, args);
- (generics, decl)
- });
- TyMethodItem(Box::new(Function { decl, generics }))
- }
- hir::TraitItemKind::Type(bounds, Some(default)) => {
- let generics = enter_impl_trait(cx, |cx| self.generics.clean(cx));
- let bounds = bounds.iter().filter_map(|x| x.clean(cx)).collect();
- let item_type = clean_middle_ty(hir_ty_to_ty(cx.tcx, default), cx, None);
- AssocTypeItem(
- Box::new(Typedef {
- type_: clean_ty(default, cx),
- generics,
- item_type: Some(item_type),
- }),
- bounds,
- )
- }
- hir::TraitItemKind::Type(bounds, None) => {
- let generics = enter_impl_trait(cx, |cx| self.generics.clean(cx));
- let bounds = bounds.iter().filter_map(|x| x.clean(cx)).collect();
- TyAssocTypeItem(Box::new(generics), bounds)
- }
- };
- let what_rustc_thinks =
- Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx);
- // Trait items always inherit the trait's visibility -- we don't want to show `pub`.
- Item { visibility: Inherited, ..what_rustc_thinks }
- })
- }
+fn clean_trait_item<'tcx>(trait_item: &hir::TraitItem<'tcx>, cx: &mut DocContext<'tcx>) -> Item {
+ let local_did = trait_item.owner_id.to_def_id();
+ cx.with_param_env(local_did, |cx| {
+ let inner = match trait_item.kind {
+ hir::TraitItemKind::Const(ty, Some(default)) => AssocConstItem(
+ clean_ty(ty, cx),
+ ConstantKind::Local { def_id: local_did, body: default },
+ ),
+ hir::TraitItemKind::Const(ty, None) => TyAssocConstItem(clean_ty(ty, cx)),
+ hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
+ let m = clean_function(cx, sig, trait_item.generics, body);
+ MethodItem(m, None)
+ }
+ hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Required(names)) => {
+ let (generics, decl) = enter_impl_trait(cx, |cx| {
+ // NOTE: generics must be cleaned before args
+ let generics = clean_generics(trait_item.generics, cx);
+ let args = clean_args_from_types_and_names(cx, sig.decl.inputs, names);
+ let decl = clean_fn_decl_with_args(cx, sig.decl, args);
+ (generics, decl)
+ });
+ TyMethodItem(Box::new(Function { decl, generics }))
+ }
+ hir::TraitItemKind::Type(bounds, Some(default)) => {
+ let generics = enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx));
+ let bounds = bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect();
+ let item_type = clean_middle_ty(hir_ty_to_ty(cx.tcx, default), cx, None);
+ AssocTypeItem(
+ Box::new(Typedef {
+ type_: clean_ty(default, cx),
+ generics,
+ item_type: Some(item_type),
+ }),
+ bounds,
+ )
+ }
+ hir::TraitItemKind::Type(bounds, None) => {
+ let generics = enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx));
+ let bounds = bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect();
+ TyAssocTypeItem(Box::new(generics), bounds)
+ }
+ };
+ let what_rustc_thinks =
+ Item::from_def_id_and_parts(local_did, Some(trait_item.ident.name), inner, cx);
+ // Trait items always inherit the trait's visibility -- we don't want to show `pub`.
+ Item { visibility: Inherited, ..what_rustc_thinks }
+ })
}
-impl<'tcx> Clean<'tcx, Item> for hir::ImplItem<'tcx> {
- fn clean(&self, cx: &mut DocContext<'tcx>) -> Item {
- let local_did = self.def_id.to_def_id();
- cx.with_param_env(local_did, |cx| {
- let inner = match self.kind {
- hir::ImplItemKind::Const(ty, expr) => {
- let default = ConstantKind::Local { def_id: local_did, body: expr };
- AssocConstItem(clean_ty(ty, cx), default)
- }
- hir::ImplItemKind::Fn(ref sig, body) => {
- let m = clean_function(cx, sig, self.generics, body);
- let defaultness = cx.tcx.impl_defaultness(self.def_id);
- MethodItem(m, Some(defaultness))
- }
- hir::ImplItemKind::TyAlias(hir_ty) => {
- let type_ = clean_ty(hir_ty, cx);
- let generics = self.generics.clean(cx);
- let item_type = clean_middle_ty(hir_ty_to_ty(cx.tcx, hir_ty), cx, None);
- AssocTypeItem(
- Box::new(Typedef { type_, generics, item_type: Some(item_type) }),
- Vec::new(),
- )
- }
- };
+pub(crate) fn clean_impl_item<'tcx>(
+ impl_: &hir::ImplItem<'tcx>,
+ cx: &mut DocContext<'tcx>,
+) -> Item {
+ let local_did = impl_.owner_id.to_def_id();
+ cx.with_param_env(local_did, |cx| {
+ let inner = match impl_.kind {
+ hir::ImplItemKind::Const(ty, expr) => {
+ let default = ConstantKind::Local { def_id: local_did, body: expr };
+ AssocConstItem(clean_ty(ty, cx), default)
+ }
+ hir::ImplItemKind::Fn(ref sig, body) => {
+ let m = clean_function(cx, sig, impl_.generics, body);
+ let defaultness = cx.tcx.impl_defaultness(impl_.owner_id);
+ MethodItem(m, Some(defaultness))
+ }
+ hir::ImplItemKind::Type(hir_ty) => {
+ let type_ = clean_ty(hir_ty, cx);
+ let generics = clean_generics(impl_.generics, cx);
+ let item_type = clean_middle_ty(hir_ty_to_ty(cx.tcx, hir_ty), cx, None);
+ AssocTypeItem(
+ Box::new(Typedef { type_, generics, item_type: Some(item_type) }),
+ Vec::new(),
+ )
+ }
+ };
- let mut what_rustc_thinks =
- Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx);
+ let mut what_rustc_thinks =
+ Item::from_def_id_and_parts(local_did, Some(impl_.ident.name), inner, cx);
- let impl_ref = cx.tcx.impl_trait_ref(cx.tcx.local_parent(self.def_id));
+ let impl_ref = cx.tcx.impl_trait_ref(cx.tcx.local_parent(impl_.owner_id.def_id));
- // Trait impl items always inherit the impl's visibility --
- // we don't want to show `pub`.
- if impl_ref.is_some() {
- what_rustc_thinks.visibility = Inherited;
- }
+ // Trait impl items always inherit the impl's visibility --
+ // we don't want to show `pub`.
+ if impl_ref.is_some() {
+ what_rustc_thinks.visibility = Inherited;
+ }
- what_rustc_thinks
- })
- }
+ what_rustc_thinks
+ })
}
-impl<'tcx> Clean<'tcx, Item> for ty::AssocItem {
- fn clean(&self, cx: &mut DocContext<'tcx>) -> Item {
- let tcx = cx.tcx;
- let kind = match self.kind {
- ty::AssocKind::Const => {
- let ty = clean_middle_ty(tcx.type_of(self.def_id), cx, Some(self.def_id));
+pub(crate) fn clean_middle_assoc_item<'tcx>(
+ assoc_item: &ty::AssocItem,
+ cx: &mut DocContext<'tcx>,
+) -> Item {
+ let tcx = cx.tcx;
+ let kind = match assoc_item.kind {
+ ty::AssocKind::Const => {
+ let ty = clean_middle_ty(tcx.type_of(assoc_item.def_id), cx, Some(assoc_item.def_id));
- let provided = match self.container {
- ty::ImplContainer => true,
- ty::TraitContainer => tcx.impl_defaultness(self.def_id).has_value(),
- };
- if provided {
- AssocConstItem(ty, ConstantKind::Extern { def_id: self.def_id })
- } else {
- TyAssocConstItem(ty)
- }
+ let provided = match assoc_item.container {
+ ty::ImplContainer => true,
+ ty::TraitContainer => tcx.impl_defaultness(assoc_item.def_id).has_value(),
+ };
+ if provided {
+ AssocConstItem(ty, ConstantKind::Extern { def_id: assoc_item.def_id })
+ } else {
+ TyAssocConstItem(ty)
}
- ty::AssocKind::Fn => {
- let generics = clean_ty_generics(
- cx,
- tcx.generics_of(self.def_id),
- tcx.explicit_predicates_of(self.def_id),
- );
- let sig = tcx.fn_sig(self.def_id);
- let mut decl = clean_fn_decl_from_did_and_sig(cx, Some(self.def_id), sig);
-
- if self.fn_has_self_parameter {
- let self_ty = match self.container {
- ty::ImplContainer => tcx.type_of(self.container_id(tcx)),
- ty::TraitContainer => tcx.types.self_param,
- };
- let self_arg_ty = sig.input(0).skip_binder();
- if self_arg_ty == self_ty {
- decl.inputs.values[0].type_ = Generic(kw::SelfUpper);
- } else if let ty::Ref(_, ty, _) = *self_arg_ty.kind() {
- if ty == self_ty {
- match decl.inputs.values[0].type_ {
- BorrowedRef { ref mut type_, .. } => {
- **type_ = Generic(kw::SelfUpper)
- }
- _ => unreachable!(),
- }
+ }
+ ty::AssocKind::Fn => {
+ let generics = clean_ty_generics(
+ cx,
+ tcx.generics_of(assoc_item.def_id),
+ tcx.explicit_predicates_of(assoc_item.def_id),
+ );
+ let sig = tcx.fn_sig(assoc_item.def_id);
+ let mut decl = clean_fn_decl_from_did_and_sig(cx, Some(assoc_item.def_id), sig);
+
+ if assoc_item.fn_has_self_parameter {
+ let self_ty = match assoc_item.container {
+ ty::ImplContainer => tcx.type_of(assoc_item.container_id(tcx)),
+ ty::TraitContainer => tcx.types.self_param,
+ };
+ let self_arg_ty = sig.input(0).skip_binder();
+ if self_arg_ty == self_ty {
+ decl.inputs.values[0].type_ = Generic(kw::SelfUpper);
+ } else if let ty::Ref(_, ty, _) = *self_arg_ty.kind() {
+ if ty == self_ty {
+ match decl.inputs.values[0].type_ {
+ BorrowedRef { ref mut type_, .. } => **type_ = Generic(kw::SelfUpper),
+ _ => unreachable!(),
}
}
}
+ }
- let provided = match self.container {
- ty::ImplContainer => true,
- ty::TraitContainer => self.defaultness(tcx).has_value(),
+ let provided = match assoc_item.container {
+ ty::ImplContainer => true,
+ ty::TraitContainer => assoc_item.defaultness(tcx).has_value(),
+ };
+ if provided {
+ let defaultness = match assoc_item.container {
+ ty::ImplContainer => Some(assoc_item.defaultness(tcx)),
+ ty::TraitContainer => None,
};
- if provided {
- let defaultness = match self.container {
- ty::ImplContainer => Some(self.defaultness(tcx)),
- ty::TraitContainer => None,
- };
- MethodItem(Box::new(Function { generics, decl }), defaultness)
- } else {
- TyMethodItem(Box::new(Function { generics, decl }))
- }
+ MethodItem(Box::new(Function { generics, decl }), defaultness)
+ } else {
+ TyMethodItem(Box::new(Function { generics, decl }))
}
- ty::AssocKind::Type => {
- let my_name = self.name;
-
- fn param_eq_arg(param: &GenericParamDef, arg: &GenericArg) -> bool {
- match (&param.kind, arg) {
- (GenericParamDefKind::Type { .. }, GenericArg::Type(Type::Generic(ty)))
- if *ty == param.name =>
- {
- true
- }
- (
- GenericParamDefKind::Lifetime { .. },
- GenericArg::Lifetime(Lifetime(lt)),
- ) if *lt == param.name => true,
- (GenericParamDefKind::Const { .. }, GenericArg::Const(c)) => {
- match &c.kind {
- ConstantKind::TyConst { expr } => expr == param.name.as_str(),
- _ => false,
- }
- }
- _ => false,
+ }
+ ty::AssocKind::Type => {
+ let my_name = assoc_item.name;
+
+ fn param_eq_arg(param: &GenericParamDef, arg: &GenericArg) -> bool {
+ match (&param.kind, arg) {
+ (GenericParamDefKind::Type { .. }, GenericArg::Type(Type::Generic(ty)))
+ if *ty == param.name =>
+ {
+ true
+ }
+ (GenericParamDefKind::Lifetime { .. }, GenericArg::Lifetime(Lifetime(lt)))
+ if *lt == param.name =>
+ {
+ true
}
+ (GenericParamDefKind::Const { .. }, GenericArg::Const(c)) => match &c.kind {
+ ConstantKind::TyConst { expr } => expr == param.name.as_str(),
+ _ => false,
+ },
+ _ => false,
}
+ }
- if let ty::TraitContainer = self.container {
- let bounds = tcx.explicit_item_bounds(self.def_id);
- let predicates = ty::GenericPredicates { parent: None, predicates: bounds };
- let mut generics =
- clean_ty_generics(cx, tcx.generics_of(self.def_id), predicates);
- // Filter out the bounds that are (likely?) directly attached to the associated type,
- // as opposed to being located in the where clause.
- let mut bounds = generics
- .where_predicates
- .drain_filter(|pred| match *pred {
- WherePredicate::BoundPredicate {
- ty: QPath { ref assoc, ref self_type, ref trait_, .. },
- ..
- } => {
- if assoc.name != my_name {
- return false;
- }
- if trait_.def_id() != self.container_id(tcx) {
- return false;
- }
- match **self_type {
- Generic(ref s) if *s == kw::SelfUpper => {}
- _ => return false,
- }
- match &assoc.args {
- GenericArgs::AngleBracketed { args, bindings } => {
- if !bindings.is_empty()
- || generics
- .params
- .iter()
- .zip(args.iter())
- .any(|(param, arg)| !param_eq_arg(param, arg))
- {
- return false;
- }
- }
- GenericArgs::Parenthesized { .. } => {
- // The only time this happens is if we're inside the rustdoc for Fn(),
- // which only has one associated type, which is not a GAT, so whatever.
+ if let ty::TraitContainer = assoc_item.container {
+ let bounds = tcx.explicit_item_bounds(assoc_item.def_id);
+ let predicates = tcx.explicit_predicates_of(assoc_item.def_id).predicates;
+ let predicates =
+ tcx.arena.alloc_from_iter(bounds.into_iter().chain(predicates).copied());
+ let mut generics = clean_ty_generics(
+ cx,
+ tcx.generics_of(assoc_item.def_id),
+ ty::GenericPredicates { parent: None, predicates },
+ );
+ // Move bounds that are (likely) directly attached to the associated type
+ // from the where clause to the associated type.
+ // There is no guarantee that this is what the user actually wrote but we have
+ // no way of knowing.
+ let mut bounds = generics
+ .where_predicates
+ .drain_filter(|pred| match *pred {
+ WherePredicate::BoundPredicate {
+ ty: QPath(box QPathData { ref assoc, ref self_type, ref trait_, .. }),
+ ..
+ } => {
+ if assoc.name != my_name {
+ return false;
+ }
+ if trait_.def_id() != assoc_item.container_id(tcx) {
+ return false;
+ }
+ match *self_type {
+ Generic(ref s) if *s == kw::SelfUpper => {}
+ _ => return false,
+ }
+ match &assoc.args {
+ GenericArgs::AngleBracketed { args, bindings } => {
+ if !bindings.is_empty()
+ || generics
+ .params
+ .iter()
+ .zip(args.iter())
+ .any(|(param, arg)| !param_eq_arg(param, arg))
+ {
+ return false;
}
}
- true
- }
- _ => false,
- })
- .flat_map(|pred| {
- if let WherePredicate::BoundPredicate { bounds, .. } = pred {
- bounds
- } else {
- unreachable!()
+ GenericArgs::Parenthesized { .. } => {
+ // The only time this happens is if we're inside the rustdoc for Fn(),
+ // which only has one associated type, which is not a GAT, so whatever.
+ }
}
- })
- .collect::<Vec<_>>();
- // Our Sized/?Sized bound didn't get handled when creating the generics
- // because we didn't actually get our whole set of bounds until just now
- // (some of them may have come from the trait). If we do have a sized
- // bound, we remove it, and if we don't then we add the `?Sized` bound
- // at the end.
- match bounds.iter().position(|b| b.is_sized_bound(cx)) {
- Some(i) => {
- bounds.remove(i);
+ true
}
- None => bounds.push(GenericBound::maybe_sized(cx)),
+ _ => false,
+ })
+ .flat_map(|pred| {
+ if let WherePredicate::BoundPredicate { bounds, .. } = pred {
+ bounds
+ } else {
+ unreachable!()
+ }
+ })
+ .collect::<Vec<_>>();
+ // Our Sized/?Sized bound didn't get handled when creating the generics
+ // because we didn't actually get our whole set of bounds until just now
+ // (some of them may have come from the trait). If we do have a sized
+ // bound, we remove it, and if we don't then we add the `?Sized` bound
+ // at the end.
+ match bounds.iter().position(|b| b.is_sized_bound(cx)) {
+ Some(i) => {
+ bounds.remove(i);
}
-
- if tcx.impl_defaultness(self.def_id).has_value() {
- AssocTypeItem(
- Box::new(Typedef {
- type_: clean_middle_ty(
- tcx.type_of(self.def_id),
- cx,
- Some(self.def_id),
- ),
- generics,
- // FIXME: should we obtain the Type from HIR and pass it on here?
- item_type: None,
- }),
- bounds,
- )
+ None => bounds.push(GenericBound::maybe_sized(cx)),
+ }
+ // Move bounds that are (likely) directly attached to the parameters of the
+ // (generic) associated type from the where clause to the respective parameter.
+ // There is no guarantee that this is what the user actually wrote but we have
+ // no way of knowing.
+ let mut where_predicates = Vec::new();
+ for mut pred in generics.where_predicates {
+ if let WherePredicate::BoundPredicate { ty: Generic(arg), bounds, .. } = &mut pred
+ && let Some(GenericParamDef {
+ kind: GenericParamDefKind::Type { bounds: param_bounds, .. },
+ ..
+ }) = generics.params.iter_mut().find(|param| &param.name == arg)
+ {
+ param_bounds.extend(mem::take(bounds));
} else {
- TyAssocTypeItem(Box::new(generics), bounds)
+ where_predicates.push(pred);
}
- } else {
- // FIXME: when could this happen? Associated items in inherent impls?
+ }
+ generics.where_predicates = where_predicates;
+
+ if tcx.impl_defaultness(assoc_item.def_id).has_value() {
AssocTypeItem(
Box::new(Typedef {
- type_: clean_middle_ty(tcx.type_of(self.def_id), cx, Some(self.def_id)),
- generics: Generics { params: Vec::new(), where_predicates: Vec::new() },
+ type_: clean_middle_ty(
+ tcx.type_of(assoc_item.def_id),
+ cx,
+ Some(assoc_item.def_id),
+ ),
+ generics,
+ // FIXME: should we obtain the Type from HIR and pass it on here?
item_type: None,
}),
- Vec::new(),
+ bounds,
)
+ } else {
+ TyAssocTypeItem(Box::new(generics), bounds)
}
+ } else {
+ // FIXME: when could this happen? Associated items in inherent impls?
+ AssocTypeItem(
+ Box::new(Typedef {
+ type_: clean_middle_ty(
+ tcx.type_of(assoc_item.def_id),
+ cx,
+ Some(assoc_item.def_id),
+ ),
+ generics: Generics { params: Vec::new(), where_predicates: Vec::new() },
+ item_type: None,
+ }),
+ Vec::new(),
+ )
}
- };
-
- let mut what_rustc_thinks =
- Item::from_def_id_and_parts(self.def_id, Some(self.name), kind, cx);
+ }
+ };
- let impl_ref = tcx.impl_trait_ref(tcx.parent(self.def_id));
+ let mut what_rustc_thinks =
+ Item::from_def_id_and_parts(assoc_item.def_id, Some(assoc_item.name), kind, cx);
- // Trait impl items always inherit the impl's visibility --
- // we don't want to show `pub`.
- if impl_ref.is_some() {
- what_rustc_thinks.visibility = Visibility::Inherited;
- }
+ let impl_ref = tcx.impl_trait_ref(tcx.parent(assoc_item.def_id));
- what_rustc_thinks
+ // Trait impl items always inherit the impl's visibility --
+ // we don't want to show `pub`.
+ if impl_ref.is_some() {
+ what_rustc_thinks.visibility = Visibility::Inherited;
}
+
+ what_rustc_thinks
}
fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type {
@@ -1328,18 +1387,18 @@ fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type
let trait_def = cx.tcx.associated_item(p.res.def_id()).container_id(cx.tcx);
let trait_ = self::Path {
res: Res::Def(DefKind::Trait, trait_def),
- segments: trait_segments.iter().map(|x| x.clean(cx)).collect(),
+ segments: trait_segments.iter().map(|x| clean_path_segment(x, cx)).collect(),
};
register_res(cx, trait_.res);
- let self_def_id = DefId::local(qself.hir_id.owner.local_def_index);
+ let self_def_id = DefId::local(qself.hir_id.owner.def_id.local_def_index);
let self_type = clean_ty(qself, cx);
let should_show_cast = compute_should_show_cast(Some(self_def_id), &trait_, &self_type);
- Type::QPath {
- assoc: Box::new(p.segments.last().expect("segments were empty").clean(cx)),
+ Type::QPath(Box::new(QPathData {
+ assoc: clean_path_segment(p.segments.last().expect("segments were empty"), cx),
should_show_cast,
- self_type: Box::new(self_type),
+ self_type,
trait_,
- }
+ }))
}
hir::QPath::TypeRelative(qself, segment) => {
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
@@ -1354,12 +1413,12 @@ fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type
let self_def_id = res.opt_def_id();
let self_type = clean_ty(qself, cx);
let should_show_cast = compute_should_show_cast(self_def_id, &trait_, &self_type);
- Type::QPath {
- assoc: Box::new(segment.clean(cx)),
+ Type::QPath(Box::new(QPathData {
+ assoc: clean_path_segment(segment, cx),
should_show_cast,
- self_type: Box::new(self_type),
+ self_type,
trait_,
- }
+ }))
}
hir::QPath::LangItem(..) => bug!("clean: requiring documentation of lang item"),
}
@@ -1372,7 +1431,7 @@ fn maybe_expand_private_type_alias<'tcx>(
let Res::Def(DefKind::TyAlias, def_id) = path.res else { return None };
// Substitute private type aliases
let def_id = def_id.as_local()?;
- let alias = if !cx.cache.access_levels.is_exported(def_id.to_def_id()) {
+ let alias = if !cx.cache.effective_visibilities.is_exported(def_id.to_def_id()) {
&cx.tcx.hir().expect_item(def_id).kind
} else {
return None;
@@ -1398,7 +1457,7 @@ fn maybe_expand_private_type_alias<'tcx>(
}
_ => None,
});
- if let Some(lt) = lifetime.cloned() {
+ if let Some(lt) = lifetime {
let lt_def_id = cx.tcx.hir().local_def_id(param.hir_id);
let cleaned =
if !lt.is_elided() { clean_lifetime(lt, cx) } else { Lifetime::elided() };
@@ -1486,7 +1545,7 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
// as we currently do not supply the parent generics to anonymous constants
// but do allow `ConstKind::Param`.
//
- // `const_eval_poly` tries to to first substitute generic parameters which
+ // `const_eval_poly` tries to first substitute generic parameters which
// results in an ICE while manually constructing the constant and using `eval`
// does nothing for `ConstKind::Param`.
let ct = ty::Const::from_anon_const(cx.tcx, def_id);
@@ -1498,30 +1557,29 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
Array(Box::new(clean_ty(ty, cx)), length)
}
TyKind::Tup(tys) => Tuple(tys.iter().map(|ty| clean_ty(ty, cx)).collect()),
- TyKind::OpaqueDef(item_id, _) => {
+ TyKind::OpaqueDef(item_id, _, _) => {
let item = cx.tcx.hir().item(item_id);
if let hir::ItemKind::OpaqueTy(ref ty) = item.kind {
- ImplTrait(ty.bounds.iter().filter_map(|x| x.clean(cx)).collect())
+ ImplTrait(ty.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect())
} else {
unreachable!()
}
}
TyKind::Path(_) => clean_qpath(ty, cx),
TyKind::TraitObject(bounds, ref lifetime, _) => {
- let bounds = bounds.iter().map(|bound| bound.clean(cx)).collect();
+ let bounds = bounds.iter().map(|bound| clean_poly_trait_ref(bound, cx)).collect();
let lifetime =
if !lifetime.is_elided() { Some(clean_lifetime(*lifetime, cx)) } else { None };
DynTrait(bounds, lifetime)
}
- TyKind::BareFn(barefn) => BareFunction(Box::new(barefn.clean(cx))),
+ TyKind::BareFn(barefn) => BareFunction(Box::new(clean_bare_fn_ty(barefn, cx))),
// Rustdoc handles `TyKind::Err`s by turning them into `Type::Infer`s.
- TyKind::Infer | TyKind::Err => Infer,
- TyKind::Typeof(..) => panic!("unimplemented type {:?}", ty.kind),
+ TyKind::Infer | TyKind::Err | TyKind::Typeof(..) => Infer,
}
}
/// Returns `None` if the type could not be normalized
-fn normalize<'tcx>(cx: &mut DocContext<'tcx>, ty: Ty<'_>) -> Option<Ty<'tcx>> {
+fn normalize<'tcx>(cx: &mut DocContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
// HACK: low-churn fix for #79459 while we wait for a trait normalization fix
if !cx.tcx.sess.opts.unstable_opts.normalize_docs {
return None;
@@ -1532,13 +1590,11 @@ fn normalize<'tcx>(cx: &mut DocContext<'tcx>, ty: Ty<'_>) -> Option<Ty<'tcx>> {
use rustc_middle::traits::ObligationCause;
// Try to normalize `<X as Y>::T` to a type
- let lifted = ty.lift_to_tcx(cx.tcx).unwrap();
- let normalized = cx.tcx.infer_ctxt().enter(|infcx| {
- infcx
- .at(&ObligationCause::dummy(), cx.param_env)
- .normalize(lifted)
- .map(|resolved| infcx.resolve_vars_if_possible(resolved.value))
- });
+ let infcx = cx.tcx.infer_ctxt().build();
+ let normalized = infcx
+ .at(&ObligationCause::dummy(), cx.param_env)
+ .normalize(ty)
+ .map(|resolved| infcx.resolve_vars_if_possible(resolved.value));
match normalized {
Ok(normalized_value) => {
debug!("normalized {:?} to {:?}", ty, normalized_value);
@@ -1552,12 +1608,12 @@ fn normalize<'tcx>(cx: &mut DocContext<'tcx>, ty: Ty<'_>) -> Option<Ty<'tcx>> {
}
pub(crate) fn clean_middle_ty<'tcx>(
- this: Ty<'tcx>,
+ ty: Ty<'tcx>,
cx: &mut DocContext<'tcx>,
def_id: Option<DefId>,
) -> Type {
- trace!("cleaning type: {:?}", this);
- let ty = normalize(cx, this).unwrap_or(this);
+ trace!("cleaning type: {:?}", ty);
+ let ty = normalize(cx, ty).unwrap_or(ty);
match *ty.kind() {
ty::Never => Primitive(PrimitiveType::Never),
ty::Bool => Primitive(PrimitiveType::Bool),
@@ -1567,8 +1623,7 @@ pub(crate) fn clean_middle_ty<'tcx>(
ty::Float(float_ty) => Primitive(float_ty.into()),
ty::Str => Primitive(PrimitiveType::Str),
ty::Slice(ty) => Slice(Box::new(clean_middle_ty(ty, cx, None))),
- ty::Array(ty, n) => {
- let mut n = cx.tcx.lift(n).expect("array lift failed");
+ ty::Array(ty, mut n) => {
n = n.eval(cx.tcx, ty::ParamEnv::reveal_all());
let n = print_const(cx, n);
Array(Box::new(clean_middle_ty(ty, cx, None)), n)
@@ -1580,7 +1635,6 @@ pub(crate) fn clean_middle_ty<'tcx>(
type_: Box::new(clean_middle_ty(ty, cx, None)),
},
ty::FnDef(..) | ty::FnPtr(_) => {
- let ty = cx.tcx.lift(this).expect("FnPtr lift failed");
let sig = ty.fn_sig(cx.tcx);
let decl = clean_fn_decl_from_did_and_sig(cx, None, sig);
BareFunction(Box::new(BareFunctionDecl {
@@ -1598,22 +1652,23 @@ pub(crate) fn clean_middle_ty<'tcx>(
AdtKind::Enum => ItemType::Enum,
};
inline::record_extern_fqn(cx, did, kind);
- let path = external_path(cx, did, false, vec![], substs);
+ let path = external_path(cx, did, false, ThinVec::new(), substs);
Type::Path { path }
}
ty::Foreign(did) => {
inline::record_extern_fqn(cx, did, ItemType::ForeignType);
- let path = external_path(cx, did, false, vec![], InternalSubsts::empty());
+ let path = external_path(cx, did, false, ThinVec::new(), InternalSubsts::empty());
Type::Path { path }
}
- ty::Dynamic(obj, ref reg) => {
+ ty::Dynamic(obj, ref reg, _) => {
// HACK: pick the first `did` as the `did` of the trait object. Someone
// might want to implement "native" support for marker-trait-only
// trait objects.
- let mut dids = obj.principal_def_id().into_iter().chain(obj.auto_traits());
- let did = dids
- .next()
- .unwrap_or_else(|| panic!("found trait object `{:?}` with no traits?", this));
+ let mut dids = obj.auto_traits();
+ let did = obj
+ .principal_def_id()
+ .or_else(|| dids.next())
+ .unwrap_or_else(|| panic!("found trait object `{:?}` with no traits?", ty));
let substs = match obj.principal() {
Some(principal) => principal.skip_binder().substs,
// marker traits have no substs.
@@ -1623,23 +1678,20 @@ pub(crate) fn clean_middle_ty<'tcx>(
inline::record_extern_fqn(cx, did, ItemType::Trait);
let lifetime = clean_middle_region(*reg);
- let mut bounds = vec![];
-
- for did in dids {
- let empty = cx.tcx.intern_substs(&[]);
- let path = external_path(cx, did, false, vec![], empty);
- inline::record_extern_fqn(cx, did, ItemType::Trait);
- let bound = PolyTrait { trait_: path, generic_params: Vec::new() };
- bounds.push(bound);
- }
+ let mut bounds = dids
+ .map(|did| {
+ let empty = cx.tcx.intern_substs(&[]);
+ let path = external_path(cx, did, false, ThinVec::new(), empty);
+ inline::record_extern_fqn(cx, did, ItemType::Trait);
+ PolyTrait { trait_: path, generic_params: Vec::new() }
+ })
+ .collect::<Vec<_>>();
- let mut bindings = vec![];
- for pb in obj.projection_bounds() {
- bindings.push(TypeBinding {
+ let bindings = obj
+ .projection_bounds()
+ .map(|pb| TypeBinding {
assoc: projection_to_path_segment(
pb.skip_binder()
- .lift_to_tcx(cx.tcx)
- .unwrap()
// HACK(compiler-errors): Doesn't actually matter what self
// type we put here, because we're only using the GAT's substs.
.with_self_ty(cx.tcx, cx.tcx.types.self_param)
@@ -1649,8 +1701,8 @@ pub(crate) fn clean_middle_ty<'tcx>(
kind: TypeBindingKind::Equality {
term: clean_middle_term(pb.skip_binder().term, cx),
},
- });
- }
+ })
+ .collect();
let path = external_path(cx, did, false, bindings, substs);
bounds.insert(0, PolyTrait { trait_: path, generic_params: Vec::new() });
@@ -1672,66 +1724,13 @@ pub(crate) fn clean_middle_ty<'tcx>(
ty::Opaque(def_id, substs) => {
// Grab the "TraitA + TraitB" from `impl TraitA + TraitB`,
// by looking up the bounds associated with the def_id.
- let substs = cx.tcx.lift(substs).expect("Opaque lift failed");
let bounds = cx
.tcx
.explicit_item_bounds(def_id)
.iter()
.map(|(bound, _)| EarlyBinder(*bound).subst(cx.tcx, substs))
.collect::<Vec<_>>();
- let mut regions = vec![];
- let mut has_sized = false;
- let mut bounds = bounds
- .iter()
- .filter_map(|bound| {
- let bound_predicate = bound.kind();
- let trait_ref = match bound_predicate.skip_binder() {
- ty::PredicateKind::Trait(tr) => bound_predicate.rebind(tr.trait_ref),
- ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(_ty, reg)) => {
- if let Some(r) = clean_middle_region(reg) {
- regions.push(GenericBound::Outlives(r));
- }
- return None;
- }
- _ => return None,
- };
-
- if let Some(sized) = cx.tcx.lang_items().sized_trait() {
- if trait_ref.def_id() == sized {
- has_sized = true;
- return None;
- }
- }
-
- let bindings: Vec<_> = bounds
- .iter()
- .filter_map(|bound| {
- if let ty::PredicateKind::Projection(proj) = bound.kind().skip_binder()
- {
- if proj.projection_ty.trait_ref(cx.tcx) == trait_ref.skip_binder() {
- Some(TypeBinding {
- assoc: projection_to_path_segment(proj.projection_ty, cx),
- kind: TypeBindingKind::Equality {
- term: clean_middle_term(proj.term, cx),
- },
- })
- } else {
- None
- }
- } else {
- None
- }
- })
- .collect();
-
- Some(clean_poly_trait_ref_with_bindings(cx, trait_ref, &bindings))
- })
- .collect::<Vec<_>>();
- bounds.extend(regions);
- if !has_sized && !bounds.is_empty() {
- bounds.insert(0, GenericBound::maybe_sized(cx));
- }
- ImplTrait(bounds)
+ clean_middle_opaque_bounds(cx, bounds)
}
ty::Closure(..) => panic!("Closure"),
@@ -1744,6 +1743,64 @@ pub(crate) fn clean_middle_ty<'tcx>(
}
}
+fn clean_middle_opaque_bounds<'tcx>(
+ cx: &mut DocContext<'tcx>,
+ bounds: Vec<ty::Predicate<'tcx>>,
+) -> Type {
+ let mut regions = vec![];
+ let mut has_sized = false;
+ let mut bounds = bounds
+ .iter()
+ .filter_map(|bound| {
+ let bound_predicate = bound.kind();
+ let trait_ref = match bound_predicate.skip_binder() {
+ ty::PredicateKind::Trait(tr) => bound_predicate.rebind(tr.trait_ref),
+ ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(_ty, reg)) => {
+ if let Some(r) = clean_middle_region(reg) {
+ regions.push(GenericBound::Outlives(r));
+ }
+ return None;
+ }
+ _ => return None,
+ };
+
+ if let Some(sized) = cx.tcx.lang_items().sized_trait() {
+ if trait_ref.def_id() == sized {
+ has_sized = true;
+ return None;
+ }
+ }
+
+ let bindings: ThinVec<_> = bounds
+ .iter()
+ .filter_map(|bound| {
+ if let ty::PredicateKind::Projection(proj) = bound.kind().skip_binder() {
+ if proj.projection_ty.trait_ref(cx.tcx) == trait_ref.skip_binder() {
+ Some(TypeBinding {
+ assoc: projection_to_path_segment(proj.projection_ty, cx),
+ kind: TypeBindingKind::Equality {
+ term: clean_middle_term(proj.term, cx),
+ },
+ })
+ } else {
+ None
+ }
+ } else {
+ None
+ }
+ })
+ .collect();
+
+ Some(clean_poly_trait_ref_with_bindings(cx, trait_ref, bindings))
+ })
+ .collect::<Vec<_>>();
+ bounds.extend(regions);
+ if !has_sized && !bounds.is_empty() {
+ bounds.insert(0, GenericBound::maybe_sized(cx));
+ }
+ ImplTrait(bounds)
+}
+
pub(crate) fn clean_field<'tcx>(field: &hir::FieldDef<'tcx>, cx: &mut DocContext<'tcx>) -> Item {
let def_id = cx.tcx.hir().local_def_id(field.hir_id).to_def_id();
clean_field_with_def_id(def_id, field.ident.name, clean_ty(field.ty, cx), cx)
@@ -1783,21 +1840,19 @@ fn is_field_vis_inherited(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
}
}
-pub(crate) fn clean_visibility(vis: ty::Visibility) -> Visibility {
+pub(crate) fn clean_visibility(vis: ty::Visibility<DefId>) -> Visibility {
match vis {
ty::Visibility::Public => Visibility::Public,
- // NOTE: this is not quite right: `ty` uses `Invisible` to mean 'private',
- // while rustdoc really does mean inherited. That means that for enum variants, such as
- // `pub enum E { V }`, `V` will be marked as `Public` by `ty`, but as `Inherited` by rustdoc.
- // Various parts of clean override `tcx.visibility` explicitly to make sure this distinction is captured.
- ty::Visibility::Invisible => Visibility::Inherited,
ty::Visibility::Restricted(module) => Visibility::Restricted(module),
}
}
pub(crate) fn clean_variant_def<'tcx>(variant: &ty::VariantDef, cx: &mut DocContext<'tcx>) -> Item {
let kind = match variant.ctor_kind {
- CtorKind::Const => Variant::CLike,
+ CtorKind::Const => Variant::CLike(match variant.discr {
+ ty::VariantDiscr::Explicit(def_id) => Some(Discriminant { expr: None, value: def_id }),
+ ty::VariantDiscr::Relative(_) => None,
+ }),
CtorKind::Fn => Variant::Tuple(
variant.fields.iter().map(|field| clean_middle_field(field, cx)).collect(),
),
@@ -1814,6 +1869,7 @@ pub(crate) fn clean_variant_def<'tcx>(variant: &ty::VariantDef, cx: &mut DocCont
fn clean_variant_data<'tcx>(
variant: &hir::VariantData<'tcx>,
+ disr_expr: &Option<hir::AnonConst>,
cx: &mut DocContext<'tcx>,
) -> Variant {
match variant {
@@ -1824,66 +1880,75 @@ fn clean_variant_data<'tcx>(
hir::VariantData::Tuple(..) => {
Variant::Tuple(variant.fields().iter().map(|x| clean_field(x, cx)).collect())
}
- hir::VariantData::Unit(..) => Variant::CLike,
+ hir::VariantData::Unit(..) => Variant::CLike(disr_expr.map(|disr| Discriminant {
+ expr: Some(disr.body),
+ value: cx.tcx.hir().local_def_id(disr.hir_id).to_def_id(),
+ })),
}
}
fn clean_path<'tcx>(path: &hir::Path<'tcx>, cx: &mut DocContext<'tcx>) -> Path {
- Path { res: path.res, segments: path.segments.iter().map(|x| x.clean(cx)).collect() }
+ Path {
+ res: path.res,
+ segments: path.segments.iter().map(|x| clean_path_segment(x, cx)).collect(),
+ }
}
-impl<'tcx> Clean<'tcx, GenericArgs> for hir::GenericArgs<'tcx> {
- fn clean(&self, cx: &mut DocContext<'tcx>) -> GenericArgs {
- if self.parenthesized {
- let output = clean_ty(self.bindings[0].ty(), cx);
- let output =
- if output != Type::Tuple(Vec::new()) { Some(Box::new(output)) } else { None };
- let inputs = self.inputs().iter().map(|x| clean_ty(x, cx)).collect::<Vec<_>>().into();
- GenericArgs::Parenthesized { inputs, output }
- } else {
- let args = self
- .args
- .iter()
- .map(|arg| match arg {
- hir::GenericArg::Lifetime(lt) if !lt.is_elided() => {
- GenericArg::Lifetime(clean_lifetime(*lt, cx))
- }
- hir::GenericArg::Lifetime(_) => GenericArg::Lifetime(Lifetime::elided()),
- hir::GenericArg::Type(ty) => GenericArg::Type(clean_ty(ty, cx)),
- hir::GenericArg::Const(ct) => GenericArg::Const(Box::new(clean_const(ct, cx))),
- hir::GenericArg::Infer(_inf) => GenericArg::Infer,
- })
- .collect::<Vec<_>>()
- .into();
- let bindings =
- self.bindings.iter().map(|x| clean_type_binding(x, cx)).collect::<Vec<_>>().into();
- GenericArgs::AngleBracketed { args, bindings }
- }
+fn clean_generic_args<'tcx>(
+ generic_args: &hir::GenericArgs<'tcx>,
+ cx: &mut DocContext<'tcx>,
+) -> GenericArgs {
+ if generic_args.parenthesized {
+ let output = clean_ty(generic_args.bindings[0].ty(), cx);
+ let output = if output != Type::Tuple(Vec::new()) { Some(Box::new(output)) } else { None };
+ let inputs =
+ generic_args.inputs().iter().map(|x| clean_ty(x, cx)).collect::<Vec<_>>().into();
+ GenericArgs::Parenthesized { inputs, output }
+ } else {
+ let args = generic_args
+ .args
+ .iter()
+ .map(|arg| match arg {
+ hir::GenericArg::Lifetime(lt) if !lt.is_elided() => {
+ GenericArg::Lifetime(clean_lifetime(*lt, cx))
+ }
+ hir::GenericArg::Lifetime(_) => GenericArg::Lifetime(Lifetime::elided()),
+ hir::GenericArg::Type(ty) => GenericArg::Type(clean_ty(ty, cx)),
+ hir::GenericArg::Const(ct) => GenericArg::Const(Box::new(clean_const(ct, cx))),
+ hir::GenericArg::Infer(_inf) => GenericArg::Infer,
+ })
+ .collect::<Vec<_>>()
+ .into();
+ let bindings =
+ generic_args.bindings.iter().map(|x| clean_type_binding(x, cx)).collect::<ThinVec<_>>();
+ GenericArgs::AngleBracketed { args, bindings }
}
}
-impl<'tcx> Clean<'tcx, PathSegment> for hir::PathSegment<'tcx> {
- fn clean(&self, cx: &mut DocContext<'tcx>) -> PathSegment {
- PathSegment { name: self.ident.name, args: self.args().clean(cx) }
- }
+fn clean_path_segment<'tcx>(
+ path: &hir::PathSegment<'tcx>,
+ cx: &mut DocContext<'tcx>,
+) -> PathSegment {
+ PathSegment { name: path.ident.name, args: clean_generic_args(path.args(), cx) }
}
-impl<'tcx> Clean<'tcx, BareFunctionDecl> for hir::BareFnTy<'tcx> {
- fn clean(&self, cx: &mut DocContext<'tcx>) -> BareFunctionDecl {
- let (generic_params, decl) = enter_impl_trait(cx, |cx| {
- // NOTE: generics must be cleaned before args
- let generic_params = self
- .generic_params
- .iter()
- .filter(|p| !is_elided_lifetime(p))
- .map(|x| clean_generic_param(cx, None, x))
- .collect();
- let args = clean_args_from_types_and_names(cx, self.decl.inputs, self.param_names);
- let decl = clean_fn_decl_with_args(cx, self.decl, args);
- (generic_params, decl)
- });
- BareFunctionDecl { unsafety: self.unsafety, abi: self.abi, decl, generic_params }
- }
+fn clean_bare_fn_ty<'tcx>(
+ bare_fn: &hir::BareFnTy<'tcx>,
+ cx: &mut DocContext<'tcx>,
+) -> BareFunctionDecl {
+ let (generic_params, decl) = enter_impl_trait(cx, |cx| {
+ // NOTE: generics must be cleaned before args
+ let generic_params = bare_fn
+ .generic_params
+ .iter()
+ .filter(|p| !is_elided_lifetime(p))
+ .map(|x| clean_generic_param(cx, None, x))
+ .collect();
+ let args = clean_args_from_types_and_names(cx, bare_fn.decl.inputs, bare_fn.param_names);
+ let decl = clean_fn_decl_with_args(cx, bare_fn.decl, args);
+ (generic_params, decl)
+ });
+ BareFunctionDecl { unsafety: bare_fn.unsafety, abi: bare_fn.abi, decl, generic_params }
}
fn clean_maybe_renamed_item<'tcx>(
@@ -1893,7 +1958,7 @@ fn clean_maybe_renamed_item<'tcx>(
) -> Vec<Item> {
use hir::ItemKind;
- let def_id = item.def_id.to_def_id();
+ let def_id = item.owner_id.to_def_id();
let mut name = renamed.unwrap_or_else(|| cx.tcx.hir().name(item.hir_id()));
cx.with_param_env(def_id, |cx| {
let kind = match item.kind {
@@ -1905,33 +1970,33 @@ fn clean_maybe_renamed_item<'tcx>(
kind: ConstantKind::Local { body: body_id, def_id },
}),
ItemKind::OpaqueTy(ref ty) => OpaqueTyItem(OpaqueTy {
- bounds: ty.bounds.iter().filter_map(|x| x.clean(cx)).collect(),
- generics: ty.generics.clean(cx),
+ bounds: ty.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
+ generics: clean_generics(ty.generics, cx),
}),
ItemKind::TyAlias(hir_ty, generics) => {
let rustdoc_ty = clean_ty(hir_ty, cx);
let ty = clean_middle_ty(hir_ty_to_ty(cx.tcx, hir_ty), cx, None);
TypedefItem(Box::new(Typedef {
type_: rustdoc_ty,
- generics: generics.clean(cx),
+ generics: clean_generics(generics, cx),
item_type: Some(ty),
}))
}
ItemKind::Enum(ref def, generics) => EnumItem(Enum {
- variants: def.variants.iter().map(|v| v.clean(cx)).collect(),
- generics: generics.clean(cx),
+ variants: def.variants.iter().map(|v| clean_variant(v, cx)).collect(),
+ generics: clean_generics(generics, cx),
}),
ItemKind::TraitAlias(generics, bounds) => TraitAliasItem(TraitAlias {
- generics: generics.clean(cx),
- bounds: bounds.iter().filter_map(|x| x.clean(cx)).collect(),
+ generics: clean_generics(generics, cx),
+ bounds: bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
}),
ItemKind::Union(ref variant_data, generics) => UnionItem(Union {
- generics: generics.clean(cx),
+ generics: clean_generics(generics, cx),
fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(),
}),
ItemKind::Struct(ref variant_data, generics) => StructItem(Struct {
struct_type: CtorKind::from_hir(variant_data),
- generics: generics.clean(cx),
+ generics: clean_generics(generics, cx),
fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(),
}),
ItemKind::Impl(impl_) => return clean_impl(impl_, item.hir_id(), cx),
@@ -1946,15 +2011,17 @@ fn clean_maybe_renamed_item<'tcx>(
})
}
ItemKind::Trait(_, _, generics, bounds, item_ids) => {
- let items =
- item_ids.iter().map(|ti| cx.tcx.hir().trait_item(ti.id).clean(cx)).collect();
+ let items = item_ids
+ .iter()
+ .map(|ti| clean_trait_item(cx.tcx.hir().trait_item(ti.id), cx))
+ .collect();
- TraitItem(Trait {
+ TraitItem(Box::new(Trait {
def_id,
items,
- generics: generics.clean(cx),
- bounds: bounds.iter().filter_map(|x| x.clean(cx)).collect(),
- })
+ generics: clean_generics(generics, cx),
+ bounds: bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
+ }))
}
ItemKind::ExternCrate(orig_name) => {
return clean_extern_crate(item, name, orig_name, cx);
@@ -1969,14 +2036,12 @@ fn clean_maybe_renamed_item<'tcx>(
})
}
-impl<'tcx> Clean<'tcx, Item> for hir::Variant<'tcx> {
- fn clean(&self, cx: &mut DocContext<'tcx>) -> Item {
- let kind = VariantItem(clean_variant_data(&self.data, cx));
- let what_rustc_thinks =
- Item::from_hir_id_and_parts(self.id, Some(self.ident.name), kind, cx);
- // don't show `pub` for variants, which are always public
- Item { visibility: Inherited, ..what_rustc_thinks }
- }
+fn clean_variant<'tcx>(variant: &hir::Variant<'tcx>, cx: &mut DocContext<'tcx>) -> Item {
+ let kind = VariantItem(clean_variant_data(&variant.data, &variant.disr_expr, cx));
+ let what_rustc_thinks =
+ Item::from_hir_id_and_parts(variant.id, Some(variant.ident.name), kind, cx);
+ // don't show `pub` for variants, which are always public
+ Item { visibility: Inherited, ..what_rustc_thinks }
}
fn clean_impl<'tcx>(
@@ -1987,8 +2052,11 @@ fn clean_impl<'tcx>(
let tcx = cx.tcx;
let mut ret = Vec::new();
let trait_ = impl_.of_trait.as_ref().map(|t| clean_trait_ref(t, cx));
- let items =
- impl_.items.iter().map(|ii| tcx.hir().impl_item(ii.id).clean(cx)).collect::<Vec<_>>();
+ let items = impl_
+ .items
+ .iter()
+ .map(|ii| clean_impl_item(tcx.hir().impl_item(ii.id), cx))
+ .collect::<Vec<_>>();
let def_id = tcx.hir().local_def_id(hir_id);
// If this impl block is an implementation of the Deref trait, then we
@@ -2005,7 +2073,7 @@ fn clean_impl<'tcx>(
let mut make_item = |trait_: Option<Path>, for_: Type, items: Vec<Item>| {
let kind = ImplItem(Box::new(Impl {
unsafety: impl_.unsafety,
- generics: impl_.generics.clean(cx),
+ generics: clean_generics(impl_.generics, cx),
trait_,
for_,
items,
@@ -2032,11 +2100,11 @@ fn clean_extern_crate<'tcx>(
cx: &mut DocContext<'tcx>,
) -> Vec<Item> {
// this is the ID of the `extern crate` statement
- let cnum = cx.tcx.extern_mod_stmt_cnum(krate.def_id).unwrap_or(LOCAL_CRATE);
+ let cnum = cx.tcx.extern_mod_stmt_cnum(krate.owner_id.def_id).unwrap_or(LOCAL_CRATE);
// this is the ID of the crate itself
let crate_def_id = cnum.as_def_id();
let attrs = cx.tcx.hir().attrs(krate.hir_id());
- let ty_vis = cx.tcx.visibility(krate.def_id);
+ let ty_vis = cx.tcx.visibility(krate.owner_id);
let please_inline = ty_vis.is_public()
&& attrs.iter().any(|a| {
a.has_name(sym::doc)
@@ -2054,7 +2122,7 @@ fn clean_extern_crate<'tcx>(
if let Some(items) = inline::try_inline(
cx,
cx.tcx.parent_module(krate.hir_id()).to_def_id(),
- Some(krate.def_id.to_def_id()),
+ Some(krate.owner_id.to_def_id()),
res,
name,
Some(attrs),
@@ -2090,11 +2158,11 @@ fn clean_use_statement<'tcx>(
return Vec::new();
}
- let visibility = cx.tcx.visibility(import.def_id);
+ let visibility = cx.tcx.visibility(import.owner_id);
let attrs = cx.tcx.hir().attrs(import.hir_id());
let inline_attr = attrs.lists(sym::doc).get_word_attr(sym::inline);
let pub_underscore = visibility.is_public() && name == kw::Underscore;
- let current_mod = cx.tcx.parent_module_from_def_id(import.def_id);
+ let current_mod = cx.tcx.parent_module_from_def_id(import.owner_id.def_id);
// The parent of the module in which this import resides. This
// is the same as `current_mod` if that's already the top
@@ -2106,8 +2174,8 @@ fn clean_use_statement<'tcx>(
// `pub(super)` or higher. If the current module is the top level
// module, there isn't really a parent module, which makes the results
// meaningless. In this case, we make sure the answer is `false`.
- let is_visible_from_parent_mod = visibility.is_accessible_from(parent_mod.to_def_id(), cx.tcx)
- && !current_mod.is_top_level_module();
+ let is_visible_from_parent_mod =
+ visibility.is_accessible_from(parent_mod, cx.tcx) && !current_mod.is_top_level_module();
if pub_underscore {
if let Some(ref inline) = inline_attr {
@@ -2165,7 +2233,7 @@ fn clean_use_statement<'tcx>(
}
if !denied {
let mut visited = FxHashSet::default();
- let import_def_id = import.def_id.to_def_id();
+ let import_def_id = import.owner_id.to_def_id();
if let Some(mut items) = inline::try_inline(
cx,
@@ -2188,7 +2256,7 @@ fn clean_use_statement<'tcx>(
Import::new_simple(name, resolve_use_source(cx, path), true)
};
- vec![Item::from_def_id_and_parts(import.def_id.to_def_id(), None, ImportItem(inner), cx)]
+ vec![Item::from_def_id_and_parts(import.owner_id.to_def_id(), None, ImportItem(inner), cx)]
}
fn clean_maybe_renamed_foreign_item<'tcx>(
@@ -2196,13 +2264,13 @@ fn clean_maybe_renamed_foreign_item<'tcx>(
item: &hir::ForeignItem<'tcx>,
renamed: Option<Symbol>,
) -> Item {
- let def_id = item.def_id.to_def_id();
+ let def_id = item.owner_id.to_def_id();
cx.with_param_env(def_id, |cx| {
let kind = match item.kind {
hir::ForeignItemKind::Fn(decl, names, generics) => {
let (generics, decl) = enter_impl_trait(cx, |cx| {
// NOTE: generics must be cleaned before args
- let generics = generics.clean(cx);
+ let generics = clean_generics(generics, cx);
let args = clean_args_from_types_and_names(cx, decl.inputs, names);
let decl = clean_fn_decl_with_args(cx, decl, args);
(generics, decl)
@@ -2229,13 +2297,16 @@ fn clean_type_binding<'tcx>(
cx: &mut DocContext<'tcx>,
) -> TypeBinding {
TypeBinding {
- assoc: PathSegment { name: type_binding.ident.name, args: type_binding.gen_args.clean(cx) },
+ assoc: PathSegment {
+ name: type_binding.ident.name,
+ args: clean_generic_args(type_binding.gen_args, cx),
+ },
kind: match type_binding.kind {
hir::TypeBindingKind::Equality { ref term } => {
TypeBindingKind::Equality { term: clean_hir_term(term, cx) }
}
hir::TypeBindingKind::Constraint { bounds } => TypeBindingKind::Constraint {
- bounds: bounds.iter().filter_map(|b| b.clean(cx)).collect(),
+ bounds: bounds.iter().filter_map(|b| clean_generic_bound(b, cx)).collect(),
},
},
}
diff --git a/src/librustdoc/clean/simplify.rs b/src/librustdoc/clean/simplify.rs
index af7813a77..1bcb9fcd5 100644
--- a/src/librustdoc/clean/simplify.rs
+++ b/src/librustdoc/clean/simplify.rs
@@ -14,7 +14,6 @@
use rustc_data_structures::fx::FxIndexMap;
use rustc_hir::def_id::DefId;
use rustc_middle::ty;
-use rustc_span::Symbol;
use crate::clean;
use crate::clean::GenericArgs as PP;
@@ -26,38 +25,37 @@ pub(crate) fn where_clauses(cx: &DocContext<'_>, clauses: Vec<WP>) -> Vec<WP> {
//
// We use `FxIndexMap` so that the insertion order is preserved to prevent messing up to
// the order of the generated bounds.
- let mut params: FxIndexMap<Symbol, (Vec<_>, Vec<_>)> = FxIndexMap::default();
+ let mut tybounds = FxIndexMap::default();
let mut lifetimes = Vec::new();
let mut equalities = Vec::new();
- let mut tybounds = Vec::new();
for clause in clauses {
match clause {
- WP::BoundPredicate { ty, bounds, bound_params } => match ty {
- clean::Generic(s) => {
- let (b, p) = params.entry(s).or_default();
- b.extend(bounds);
- p.extend(bound_params);
- }
- t => tybounds.push((t, (bounds, bound_params))),
- },
+ WP::BoundPredicate { ty, bounds, bound_params } => {
+ let (b, p): &mut (Vec<_>, Vec<_>) = tybounds.entry(ty).or_default();
+ b.extend(bounds);
+ p.extend(bound_params);
+ }
WP::RegionPredicate { lifetime, bounds } => {
lifetimes.push((lifetime, bounds));
}
- WP::EqPredicate { lhs, rhs } => equalities.push((lhs, rhs)),
+ WP::EqPredicate { lhs, rhs, bound_params } => equalities.push((lhs, rhs, bound_params)),
}
}
// Look for equality predicates on associated types that can be merged into
- // general bound predicates
- equalities.retain(|&(ref lhs, ref rhs)| {
- let Some((self_, trait_did, name)) = lhs.projection() else {
- return true;
- };
- let clean::Generic(generic) = self_ else { return true };
- let Some((bounds, _)) = params.get_mut(generic) else { return true };
-
- merge_bounds(cx, bounds, trait_did, name, rhs)
+ // general bound predicates.
+ equalities.retain(|&(ref lhs, ref rhs, ref bound_params)| {
+ let Some((ty, trait_did, name)) = lhs.projection() else { return true; };
+ let Some((bounds, _)) = tybounds.get_mut(ty) else { return true };
+ let bound_params = bound_params
+ .into_iter()
+ .map(|param| clean::GenericParamDef {
+ name: param.0,
+ kind: clean::GenericParamDefKind::Lifetime { outlives: Vec::new() },
+ })
+ .collect();
+ merge_bounds(cx, bounds, bound_params, trait_did, name, rhs)
});
// And finally, let's reassemble everything
@@ -65,23 +63,23 @@ pub(crate) fn where_clauses(cx: &DocContext<'_>, clauses: Vec<WP>) -> Vec<WP> {
clauses.extend(
lifetimes.into_iter().map(|(lt, bounds)| WP::RegionPredicate { lifetime: lt, bounds }),
);
- clauses.extend(params.into_iter().map(|(k, (bounds, params))| WP::BoundPredicate {
- ty: clean::Generic(k),
- bounds,
- bound_params: params,
- }));
clauses.extend(tybounds.into_iter().map(|(ty, (bounds, bound_params))| WP::BoundPredicate {
ty,
bounds,
bound_params,
}));
- clauses.extend(equalities.into_iter().map(|(lhs, rhs)| WP::EqPredicate { lhs, rhs }));
+ clauses.extend(equalities.into_iter().map(|(lhs, rhs, bound_params)| WP::EqPredicate {
+ lhs,
+ rhs,
+ bound_params,
+ }));
clauses
}
pub(crate) fn merge_bounds(
cx: &clean::DocContext<'_>,
bounds: &mut Vec<clean::GenericBound>,
+ mut bound_params: Vec<clean::GenericParamDef>,
trait_did: DefId,
assoc: clean::PathSegment,
rhs: &clean::Term,
@@ -98,6 +96,14 @@ pub(crate) fn merge_bounds(
return false;
}
let last = trait_ref.trait_.segments.last_mut().expect("segments were empty");
+
+ trait_ref.generic_params.append(&mut bound_params);
+ // Since the parameters (probably) originate from `tcx.collect_*_late_bound_regions` which
+ // returns a hash set, sort them alphabetically to guarantee a stable and deterministic
+ // output (and to fully deduplicate them).
+ trait_ref.generic_params.sort_unstable_by(|p, q| p.name.as_str().cmp(q.name.as_str()));
+ trait_ref.generic_params.dedup_by_key(|p| p.name);
+
match last.args {
PP::AngleBracketed { ref mut bindings, .. } => {
bindings.push(clean::TypeBinding {
diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs
index 0e6de842c..cd1f972dc 100644
--- a/src/librustdoc/clean/types.rs
+++ b/src/librustdoc/clean/types.rs
@@ -8,6 +8,7 @@ use std::sync::OnceLock as OnceCell;
use std::{cmp, fmt, iter};
use arrayvec::ArrayVec;
+use thin_vec::ThinVec;
use rustc_ast::attr;
use rustc_ast::util::comments::beautify_doc_string;
@@ -15,12 +16,12 @@ use rustc_ast::{self as ast, AttrStyle};
use rustc_attr::{ConstStability, Deprecation, Stability, StabilityLevel};
use rustc_const_eval::const_eval::is_unstable_const_fn;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
-use rustc_data_structures::thin_vec::ThinVec;
use rustc_hir as hir;
use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use rustc_hir::lang_items::LangItem;
use rustc_hir::{BodyId, Mutability};
+use rustc_hir_analysis::check::intrinsic::intrinsic_operation_unsafety;
use rustc_index::vec::IndexVec;
use rustc_middle::ty::fast_reject::SimplifiedType;
use rustc_middle::ty::{self, TyCtxt};
@@ -31,7 +32,6 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{self, FileName, Loc};
use rustc_target::abi::VariantIdx;
use rustc_target::spec::abi::Abi;
-use rustc_typeck::check::intrinsic::intrinsic_operation_unsafety;
use crate::clean::cfg::Cfg;
use crate::clean::clean_visibility;
@@ -119,7 +119,7 @@ pub(crate) struct Crate {
pub(crate) module: Item,
pub(crate) primitives: ThinVec<(DefId, PrimitiveType)>,
/// Only here so that they can be filtered through the rustdoc passes.
- pub(crate) external_traits: Rc<RefCell<FxHashMap<DefId, TraitWithExtraInfo>>>,
+ pub(crate) external_traits: Rc<RefCell<FxHashMap<DefId, Trait>>>,
}
impl Crate {
@@ -132,13 +132,6 @@ impl Crate {
}
}
-/// This struct is used to wrap additional information added by rustdoc on a `trait` item.
-#[derive(Clone, Debug)]
-pub(crate) struct TraitWithExtraInfo {
- pub(crate) trait_: Trait,
- pub(crate) is_notable: bool,
-}
-
#[derive(Copy, Clone, Debug)]
pub(crate) struct ExternalCrate {
pub(crate) crate_num: CrateNum,
@@ -247,13 +240,13 @@ impl ExternalCrate {
let item = tcx.hir().item(id);
match item.kind {
hir::ItemKind::Mod(_) => {
- as_keyword(Res::Def(DefKind::Mod, id.def_id.to_def_id()))
+ as_keyword(Res::Def(DefKind::Mod, id.owner_id.to_def_id()))
}
hir::ItemKind::Use(path, hir::UseKind::Single)
- if tcx.visibility(id.def_id).is_public() =>
+ if tcx.visibility(id.owner_id).is_public() =>
{
as_keyword(path.res.expect_non_local())
- .map(|(_, prim)| (id.def_id.to_def_id(), prim))
+ .map(|(_, prim)| (id.owner_id.to_def_id(), prim))
}
_ => None,
}
@@ -315,14 +308,14 @@ impl ExternalCrate {
let item = tcx.hir().item(id);
match item.kind {
hir::ItemKind::Mod(_) => {
- as_primitive(Res::Def(DefKind::Mod, id.def_id.to_def_id()))
+ as_primitive(Res::Def(DefKind::Mod, id.owner_id.to_def_id()))
}
hir::ItemKind::Use(path, hir::UseKind::Single)
- if tcx.visibility(id.def_id).is_public() =>
+ if tcx.visibility(id.owner_id).is_public() =>
{
as_primitive(path.res.expect_non_local()).map(|(_, prim)| {
// Pretend the primitive is local.
- (id.def_id.to_def_id(), prim)
+ (id.owner_id.to_def_id(), prim)
})
}
_ => None,
@@ -415,29 +408,28 @@ impl Item {
.unwrap_or(false)
}
- pub(crate) fn span(&self, tcx: TyCtxt<'_>) -> Span {
+ pub(crate) fn span(&self, tcx: TyCtxt<'_>) -> Option<Span> {
let kind = match &*self.kind {
ItemKind::StrippedItem(k) => k,
_ => &*self.kind,
};
match kind {
- ItemKind::ModuleItem(Module { span, .. }) => *span,
- ItemKind::ImplItem(box Impl { kind: ImplKind::Auto, .. }) => Span::dummy(),
+ ItemKind::ModuleItem(Module { span, .. }) => Some(*span),
+ ItemKind::ImplItem(box Impl { kind: ImplKind::Auto, .. }) => None,
ItemKind::ImplItem(box Impl { kind: ImplKind::Blanket(_), .. }) => {
if let ItemId::Blanket { impl_id, .. } = self.item_id {
- rustc_span(impl_id, tcx)
+ Some(rustc_span(impl_id, tcx))
} else {
panic!("blanket impl item has non-blanket ID")
}
}
- _ => {
- self.item_id.as_def_id().map(|did| rustc_span(did, tcx)).unwrap_or_else(Span::dummy)
- }
+ _ => self.item_id.as_def_id().map(|did| rustc_span(did, tcx)),
}
}
pub(crate) fn attr_span(&self, tcx: TyCtxt<'_>) -> rustc_span::Span {
- crate::passes::span_of_attrs(&self.attrs).unwrap_or_else(|| self.span(tcx).inner())
+ crate::passes::span_of_attrs(&self.attrs)
+ .unwrap_or_else(|| self.span(tcx).map_or(rustc_span::DUMMY_SP, |span| span.inner()))
}
/// Finds the `doc` attribute as a NameValue and returns the corresponding
@@ -483,7 +475,7 @@ impl Item {
cx: &mut DocContext<'_>,
cfg: Option<Arc<Cfg>>,
) -> Item {
- trace!("name={:?}, def_id={:?}", name, def_id);
+ trace!("name={:?}, def_id={:?} cfg={:?}", name, def_id, cfg);
// Primitives and Keywords are written in the source code as private modules.
// The modules need to be private so that nobody actually uses them, but the
@@ -511,7 +503,7 @@ impl Item {
.get(&self.item_id)
.map_or(&[][..], |v| v.as_slice())
.iter()
- .filter_map(|ItemLink { link: s, link_text, did, ref fragment }| {
+ .filter_map(|ItemLink { link: s, link_text, page_id: did, ref fragment }| {
debug!(?did);
if let Ok((mut href, ..)) = href(*did, cx) {
debug!(?href);
@@ -690,7 +682,7 @@ impl Item {
let abi = tcx.fn_sig(self.item_id.as_def_id().unwrap()).abi();
hir::FnHeader {
unsafety: if abi == Abi::RustIntrinsic {
- intrinsic_operation_unsafety(self.name.unwrap())
+ intrinsic_operation_unsafety(tcx, self.item_id.as_def_id().unwrap())
} else {
hir::Unsafety::Unsafe
},
@@ -728,7 +720,7 @@ pub(crate) enum ItemKind {
OpaqueTyItem(OpaqueTy),
StaticItem(Static),
ConstantItem(Constant),
- TraitItem(Trait),
+ TraitItem(Box<Trait>),
TraitAliasItem(TraitAlias),
ImplItem(Box<Impl>),
/// A required method in a trait declaration meaning it's only a function signature.
@@ -802,6 +794,31 @@ impl ItemKind {
| KeywordItem => [].iter(),
}
}
+
+ /// Returns `true` if this item does not appear inside an impl block.
+ pub(crate) fn is_non_assoc(&self) -> bool {
+ matches!(
+ self,
+ StructItem(_)
+ | UnionItem(_)
+ | EnumItem(_)
+ | TraitItem(_)
+ | ModuleItem(_)
+ | ExternCrateItem { .. }
+ | FunctionItem(_)
+ | TypedefItem(_)
+ | OpaqueTyItem(_)
+ | StaticItem(_)
+ | ConstantItem(_)
+ | TraitAliasItem(_)
+ | ForeignFunctionItem(_)
+ | ForeignStaticItem(_)
+ | ForeignTypeItem
+ | MacroItem(_)
+ | ProcMacroItem(_)
+ | PrimitiveItem(_)
+ )
+ }
}
#[derive(Clone, Debug)]
@@ -821,8 +838,6 @@ pub(crate) trait AttributesExt {
fn inner_docs(&self) -> bool;
- fn other_attrs(&self) -> Vec<ast::Attribute>;
-
fn cfg(&self, tcx: TyCtxt<'_>, hidden_cfg: &FxHashSet<Cfg>) -> Option<Arc<Cfg>>;
}
@@ -849,10 +864,6 @@ impl AttributesExt for [ast::Attribute] {
self.iter().find(|a| a.doc_str().is_some()).map_or(true, |a| a.style == AttrStyle::Inner)
}
- fn other_attrs(&self) -> Vec<ast::Attribute> {
- self.iter().filter(|attr| attr.doc_str().is_none()).cloned().collect()
- }
-
fn cfg(&self, tcx: TyCtxt<'_>, hidden_cfg: &FxHashSet<Cfg>) -> Option<Arc<Cfg>> {
let sess = tcx.sess;
let doc_cfg_active = tcx.features().doc_cfg;
@@ -1116,7 +1127,10 @@ pub(crate) struct ItemLink {
/// This may not be the same as `link` if there was a disambiguator
/// in an intra-doc link (e.g. \[`fn@f`\])
pub(crate) link_text: String,
- pub(crate) did: DefId,
+ /// The `DefId` of the Item whose **HTML Page** contains the item being
+ /// linked to. This will be different to `item_id` on item's that don't
+ /// have their own page, such as struct fields and enum variants.
+ pub(crate) page_id: DefId,
/// The url fragment to append to the link
pub(crate) fragment: Option<UrlFragment>,
}
@@ -1137,7 +1151,7 @@ pub struct RenderedLink {
#[derive(Clone, Debug, Default)]
pub(crate) struct Attributes {
pub(crate) doc_strings: Vec<DocFragment>,
- pub(crate) other_attrs: Vec<ast::Attribute>,
+ pub(crate) other_attrs: ast::AttrVec,
}
impl Attributes {
@@ -1180,7 +1194,7 @@ impl Attributes {
doc_only: bool,
) -> Attributes {
let mut doc_strings = Vec::new();
- let mut other_attrs = Vec::new();
+ let mut other_attrs = ast::AttrVec::new();
for (attr, parent_module) in attrs {
if let Some((doc_str, comment_kind)) = attr.doc_str_and_comment_kind() {
trace!("got doc_str={doc_str:?}");
@@ -1285,7 +1299,7 @@ impl GenericBound {
pub(crate) fn maybe_sized(cx: &mut DocContext<'_>) -> GenericBound {
let did = cx.tcx.require_lang_item(LangItem::Sized, None);
let empty = cx.tcx.intern_substs(&[]);
- let path = external_path(cx, did, false, vec![], empty);
+ let path = external_path(cx, did, false, ThinVec::new(), empty);
inline::record_extern_fqn(cx, did, ItemType::Trait);
GenericBound::TraitBound(
PolyTrait { trait_: path, generic_params: Vec::new() },
@@ -1336,7 +1350,7 @@ impl Lifetime {
pub(crate) enum WherePredicate {
BoundPredicate { ty: Type, bounds: Vec<GenericBound>, bound_params: Vec<Lifetime> },
RegionPredicate { lifetime: Lifetime, bounds: Vec<GenericBound> },
- EqPredicate { lhs: Type, rhs: Term },
+ EqPredicate { lhs: Box<Type>, rhs: Box<Term>, bound_params: Vec<Lifetime> },
}
impl WherePredicate {
@@ -1347,6 +1361,15 @@ impl WherePredicate {
_ => None,
}
}
+
+ pub(crate) fn get_bound_params(&self) -> Option<&[Lifetime]> {
+ match self {
+ Self::BoundPredicate { bound_params, .. } | Self::EqPredicate { bound_params, .. } => {
+ Some(bound_params)
+ }
+ _ => None,
+ }
+ }
}
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
@@ -1509,6 +1532,9 @@ impl Trait {
pub(crate) fn is_auto(&self, tcx: TyCtxt<'_>) -> bool {
tcx.trait_is_auto(self.def_id)
}
+ pub(crate) fn is_notable_trait(&self, tcx: TyCtxt<'_>) -> bool {
+ tcx.is_doc_notable_trait(self.def_id)
+ }
pub(crate) fn unsafety(&self, tcx: TyCtxt<'_>) -> hir::Unsafety {
tcx.trait_def(self.def_id).unsafety
}
@@ -1557,13 +1583,7 @@ pub(crate) enum Type {
BorrowedRef { lifetime: Option<Lifetime>, mutability: Mutability, type_: Box<Type> },
/// A qualified path to an associated item: `<Type as Trait>::Name`
- QPath {
- assoc: Box<PathSegment>,
- self_type: Box<Type>,
- /// FIXME: compute this field on demand.
- should_show_cast: bool,
- trait_: Path,
- },
+ QPath(Box<QPathData>),
/// A type that is inferred: `_`
Infer,
@@ -1661,8 +1681,8 @@ impl Type {
}
pub(crate) fn projection(&self) -> Option<(&Type, DefId, PathSegment)> {
- if let QPath { self_type, trait_, assoc, .. } = self {
- Some((self_type, trait_.def_id(), *assoc.clone()))
+ if let QPath(box QPathData { self_type, trait_, assoc, .. }) = self {
+ Some((self_type, trait_.def_id(), assoc.clone()))
} else {
None
}
@@ -1686,7 +1706,7 @@ impl Type {
Slice(..) => PrimitiveType::Slice,
Array(..) => PrimitiveType::Array,
RawPointer(..) => PrimitiveType::RawPointer,
- QPath { ref self_type, .. } => return self_type.inner_def_id(cache),
+ QPath(box QPathData { ref self_type, .. }) => return self_type.inner_def_id(cache),
Generic(_) | Infer | ImplTrait(_) => return None,
};
cache.and_then(|c| Primitive(t).def_id(c))
@@ -1700,6 +1720,15 @@ impl Type {
}
}
+#[derive(Clone, PartialEq, Eq, Debug, Hash)]
+pub(crate) struct QPathData {
+ pub assoc: PathSegment,
+ pub self_type: Type,
+ /// FIXME: compute this field on demand.
+ pub should_show_cast: bool,
+ pub trait_: Path,
+}
+
/// A primitive (aka, builtin) type.
///
/// This represents things like `i32`, `str`, etc.
@@ -2077,7 +2106,7 @@ impl Enum {
#[derive(Clone, Debug)]
pub(crate) enum Variant {
- CLike,
+ CLike(Option<Discriminant>),
Tuple(Vec<Item>),
Struct(VariantStruct),
}
@@ -2086,11 +2115,31 @@ impl Variant {
pub(crate) fn has_stripped_entries(&self) -> Option<bool> {
match *self {
Self::Struct(ref struct_) => Some(struct_.has_stripped_entries()),
- Self::CLike | Self::Tuple(_) => None,
+ Self::CLike(..) | Self::Tuple(_) => None,
}
}
}
+#[derive(Clone, Debug)]
+pub(crate) struct Discriminant {
+ // In the case of cross crate re-exports, we don't have the nessesary information
+ // to reconstruct the expression of the discriminant, only the value.
+ pub(super) expr: Option<BodyId>,
+ pub(super) value: DefId,
+}
+
+impl Discriminant {
+ /// Will be `None` in the case of cross-crate reexports, and may be
+ /// simplified
+ pub(crate) fn expr(&self, tcx: TyCtxt<'_>) -> Option<String> {
+ self.expr.map(|body| print_const_expr(tcx, body))
+ }
+ /// Will always be a machine readable number, without underscores or suffixes.
+ pub(crate) fn value(&self, tcx: TyCtxt<'_>) -> String {
+ print_evaluated_const(tcx, self.value, false).unwrap()
+ }
+}
+
/// Small wrapper around [`rustc_span::Span`] that adds helper methods
/// and enforces calling [`rustc_span::Span::source_callsite()`].
#[derive(Copy, Clone, Debug)]
@@ -2109,14 +2158,6 @@ impl Span {
self.0
}
- pub(crate) fn dummy() -> Self {
- Self(rustc_span::DUMMY_SP)
- }
-
- pub(crate) fn is_dummy(&self) -> bool {
- self.0.is_dummy()
- }
-
pub(crate) fn filename(&self, sess: &Session) -> FileName {
sess.source_map().span_to_filename(self.0)
}
@@ -2165,8 +2206,11 @@ impl Path {
/// Checks if this is a `T::Name` path for an associated type.
pub(crate) fn is_assoc_ty(&self) -> bool {
match self.res {
- Res::SelfTy { .. } if self.segments.len() != 1 => true,
- Res::Def(DefKind::TyParam, _) if self.segments.len() != 1 => true,
+ Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } | Res::Def(DefKind::TyParam, _)
+ if self.segments.len() != 1 =>
+ {
+ true
+ }
Res::Def(DefKind::AssocTy, _) => true,
_ => false,
}
@@ -2325,7 +2369,7 @@ impl ConstantKind {
match *self {
ConstantKind::TyConst { .. } | ConstantKind::Anonymous { .. } => None,
ConstantKind::Extern { def_id } | ConstantKind::Local { def_id, .. } => {
- print_evaluated_const(tcx, def_id)
+ print_evaluated_const(tcx, def_id, true)
}
}
}
@@ -2495,14 +2539,16 @@ impl SubstParam {
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
mod size_asserts {
use super::*;
- // These are in alphabetical order, which is easy to maintain.
- rustc_data_structures::static_assert_size!(Crate, 72); // frequently moved by-value
- rustc_data_structures::static_assert_size!(DocFragment, 32);
- rustc_data_structures::static_assert_size!(GenericArg, 80);
- rustc_data_structures::static_assert_size!(GenericArgs, 32);
- rustc_data_structures::static_assert_size!(GenericParamDef, 56);
- rustc_data_structures::static_assert_size!(Item, 56);
- rustc_data_structures::static_assert_size!(ItemKind, 112);
- rustc_data_structures::static_assert_size!(PathSegment, 40);
- rustc_data_structures::static_assert_size!(Type, 72);
+ use rustc_data_structures::static_assert_size;
+ // tidy-alphabetical-start
+ static_assert_size!(Crate, 72); // frequently moved by-value
+ static_assert_size!(DocFragment, 32);
+ static_assert_size!(GenericArg, 48);
+ static_assert_size!(GenericArgs, 32);
+ static_assert_size!(GenericParamDef, 56);
+ static_assert_size!(Item, 56);
+ static_assert_size!(ItemKind, 88);
+ static_assert_size!(PathSegment, 40);
+ static_assert_size!(Type, 48);
+ // tidy-alphabetical-end
}
diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs
index 43e71e90a..58767d3a4 100644
--- a/src/librustdoc/clean/utils.rs
+++ b/src/librustdoc/clean/utils.rs
@@ -2,17 +2,15 @@ use crate::clean::auto_trait::AutoTraitFinder;
use crate::clean::blanket_impl::BlanketImplFinder;
use crate::clean::render_macro_matchers::render_macro_matcher;
use crate::clean::{
- clean_middle_const, clean_middle_region, clean_middle_ty, inline, Clean, Crate, ExternalCrate,
- Generic, GenericArg, GenericArgs, ImportSource, Item, ItemKind, Lifetime, Path, PathSegment,
- Primitive, PrimitiveType, Type, TypeBinding, Visibility,
+ clean_doc_module, clean_middle_const, clean_middle_region, clean_middle_ty, inline, Crate,
+ ExternalCrate, Generic, GenericArg, GenericArgs, ImportSource, Item, ItemKind, Lifetime, Path,
+ PathSegment, Primitive, PrimitiveType, Type, TypeBinding, Visibility,
};
use crate::core::DocContext;
-use crate::formats::item_type::ItemType;
use crate::visit_lib::LibEmbargoVisitor;
use rustc_ast as ast;
use rustc_ast::tokenstream::TokenTree;
-use rustc_data_structures::thin_vec::ThinVec;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
@@ -23,6 +21,7 @@ use rustc_middle::ty::{self, DefIdTree, TyCtxt};
use rustc_span::symbol::{kw, sym, Symbol};
use std::fmt::Write as _;
use std::mem;
+use thin_vec::ThinVec;
#[cfg(test)]
mod tests;
@@ -37,7 +36,7 @@ pub(crate) fn krate(cx: &mut DocContext<'_>) -> Crate {
// Clean the crate, translating the entire librustc_ast AST to one that is
// understood by rustdoc.
- let mut module = module.clean(cx);
+ let mut module = clean_doc_module(&module, cx);
match *module.kind {
ItemKind::ModuleItem(ref module) => {
@@ -102,7 +101,7 @@ fn external_generic_args<'tcx>(
cx: &mut DocContext<'tcx>,
did: DefId,
has_self: bool,
- bindings: Vec<TypeBinding>,
+ bindings: ThinVec<TypeBinding>,
substs: SubstsRef<'tcx>,
) -> GenericArgs {
let args = substs_to_args(cx, substs, has_self);
@@ -112,7 +111,7 @@ fn external_generic_args<'tcx>(
// The trait's first substitution is the one after self, if there is one.
match substs.iter().nth(if has_self { 1 } else { 0 }).unwrap().expect_ty().kind() {
ty::Tuple(tys) => tys.iter().map(|t| clean_middle_ty(t, cx, None)).collect::<Vec<_>>().into(),
- _ => return GenericArgs::AngleBracketed { args: args.into(), bindings: bindings.into() },
+ _ => return GenericArgs::AngleBracketed { args: args.into(), bindings },
};
let output = None;
// FIXME(#20299) return type comes from a projection now
@@ -130,7 +129,7 @@ pub(super) fn external_path<'tcx>(
cx: &mut DocContext<'tcx>,
did: DefId,
has_self: bool,
- bindings: Vec<TypeBinding>,
+ bindings: ThinVec<TypeBinding>,
substs: SubstsRef<'tcx>,
) -> Path {
let def_kind = cx.tcx.def_kind(did);
@@ -234,15 +233,13 @@ pub(crate) fn name_from_pat(p: &hir::Pat<'_>) -> Symbol {
pub(crate) fn print_const(cx: &DocContext<'_>, n: ty::Const<'_>) -> String {
match n.kind() {
- ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs: _, promoted }) => {
- let mut s = if let Some(def) = def.as_local() {
+ ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs: _ }) => {
+ let s = if let Some(def) = def.as_local() {
print_const_expr(cx.tcx, cx.tcx.hir().body_owned_by(def.did))
} else {
inline::print_inlined_const(cx.tcx, def.did)
};
- if let Some(promoted) = promoted {
- s.push_str(&format!("::{:?}", promoted))
- }
+
s
}
_ => {
@@ -261,7 +258,11 @@ pub(crate) fn print_const(cx: &DocContext<'_>, n: ty::Const<'_>) -> String {
}
}
-pub(crate) fn print_evaluated_const(tcx: TyCtxt<'_>, def_id: DefId) -> Option<String> {
+pub(crate) fn print_evaluated_const(
+ tcx: TyCtxt<'_>,
+ def_id: DefId,
+ underscores_and_type: bool,
+) -> Option<String> {
tcx.const_eval_poly(def_id).ok().and_then(|val| {
let ty = tcx.type_of(def_id);
match (val, ty.kind()) {
@@ -269,7 +270,7 @@ pub(crate) fn print_evaluated_const(tcx: TyCtxt<'_>, def_id: DefId) -> Option<St
(ConstValue::Scalar(_), &ty::Adt(_, _)) => None,
(ConstValue::Scalar(_), _) => {
let const_ = mir::ConstantKind::from_value(val, ty);
- Some(print_const_with_custom_print_scalar(tcx, const_))
+ Some(print_const_with_custom_print_scalar(tcx, const_, underscores_and_type))
}
_ => None,
}
@@ -302,23 +303,35 @@ fn format_integer_with_underscore_sep(num: &str) -> String {
.collect()
}
-fn print_const_with_custom_print_scalar(tcx: TyCtxt<'_>, ct: mir::ConstantKind<'_>) -> String {
+fn print_const_with_custom_print_scalar<'tcx>(
+ tcx: TyCtxt<'tcx>,
+ ct: mir::ConstantKind<'tcx>,
+ underscores_and_type: bool,
+) -> String {
// Use a slightly different format for integer types which always shows the actual value.
// For all other types, fallback to the original `pretty_print_const`.
match (ct, ct.ty().kind()) {
(mir::ConstantKind::Val(ConstValue::Scalar(int), _), ty::Uint(ui)) => {
- format!("{}{}", format_integer_with_underscore_sep(&int.to_string()), ui.name_str())
+ if underscores_and_type {
+ format!("{}{}", format_integer_with_underscore_sep(&int.to_string()), ui.name_str())
+ } else {
+ int.to_string()
+ }
}
(mir::ConstantKind::Val(ConstValue::Scalar(int), _), ty::Int(i)) => {
- let ty = tcx.lift(ct.ty()).unwrap();
+ let ty = ct.ty();
let size = tcx.layout_of(ty::ParamEnv::empty().and(ty)).unwrap().size;
let data = int.assert_bits(size);
let sign_extended_data = size.sign_extend(data) as i128;
- format!(
- "{}{}",
- format_integer_with_underscore_sep(&sign_extended_data.to_string()),
- i.name_str()
- )
+ if underscores_and_type {
+ format!(
+ "{}{}",
+ format_integer_with_underscore_sep(&sign_extended_data.to_string()),
+ i.name_str()
+ )
+ } else {
+ sign_extended_data.to_string()
+ }
}
_ => ct.to_string(),
}
@@ -440,7 +453,9 @@ pub(crate) fn resolve_type(cx: &mut DocContext<'_>, path: Path) -> Type {
match path.res {
Res::PrimTy(p) => Primitive(PrimitiveType::from(p)),
- Res::SelfTy { .. } if path.segments.len() == 1 => Generic(kw::SelfUpper),
+ Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } if path.segments.len() == 1 => {
+ Generic(kw::SelfUpper)
+ }
Res::Def(DefKind::TyParam, _) if path.segments.len() == 1 => Generic(path.segments[0].name),
_ => {
let _ = register_res(cx, path.res);
@@ -475,38 +490,19 @@ pub(crate) fn register_res(cx: &mut DocContext<'_>, res: Res) -> DefId {
use DefKind::*;
debug!("register_res({:?})", res);
- let (did, kind) = match res {
- // These should be added to the cache using `record_extern_fqn`.
+ let (kind, did) = match res {
Res::Def(
kind @ (AssocTy | AssocFn | AssocConst | Variant | Fn | TyAlias | Enum | Trait | Struct
| Union | Mod | ForeignTy | Const | Static(_) | Macro(..) | TraitAlias),
- i,
- ) => (i, kind.into()),
- // This is part of a trait definition or trait impl; document the trait.
- Res::SelfTy { trait_: Some(trait_def_id), alias_to: _ } => (trait_def_id, ItemType::Trait),
- // This is an inherent impl or a type definition; it doesn't have its own page.
- Res::SelfTy { trait_: None, alias_to: Some((item_def_id, _)) } => return item_def_id,
- Res::SelfTy { trait_: None, alias_to: None }
- | Res::PrimTy(_)
- | Res::ToolMod
- | Res::SelfCtor(_)
- | Res::Local(_)
- | Res::NonMacroAttr(_)
- | Res::Err => return res.def_id(),
- Res::Def(
- TyParam | ConstParam | Ctor(..) | ExternCrate | Use | ForeignMod | AnonConst
- | InlineConst | OpaqueTy | Field | LifetimeParam | GlobalAsm | Impl | Closure
- | Generator,
- id,
- ) => return id,
+ did,
+ ) => (kind.into(), did),
+
+ _ => panic!("register_res: unexpected {:?}", res),
};
if did.is_local() {
return did;
}
inline::record_extern_fqn(cx, did, kind);
- if let ItemType::Trait = kind {
- inline::record_extern_trait(cx, did);
- }
did
}
diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs
index 8a8cc272e..67ea39fb9 100644
--- a/src/librustdoc/config.rs
+++ b/src/librustdoc/config.rs
@@ -142,8 +142,6 @@ pub(crate) struct Options {
// Options that alter generated documentation pages
/// Crate version to note on the sidebar of generated docs.
pub(crate) crate_version: Option<String>,
- /// Collected options specific to outputting final pages.
- pub(crate) render_options: RenderOptions,
/// The format that we output when rendering.
///
/// Currently used only for the `--show-coverage` option.
@@ -159,6 +157,10 @@ pub(crate) struct Options {
/// Configuration for scraping examples from the current crate. If this option is Some(..) then
/// the compiler will scrape examples and not generate documentation.
pub(crate) scrape_examples_options: Option<ScrapeExamplesOptions>,
+
+ /// Note: this field is duplicated in `RenderOptions` because it's useful
+ /// to have it in both places.
+ pub(crate) unstable_features: rustc_feature::UnstableFeatures,
}
impl fmt::Debug for Options {
@@ -194,7 +196,6 @@ impl fmt::Debug for Options {
.field("persist_doctests", &self.persist_doctests)
.field("show_coverage", &self.show_coverage)
.field("crate_version", &self.crate_version)
- .field("render_options", &self.render_options)
.field("runtool", &self.runtool)
.field("runtool_args", &self.runtool_args)
.field("enable-per-target-ignores", &self.enable_per_target_ignores)
@@ -202,6 +203,7 @@ impl fmt::Debug for Options {
.field("no_run", &self.no_run)
.field("nocapture", &self.nocapture)
.field("scrape_examples_options", &self.scrape_examples_options)
+ .field("unstable_features", &self.unstable_features)
.finish()
}
}
@@ -267,6 +269,8 @@ pub(crate) struct RenderOptions {
pub(crate) generate_redirect_map: bool,
/// Show the memory layout of types in the docs.
pub(crate) show_type_layout: bool,
+ /// Note: this field is duplicated in `Options` because it's useful to have
+ /// it in both places.
pub(crate) unstable_features: rustc_feature::UnstableFeatures,
pub(crate) emit: Vec<EmitType>,
/// If `true`, HTML source pages will generate links for items to their definition.
@@ -316,7 +320,7 @@ impl Options {
pub(crate) fn from_matches(
matches: &getopts::Matches,
args: Vec<String>,
- ) -> Result<Options, i32> {
+ ) -> Result<(Options, RenderOptions), i32> {
let args = &args[1..];
// Check for unstable options.
nightly_options::check_nightly_options(matches, &opts());
@@ -412,7 +416,13 @@ impl Options {
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 paths = match theme::load_css_paths(static_files::themes::LIGHT) {
+ Ok(p) => p,
+ Err(e) => {
+ diag.struct_err(&e.to_string()).emit();
+ return Err(1);
+ }
+ };
let mut errors = 0;
println!("rustdoc: [check-theme] Starting tests! (Ignoring all other arguments)");
@@ -547,7 +557,13 @@ impl Options {
let mut themes = Vec::new();
if matches.opt_present("theme") {
- let paths = theme::load_css_paths(static_files::themes::LIGHT.as_bytes());
+ let paths = match theme::load_css_paths(static_files::themes::LIGHT) {
+ Ok(p) => p,
+ Err(e) => {
+ diag.struct_err(&e.to_string()).emit();
+ return Err(1);
+ }
+ };
for (theme_file, theme_s) in
matches.opt_strs("theme").iter().map(|s| (PathBuf::from(&s), s.to_owned()))
@@ -698,7 +714,9 @@ impl Options {
let with_examples = matches.opt_strs("with-examples");
let call_locations = crate::scrape_examples::load_call_locations(with_examples, &diag)?;
- Ok(Options {
+ let unstable_features =
+ rustc_feature::UnstableFeatures::from_environment(crate_name.as_deref());
+ let options = Options {
input,
proc_macro_crate,
error_format,
@@ -732,42 +750,42 @@ impl Options {
run_check,
no_run,
nocapture,
- render_options: RenderOptions {
- output,
- external_html,
- id_map,
- playground_url,
- module_sorting,
- themes,
- extension_css,
- extern_html_root_urls,
- extern_html_root_takes_precedence,
- default_settings,
- resource_suffix,
- enable_minification,
- enable_index_page,
- index_page,
- static_root_path,
- markdown_no_toc,
- markdown_css,
- markdown_playground_url,
- document_private,
- document_hidden,
- generate_redirect_map,
- show_type_layout,
- unstable_features: rustc_feature::UnstableFeatures::from_environment(
- crate_name.as_deref(),
- ),
- emit,
- generate_link_to_definition,
- call_locations,
- no_emit_shared: false,
- },
crate_name,
output_format,
json_unused_externs,
scrape_examples_options,
- })
+ unstable_features,
+ };
+ let render_options = RenderOptions {
+ output,
+ external_html,
+ id_map,
+ playground_url,
+ module_sorting,
+ themes,
+ extension_css,
+ extern_html_root_urls,
+ extern_html_root_takes_precedence,
+ default_settings,
+ resource_suffix,
+ enable_minification,
+ enable_index_page,
+ index_page,
+ static_root_path,
+ markdown_no_toc,
+ markdown_css,
+ markdown_playground_url,
+ document_private,
+ document_hidden,
+ generate_redirect_map,
+ show_type_layout,
+ unstable_features,
+ emit,
+ generate_link_to_definition,
+ call_locations,
+ no_emit_shared: false,
+ };
+ Ok((options, render_options))
}
/// Returns `true` if the file given as `self.input` is a Markdown file.
diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs
index c48b25aea..3e5f42b7a 100644
--- a/src/librustdoc/core.rs
+++ b/src/librustdoc/core.rs
@@ -1,6 +1,7 @@
use rustc_ast::NodeId;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::sync::{self, Lrc};
+use rustc_data_structures::unord::UnordSet;
use rustc_errors::emitter::{Emitter, EmitterWriter};
use rustc_errors::json::JsonEmitter;
use rustc_feature::UnstableFeatures;
@@ -10,12 +11,10 @@ use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{HirId, Path, TraitCandidate};
use rustc_interface::interface;
use rustc_middle::hir::nested_filter;
-use rustc_middle::middle::privacy::AccessLevels;
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
use rustc_resolve as resolve;
use rustc_session::config::{self, CrateType, ErrorOutputType};
use rustc_session::lint;
-use rustc_session::DiagnosticOutput;
use rustc_session::Session;
use rustc_span::symbol::sym;
use rustc_span::{source_map, Span, Symbol};
@@ -26,7 +25,7 @@ use std::rc::Rc;
use std::sync::LazyLock;
use crate::clean::inline::build_external_trait;
-use crate::clean::{self, ItemId, TraitWithExtraInfo};
+use crate::clean::{self, ItemId};
use crate::config::{Options as RustdocOptions, OutputFormat, RenderOptions};
use crate::formats::cache::Cache;
use crate::passes::collect_intra_doc_links::PreprocessedMarkdownLink;
@@ -40,7 +39,6 @@ pub(crate) struct ResolverCaches {
/// Traits in scope for a given module.
/// See `collect_intra_doc_links::traits_implemented_by` for more details.
pub(crate) traits_in_scope: DefIdMap<Vec<TraitCandidate>>,
- pub(crate) all_traits: Option<Vec<DefId>>,
pub(crate) all_trait_impls: Option<Vec<DefId>>,
pub(crate) all_macro_rules: FxHashMap<Symbol, Res<NodeId>>,
}
@@ -59,7 +57,7 @@ pub(crate) struct DocContext<'tcx> {
/// Most of this logic is copied from rustc_lint::late.
pub(crate) param_env: ParamEnv<'tcx>,
/// Later on moved through `clean::Crate` into `cache`
- pub(crate) external_traits: Rc<RefCell<FxHashMap<DefId, clean::TraitWithExtraInfo>>>,
+ pub(crate) external_traits: Rc<RefCell<FxHashMap<DefId, clean::Trait>>>,
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
/// the same time.
pub(crate) active_extern_traits: FxHashSet<DefId>,
@@ -136,12 +134,6 @@ impl<'tcx> DocContext<'tcx> {
}
}
- pub(crate) fn with_all_traits(&mut self, f: impl FnOnce(&mut Self, &[DefId])) {
- let all_traits = self.resolver_caches.all_traits.take();
- f(self, all_traits.as_ref().expect("`all_traits` are already borrowed"));
- self.resolver_caches.all_traits = all_traits;
- }
-
pub(crate) fn with_all_trait_impls(&mut self, f: impl FnOnce(&mut Self, &[DefId])) {
let all_trait_impls = self.resolver_caches.all_trait_impls.take();
f(self, all_trait_impls.as_ref().expect("`all_trait_impls` are already borrowed"));
@@ -287,19 +279,17 @@ pub(crate) fn create_config(
output_file: None,
output_dir: None,
file_loader: None,
- diagnostic_output: DiagnosticOutput::Default,
lint_caps,
parse_sess_created: None,
register_lints: Some(Box::new(crate::lint::register_lints)),
override_queries: Some(|_sess, providers, _external_providers| {
// Most lints will require typechecking, so just don't run them.
providers.lint_mod = |_, _| {};
- // Prevent `rustc_typeck::check_crate` from calling `typeck` on all bodies.
+ // Prevent `rustc_hir_analysis::check_crate` from calling `typeck` on all bodies.
providers.typeck_item_bodies = |_, _| {};
// hack so that `used_trait_imports` won't try to call typeck
providers.used_trait_imports = |_, _| {
- static EMPTY_SET: LazyLock<FxHashSet<LocalDefId>> =
- LazyLock::new(FxHashSet::default);
+ static EMPTY_SET: LazyLock<UnordSet<LocalDefId>> = LazyLock::new(UnordSet::default);
&EMPTY_SET
};
// In case typeck does end up being called, don't ICE in case there were name resolution errors
@@ -356,17 +346,9 @@ pub(crate) fn run_global_ctxt(
});
rustc_passes::stability::check_unused_or_stable_features(tcx);
- let auto_traits = resolver_caches
- .all_traits
- .as_ref()
- .expect("`all_traits` are already borrowed")
- .iter()
- .copied()
- .filter(|&trait_def_id| tcx.trait_is_auto(trait_def_id))
- .collect();
- let access_levels = AccessLevels {
- map: tcx.privacy_access_levels(()).map.iter().map(|(k, v)| (k.to_def_id(), *v)).collect(),
- };
+ let auto_traits =
+ tcx.all_traits().filter(|&trait_def_id| tcx.trait_is_auto(trait_def_id)).collect();
+ let effective_visibilities = tcx.effective_visibilities(()).map_id(Into::into);
let mut ctxt = DocContext {
tcx,
@@ -379,7 +361,7 @@ pub(crate) fn run_global_ctxt(
impl_trait_bounds: Default::default(),
generated_synthetics: Default::default(),
auto_traits,
- cache: Cache::new(access_levels, render_options.document_private),
+ cache: Cache::new(effective_visibilities, render_options.document_private),
inlined: FxHashSet::default(),
output_format,
render_options,
@@ -391,9 +373,7 @@ pub(crate) fn run_global_ctxt(
// Note that in case of `#![no_core]`, the trait is not available.
if let Some(sized_trait_did) = ctxt.tcx.lang_items().sized_trait() {
let sized_trait = build_external_trait(&mut ctxt, sized_trait_did);
- ctxt.external_traits
- .borrow_mut()
- .insert(sized_trait_did, TraitWithExtraInfo { trait_: sized_trait, is_notable: false });
+ ctxt.external_traits.borrow_mut().insert(sized_trait_did, sized_trait);
}
debug!("crate: {:?}", tcx.hir().krate());
@@ -409,12 +389,8 @@ pub(crate) fn run_global_ctxt(
tcx.struct_lint_node(
crate::lint::MISSING_CRATE_LEVEL_DOCS,
DocContext::as_local_hir_id(tcx, krate.module.item_id).unwrap(),
- |lint| {
- let mut diag =
- lint.build("no documentation found for this crate's top-level module");
- diag.help(&help);
- diag.emit();
- },
+ "no documentation found for this crate's top-level module",
+ |lint| lint.help(help),
);
}
diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs
index 35964e3ba..db70029f6 100644
--- a/src/librustdoc/doctest.rs
+++ b/src/librustdoc/doctest.rs
@@ -14,11 +14,10 @@ use rustc_parse::maybe_new_parser_from_source_str;
use rustc_parse::parser::attr::InnerAttrPolicy;
use rustc_session::config::{self, CrateType, ErrorOutputType};
use rustc_session::parse::ParseSess;
-use rustc_session::{lint, DiagnosticOutput, Session};
+use rustc_session::{lint, Session};
use rustc_span::edition::Edition;
use rustc_span::source_map::SourceMap;
use rustc_span::symbol::sym;
-use rustc_span::Symbol;
use rustc_span::{BytePos, FileName, Pos, Span, DUMMY_SP};
use rustc_target::spec::TargetTriple;
use tempfile::Builder as TempFileBuilder;
@@ -80,7 +79,7 @@ pub(crate) fn run(options: RustdocOptions) -> Result<(), ErrorGuaranteed> {
lint_cap: Some(options.lint_cap.unwrap_or(lint::Forbid)),
cg: options.codegen_options.clone(),
externs: options.externs.clone(),
- unstable_features: options.render_options.unstable_features,
+ unstable_features: options.unstable_features,
actually_rustdoc: true,
edition: options.edition,
target_triple: options.target.clone(),
@@ -100,7 +99,6 @@ pub(crate) fn run(options: RustdocOptions) -> Result<(), ErrorGuaranteed> {
output_file: None,
output_dir: None,
file_loader: None,
- diagnostic_output: DiagnosticOutput::Default,
lint_caps,
parse_sess_created: None,
register_lints: Some(Box::new(crate::lint::register_lints)),
@@ -125,7 +123,7 @@ pub(crate) fn run(options: RustdocOptions) -> Result<(), ErrorGuaranteed> {
let opts = scrape_test_config(crate_attrs);
let enable_per_target_ignores = options.enable_per_target_ignores;
let mut collector = Collector::new(
- tcx.crate_name(LOCAL_CRATE),
+ tcx.crate_name(LOCAL_CRATE).to_string(),
options,
false,
opts,
@@ -210,12 +208,13 @@ pub(crate) fn run(options: RustdocOptions) -> Result<(), ErrorGuaranteed> {
pub(crate) fn run_tests(
mut test_args: Vec<String>,
nocapture: bool,
- tests: Vec<test::TestDescAndFn>,
+ mut tests: Vec<test::TestDescAndFn>,
) {
test_args.insert(0, "rustdoctest".to_string());
if nocapture {
test_args.push("--nocapture".to_string());
}
+ tests.sort_by(|a, b| a.desc.name.as_slice().cmp(&b.desc.name.as_slice()));
test::test_main(&test_args, tests, None);
}
@@ -909,7 +908,7 @@ pub(crate) struct Collector {
rustdoc_options: RustdocOptions,
use_headers: bool,
enable_per_target_ignores: bool,
- crate_name: Symbol,
+ crate_name: String,
opts: GlobalTestOptions,
position: Span,
source_map: Option<Lrc<SourceMap>>,
@@ -921,7 +920,7 @@ pub(crate) struct Collector {
impl Collector {
pub(crate) fn new(
- crate_name: Symbol,
+ crate_name: String,
rustdoc_options: RustdocOptions,
use_headers: bool,
opts: GlobalTestOptions,
@@ -984,7 +983,7 @@ impl Tester for Collector {
fn add_test(&mut self, test: String, config: LangString, line: usize) {
let filename = self.get_filename();
let name = self.generate_name(line, &filename);
- let crate_name = self.crate_name.to_string();
+ let crate_name = self.crate_name.clone();
let opts = self.opts.clone();
let edition = config.edition.unwrap_or(self.rustdoc_options.edition);
let rustdoc_options = self.rustdoc_options.clone();
@@ -1134,6 +1133,7 @@ impl Tester for Collector {
panic::resume_unwind(Box::new(()));
}
+ Ok(())
})),
});
}
@@ -1289,14 +1289,9 @@ impl<'a, 'hir, 'tcx> intravisit::Visitor<'hir> for HirCollector<'a, 'hir, 'tcx>
});
}
- fn visit_variant(
- &mut self,
- v: &'hir hir::Variant<'_>,
- g: &'hir hir::Generics<'_>,
- item_id: hir::HirId,
- ) {
+ fn visit_variant(&mut self, v: &'hir hir::Variant<'_>) {
self.visit_testable(v.ident.to_string(), v.id, v.span, |this| {
- intravisit::walk_variant(this, v, g, item_id);
+ intravisit::walk_variant(this, v);
});
}
diff --git a/src/librustdoc/fold.rs b/src/librustdoc/fold.rs
index 6b7e67e2c..c6f1f9de5 100644
--- a/src/librustdoc/fold.rs
+++ b/src/librustdoc/fold.rs
@@ -46,7 +46,7 @@ pub(crate) trait DocFolder: Sized {
let fields = fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
VariantItem(Variant::Tuple(fields))
}
- Variant::CLike => VariantItem(Variant::CLike),
+ Variant::CLike(disr) => VariantItem(Variant::CLike(disr)),
},
ExternCrateItem { src: _ }
| ImportItem(_)
@@ -94,7 +94,7 @@ pub(crate) trait DocFolder: Sized {
let external_traits = { std::mem::take(&mut *c.external_traits.borrow_mut()) };
for (k, mut v) in external_traits {
- v.trait_.items = v.trait_.items.into_iter().filter_map(|i| self.fold_item(i)).collect();
+ v.items = v.items.into_iter().filter_map(|i| self.fold_item(i)).collect();
c.external_traits.borrow_mut().insert(k, v);
}
diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs
index 2b2691e53..afe2264e8 100644
--- a/src/librustdoc/formats/cache.rs
+++ b/src/librustdoc/formats/cache.rs
@@ -2,9 +2,9 @@ use std::mem;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::def_id::{CrateNum, DefId};
-use rustc_middle::middle::privacy::AccessLevels;
+use rustc_middle::middle::privacy::EffectiveVisibilities;
use rustc_middle::ty::{self, TyCtxt};
-use rustc_span::{sym, Symbol};
+use rustc_span::Symbol;
use crate::clean::{self, types::ExternalLocation, ExternalCrate, ItemId, PrimitiveType};
use crate::core::DocContext;
@@ -62,7 +62,7 @@ pub(crate) struct Cache {
/// Implementations of a crate should inherit the documentation of the
/// parent trait if no extra documentation is specified, and default methods
/// should show up in documentation about trait implementations.
- pub(crate) traits: FxHashMap<DefId, clean::TraitWithExtraInfo>,
+ pub(crate) traits: FxHashMap<DefId, clean::Trait>,
/// When rendering traits, it's often useful to be able to list all
/// implementors of the trait, and this mapping is exactly, that: a mapping
@@ -77,8 +77,8 @@ pub(crate) struct Cache {
// Note that external items for which `doc(hidden)` applies to are shown as
// non-reachable while local items aren't. This is because we're reusing
- // the access levels from the privacy check pass.
- pub(crate) access_levels: AccessLevels<DefId>,
+ // the effective visibilities from the privacy check pass.
+ pub(crate) effective_visibilities: EffectiveVisibilities<DefId>,
/// The version of the crate being documented, if given from the `--crate-version` flag.
pub(crate) crate_version: Option<String>,
@@ -132,8 +132,11 @@ struct CacheBuilder<'a, 'tcx> {
}
impl Cache {
- pub(crate) fn new(access_levels: AccessLevels<DefId>, document_private: bool) -> Self {
- Cache { access_levels, document_private, ..Cache::default() }
+ pub(crate) fn new(
+ effective_visibilities: EffectiveVisibilities<DefId>,
+ document_private: bool,
+ ) -> Self {
+ Cache { effective_visibilities, document_private, ..Cache::default() }
}
/// Populates the `Cache` with more data. The returned `Crate` will be missing some data that was
@@ -225,12 +228,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
// Propagate a trait method's documentation to all implementors of the
// trait.
if let clean::TraitItem(ref t) = *item.kind {
- self.cache.traits.entry(item.item_id.expect_def_id()).or_insert_with(|| {
- clean::TraitWithExtraInfo {
- trait_: t.clone(),
- is_notable: item.attrs.has_doc_flag(sym::notable_trait),
- }
- });
+ self.cache.traits.entry(item.item_id.expect_def_id()).or_insert_with(|| (**t).clone());
}
// Collect all the implementors of traits.
@@ -386,7 +384,10 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
// paths map if there was already an entry present and we're
// not a public item.
if !self.cache.paths.contains_key(&item.item_id.expect_def_id())
- || self.cache.access_levels.is_public(item.item_id.expect_def_id())
+ || self
+ .cache
+ .effective_visibilities
+ .is_directly_public(item.item_id.expect_def_id())
{
self.cache.paths.insert(
item.item_id.expect_def_id(),
diff --git a/src/librustdoc/formats/item_type.rs b/src/librustdoc/formats/item_type.rs
index 0a7ee2005..f21e60a64 100644
--- a/src/librustdoc/formats/item_type.rs
+++ b/src/librustdoc/formats/item_type.rs
@@ -135,6 +135,7 @@ impl From<DefKind> for ItemType {
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::OpaqueTy
+ | DefKind::ImplTraitPlaceholder
| DefKind::Field
| DefKind::LifetimeParam
| DefKind::GlobalAsm
diff --git a/src/librustdoc/formats/renderer.rs b/src/librustdoc/formats/renderer.rs
index 62ba984ac..6f9cc0266 100644
--- a/src/librustdoc/formats/renderer.rs
+++ b/src/librustdoc/formats/renderer.rs
@@ -58,7 +58,7 @@ pub(crate) fn run_format<'tcx, T: FormatRenderer<'tcx>>(
let emit_crate = options.should_emit_crate();
let (mut format_renderer, krate) = prof
- .extra_verbose_generic_activity("create_renderer", T::descr())
+ .verbose_generic_activity_with_arg("create_renderer", T::descr())
.run(|| T::init(krate, options, cache, tcx))?;
if !emit_crate {
@@ -92,6 +92,6 @@ pub(crate) fn run_format<'tcx, T: FormatRenderer<'tcx>>(
.run(|| cx.item(item))?;
}
}
- prof.extra_verbose_generic_activity("renderer_after_krate", T::descr())
+ prof.verbose_generic_activity_with_arg("renderer_after_krate", T::descr())
.run(|| format_renderer.after_krate())
}
diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs
index 36a47b05c..92e7f2739 100644
--- a/src/librustdoc/html/format.rs
+++ b/src/librustdoc/html/format.rs
@@ -152,7 +152,7 @@ impl Buffer {
}
}
-fn comma_sep<T: fmt::Display>(
+pub(crate) fn comma_sep<T: fmt::Display>(
items: impl Iterator<Item = T>,
space_after_comma: bool,
) -> impl fmt::Display {
@@ -331,7 +331,8 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
bounds_display.truncate(bounds_display.len() - " + ".len());
write!(f, "{}: {bounds_display}", lifetime.print())
}
- clean::WherePredicate::EqPredicate { lhs, rhs } => {
+ // FIXME(fmease): Render bound params.
+ clean::WherePredicate::EqPredicate { lhs, rhs, bound_params: _ } => {
if f.alternate() {
write!(f, "{:#} == {:#}", lhs.print(cx), rhs.print(cx))
} else {
@@ -349,8 +350,7 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
let where_preds = comma_sep(where_predicates, false);
let clause = if f.alternate() {
if ending == Ending::Newline {
- // add a space so stripping <br> tags and breaking spaces still renders properly
- format!(" where{where_preds}, ")
+ format!(" where{where_preds},")
} else {
format!(" where{where_preds}")
}
@@ -364,20 +364,16 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
if ending == Ending::Newline {
let mut clause = "&nbsp;".repeat(indent.saturating_sub(1));
- // add a space so stripping <br> tags and breaking spaces still renders properly
- write!(
- clause,
- " <span class=\"where fmt-newline\">where{where_preds},&nbsp;</span>"
- )?;
+ write!(clause, "<span class=\"where fmt-newline\">where{where_preds},</span>")?;
clause
} else {
// insert a <br> tag after a single space but before multiple spaces at the start
if indent == 0 {
- format!(" <br><span class=\"where\">where{where_preds}</span>")
+ format!("<br><span class=\"where\">where{where_preds}</span>")
} else {
let mut clause = br_with_padding;
- clause.truncate(clause.len() - 5 * "&nbsp;".len());
- write!(clause, " <span class=\"where\">where{where_preds}</span>")?;
+ clause.truncate(clause.len() - 4 * "&nbsp;".len());
+ write!(clause, "<span class=\"where\">where{where_preds}</span>")?;
clause
}
}
@@ -592,7 +588,7 @@ fn generate_macro_def_id_path(
}
})
.collect();
- let relative = fqp.iter().map(|elem| elem.to_string());
+ let mut relative = fqp.iter().map(|elem| elem.to_string());
let cstore = CStore::from_tcx(tcx);
// We need this to prevent a `panic` when this function is used from intra doc links...
if !cstore.has_crate_data(def_id.krate) {
@@ -612,11 +608,11 @@ fn generate_macro_def_id_path(
let mut path = if is_macro_2 {
once(crate_name.clone()).chain(relative).collect()
} else {
- vec![crate_name.clone(), relative.last().unwrap()]
+ vec![crate_name.clone(), relative.next_back().unwrap()]
};
if path.len() < 2 {
// The minimum we can have is the crate name followed by the macro name. If shorter, then
- // it means that that `relative` was empty, which is an error.
+ // it means that `relative` was empty, which is an error.
debug!("macro path cannot be empty!");
return Err(HrefError::NotInExternalCache);
}
@@ -663,7 +659,7 @@ pub(crate) fn href_with_root_path(
}
if !did.is_local()
- && !cache.access_levels.is_public(did)
+ && !cache.effective_visibilities.is_directly_public(did)
&& !cache.document_private
&& !cache.primitive_locations.values().any(|&id| id == did)
{
@@ -1015,15 +1011,25 @@ fn fmt_type<'cx>(
write!(f, "]")
}
},
- clean::Array(ref t, ref n) => {
- primitive_link(f, PrimitiveType::Array, "[", cx)?;
- fmt::Display::fmt(&t.print(cx), f)?;
- if f.alternate() {
- primitive_link(f, PrimitiveType::Array, &format!("; {}]", n), cx)
- } else {
- primitive_link(f, PrimitiveType::Array, &format!("; {}]", Escape(n)), cx)
+ clean::Array(ref t, ref n) => match **t {
+ clean::Generic(name) if !f.alternate() => primitive_link(
+ f,
+ PrimitiveType::Array,
+ &format!("[{name}; {n}]", n = Escape(n)),
+ cx,
+ ),
+ _ => {
+ write!(f, "[")?;
+ fmt::Display::fmt(&t.print(cx), f)?;
+ if f.alternate() {
+ write!(f, "; {n}")?;
+ } else {
+ write!(f, "; ")?;
+ primitive_link(f, PrimitiveType::Array, &format!("{n}", n = Escape(n)), cx)?;
+ }
+ write!(f, "]")
}
- }
+ },
clean::RawPointer(m, ref t) => {
let m = match m {
hir::Mutability::Mut => "mut",
@@ -1079,7 +1085,12 @@ fn fmt_type<'cx>(
write!(f, "impl {}", print_generic_bounds(bounds, cx))
}
}
- clean::QPath { ref assoc, ref self_type, ref trait_, should_show_cast } => {
+ clean::QPath(box clean::QPathData {
+ ref assoc,
+ ref self_type,
+ ref trait_,
+ should_show_cast,
+ }) => {
if f.alternate() {
if should_show_cast {
write!(f, "<{:#} as {:#}>::", self_type.print(cx), trait_.print(cx))?
@@ -1305,22 +1316,19 @@ impl clean::FnDecl {
/// <br>Used to determine line-wrapping.
/// * `indent`: The number of spaces to indent each successive line with, if line-wrapping is
/// necessary.
- /// * `asyncness`: Whether the function is async or not.
pub(crate) fn full_print<'a, 'tcx: 'a>(
&'a self,
header_len: usize,
indent: usize,
- asyncness: hir::IsAsync,
cx: &'a Context<'tcx>,
) -> impl fmt::Display + 'a + Captures<'tcx> {
- display_fn(move |f| self.inner_full_print(header_len, indent, asyncness, f, cx))
+ display_fn(move |f| self.inner_full_print(header_len, indent, f, cx))
}
fn inner_full_print(
&self,
header_len: usize,
indent: usize,
- asyncness: hir::IsAsync,
f: &mut fmt::Formatter<'_>,
cx: &Context<'_>,
) -> fmt::Result {
@@ -1385,15 +1393,9 @@ impl clean::FnDecl {
args_plain.push_str(", ...");
}
- let arrow_plain;
- let arrow = if let hir::IsAsync::Async = asyncness {
- let output = self.sugared_async_return_type();
- arrow_plain = format!("{:#}", output.print(cx));
- if f.alternate() { arrow_plain.clone() } else { format!("{}", output.print(cx)) }
- } else {
- arrow_plain = format!("{:#}", self.output.print(cx));
- if f.alternate() { arrow_plain.clone() } else { format!("{}", self.output.print(cx)) }
- };
+ let arrow_plain = format!("{:#}", self.output.print(cx));
+ let arrow =
+ if f.alternate() { arrow_plain.clone() } else { format!("{}", self.output.print(cx)) };
let declaration_len = header_len + args_plain.len() + arrow_plain.len();
let output = if declaration_len > 80 {
diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs
index 05547ea15..5e28204b2 100644
--- a/src/librustdoc/html/highlight.rs
+++ b/src/librustdoc/html/highlight.rs
@@ -7,19 +7,18 @@
use crate::clean::PrimitiveType;
use crate::html::escape::Escape;
-use crate::html::render::Context;
+use crate::html::render::{Context, LinkFromSrc};
use std::collections::VecDeque;
use std::fmt::{Display, Write};
use rustc_data_structures::fx::FxHashMap;
-use rustc_lexer::{LiteralKind, TokenKind};
+use rustc_lexer::{Cursor, LiteralKind, TokenKind};
use rustc_span::edition::Edition;
use rustc_span::symbol::Symbol;
use rustc_span::{BytePos, Span, DUMMY_SP};
use super::format::{self, Buffer};
-use super::render::LinkFromSrc;
/// This type is needed in case we want to render links on items to allow to go to their definition.
pub(crate) struct HrefContext<'a, 'b, 'c> {
@@ -29,31 +28,74 @@ pub(crate) struct HrefContext<'a, 'b, 'c> {
/// This field is used to know "how far" from the top of the directory we are to link to either
/// documentation pages or other source pages.
pub(crate) root_path: &'c str,
+ /// This field is used to calculate precise local URLs.
+ pub(crate) current_href: &'c str,
}
/// Decorations are represented as a map from CSS class to vector of character ranges.
/// Each range will be wrapped in a span with that class.
+#[derive(Default)]
pub(crate) struct DecorationInfo(pub(crate) FxHashMap<&'static str, Vec<(u32, u32)>>);
-/// Highlights `src`, returning the HTML output.
-pub(crate) fn render_with_highlighting(
+#[derive(Eq, PartialEq, Clone, Copy)]
+pub(crate) enum Tooltip {
+ Ignore,
+ CompileFail,
+ ShouldPanic,
+ Edition(Edition),
+ None,
+}
+
+/// Highlights `src` as an inline example, returning the HTML output.
+pub(crate) fn render_example_with_highlighting(
src: &str,
out: &mut Buffer,
- class: Option<&str>,
+ tooltip: Tooltip,
playground_button: Option<&str>,
- tooltip: Option<(Option<Edition>, &str)>,
- edition: Edition,
- extra_content: Option<Buffer>,
- href_context: Option<HrefContext<'_, '_, '_>>,
- decoration_info: Option<DecorationInfo>,
) {
- debug!("highlighting: ================\n{}\n==============", src);
- if let Some((edition_info, class)) = tooltip {
+ write_header(out, "rust-example-rendered", None, tooltip);
+ write_code(out, src, None, None);
+ write_footer(out, playground_button);
+}
+
+/// Highlights `src` as a macro, returning the HTML output.
+pub(crate) fn render_macro_with_highlighting(src: &str, out: &mut Buffer) {
+ write_header(out, "macro", None, Tooltip::None);
+ write_code(out, src, None, None);
+ write_footer(out, None);
+}
+
+/// Highlights `src` as a source code page, returning the HTML output.
+pub(crate) fn render_source_with_highlighting(
+ src: &str,
+ out: &mut Buffer,
+ line_numbers: Buffer,
+ href_context: HrefContext<'_, '_, '_>,
+ decoration_info: DecorationInfo,
+) {
+ write_header(out, "", Some(line_numbers), Tooltip::None);
+ write_code(out, src, Some(href_context), Some(decoration_info));
+ write_footer(out, None);
+}
+
+fn write_header(out: &mut Buffer, class: &str, extra_content: Option<Buffer>, tooltip: Tooltip) {
+ write!(
+ out,
+ "<div class=\"example-wrap{}\">",
+ match tooltip {
+ Tooltip::Ignore => " ignore",
+ Tooltip::CompileFail => " compile_fail",
+ Tooltip::ShouldPanic => " should_panic",
+ Tooltip::Edition(_) => " edition",
+ Tooltip::None => "",
+ },
+ );
+
+ if tooltip != Tooltip::None {
write!(
out,
- "<div class='information'><div class='tooltip {}'{}>ⓘ</div></div>",
- class,
- if let Some(edition_info) = edition_info {
+ "<div class='tooltip'{}>ⓘ</div>",
+ if let Tooltip::Edition(edition_info) = tooltip {
format!(" data-edition=\"{}\"", edition_info)
} else {
String::new()
@@ -61,24 +103,115 @@ pub(crate) fn render_with_highlighting(
);
}
- write_header(out, class, extra_content);
- write_code(out, src, edition, href_context, decoration_info);
- write_footer(out, playground_button);
-}
-
-fn write_header(out: &mut Buffer, class: Option<&str>, extra_content: Option<Buffer>) {
- write!(out, "<div class=\"example-wrap\">");
if let Some(extra) = extra_content {
out.push_buffer(extra);
}
- if let Some(class) = class {
- write!(out, "<pre class=\"rust {}\">", class);
- } else {
+ if class.is_empty() {
write!(out, "<pre class=\"rust\">");
+ } else {
+ write!(out, "<pre class=\"rust {class}\">");
}
write!(out, "<code>");
}
+/// Check if two `Class` can be merged together. In the following rules, "unclassified" means `None`
+/// basically (since it's `Option<Class>`). The following rules apply:
+///
+/// * If two `Class` have the same variant, then they can be merged.
+/// * If the other `Class` is unclassified and only contains white characters (backline,
+/// whitespace, etc), it can be merged.
+/// * `Class::Ident` is considered the same as unclassified (because it doesn't have an associated
+/// CSS class).
+fn can_merge(class1: Option<Class>, class2: Option<Class>, text: &str) -> bool {
+ match (class1, class2) {
+ (Some(c1), Some(c2)) => c1.is_equal_to(c2),
+ (Some(Class::Ident(_)), None) | (None, Some(Class::Ident(_))) => true,
+ (Some(_), None) | (None, Some(_)) => text.trim().is_empty(),
+ (None, None) => true,
+ }
+}
+
+/// This type is used as a conveniency to prevent having to pass all its fields as arguments into
+/// the various functions (which became its methods).
+struct TokenHandler<'a, 'b, 'c, 'd, 'e> {
+ out: &'a mut Buffer,
+ /// It contains the closing tag and the associated `Class`.
+ closing_tags: Vec<(&'static str, Class)>,
+ /// This is used because we don't automatically generate the closing tag on `ExitSpan` in
+ /// case an `EnterSpan` event with the same class follows.
+ pending_exit_span: Option<Class>,
+ /// `current_class` and `pending_elems` are used to group HTML elements with same `class`
+ /// attributes to reduce the DOM size.
+ current_class: Option<Class>,
+ /// We need to keep the `Class` for each element because it could contain a `Span` which is
+ /// used to generate links.
+ pending_elems: Vec<(&'b str, Option<Class>)>,
+ href_context: Option<HrefContext<'c, 'd, 'e>>,
+}
+
+impl<'a, 'b, 'c, 'd, 'e> TokenHandler<'a, 'b, 'c, 'd, 'e> {
+ fn handle_exit_span(&mut self) {
+ // We can't get the last `closing_tags` element using `pop()` because `closing_tags` is
+ // being used in `write_pending_elems`.
+ let class = self.closing_tags.last().expect("ExitSpan without EnterSpan").1;
+ // We flush everything just in case...
+ self.write_pending_elems(Some(class));
+
+ exit_span(self.out, self.closing_tags.pop().expect("ExitSpan without EnterSpan").0);
+ self.pending_exit_span = None;
+ }
+
+ /// Write all the pending elements sharing a same (or at mergeable) `Class`.
+ ///
+ /// If there is a "parent" (if a `EnterSpan` event was encountered) and the parent can be merged
+ /// with the elements' class, then we simply write the elements since the `ExitSpan` event will
+ /// close the tag.
+ ///
+ /// Otherwise, if there is only one pending element, we let the `string` function handle both
+ /// opening and closing the tag, otherwise we do it into this function.
+ ///
+ /// It returns `true` if `current_class` must be set to `None` afterwards.
+ fn write_pending_elems(&mut self, current_class: Option<Class>) -> bool {
+ if self.pending_elems.is_empty() {
+ return false;
+ }
+ if let Some((_, parent_class)) = self.closing_tags.last() &&
+ can_merge(current_class, Some(*parent_class), "")
+ {
+ for (text, class) in self.pending_elems.iter() {
+ string(self.out, Escape(text), *class, &self.href_context, false);
+ }
+ } else {
+ // We only want to "open" the tag ourselves if we have more than one pending and if the
+ // current parent tag is not the same as our pending content.
+ let close_tag = if self.pending_elems.len() > 1 && current_class.is_some() {
+ Some(enter_span(self.out, current_class.unwrap(), &self.href_context))
+ } else {
+ None
+ };
+ for (text, class) in self.pending_elems.iter() {
+ string(self.out, Escape(text), *class, &self.href_context, close_tag.is_none());
+ }
+ if let Some(close_tag) = close_tag {
+ exit_span(self.out, close_tag);
+ }
+ }
+ self.pending_elems.clear();
+ true
+ }
+}
+
+impl<'a, 'b, 'c, 'd, 'e> Drop for TokenHandler<'a, 'b, 'c, 'd, 'e> {
+ /// When leaving, we need to flush all pending data to not have missing content.
+ fn drop(&mut self) {
+ if self.pending_exit_span.is_some() {
+ self.handle_exit_span();
+ } else {
+ self.write_pending_elems(self.current_class);
+ }
+ }
+}
+
/// Convert the given `src` source code into HTML by adding classes for highlighting.
///
/// This code is used to render code blocks (in the documentation) as well as the source code pages.
@@ -93,27 +226,74 @@ fn write_header(out: &mut Buffer, class: Option<&str>, extra_content: Option<Buf
fn write_code(
out: &mut Buffer,
src: &str,
- edition: Edition,
href_context: Option<HrefContext<'_, '_, '_>>,
decoration_info: Option<DecorationInfo>,
) {
// This replace allows to fix how the code source with DOS backline characters is displayed.
let src = src.replace("\r\n", "\n");
- let mut closing_tags: Vec<&'static str> = Vec::new();
+ let mut token_handler = TokenHandler {
+ out,
+ closing_tags: Vec::new(),
+ pending_exit_span: None,
+ current_class: None,
+ pending_elems: Vec::new(),
+ href_context,
+ };
+
Classifier::new(
&src,
- edition,
- href_context.as_ref().map(|c| c.file_span).unwrap_or(DUMMY_SP),
+ token_handler.href_context.as_ref().map(|c| c.file_span).unwrap_or(DUMMY_SP),
decoration_info,
)
.highlight(&mut |highlight| {
match highlight {
- Highlight::Token { text, class } => string(out, Escape(text), class, &href_context),
+ Highlight::Token { text, class } => {
+ // If we received a `ExitSpan` event and then have a non-compatible `Class`, we
+ // need to close the `<span>`.
+ let need_current_class_update = if let Some(pending) = token_handler.pending_exit_span &&
+ !can_merge(Some(pending), class, text) {
+ token_handler.handle_exit_span();
+ true
+ // If the two `Class` are different, time to flush the current content and start
+ // a new one.
+ } else if !can_merge(token_handler.current_class, class, text) {
+ token_handler.write_pending_elems(token_handler.current_class);
+ true
+ } else {
+ token_handler.current_class.is_none()
+ };
+
+ if need_current_class_update {
+ token_handler.current_class = class.map(Class::dummy);
+ }
+ token_handler.pending_elems.push((text, class));
+ }
Highlight::EnterSpan { class } => {
- closing_tags.push(enter_span(out, class, &href_context))
+ let mut should_add = true;
+ if let Some(pending_exit_span) = token_handler.pending_exit_span {
+ if class.is_equal_to(pending_exit_span) {
+ should_add = false;
+ } else {
+ token_handler.handle_exit_span();
+ }
+ } else {
+ // We flush everything just in case...
+ if token_handler.write_pending_elems(token_handler.current_class) {
+ token_handler.current_class = None;
+ }
+ }
+ if should_add {
+ let closing_tag = enter_span(token_handler.out, class, &token_handler.href_context);
+ token_handler.closing_tags.push((closing_tag, class));
+ }
+
+ token_handler.current_class = None;
+ token_handler.pending_exit_span = None;
}
Highlight::ExitSpan => {
- exit_span(out, closing_tags.pop().expect("ExitSpan without EnterSpan"))
+ token_handler.current_class = None;
+ token_handler.pending_exit_span =
+ Some(token_handler.closing_tags.last().as_ref().expect("ExitSpan without EnterSpan").1);
}
};
});
@@ -130,15 +310,15 @@ enum Class {
DocComment,
Attribute,
KeyWord,
- // Keywords that do pointer/reference stuff.
+ /// Keywords that do pointer/reference stuff.
RefKeyWord,
Self_(Span),
- Op,
Macro(Span),
MacroNonTerminal,
String,
Number,
Bool,
+ /// `Ident` isn't rendered in the HTML but we still need it for the `Span` it contains.
Ident(Span),
Lifetime,
PreludeTy,
@@ -148,6 +328,31 @@ enum Class {
}
impl Class {
+ /// It is only looking at the variant, not the variant content.
+ ///
+ /// It is used mostly to group multiple similar HTML elements into one `<span>` instead of
+ /// multiple ones.
+ fn is_equal_to(self, other: Self) -> bool {
+ match (self, other) {
+ (Self::Self_(_), Self::Self_(_))
+ | (Self::Macro(_), Self::Macro(_))
+ | (Self::Ident(_), Self::Ident(_)) => true,
+ (Self::Decoration(c1), Self::Decoration(c2)) => c1 == c2,
+ (x, y) => x == y,
+ }
+ }
+
+ /// If `self` contains a `Span`, it'll be replaced with `DUMMY_SP` to prevent creating links
+ /// on "empty content" (because of the attributes merge).
+ fn dummy(self) -> Self {
+ match self {
+ Self::Self_(_) => Self::Self_(DUMMY_SP),
+ Self::Macro(_) => Self::Macro(DUMMY_SP),
+ Self::Ident(_) => Self::Ident(DUMMY_SP),
+ s => s,
+ }
+ }
+
/// Returns the css class expected by rustdoc for each `Class`.
fn as_html(self) -> &'static str {
match self {
@@ -157,13 +362,12 @@ impl Class {
Class::KeyWord => "kw",
Class::RefKeyWord => "kw-2",
Class::Self_(_) => "self",
- Class::Op => "op",
Class::Macro(_) => "macro",
Class::MacroNonTerminal => "macro-nonterminal",
Class::String => "string",
Class::Number => "number",
Class::Bool => "bool-val",
- Class::Ident(_) => "ident",
+ Class::Ident(_) => "",
Class::Lifetime => "lifetime",
Class::PreludeTy => "prelude-ty",
Class::PreludeVal => "prelude-val",
@@ -182,7 +386,6 @@ impl Class {
| Self::Attribute
| Self::KeyWord
| Self::RefKeyWord
- | Self::Op
| Self::MacroNonTerminal
| Self::String
| Self::Number
@@ -204,15 +407,16 @@ enum Highlight<'a> {
struct TokenIter<'a> {
src: &'a str,
+ cursor: Cursor<'a>,
}
impl<'a> Iterator for TokenIter<'a> {
type Item = (TokenKind, &'a str);
fn next(&mut self) -> Option<(TokenKind, &'a str)> {
- if self.src.is_empty() {
+ let token = self.cursor.advance_token();
+ if token.kind == TokenKind::Eof {
return None;
}
- let token = rustc_lexer::first_token(self.src);
let (text, rest) = self.src.split_at(token.len as usize);
self.src = rest;
Some((token.kind, text))
@@ -220,7 +424,7 @@ impl<'a> Iterator for TokenIter<'a> {
}
/// Classifies into identifier class; returns `None` if this is a non-keyword identifier.
-fn get_real_ident_class(text: &str, edition: Edition, allow_path_keywords: bool) -> Option<Class> {
+fn get_real_ident_class(text: &str, allow_path_keywords: bool) -> Option<Class> {
let ignore: &[&str] =
if allow_path_keywords { &["self", "Self", "super", "crate"] } else { &["self", "Self"] };
if ignore.iter().any(|k| *k == text) {
@@ -229,7 +433,7 @@ fn get_real_ident_class(text: &str, edition: Edition, allow_path_keywords: bool)
Some(match text {
"ref" | "mut" => Class::RefKeyWord,
"false" | "true" => Class::Bool,
- _ if Symbol::intern(text).is_reserved(|| edition) => Class::KeyWord,
+ _ if Symbol::intern(text).is_reserved(|| Edition::Edition2021) => Class::KeyWord,
_ => return None,
})
}
@@ -250,7 +454,7 @@ impl<'a> PeekIter<'a> {
fn new(iter: TokenIter<'a>) -> Self {
Self { stored: VecDeque::new(), peek_pos: 0, iter }
}
- /// Returns the next item after the current one. It doesn't interfer with `peek_next` output.
+ /// Returns the next item after the current one. It doesn't interfere with `peek_next` output.
fn peek(&mut self) -> Option<&(TokenKind, &'a str)> {
if self.stored.is_empty() {
if let Some(next) = self.iter.next() {
@@ -259,7 +463,7 @@ impl<'a> PeekIter<'a> {
}
self.stored.front()
}
- /// Returns the next item after the last one peeked. It doesn't interfer with `peek` output.
+ /// Returns the next item after the last one peeked. It doesn't interfere with `peek` output.
fn peek_next(&mut self) -> Option<&(TokenKind, &'a str)> {
self.peek_pos += 1;
if self.peek_pos - 1 < self.stored.len() {
@@ -311,7 +515,6 @@ struct Classifier<'a> {
in_attribute: bool,
in_macro: bool,
in_macro_nonterminal: bool,
- edition: Edition,
byte_pos: u32,
file_span: Span,
src: &'a str,
@@ -321,20 +524,14 @@ struct Classifier<'a> {
impl<'a> Classifier<'a> {
/// Takes as argument the source code to HTML-ify, the rust edition to use and the source code
/// file span which will be used later on by the `span_correspondance_map`.
- fn new(
- src: &str,
- edition: Edition,
- file_span: Span,
- decoration_info: Option<DecorationInfo>,
- ) -> Classifier<'_> {
- let tokens = PeekIter::new(TokenIter { src });
+ fn new(src: &str, file_span: Span, decoration_info: Option<DecorationInfo>) -> Classifier<'_> {
+ let tokens = PeekIter::new(TokenIter { src, cursor: Cursor::new(src) });
let decorations = decoration_info.map(Decorations::new);
Classifier {
tokens,
in_attribute: false,
in_macro: false,
in_macro_nonterminal: false,
- edition,
byte_pos: 0,
file_span,
src,
@@ -354,7 +551,6 @@ impl<'a> Classifier<'a> {
let start = self.byte_pos as usize;
let mut pos = start;
let mut has_ident = false;
- let edition = self.edition;
loop {
let mut nb = 0;
@@ -376,7 +572,7 @@ impl<'a> Classifier<'a> {
if let Some((None, text)) = self.tokens.peek().map(|(token, text)| {
if *token == TokenKind::Ident {
- let class = get_real_ident_class(text, edition, true);
+ let class = get_real_ident_class(text, true);
(class, text)
} else {
// Doesn't matter which Class we put in here...
@@ -494,7 +690,7 @@ impl<'a> Classifier<'a> {
// or a reference or pointer type. Unless, of course, it looks like
// a logical and or a multiplication operator: `&&` or `* `.
TokenKind::Star => match self.tokens.peek() {
- Some((TokenKind::Whitespace, _)) => Class::Op,
+ Some((TokenKind::Whitespace, _)) => return no_highlight(sink),
Some((TokenKind::Ident, "mut")) => {
self.next();
sink(Highlight::Token { text: "*mut", class: Some(Class::RefKeyWord) });
@@ -510,15 +706,15 @@ impl<'a> Classifier<'a> {
TokenKind::And => match self.tokens.peek() {
Some((TokenKind::And, _)) => {
self.next();
- sink(Highlight::Token { text: "&&", class: Some(Class::Op) });
+ sink(Highlight::Token { text: "&&", class: None });
return;
}
Some((TokenKind::Eq, _)) => {
self.next();
- sink(Highlight::Token { text: "&=", class: Some(Class::Op) });
+ sink(Highlight::Token { text: "&=", class: None });
return;
}
- Some((TokenKind::Whitespace, _)) => Class::Op,
+ Some((TokenKind::Whitespace, _)) => return no_highlight(sink),
Some((TokenKind::Ident, "mut")) => {
self.next();
sink(Highlight::Token { text: "&mut", class: Some(Class::RefKeyWord) });
@@ -531,7 +727,7 @@ impl<'a> Classifier<'a> {
TokenKind::Eq => match lookahead {
Some(TokenKind::Eq) => {
self.next();
- sink(Highlight::Token { text: "==", class: Some(Class::Op) });
+ sink(Highlight::Token { text: "==", class: None });
return;
}
Some(TokenKind::Gt) => {
@@ -539,7 +735,7 @@ impl<'a> Classifier<'a> {
sink(Highlight::Token { text: "=>", class: None });
return;
}
- _ => Class::Op,
+ _ => return no_highlight(sink),
},
TokenKind::Minus if lookahead == Some(TokenKind::Gt) => {
self.next();
@@ -556,7 +752,7 @@ impl<'a> Classifier<'a> {
| TokenKind::Percent
| TokenKind::Bang
| TokenKind::Lt
- | TokenKind::Gt => Class::Op,
+ | TokenKind::Gt => return no_highlight(sink),
// Miscellaneous, no highlighting.
TokenKind::Dot
@@ -634,7 +830,7 @@ impl<'a> Classifier<'a> {
sink(Highlight::Token { text, class: None });
return;
}
- TokenKind::Ident => match get_real_ident_class(text, self.edition, false) {
+ TokenKind::Ident => match get_real_ident_class(text, false) {
None => match text {
"Option" | "Result" => Class::PreludeTy,
"Some" | "None" | "Ok" | "Err" => Class::PreludeVal,
@@ -654,6 +850,7 @@ impl<'a> Classifier<'a> {
Class::Ident(self.new_span(before, text))
}
TokenKind::Lifetime { .. } => Class::Lifetime,
+ TokenKind::Eof => panic!("Eof in advance"),
};
// Anything that didn't return above is the simple case where we the
// class just spans a single token, so we can use the `string` method.
@@ -682,7 +879,7 @@ fn enter_span(
klass: Class,
href_context: &Option<HrefContext<'_, '_, '_>>,
) -> &'static str {
- string_without_closing_tag(out, "", Some(klass), href_context).expect(
+ string_without_closing_tag(out, "", Some(klass), href_context, true).expect(
"internal error: enter_span was called with Some(klass) but did not return a \
closing HTML tag",
)
@@ -714,8 +911,10 @@ fn string<T: Display>(
text: T,
klass: Option<Class>,
href_context: &Option<HrefContext<'_, '_, '_>>,
+ open_tag: bool,
) {
- if let Some(closing_tag) = string_without_closing_tag(out, text, klass, href_context) {
+ if let Some(closing_tag) = string_without_closing_tag(out, text, klass, href_context, open_tag)
+ {
out.write_str(closing_tag);
}
}
@@ -734,6 +933,7 @@ fn string_without_closing_tag<T: Display>(
text: T,
klass: Option<Class>,
href_context: &Option<HrefContext<'_, '_, '_>>,
+ open_tag: bool,
) -> Option<&'static str> {
let Some(klass) = klass
else {
@@ -742,6 +942,10 @@ fn string_without_closing_tag<T: Display>(
};
let Some(def_span) = klass.get_span()
else {
+ if !open_tag {
+ write!(out, "{}", text);
+ return None;
+ }
write!(out, "<span class=\"{}\">{}", klass.as_html(), text);
return Some("</span>");
};
@@ -765,6 +969,7 @@ fn string_without_closing_tag<T: Display>(
path
});
}
+
if let Some(href_context) = href_context {
if let Some(href) =
href_context.context.shared.span_correspondance_map.get(&def_span).and_then(|href| {
@@ -775,9 +980,9 @@ fn string_without_closing_tag<T: Display>(
// a link to their definition can be generated using this:
// https://github.com/rust-lang/rust/blob/60f1a2fc4b535ead9c85ce085fdce49b1b097531/src/librustdoc/html/render/context.rs#L315-L338
match href {
- LinkFromSrc::Local(span) => context
- .href_from_span(*span, true)
- .map(|s| format!("{}{}", href_context.root_path, s)),
+ LinkFromSrc::Local(span) => {
+ context.href_from_span_relative(*span, href_context.current_href)
+ }
LinkFromSrc::External(def_id) => {
format::href_with_root_path(*def_id, context, Some(href_context.root_path))
.ok()
@@ -793,12 +998,33 @@ fn string_without_closing_tag<T: Display>(
}
})
{
- write!(out, "<a class=\"{}\" href=\"{}\">{}", klass.as_html(), href, text_s);
+ if !open_tag {
+ // We're already inside an element which has the same klass, no need to give it
+ // again.
+ write!(out, "<a href=\"{}\">{}", href, text_s);
+ } else {
+ let klass_s = klass.as_html();
+ if klass_s.is_empty() {
+ write!(out, "<a href=\"{}\">{}", href, text_s);
+ } else {
+ write!(out, "<a class=\"{}\" href=\"{}\">{}", klass_s, href, text_s);
+ }
+ }
return Some("</a>");
}
}
- write!(out, "<span class=\"{}\">{}", klass.as_html(), text_s);
- Some("</span>")
+ if !open_tag {
+ write!(out, "{}", text_s);
+ return None;
+ }
+ let klass_s = klass.as_html();
+ if klass_s.is_empty() {
+ write!(out, "{}", text_s);
+ Some("")
+ } else {
+ write!(out, "<span class=\"{}\">{}", klass_s, text_s);
+ Some("</span>")
+ }
}
#[cfg(test)]
diff --git a/src/librustdoc/html/highlight/fixtures/decorations.html b/src/librustdoc/html/highlight/fixtures/decorations.html
index 45f567880..ebf29f9cb 100644
--- a/src/librustdoc/html/highlight/fixtures/decorations.html
+++ b/src/librustdoc/html/highlight/fixtures/decorations.html
@@ -1,2 +1,4 @@
-<span class="example"><span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">1</span>;</span>
-<span class="kw">let</span> <span class="ident">y</span> <span class="op">=</span> <span class="number">2</span>; \ No newline at end of file
+<span class="example"><span class="kw">let </span>x = <span class="number">1</span>;
+<span class="kw">let </span>y = <span class="number">2</span>;
+</span><span class="example2"><span class="kw">let </span>z = <span class="number">3</span>;
+</span><span class="kw">let </span>a = <span class="number">4</span>; \ No newline at end of file
diff --git a/src/librustdoc/html/highlight/fixtures/dos_line.html b/src/librustdoc/html/highlight/fixtures/dos_line.html
index 1c8dbffe7..30b50ca7c 100644
--- a/src/librustdoc/html/highlight/fixtures/dos_line.html
+++ b/src/librustdoc/html/highlight/fixtures/dos_line.html
@@ -1,3 +1,3 @@
-<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">foo</span>() {
+<span class="kw">pub fn </span>foo() {
<span class="macro">println!</span>(<span class="string">&quot;foo&quot;</span>);
}
diff --git a/src/librustdoc/html/highlight/fixtures/highlight.html b/src/librustdoc/html/highlight/fixtures/highlight.html
index abc2db179..9f73e03f9 100644
--- a/src/librustdoc/html/highlight/fixtures/highlight.html
+++ b/src/librustdoc/html/highlight/fixtures/highlight.html
@@ -1,4 +1,4 @@
-<span class="kw">use</span> <span class="ident"><span class="kw">crate</span>::a::foo</span>;
-<span class="kw">use</span> <span class="ident"><span class="self">self</span>::whatever</span>;
-<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="ident"><span class="kw">super</span>::b::foo</span>;
-<span class="kw">let</span> <span class="ident">y</span> <span class="op">=</span> <span class="ident"><span class="self">Self</span>::whatever</span>; \ No newline at end of file
+<span class="kw">use </span><span class="kw">crate</span>::a::foo;
+<span class="kw">use </span><span class="self">self</span>::whatever;
+<span class="kw">let </span>x = <span class="kw">super</span>::b::foo;
+<span class="kw">let </span>y = <span class="self">Self</span>::whatever; \ No newline at end of file
diff --git a/src/librustdoc/html/highlight/fixtures/sample.html b/src/librustdoc/html/highlight/fixtures/sample.html
index b117a12e3..4a5a3cf60 100644
--- a/src/librustdoc/html/highlight/fixtures/sample.html
+++ b/src/librustdoc/html/highlight/fixtures/sample.html
@@ -8,30 +8,31 @@
.lifetime { color: #B76514; }
.question-mark { color: #ff9011; }
</style>
-<pre><code><span class="attribute">#![<span class="ident">crate_type</span> <span class="op">=</span> <span class="string">&quot;lib&quot;</span>]</span>
+<pre><code><span class="attribute">#![crate_type = <span class="string">&quot;lib&quot;</span>]
-<span class="kw">use</span> <span class="ident">std::path</span>::{<span class="ident">Path</span>, <span class="ident">PathBuf</span>};
+</span><span class="kw">use </span>std::path::{Path, PathBuf};
-<span class="attribute">#[<span class="ident">cfg</span>(<span class="ident">target_os</span> <span class="op">=</span> <span class="string">&quot;linux&quot;</span>)]</span>
-<span class="kw">fn</span> <span class="ident">main</span>() -&gt; () {
- <span class="kw">let</span> <span class="ident">foo</span> <span class="op">=</span> <span class="bool-val">true</span> <span class="op">&amp;&amp;</span> <span class="bool-val">false</span> <span class="op">|</span><span class="op">|</span> <span class="bool-val">true</span>;
- <span class="kw">let</span> <span class="kw">_</span>: <span class="kw-2">*const</span> () <span class="op">=</span> <span class="number">0</span>;
- <span class="kw">let</span> <span class="kw">_</span> <span class="op">=</span> <span class="kw-2">&amp;</span><span class="ident">foo</span>;
- <span class="kw">let</span> <span class="kw">_</span> <span class="op">=</span> <span class="op">&amp;&amp;</span><span class="ident">foo</span>;
- <span class="kw">let</span> <span class="kw">_</span> <span class="op">=</span> <span class="kw-2">*</span><span class="ident">foo</span>;
- <span class="macro">mac!</span>(<span class="ident">foo</span>, <span class="kw-2">&amp;mut</span> <span class="ident">bar</span>);
- <span class="macro">assert!</span>(<span class="self">self</span>.<span class="ident">length</span> <span class="op">&lt;</span> <span class="ident">N</span> <span class="op">&amp;&amp;</span> <span class="ident">index</span> <span class="op">&lt;</span><span class="op">=</span> <span class="self">self</span>.<span class="ident">length</span>);
- <span class="ident">::std::env::var</span>(<span class="string">&quot;gateau&quot;</span>).<span class="ident">is_ok</span>();
- <span class="attribute">#[<span class="ident">rustfmt::skip</span>]</span>
- <span class="kw">let</span> <span class="ident">s</span>:<span class="ident">std::path::PathBuf</span> <span class="op">=</span> <span class="ident">std::path::PathBuf::new</span>();
- <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">String::new</span>();
+<span class="attribute">#[cfg(target_os = <span class="string">&quot;linux&quot;</span>)]
+#[cfg(target_os = <span class="string">&quot;windows&quot;</span>)]
+</span><span class="kw">fn </span>main() -&gt; () {
+ <span class="kw">let </span>foo = <span class="bool-val">true </span>&amp;&amp; <span class="bool-val">false </span>|| <span class="bool-val">true</span>;
+ <span class="kw">let _</span>: <span class="kw-2">*const </span>() = <span class="number">0</span>;
+ <span class="kw">let _ </span>= <span class="kw-2">&amp;</span>foo;
+ <span class="kw">let _ </span>= &amp;&amp;foo;
+ <span class="kw">let _ </span>= <span class="kw-2">*</span>foo;
+ <span class="macro">mac!</span>(foo, <span class="kw-2">&amp;mut </span>bar);
+ <span class="macro">assert!</span>(<span class="self">self</span>.length &lt; N &amp;&amp; index &lt;= <span class="self">self</span>.length);
+ ::std::env::var(<span class="string">&quot;gateau&quot;</span>).is_ok();
+ <span class="attribute">#[rustfmt::skip]
+ </span><span class="kw">let </span>s:std::path::PathBuf = std::path::PathBuf::new();
+ <span class="kw">let </span><span class="kw-2">mut </span>s = String::new();
- <span class="kw">match</span> <span class="kw-2">&amp;</span><span class="ident">s</span> {
- <span class="kw-2">ref</span> <span class="kw-2">mut</span> <span class="ident">x</span> =&gt; {}
+ <span class="kw">match </span><span class="kw-2">&amp;</span>s {
+ <span class="kw-2">ref mut </span>x =&gt; {}
}
}
-<span class="macro">macro_rules!</span> <span class="ident">bar</span> {
- (<span class="macro-nonterminal">$</span><span class="macro-nonterminal">foo</span>:<span class="ident">tt</span>) =&gt; {};
+<span class="macro">macro_rules! </span>bar {
+ (<span class="macro-nonterminal">$foo</span>:tt) =&gt; {};
}
</code></pre>
diff --git a/src/librustdoc/html/highlight/fixtures/sample.rs b/src/librustdoc/html/highlight/fixtures/sample.rs
index fbfdc6767..ef85b566c 100644
--- a/src/librustdoc/html/highlight/fixtures/sample.rs
+++ b/src/librustdoc/html/highlight/fixtures/sample.rs
@@ -3,6 +3,7 @@
use std::path::{Path, PathBuf};
#[cfg(target_os = "linux")]
+#[cfg(target_os = "windows")]
fn main() -> () {
let foo = true && false || true;
let _: *const () = 0;
diff --git a/src/librustdoc/html/highlight/fixtures/union.html b/src/librustdoc/html/highlight/fixtures/union.html
index c0acf31a0..9f8915282 100644
--- a/src/librustdoc/html/highlight/fixtures/union.html
+++ b/src/librustdoc/html/highlight/fixtures/union.html
@@ -1,8 +1,8 @@
-<span class="kw">union</span> <span class="ident">Foo</span> {
- <span class="ident">i</span>: <span class="ident">i8</span>,
- <span class="ident">u</span>: <span class="ident">i8</span>,
+<span class="kw">union </span>Foo {
+ i: i8,
+ u: i8,
}
-<span class="kw">fn</span> <span class="ident">main</span>() {
- <span class="kw">let</span> <span class="ident">union</span> <span class="op">=</span> <span class="number">0</span>;
+<span class="kw">fn </span>main() {
+ <span class="kw">let </span>union = <span class="number">0</span>;
}
diff --git a/src/librustdoc/html/highlight/tests.rs b/src/librustdoc/html/highlight/tests.rs
index 1fea7e983..a5e633df4 100644
--- a/src/librustdoc/html/highlight/tests.rs
+++ b/src/librustdoc/html/highlight/tests.rs
@@ -3,7 +3,6 @@ use crate::html::format::Buffer;
use expect_test::expect_file;
use rustc_data_structures::fx::FxHashMap;
use rustc_span::create_default_session_globals_then;
-use rustc_span::edition::Edition;
const STYLE: &str = r#"
<style>
@@ -23,7 +22,7 @@ fn test_html_highlighting() {
let src = include_str!("fixtures/sample.rs");
let html = {
let mut out = Buffer::new();
- write_code(&mut out, src, Edition::Edition2018, None, None);
+ write_code(&mut out, src, None, None);
format!("{}<pre><code>{}</code></pre>\n", STYLE, out.into_inner())
};
expect_file!["fixtures/sample.html"].assert_eq(&html);
@@ -37,7 +36,7 @@ fn test_dos_backline() {
println!(\"foo\");\r\n\
}\r\n";
let mut html = Buffer::new();
- write_code(&mut html, src, Edition::Edition2018, None, None);
+ write_code(&mut html, src, None, None);
expect_file!["fixtures/dos_line.html"].assert_eq(&html.into_inner());
});
}
@@ -51,7 +50,7 @@ let x = super::b::foo;
let y = Self::whatever;";
let mut html = Buffer::new();
- write_code(&mut html, src, Edition::Edition2018, None, None);
+ write_code(&mut html, src, None, None);
expect_file!["fixtures/highlight.html"].assert_eq(&html.into_inner());
});
}
@@ -61,7 +60,7 @@ fn test_union_highlighting() {
create_default_session_globals_then(|| {
let src = include_str!("fixtures/union.rs");
let mut html = Buffer::new();
- write_code(&mut html, src, Edition::Edition2018, None, None);
+ write_code(&mut html, src, None, None);
expect_file!["fixtures/union.html"].assert_eq(&html.into_inner());
});
}
@@ -70,12 +69,15 @@ fn test_union_highlighting() {
fn test_decorations() {
create_default_session_globals_then(|| {
let src = "let x = 1;
-let y = 2;";
+let y = 2;
+let z = 3;
+let a = 4;";
let mut decorations = FxHashMap::default();
- decorations.insert("example", vec![(0, 10)]);
+ decorations.insert("example", vec![(0, 10), (11, 21)]);
+ decorations.insert("example2", vec![(22, 32)]);
let mut html = Buffer::new();
- write_code(&mut html, src, Edition::Edition2018, None, Some(DecorationInfo(decorations)));
+ write_code(&mut html, src, None, Some(DecorationInfo(decorations)));
expect_file!["fixtures/decorations.html"].assert_eq(&html.into_inner());
});
}
diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs
index 52a2effca..1e1c657b0 100644
--- a/src/librustdoc/html/markdown.rs
+++ b/src/librustdoc/html/markdown.rs
@@ -111,14 +111,9 @@ pub(crate) struct MarkdownWithToc<'a>(
pub(crate) Edition,
pub(crate) &'a Option<Playground>,
);
-/// A tuple struct like `Markdown` that renders the markdown escaping HTML tags.
-pub(crate) struct MarkdownHtml<'a>(
- pub(crate) &'a str,
- pub(crate) &'a mut IdMap,
- pub(crate) ErrorCodes,
- pub(crate) Edition,
- pub(crate) &'a Option<Playground>,
-);
+/// A tuple struct like `Markdown` that renders the markdown escaping HTML tags
+/// and includes no paragraph tags.
+pub(crate) struct MarkdownItemInfo<'a>(pub(crate) &'a str, pub(crate) &'a mut IdMap);
/// A tuple struct like `Markdown` that renders only the first paragraph.
pub(crate) struct MarkdownSummaryLine<'a>(pub &'a str, pub &'a [RenderedLink]);
@@ -251,8 +246,6 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
_ => {}
}
}
- let lines = origtext.lines().filter_map(|l| map_line(l).for_html());
- let text = lines.intersperse("\n".into()).collect::<String>();
let parse_result = match kind {
CodeBlockKind::Fenced(ref lang) => {
@@ -265,7 +258,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
<pre class=\"language-{}\"><code>{}</code></pre>\
</div>",
lang,
- Escape(&text),
+ Escape(&origtext),
)
.into(),
));
@@ -275,6 +268,9 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
CodeBlockKind::Indented => Default::default(),
};
+ let lines = origtext.lines().filter_map(|l| map_line(l).for_html());
+ let text = lines.intersperse("\n".into()).collect::<String>();
+
compile_fail = parse_result.compile_fail;
should_panic = parse_result.should_panic;
ignore = parse_result.ignore;
@@ -330,34 +326,27 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
});
let tooltip = if ignore != Ignore::None {
- Some((None, "ignore"))
+ highlight::Tooltip::Ignore
} else if compile_fail {
- Some((None, "compile_fail"))
+ highlight::Tooltip::CompileFail
} else if should_panic {
- Some((None, "should_panic"))
+ highlight::Tooltip::ShouldPanic
} else if explicit_edition {
- Some((Some(edition), "edition"))
+ highlight::Tooltip::Edition(edition)
} else {
- None
+ highlight::Tooltip::None
};
// insert newline to clearly separate it from the
// previous block so we can shorten the html output
let mut s = Buffer::new();
s.push_str("\n");
- highlight::render_with_highlighting(
+
+ highlight::render_example_with_highlighting(
&text,
&mut s,
- Some(&format!(
- "rust-example-rendered{}",
- if let Some((_, class)) = tooltip { format!(" {}", class) } else { String::new() }
- )),
- playground_button.as_deref(),
tooltip,
- edition,
- None,
- None,
- None,
+ playground_button.as_deref(),
);
Some(Event::Html(s.into_inner().into()))
}
@@ -825,11 +814,8 @@ impl<'tcx> ExtraInfo<'tcx> {
crate::lint::INVALID_CODEBLOCK_ATTRIBUTES,
hir_id,
self.sp,
- |lint| {
- let mut diag = lint.build(msg);
- diag.help(help);
- diag.emit();
- },
+ msg,
+ |lint| lint.help(help),
);
}
}
@@ -1079,9 +1065,9 @@ impl MarkdownWithToc<'_> {
}
}
-impl MarkdownHtml<'_> {
+impl MarkdownItemInfo<'_> {
pub(crate) fn into_string(self) -> String {
- let MarkdownHtml(md, ids, codes, edition, playground) = self;
+ let MarkdownItemInfo(md, ids) = self;
// This is actually common enough to special-case
if md.is_empty() {
@@ -1100,7 +1086,9 @@ impl MarkdownHtml<'_> {
let p = HeadingLinks::new(p, None, ids, HeadingOffset::H1);
let p = Footnotes::new(p);
let p = TableWrapper::new(p.map(|(ev, _)| ev));
- let p = CodeBlocks::new(p, codes, edition, playground);
+ let p = p.filter(|event| {
+ !matches!(event, Event::Start(Tag::Paragraph) | Event::End(Tag::Paragraph))
+ });
html::push_html(&mut s, p);
s
@@ -1126,7 +1114,11 @@ impl MarkdownSummaryLine<'_> {
let mut s = String::new();
- html::push_html(&mut s, LinkReplacer::new(SummaryLine::new(p), links));
+ let without_paragraphs = LinkReplacer::new(SummaryLine::new(p), links).filter(|event| {
+ !matches!(event, Event::Start(Tag::Paragraph) | Event::End(Tag::Paragraph))
+ });
+
+ html::push_html(&mut s, without_paragraphs);
s
}
@@ -1442,10 +1434,13 @@ static DEFAULT_ID_MAP: Lazy<FxHashMap<Cow<'static, str>, usize>> = Lazy::new(||
fn init_id_map() -> FxHashMap<Cow<'static, str>, usize> {
let mut map = FxHashMap::default();
// This is the list of IDs used in Javascript.
+ map.insert("help".into(), 1);
map.insert("settings".into(), 1);
map.insert("not-displayed".into(), 1);
map.insert("alternative-display".into(), 1);
map.insert("search".into(), 1);
+ map.insert("crate-search".into(), 1);
+ map.insert("crate-search-div".into(), 1);
// This is the list of IDs used in HTML generated in Rust (including the ones
// used in tera template files).
map.insert("mainThemeStyle".into(), 1);
@@ -1453,7 +1448,6 @@ fn init_id_map() -> FxHashMap<Cow<'static, str>, usize> {
map.insert("settings-menu".into(), 1);
map.insert("help-button".into(), 1);
map.insert("main-content".into(), 1);
- map.insert("crate-search".into(), 1);
map.insert("toggle-all-docs".into(), 1);
map.insert("all-types".into(), 1);
map.insert("default-settings".into(), 1);
diff --git a/src/librustdoc/html/markdown/tests.rs b/src/librustdoc/html/markdown/tests.rs
index 5c0bf0ed9..68b31a6ee 100644
--- a/src/librustdoc/html/markdown/tests.rs
+++ b/src/librustdoc/html/markdown/tests.rs
@@ -1,5 +1,5 @@
use super::{find_testable_code, plain_text_summary, short_markdown_summary};
-use super::{ErrorCodes, HeadingOffset, IdMap, Ignore, LangString, Markdown, MarkdownHtml};
+use super::{ErrorCodes, HeadingOffset, IdMap, Ignore, LangString, Markdown, MarkdownItemInfo};
use rustc_span::edition::{Edition, DEFAULT_EDITION};
#[test]
@@ -279,14 +279,13 @@ fn test_plain_text_summary() {
fn test_markdown_html_escape() {
fn t(input: &str, expect: &str) {
let mut idmap = IdMap::new();
- let output =
- MarkdownHtml(input, &mut idmap, ErrorCodes::Yes, DEFAULT_EDITION, &None).into_string();
+ let output = MarkdownItemInfo(input, &mut idmap).into_string();
assert_eq!(output, expect, "original: {}", input);
}
- t("`Struct<'a, T>`", "<p><code>Struct&lt;'a, T&gt;</code></p>\n");
- t("Struct<'a, T>", "<p>Struct&lt;’a, T&gt;</p>\n");
- t("Struct<br>", "<p>Struct&lt;br&gt;</p>\n");
+ t("`Struct<'a, T>`", "<code>Struct&lt;'a, T&gt;</code>");
+ t("Struct<'a, T>", "Struct&lt;’a, T&gt;");
+ t("Struct<br>", "Struct&lt;br&gt;");
}
#[test]
@@ -310,3 +309,40 @@ fn test_find_testable_code_line() {
t("```rust\n```\n```rust\n```", &[1, 3]);
t("```rust\n```\n ```rust\n```", &[1, 3]);
}
+
+#[test]
+fn test_ascii_with_prepending_hashtag() {
+ fn t(input: &str, expect: &str) {
+ let mut map = IdMap::new();
+ let output = Markdown {
+ content: input,
+ links: &[],
+ ids: &mut map,
+ error_codes: ErrorCodes::Yes,
+ edition: DEFAULT_EDITION,
+ playground: &None,
+ heading_offset: HeadingOffset::H2,
+ }
+ .into_string();
+ assert_eq!(output, expect, "original: {}", input);
+ }
+
+ t(
+ r#"```ascii
+#..#.####.#....#.....##..
+#..#.#....#....#....#..#.
+####.###..#....#....#..#.
+#..#.#....#....#....#..#.
+#..#.#....#....#....#..#.
+#..#.####.####.####..##..
+```"#,
+ "<div class=\"example-wrap\"><pre class=\"language-ascii\"><code>\
+#..#.####.#....#.....##..
+#..#.#....#....#....#..#.
+####.###..#....#....#..#.
+#..#.#....#....#....#..#.
+#..#.#....#....#....#..#.
+#..#.####.####.####..##..
+</code></pre></div>",
+ );
+}
diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs
index 2ed7a6f1b..5733d1f9c 100644
--- a/src/librustdoc/html/render/context.rs
+++ b/src/librustdoc/html/render/context.rs
@@ -17,8 +17,8 @@ use super::print_item::{full_path, item_path, print_item};
use super::search_index::build_index;
use super::write_shared::write_shared;
use super::{
- collect_spans_and_sources, print_sidebar, scrape_examples_help, AllTypes, LinkFromSrc, NameDoc,
- StylePath, BASIC_KEYWORDS,
+ collect_spans_and_sources, print_sidebar, scrape_examples_help, sidebar_module_like, AllTypes,
+ LinkFromSrc, NameDoc, StylePath, BASIC_KEYWORDS,
};
use crate::clean::{self, types::ExternalLocation, ExternalCrate};
@@ -31,6 +31,7 @@ use crate::formats::FormatRenderer;
use crate::html::escape::Escape;
use crate::html::format::{join_with_double_colon, Buffer};
use crate::html::markdown::{self, plain_text_summary, ErrorCodes, IdMap};
+use crate::html::url_parts_builder::UrlPartsBuilder;
use crate::html::{layout, sources};
use crate::scrape_examples::AllCallLocations;
use crate::try_err;
@@ -71,7 +72,7 @@ pub(crate) struct Context<'tcx> {
}
// `Context` is cloned a lot, so we don't want the size to grow unexpectedly.
-#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
+#[cfg(all(not(windows), target_arch = "x86_64", target_pointer_width = "64"))]
rustc_data_structures::static_assert_size!(Context<'_>, 128);
/// Shared mutable state used in [`Context`] and elsewhere.
@@ -301,13 +302,10 @@ impl<'tcx> Context<'tcx> {
/// may happen, for example, with externally inlined items where the source
/// of their crate documentation isn't known.
pub(super) fn src_href(&self, item: &clean::Item) -> Option<String> {
- self.href_from_span(item.span(self.tcx()), true)
+ self.href_from_span(item.span(self.tcx())?, true)
}
pub(crate) fn href_from_span(&self, span: clean::Span, with_lines: bool) -> Option<String> {
- if span.is_dummy() {
- return None;
- }
let mut root = self.root_path();
let mut path = String::new();
let cnum = span.cnum(self.sess());
@@ -373,6 +371,35 @@ impl<'tcx> Context<'tcx> {
anchor = anchor
))
}
+
+ pub(crate) fn href_from_span_relative(
+ &self,
+ span: clean::Span,
+ relative_to: &str,
+ ) -> Option<String> {
+ self.href_from_span(span, false).map(|s| {
+ let mut url = UrlPartsBuilder::new();
+ let mut dest_href_parts = s.split('/');
+ let mut cur_href_parts = relative_to.split('/');
+ for (cur_href_part, dest_href_part) in (&mut cur_href_parts).zip(&mut dest_href_parts) {
+ if cur_href_part != dest_href_part {
+ url.push(dest_href_part);
+ break;
+ }
+ }
+ for dest_href_part in dest_href_parts {
+ url.push(dest_href_part);
+ }
+ let loline = span.lo(self.sess()).line;
+ let hiline = span.hi(self.sess()).line;
+ format!(
+ "{}{}#{}",
+ "../".repeat(cur_href_parts.count()),
+ url.finish(),
+ if loline == hiline { loline.to_string() } else { format!("{loline}-{hiline}") }
+ )
+ })
+ }
}
/// Generates the documentation for `crate` into the directory `dst`
@@ -403,7 +430,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
extension_css,
resource_suffix,
static_root_path,
- unstable_features,
generate_redirect_map,
show_type_layout,
generate_link_to_definition,
@@ -484,7 +510,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
resource_suffix,
static_root_path,
fs: DocFS::new(sender),
- codes: ErrorCodes::from(unstable_features.is_nightly_build()),
+ codes: ErrorCodes::from(options.unstable_features.is_nightly_build()),
playground,
all: RefCell::new(AllTypes::new()),
errors: receiver,
@@ -554,6 +580,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
let crate_name = self.tcx().crate_name(LOCAL_CRATE);
let final_file = self.dst.join(crate_name.as_str()).join("all.html");
let settings_file = self.dst.join("settings.html");
+ let help_file = self.dst.join("help.html");
let scrape_examples_help_file = self.dst.join("scrape-examples-help.html");
let mut root_path = self.dst.to_str().expect("invalid path").to_owned();
@@ -570,16 +597,24 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
keywords: BASIC_KEYWORDS,
resource_suffix: &shared.resource_suffix,
};
- let sidebar = if shared.cache.crate_version.is_some() {
- format!("<h2 class=\"location\">Crate {}</h2>", crate_name)
- } else {
- String::new()
- };
let all = shared.all.replace(AllTypes::new());
+ let mut sidebar = Buffer::html();
+ if shared.cache.crate_version.is_some() {
+ write!(sidebar, "<h2 class=\"location\">Crate {}</h2>", crate_name)
+ };
+
+ let mut items = Buffer::html();
+ sidebar_module_like(&mut items, all.item_sections());
+ if !items.is_empty() {
+ sidebar.push_str("<div class=\"sidebar-elems\">");
+ sidebar.push_buffer(items);
+ sidebar.push_str("</div>");
+ }
+
let v = layout::render(
&shared.layout,
&page,
- sidebar,
+ sidebar.into_inner(),
|buf: &mut Buffer| all.print(buf),
&shared.style_files,
);
@@ -599,9 +634,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
write!(
buf,
"<div class=\"main-heading\">\
- <h1 class=\"fqn\">\
- <span class=\"in-band\">Rustdoc settings</span>\
- </h1>\
+ <h1 class=\"fqn\">Rustdoc settings</h1>\
<span class=\"out-of-band\">\
<a id=\"back\" href=\"javascript:void(0)\" onclick=\"history.back();\">\
Back\
@@ -624,6 +657,39 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
);
shared.fs.write(settings_file, v)?;
+ // Generating help page.
+ page.title = "Rustdoc help";
+ page.description = "Documentation for Rustdoc";
+ page.root_path = "./";
+
+ let sidebar = "<h2 class=\"location\">Help</h2><div class=\"sidebar-elems\"></div>";
+ let v = layout::render(
+ &shared.layout,
+ &page,
+ sidebar,
+ |buf: &mut Buffer| {
+ write!(
+ buf,
+ "<div class=\"main-heading\">\
+ <h1 class=\"fqn\">Rustdoc help</h1>\
+ <span class=\"out-of-band\">\
+ <a id=\"back\" href=\"javascript:void(0)\" onclick=\"history.back();\">\
+ Back\
+ </a>\
+ </span>\
+ </div>\
+ <noscript>\
+ <section>\
+ <p>You need to enable Javascript to use keyboard commands or search.</p>\
+ <p>For more information, browse the <a href=\"https://doc.rust-lang.org/rustdoc/\">rustdoc handbook</a>.</p>\
+ </section>\
+ </noscript>",
+ )
+ },
+ &shared.style_files,
+ );
+ shared.fs.write(help_file, v)?;
+
if shared.layout.scrape_examples_extension {
page.title = "About scraped examples";
page.description = "How the scraped examples feature works in Rustdoc";
diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs
index a262c8f7d..96c57c8c8 100644
--- a/src/librustdoc/html/render/mod.rs
+++ b/src/librustdoc/html/render/mod.rs
@@ -74,7 +74,9 @@ use crate::html::format::{
PrintWithSpace,
};
use crate::html::highlight;
-use crate::html::markdown::{HeadingOffset, IdMap, Markdown, MarkdownHtml, MarkdownSummaryLine};
+use crate::html::markdown::{
+ HeadingOffset, IdMap, Markdown, MarkdownItemInfo, MarkdownSummaryLine,
+};
use crate::html::sources;
use crate::html::static_files::SCRAPE_EXAMPLES_HELP_MD;
use crate::scrape_examples::{CallData, CallLocation};
@@ -191,12 +193,6 @@ impl StylePath {
}
}
-fn write_srclink(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer) {
- if let Some(l) = cx.src_href(item) {
- write!(buf, "<a class=\"srclink\" href=\"{}\">source</a>", l)
- }
-}
-
#[derive(Debug, Eq, PartialEq, Hash)]
struct ItemEntry {
url: String,
@@ -245,8 +241,8 @@ struct AllTypes {
opaque_tys: FxHashSet<ItemEntry>,
statics: FxHashSet<ItemEntry>,
constants: FxHashSet<ItemEntry>,
- attributes: FxHashSet<ItemEntry>,
- derives: FxHashSet<ItemEntry>,
+ attribute_macros: FxHashSet<ItemEntry>,
+ derive_macros: FxHashSet<ItemEntry>,
trait_aliases: FxHashSet<ItemEntry>,
}
@@ -265,8 +261,8 @@ impl AllTypes {
opaque_tys: new_set(100),
statics: new_set(100),
constants: new_set(100),
- attributes: new_set(100),
- derives: new_set(100),
+ attribute_macros: new_set(100),
+ derive_macros: new_set(100),
trait_aliases: new_set(100),
}
}
@@ -289,27 +285,75 @@ impl AllTypes {
ItemType::OpaqueTy => self.opaque_tys.insert(ItemEntry::new(new_url, name)),
ItemType::Static => self.statics.insert(ItemEntry::new(new_url, name)),
ItemType::Constant => self.constants.insert(ItemEntry::new(new_url, name)),
- ItemType::ProcAttribute => self.attributes.insert(ItemEntry::new(new_url, name)),
- ItemType::ProcDerive => self.derives.insert(ItemEntry::new(new_url, name)),
+ ItemType::ProcAttribute => {
+ self.attribute_macros.insert(ItemEntry::new(new_url, name))
+ }
+ ItemType::ProcDerive => self.derive_macros.insert(ItemEntry::new(new_url, name)),
ItemType::TraitAlias => self.trait_aliases.insert(ItemEntry::new(new_url, name)),
_ => true,
};
}
}
-}
-impl AllTypes {
+ fn item_sections(&self) -> FxHashSet<ItemSection> {
+ let mut sections = FxHashSet::default();
+
+ if !self.structs.is_empty() {
+ sections.insert(ItemSection::Structs);
+ }
+ if !self.enums.is_empty() {
+ sections.insert(ItemSection::Enums);
+ }
+ if !self.unions.is_empty() {
+ sections.insert(ItemSection::Unions);
+ }
+ if !self.primitives.is_empty() {
+ sections.insert(ItemSection::PrimitiveTypes);
+ }
+ if !self.traits.is_empty() {
+ sections.insert(ItemSection::Traits);
+ }
+ if !self.macros.is_empty() {
+ sections.insert(ItemSection::Macros);
+ }
+ if !self.functions.is_empty() {
+ sections.insert(ItemSection::Functions);
+ }
+ if !self.typedefs.is_empty() {
+ sections.insert(ItemSection::TypeDefinitions);
+ }
+ if !self.opaque_tys.is_empty() {
+ sections.insert(ItemSection::OpaqueTypes);
+ }
+ if !self.statics.is_empty() {
+ sections.insert(ItemSection::Statics);
+ }
+ if !self.constants.is_empty() {
+ sections.insert(ItemSection::Constants);
+ }
+ if !self.attribute_macros.is_empty() {
+ sections.insert(ItemSection::AttributeMacros);
+ }
+ if !self.derive_macros.is_empty() {
+ sections.insert(ItemSection::DeriveMacros);
+ }
+ if !self.trait_aliases.is_empty() {
+ sections.insert(ItemSection::TraitAliases);
+ }
+
+ sections
+ }
+
fn print(self, f: &mut Buffer) {
- fn print_entries(f: &mut Buffer, e: &FxHashSet<ItemEntry>, title: &str, class: &str) {
+ fn print_entries(f: &mut Buffer, e: &FxHashSet<ItemEntry>, kind: ItemSection) {
if !e.is_empty() {
let mut e: Vec<&ItemEntry> = e.iter().collect();
e.sort();
write!(
f,
- "<h3 id=\"{}\">{}</h3><ul class=\"{} docblock\">",
- title.replace(' ', "-"), // IDs cannot contain whitespaces.
- title,
- class
+ "<h3 id=\"{id}\">{title}</h3><ul class=\"all-items\">",
+ id = kind.id(),
+ title = kind.name(),
);
for s in e.iter() {
@@ -320,27 +364,23 @@ impl AllTypes {
}
}
- f.write_str(
- "<h1 class=\"fqn\">\
- <span class=\"in-band\">List of all items</span>\
- </h1>",
- );
+ f.write_str("<h1 class=\"fqn\">List of all items</h1>");
// Note: print_entries does not escape the title, because we know the current set of titles
// doesn't require escaping.
- print_entries(f, &self.structs, "Structs", "structs");
- print_entries(f, &self.enums, "Enums", "enums");
- print_entries(f, &self.unions, "Unions", "unions");
- print_entries(f, &self.primitives, "Primitives", "primitives");
- print_entries(f, &self.traits, "Traits", "traits");
- print_entries(f, &self.macros, "Macros", "macros");
- print_entries(f, &self.attributes, "Attribute Macros", "attributes");
- print_entries(f, &self.derives, "Derive Macros", "derives");
- print_entries(f, &self.functions, "Functions", "functions");
- print_entries(f, &self.typedefs, "Typedefs", "typedefs");
- print_entries(f, &self.trait_aliases, "Trait Aliases", "trait-aliases");
- print_entries(f, &self.opaque_tys, "Opaque Types", "opaque-types");
- print_entries(f, &self.statics, "Statics", "statics");
- print_entries(f, &self.constants, "Constants", "constants")
+ print_entries(f, &self.structs, ItemSection::Structs);
+ print_entries(f, &self.enums, ItemSection::Enums);
+ print_entries(f, &self.unions, ItemSection::Unions);
+ print_entries(f, &self.primitives, ItemSection::PrimitiveTypes);
+ print_entries(f, &self.traits, ItemSection::Traits);
+ print_entries(f, &self.macros, ItemSection::Macros);
+ print_entries(f, &self.attribute_macros, ItemSection::AttributeMacros);
+ print_entries(f, &self.derive_macros, ItemSection::DeriveMacros);
+ print_entries(f, &self.functions, ItemSection::Functions);
+ print_entries(f, &self.typedefs, ItemSection::TypeDefinitions);
+ print_entries(f, &self.trait_aliases, ItemSection::TraitAliases);
+ print_entries(f, &self.opaque_tys, ItemSection::OpaqueTypes);
+ print_entries(f, &self.statics, ItemSection::Statics);
+ print_entries(f, &self.constants, ItemSection::Constants);
}
}
@@ -354,9 +394,7 @@ fn scrape_examples_help(shared: &SharedContext<'_>) -> String {
let mut ids = IdMap::default();
format!(
"<div class=\"main-heading\">\
- <h1 class=\"fqn\">\
- <span class=\"in-band\">About scraped examples</span>\
- </h1>\
+ <h1 class=\"fqn\">About scraped examples</h1>\
</div>\
<div>{}</div>",
Markdown {
@@ -522,7 +560,14 @@ fn portability(item: &clean::Item, parent: Option<&clean::Item>) -> Option<Strin
(cfg, _) => cfg.as_deref().cloned(),
};
- debug!("Portability {:?} - {:?} = {:?}", item.cfg, parent.and_then(|p| p.cfg.as_ref()), cfg);
+ debug!(
+ "Portability {:?} {:?} (parent: {:?}) - {:?} = {:?}",
+ item.name,
+ item.cfg,
+ parent,
+ parent.and_then(|p| p.cfg.as_ref()),
+ cfg
+ );
Some(format!("<div class=\"stab portability\">{}</div>", cfg?.render_long_html()))
}
@@ -535,7 +580,6 @@ fn short_item_info(
parent: Option<&clean::Item>,
) -> Vec<String> {
let mut extra_info = vec![];
- let error_codes = cx.shared.codes;
if let Some(depr @ Deprecation { note, since, is_since_rustc_version: _, suggestion: _ }) =
item.deprecation(cx.tcx())
@@ -559,17 +603,14 @@ fn short_item_info(
if let Some(note) = note {
let note = note.as_str();
- let html = MarkdownHtml(
- note,
- &mut cx.id_map,
- error_codes,
- cx.shared.edition(),
- &cx.shared.playground,
- );
+ let html = MarkdownItemInfo(note, &mut cx.id_map);
message.push_str(&format!(": {}", html.into_string()));
}
extra_info.push(format!(
- "<div class=\"stab deprecated\"><span class=\"emoji\">👎</span> {}</div>",
+ "<div class=\"stab deprecated\">\
+ <span class=\"emoji\">👎</span>\
+ <span>{}</span>\
+ </div>",
message,
));
}
@@ -582,8 +623,9 @@ fn short_item_info(
.filter(|stab| stab.feature != sym::rustc_private)
.map(|stab| (stab.level, stab.feature))
{
- let mut message =
- "<span class=\"emoji\">🔬</span> This is a nightly-only experimental API.".to_owned();
+ let mut message = "<span class=\"emoji\">🔬</span>\
+ <span>This is a nightly-only experimental API."
+ .to_owned();
let mut feature = format!("<code>{}</code>", Escape(feature.as_str()));
if let (Some(url), Some(issue)) = (&cx.shared.issue_tracker_base_url, issue) {
@@ -594,7 +636,7 @@ fn short_item_info(
));
}
- message.push_str(&format!(" ({})", feature));
+ message.push_str(&format!(" ({})</span>", feature));
extra_info.push(format!("<div class=\"stab unstable\">{}</div>", message));
}
@@ -608,10 +650,10 @@ fn short_item_info(
// Render the list of items inside one of the sections "Trait Implementations",
// "Auto Trait Implementations," "Blanket Trait Implementations" (on struct/enum pages).
-fn render_impls(
+pub(crate) fn render_impls(
cx: &mut Context<'_>,
w: &mut Buffer,
- impls: &[&&Impl],
+ impls: &[&Impl],
containing_item: &clean::Item,
toggle_open_by_default: bool,
) {
@@ -816,7 +858,7 @@ fn assoc_method(
href = href,
name = name,
generics = g.print(cx),
- decl = d.full_print(header_len, indent, header.asyncness, cx),
+ decl = d.full_print(header_len, indent, cx),
notable_traits = notable_traits_decl(d, cx),
where_clause = print_where_clause(g, cx, indent, end_newline),
)
@@ -836,12 +878,13 @@ fn assoc_method(
/// Note that it is possible for an unstable function to be const-stable. In that case, the span
/// will include the const-stable version, but no stable version will be emitted, as a natural
/// consequence of the above rules.
-fn render_stability_since_raw(
+fn render_stability_since_raw_with_extra(
w: &mut Buffer,
ver: Option<Symbol>,
const_stability: Option<ConstStability>,
containing_ver: Option<Symbol>,
containing_const_ver: Option<Symbol>,
+ extra_class: &str,
) -> bool {
let stable_version = ver.filter(|inner| !inner.is_empty() && Some(*inner) != containing_ver);
@@ -889,12 +932,30 @@ fn render_stability_since_raw(
}
if !stability.is_empty() {
- write!(w, r#"<span class="since" title="{}">{}</span>"#, title, stability);
+ write!(w, r#"<span class="since{extra_class}" title="{title}">{stability}</span>"#);
}
!stability.is_empty()
}
+#[inline]
+fn render_stability_since_raw(
+ w: &mut Buffer,
+ ver: Option<Symbol>,
+ const_stability: Option<ConstStability>,
+ containing_ver: Option<Symbol>,
+ containing_const_ver: Option<Symbol>,
+) -> bool {
+ render_stability_since_raw_with_extra(
+ w,
+ ver,
+ const_stability,
+ containing_ver,
+ containing_const_ver,
+ "",
+ )
+}
+
fn render_assoc_item(
w: &mut Buffer,
item: &clean::Item,
@@ -1001,6 +1062,47 @@ impl<'a> AssocItemLink<'a> {
}
}
+fn write_impl_section_heading(w: &mut Buffer, title: &str, id: &str) {
+ write!(
+ w,
+ "<h2 id=\"{id}\" class=\"small-section-header\">\
+ {title}\
+ <a href=\"#{id}\" class=\"anchor\"></a>\
+ </h2>"
+ );
+}
+
+pub(crate) fn render_all_impls(
+ w: &mut Buffer,
+ cx: &mut Context<'_>,
+ containing_item: &clean::Item,
+ concrete: &[&Impl],
+ synthetic: &[&Impl],
+ blanket_impl: &[&Impl],
+) {
+ let mut impls = Buffer::empty_from(w);
+ render_impls(cx, &mut impls, concrete, containing_item, true);
+ let impls = impls.into_inner();
+ if !impls.is_empty() {
+ write_impl_section_heading(w, "Trait Implementations", "trait-implementations");
+ write!(w, "<div id=\"trait-implementations-list\">{}</div>", impls);
+ }
+
+ if !synthetic.is_empty() {
+ write_impl_section_heading(w, "Auto Trait Implementations", "synthetic-implementations");
+ w.write_str("<div id=\"synthetic-implementations-list\">");
+ render_impls(cx, w, synthetic, containing_item, false);
+ w.write_str("</div>");
+ }
+
+ if !blanket_impl.is_empty() {
+ write_impl_section_heading(w, "Blanket Implementations", "blanket-implementations");
+ w.write_str("<div id=\"blanket-implementations-list\">");
+ render_impls(cx, w, blanket_impl, containing_item, false);
+ w.write_str("</div>");
+ }
+}
+
fn render_assoc_items(
w: &mut Buffer,
cx: &mut Context<'_>,
@@ -1030,12 +1132,7 @@ fn render_assoc_items_inner(
let mut tmp_buf = Buffer::empty_from(w);
let (render_mode, id) = match what {
AssocItemRender::All => {
- tmp_buf.write_str(
- "<h2 id=\"implementations\" class=\"small-section-header\">\
- Implementations\
- <a href=\"#implementations\" class=\"anchor\"></a>\
- </h2>",
- );
+ write_impl_section_heading(&mut tmp_buf, "Implementations", "implementations");
(RenderMode::Normal, "implementations-list".to_owned())
}
AssocItemRender::DerefFor { trait_, type_, deref_mut_ } => {
@@ -1044,15 +1141,14 @@ fn render_assoc_items_inner(
if let Some(def_id) = type_.def_id(cx.cache()) {
cx.deref_id_map.insert(def_id, id.clone());
}
- write!(
- tmp_buf,
- "<h2 id=\"{id}\" class=\"small-section-header\">\
- <span>Methods from {trait_}&lt;Target = {type_}&gt;</span>\
- <a href=\"#{id}\" class=\"anchor\"></a>\
- </h2>",
- id = id,
- trait_ = trait_.print(cx),
- type_ = type_.print(cx),
+ write_impl_section_heading(
+ &mut tmp_buf,
+ &format!(
+ "<span>Methods from {trait_}&lt;Target = {type_}&gt;</span>",
+ trait_ = trait_.print(cx),
+ type_ = type_.print(cx),
+ ),
+ &id,
);
(RenderMode::ForDeref { mut_: deref_mut_ }, cx.derive_id(id))
}
@@ -1099,49 +1195,12 @@ fn render_assoc_items_inner(
return;
}
- let (synthetic, concrete): (Vec<&&Impl>, Vec<&&Impl>) =
- traits.iter().partition(|t| t.inner_impl().kind.is_auto());
- let (blanket_impl, concrete): (Vec<&&Impl>, _) =
+ let (synthetic, concrete): (Vec<&Impl>, Vec<&Impl>) =
+ traits.into_iter().partition(|t| t.inner_impl().kind.is_auto());
+ let (blanket_impl, concrete): (Vec<&Impl>, _) =
concrete.into_iter().partition(|t| t.inner_impl().kind.is_blanket());
- let mut impls = Buffer::empty_from(w);
- render_impls(cx, &mut impls, &concrete, containing_item, true);
- let impls = impls.into_inner();
- if !impls.is_empty() {
- write!(
- w,
- "<h2 id=\"trait-implementations\" class=\"small-section-header\">\
- Trait Implementations\
- <a href=\"#trait-implementations\" class=\"anchor\"></a>\
- </h2>\
- <div id=\"trait-implementations-list\">{}</div>",
- impls
- );
- }
-
- if !synthetic.is_empty() {
- w.write_str(
- "<h2 id=\"synthetic-implementations\" class=\"small-section-header\">\
- Auto Trait Implementations\
- <a href=\"#synthetic-implementations\" class=\"anchor\"></a>\
- </h2>\
- <div id=\"synthetic-implementations-list\">",
- );
- render_impls(cx, w, &synthetic, containing_item, false);
- w.write_str("</div>");
- }
-
- if !blanket_impl.is_empty() {
- w.write_str(
- "<h2 id=\"blanket-implementations\" class=\"small-section-header\">\
- Blanket Implementations\
- <a href=\"#blanket-implementations\" class=\"anchor\"></a>\
- </h2>\
- <div id=\"blanket-implementations-list\">",
- );
- render_impls(cx, w, &blanket_impl, containing_item, false);
- w.write_str("</div>");
- }
+ render_all_impls(w, cx, containing_item, &concrete, &synthetic, &blanket_impl);
}
}
@@ -1217,6 +1276,15 @@ fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String {
if let Some((did, ty)) = decl.output.as_return().and_then(|t| Some((t.def_id(cx.cache())?, t)))
{
+ // Box has pass-through impls for Read, Write, Iterator, and Future when the
+ // boxed type implements one of those. We don't want to treat every Box return
+ // as being notably an Iterator (etc), though, so we exempt it. Pin has the same
+ // issue, with a pass-through impl for Future.
+ if Some(did) == cx.tcx().lang_items().owned_box()
+ || Some(did) == cx.tcx().lang_items().pin_type()
+ {
+ return "".to_string();
+ }
if let Some(impls) = cx.cache().impls.get(&did) {
for i in impls {
let impl_ = i.inner_impl();
@@ -1229,7 +1297,12 @@ fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String {
if let Some(trait_) = &impl_.trait_ {
let trait_did = trait_.def_id();
- if cx.cache().traits.get(&trait_did).map_or(false, |t| t.is_notable) {
+ if cx
+ .cache()
+ .traits
+ .get(&trait_did)
+ .map_or(false, |t| t.is_notable_trait(cx.tcx()))
+ {
if out.is_empty() {
write!(
&mut out,
@@ -1533,7 +1606,7 @@ fn render_impl(
link,
render_mode,
false,
- trait_.map(|t| &t.trait_),
+ trait_,
rendering_params,
);
}
@@ -1550,6 +1623,15 @@ fn render_impl(
rendering_params: ImplRenderingParameters,
) {
for trait_item in &t.items {
+ // Skip over any default trait items that are impossible to call
+ // (e.g. if it has a `Self: Sized` bound on an unsized type).
+ if let Some(impl_def_id) = parent.item_id.as_def_id()
+ && let Some(trait_item_def_id) = trait_item.item_id.as_def_id()
+ && cx.tcx().is_impossible_method((impl_def_id, trait_item_def_id))
+ {
+ continue;
+ }
+
let n = trait_item.name;
if i.items.iter().any(|m| m.name == n) {
continue;
@@ -1584,7 +1666,7 @@ fn render_impl(
&mut default_impl_items,
&mut impl_items,
cx,
- &t.trait_,
+ t,
i.inner_impl(),
&i.impl_item,
parent,
@@ -1668,23 +1750,29 @@ fn render_rightside(
RenderMode::Normal => (item.const_stability(tcx), containing_item.const_stable_since(tcx)),
RenderMode::ForDeref { .. } => (None, None),
};
+ let src_href = cx.src_href(item);
+ let has_src_ref = src_href.is_some();
let mut rightside = Buffer::new();
- let has_stability = render_stability_since_raw(
+ let has_stability = render_stability_since_raw_with_extra(
&mut rightside,
item.stable_since(tcx),
const_stability,
containing_item.stable_since(tcx),
const_stable_since,
+ if has_src_ref { "" } else { " rightside" },
);
- let mut srclink = Buffer::empty_from(w);
- write_srclink(cx, item, &mut srclink);
- if has_stability && !srclink.is_empty() {
- rightside.write_str(" · ");
+ if let Some(l) = src_href {
+ if has_stability {
+ write!(rightside, " · <a class=\"srclink\" href=\"{}\">source</a>", l)
+ } else {
+ write!(rightside, "<a class=\"srclink rightside\" href=\"{}\">source</a>", l)
+ }
}
- rightside.push_buffer(srclink);
- if !rightside.is_empty() {
+ if has_stability && has_src_ref {
write!(w, "<span class=\"rightside\">{}</span>", rightside.into_inner());
+ } else {
+ w.push_buffer(rightside);
}
}
@@ -1700,8 +1788,8 @@ pub(crate) fn render_impl_summary(
// in documentation pages for trait with automatic implementations like "Send" and "Sync".
aliases: &[String],
) {
- let id =
- cx.derive_id(get_id_for_impl(&i.inner_impl().for_, i.inner_impl().trait_.as_ref(), cx));
+ let inner_impl = i.inner_impl();
+ let id = cx.derive_id(get_id_for_impl(&inner_impl.for_, inner_impl.trait_.as_ref(), cx));
let aliases = if aliases.is_empty() {
String::new()
} else {
@@ -1710,12 +1798,12 @@ pub(crate) fn render_impl_summary(
write!(w, "<section id=\"{}\" class=\"impl has-srclink\"{}>", id, aliases);
render_rightside(w, cx, &i.impl_item, containing_item, RenderMode::Normal);
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
- write!(w, "<h3 class=\"code-header in-band\">");
+ write!(w, "<h3 class=\"code-header\">");
if let Some(use_absolute) = use_absolute {
- write!(w, "{}", i.inner_impl().print(use_absolute, cx));
+ write!(w, "{}", inner_impl.print(use_absolute, cx));
if show_def_docs {
- for it in &i.inner_impl().items {
+ for it in &inner_impl.items {
if let clean::AssocTypeItem(ref tydef, ref _bounds) = *it.kind {
w.write_str("<span class=\"where fmt-newline\"> ");
assoc_type(
@@ -1733,11 +1821,11 @@ pub(crate) fn render_impl_summary(
}
}
} else {
- write!(w, "{}", i.inner_impl().print(false, cx));
+ write!(w, "{}", inner_impl.print(false, cx));
}
write!(w, "</h3>");
- let is_trait = i.inner_impl().trait_.is_some();
+ let is_trait = inner_impl.trait_.is_some();
if is_trait {
if let Some(portability) = portability(&i.impl_item, Some(parent)) {
write!(w, "<span class=\"item-info\">{}</span>", portability);
@@ -1774,12 +1862,12 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) {
buffer.write_str("<div class=\"sidebar-elems\">");
if it.is_crate() {
- write!(buffer, "<div class=\"block\"><ul>");
+ write!(buffer, "<ul class=\"block\">");
if let Some(ref version) = cx.cache().crate_version {
write!(buffer, "<li class=\"version\">Version {}</li>", Escape(version));
}
write!(buffer, "<li><a id=\"all-types\" href=\"all.html\">All Items</a></li>");
- buffer.write_str("</ul></div>");
+ buffer.write_str("</ul>");
}
match *it.kind {
@@ -1805,7 +1893,7 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) {
if !it.is_mod() {
let path: String = cx.current.iter().map(|s| s.as_str()).intersperse("::").collect();
- write!(buffer, "<h2 class=\"location\"><a href=\"index.html\">In {}</a></h2>", path);
+ write!(buffer, "<h2><a href=\"index.html\">In {}</a></h2>", path);
}
// Closes sidebar-elems div.
@@ -1931,6 +2019,70 @@ fn small_url_encode(s: String) -> String {
}
}
+pub(crate) fn sidebar_render_assoc_items(
+ cx: &Context<'_>,
+ out: &mut Buffer,
+ id_map: &mut IdMap,
+ concrete: Vec<&Impl>,
+ synthetic: Vec<&Impl>,
+ blanket_impl: Vec<&Impl>,
+) {
+ let format_impls = |impls: Vec<&Impl>, id_map: &mut IdMap| {
+ let mut links = FxHashSet::default();
+
+ let mut ret = impls
+ .iter()
+ .filter_map(|it| {
+ let trait_ = it.inner_impl().trait_.as_ref()?;
+ let encoded =
+ id_map.derive(get_id_for_impl(&it.inner_impl().for_, Some(trait_), cx));
+
+ let i_display = format!("{:#}", trait_.print(cx));
+ let out = Escape(&i_display);
+ let prefix = match it.inner_impl().polarity {
+ ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => "",
+ ty::ImplPolarity::Negative => "!",
+ };
+ let generated = format!("<a href=\"#{}\">{}{}</a>", encoded, prefix, out);
+ if links.insert(generated.clone()) { Some(generated) } else { None }
+ })
+ .collect::<Vec<String>>();
+ ret.sort();
+ ret
+ };
+
+ let concrete_format = format_impls(concrete, id_map);
+ let synthetic_format = format_impls(synthetic, id_map);
+ let blanket_format = format_impls(blanket_impl, id_map);
+
+ if !concrete_format.is_empty() {
+ print_sidebar_block(
+ out,
+ "trait-implementations",
+ "Trait Implementations",
+ concrete_format.iter(),
+ );
+ }
+
+ if !synthetic_format.is_empty() {
+ print_sidebar_block(
+ out,
+ "synthetic-implementations",
+ "Auto Trait Implementations",
+ synthetic_format.iter(),
+ );
+ }
+
+ if !blanket_format.is_empty() {
+ print_sidebar_block(
+ out,
+ "blanket-implementations",
+ "Blanket Implementations",
+ blanket_format.iter(),
+ );
+ }
+}
+
fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
let did = it.item_id.expect_def_id();
let cache = cx.cache();
@@ -1976,68 +2128,15 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
{
let mut derefs = FxHashSet::default();
derefs.insert(did);
- sidebar_deref_methods(cx, out, impl_, v, &mut derefs);
+ sidebar_deref_methods(cx, out, impl_, v, &mut derefs, &mut used_links);
}
- let format_impls = |impls: Vec<&Impl>, id_map: &mut IdMap| {
- let mut links = FxHashSet::default();
-
- let mut ret = impls
- .iter()
- .filter_map(|it| {
- let trait_ = it.inner_impl().trait_.as_ref()?;
- let encoded =
- id_map.derive(get_id_for_impl(&it.inner_impl().for_, Some(trait_), cx));
-
- let i_display = format!("{:#}", trait_.print(cx));
- let out = Escape(&i_display);
- let prefix = match it.inner_impl().polarity {
- ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => "",
- ty::ImplPolarity::Negative => "!",
- };
- let generated = format!("<a href=\"#{}\">{}{}</a>", encoded, prefix, out);
- if links.insert(generated.clone()) { Some(generated) } else { None }
- })
- .collect::<Vec<String>>();
- ret.sort();
- ret
- };
-
let (synthetic, concrete): (Vec<&Impl>, Vec<&Impl>) =
v.iter().partition::<Vec<_>, _>(|i| i.inner_impl().kind.is_auto());
let (blanket_impl, concrete): (Vec<&Impl>, Vec<&Impl>) =
concrete.into_iter().partition::<Vec<_>, _>(|i| i.inner_impl().kind.is_blanket());
- let concrete_format = format_impls(concrete, &mut id_map);
- let synthetic_format = format_impls(synthetic, &mut id_map);
- let blanket_format = format_impls(blanket_impl, &mut id_map);
-
- if !concrete_format.is_empty() {
- print_sidebar_block(
- out,
- "trait-implementations",
- "Trait Implementations",
- concrete_format.iter(),
- );
- }
-
- if !synthetic_format.is_empty() {
- print_sidebar_block(
- out,
- "synthetic-implementations",
- "Auto Trait Implementations",
- synthetic_format.iter(),
- );
- }
-
- if !blanket_format.is_empty() {
- print_sidebar_block(
- out,
- "blanket-implementations",
- "Blanket Implementations",
- blanket_format.iter(),
- );
- }
+ sidebar_render_assoc_items(cx, out, &mut id_map, concrete, synthetic, blanket_impl);
}
}
}
@@ -2048,6 +2147,7 @@ fn sidebar_deref_methods(
impl_: &Impl,
v: &[Impl],
derefs: &mut FxHashSet<DefId>,
+ used_links: &mut FxHashSet<String>,
) {
let c = cx.cache();
@@ -2080,13 +2180,10 @@ fn sidebar_deref_methods(
.and_then(|did| c.impls.get(&did));
if let Some(impls) = inner_impl {
debug!("found inner_impl: {:?}", impls);
- let mut used_links = FxHashSet::default();
let mut ret = impls
.iter()
.filter(|i| i.inner_impl().trait_.is_none())
- .flat_map(|i| {
- get_methods(i.inner_impl(), true, &mut used_links, deref_mut, cx.tcx())
- })
+ .flat_map(|i| get_methods(i.inner_impl(), true, used_links, deref_mut, cx.tcx()))
.collect::<Vec<_>>();
if !ret.is_empty() {
let id = if let Some(target_def_id) = real_target.def_id(c) {
@@ -2115,7 +2212,14 @@ fn sidebar_deref_methods(
.map(|t| Some(t.def_id()) == cx.tcx().lang_items().deref_trait())
.unwrap_or(false)
}) {
- sidebar_deref_methods(cx, out, target_deref_impl, target_impls, derefs);
+ sidebar_deref_methods(
+ cx,
+ out,
+ target_deref_impl,
+ target_impls,
+ derefs,
+ used_links,
+ );
}
}
}
@@ -2163,21 +2267,8 @@ fn extract_for_impl_name(item: &clean::Item, cx: &Context<'_>) -> Option<(String
}
}
-/// Don't call this function directly!!! Use `print_sidebar_title` or `print_sidebar_block` instead!
-fn print_sidebar_title_inner(buf: &mut Buffer, id: &str, title: &str) {
- write!(
- buf,
- "<h3 class=\"sidebar-title\">\
- <a href=\"#{}\">{}</a>\
- </h3>",
- id, title
- );
-}
-
fn print_sidebar_title(buf: &mut Buffer, id: &str, title: &str) {
- buf.push_str("<div class=\"block\">");
- print_sidebar_title_inner(buf, id, title);
- buf.push_str("</div>");
+ write!(buf, "<h3><a href=\"#{}\">{}</a></h3>", id, title);
}
fn print_sidebar_block(
@@ -2186,13 +2277,12 @@ fn print_sidebar_block(
title: &str,
items: impl Iterator<Item = impl fmt::Display>,
) {
- buf.push_str("<div class=\"block\">");
- print_sidebar_title_inner(buf, id, title);
- buf.push_str("<ul>");
+ print_sidebar_title(buf, id, title);
+ buf.push_str("<ul class=\"block\">");
for item in items {
write!(buf, "<li>{}</li>", item);
}
- buf.push_str("</ul></div>");
+ buf.push_str("</ul>");
}
fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean::Trait) {
@@ -2302,9 +2392,54 @@ fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean
buf.push_str("</section>")
}
+/// Returns the list of implementations for the primitive reference type, filtering out any
+/// implementations that are on concrete or partially generic types, only keeping implementations
+/// of the form `impl<T> Trait for &T`.
+pub(crate) fn get_filtered_impls_for_reference<'a>(
+ shared: &'a Rc<SharedContext<'_>>,
+ it: &clean::Item,
+) -> (Vec<&'a Impl>, Vec<&'a Impl>, Vec<&'a Impl>) {
+ let def_id = it.item_id.expect_def_id();
+ // If the reference primitive is somehow not defined, exit early.
+ let Some(v) = shared.cache.impls.get(&def_id) else { return (Vec::new(), Vec::new(), Vec::new()) };
+ // Since there is no "direct implementation" on the reference primitive type, we filter out
+ // every implementation which isn't a trait implementation.
+ let traits = v.iter().filter(|i| i.inner_impl().trait_.is_some());
+ let (synthetic, concrete): (Vec<&Impl>, Vec<&Impl>) =
+ traits.partition(|t| t.inner_impl().kind.is_auto());
+
+ let (blanket_impl, concrete): (Vec<&Impl>, _) =
+ concrete.into_iter().partition(|t| t.inner_impl().kind.is_blanket());
+ // Now we keep only references over full generic types.
+ let concrete: Vec<_> = concrete
+ .into_iter()
+ .filter(|t| match t.inner_impl().for_ {
+ clean::Type::BorrowedRef { ref type_, .. } => type_.is_full_generic(),
+ _ => false,
+ })
+ .collect();
+
+ (concrete, synthetic, blanket_impl)
+}
+
fn sidebar_primitive(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item) {
let mut sidebar = Buffer::new();
- sidebar_assoc_items(cx, &mut sidebar, it);
+
+ if it.name.map(|n| n.as_str() != "reference").unwrap_or(false) {
+ sidebar_assoc_items(cx, &mut sidebar, it);
+ } else {
+ let shared = Rc::clone(&cx.shared);
+ let (concrete, synthetic, blanket_impl) = get_filtered_impls_for_reference(&shared, it);
+
+ sidebar_render_assoc_items(
+ cx,
+ &mut sidebar,
+ &mut IdMap::new(),
+ concrete,
+ synthetic,
+ blanket_impl,
+ );
+ }
if !sidebar.is_empty() {
write!(buf, "<section>{}</section>", sidebar.into_inner());
@@ -2371,7 +2506,7 @@ fn sidebar_enum(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, e: &clean:
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
-enum ItemSection {
+pub(crate) enum ItemSection {
Reexports,
PrimitiveTypes,
Modules,
@@ -2523,11 +2658,27 @@ fn item_ty_to_section(ty: ItemType) -> ItemSection {
}
}
-fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
+pub(crate) fn sidebar_module_like(buf: &mut Buffer, item_sections_in_use: FxHashSet<ItemSection>) {
use std::fmt::Write as _;
let mut sidebar = String::new();
+ for &sec in ItemSection::ALL.iter().filter(|sec| item_sections_in_use.contains(sec)) {
+ let _ = write!(sidebar, "<li><a href=\"#{}\">{}</a></li>", sec.id(), sec.name());
+ }
+
+ if !sidebar.is_empty() {
+ write!(
+ buf,
+ "<section>\
+ <ul class=\"block\">{}</ul>\
+ </section>",
+ sidebar
+ );
+ }
+}
+
+fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
let item_sections_in_use: FxHashSet<_> = items
.iter()
.filter(|it| {
@@ -2542,21 +2693,8 @@ fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
})
.map(|it| item_ty_to_section(it.type_()))
.collect();
- for &sec in ItemSection::ALL.iter().filter(|sec| item_sections_in_use.contains(sec)) {
- let _ = write!(sidebar, "<li><a href=\"#{}\">{}</a></li>", sec.id(), sec.name());
- }
- if !sidebar.is_empty() {
- write!(
- buf,
- "<section>\
- <div class=\"block\">\
- <ul>{}</ul>\
- </div>\
- </section>",
- sidebar
- );
- }
+ sidebar_module_like(buf, item_sections_in_use);
}
fn sidebar_foreign_type(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item) {
@@ -2614,8 +2752,8 @@ fn collect_paths_for_type(first_ty: clean::Type, cache: &Cache) -> Vec<String> {
clean::Type::BorrowedRef { type_, .. } => {
work.push_back(*type_);
}
- clean::Type::QPath { self_type, trait_, .. } => {
- work.push_back(*self_type);
+ clean::Type::QPath(box clean::QPathData { self_type, trait_, .. }) => {
+ work.push_back(self_type);
process_path(trait_.def_id());
}
_ => {}
@@ -2668,7 +2806,7 @@ fn render_call_locations(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Ite
let contents = match fs::read_to_string(&path) {
Ok(contents) => contents,
Err(err) => {
- let span = item.span(tcx).inner();
+ let span = item.span(tcx).map_or(rustc_span::DUMMY_SP, |span| span.inner());
tcx.sess
.span_err(span, &format!("failed to read file {}: {}", path.display(), err));
return false;
@@ -2764,11 +2902,10 @@ fn render_call_locations(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Ite
sources::print_src(
w,
contents_subset,
- call_data.edition,
file_span,
cx,
&root_path,
- Some(highlight::DecorationInfo(decoration_info)),
+ highlight::DecorationInfo(decoration_info),
sources::SourceContext::Embedded { offset: line_min },
);
write!(w, "</div></div>");
diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs
index 99cf42919..632781736 100644
--- a/src/librustdoc/html/render/print_item.rs
+++ b/src/librustdoc/html/render/print_item.rs
@@ -16,10 +16,10 @@ use std::fmt;
use std::rc::Rc;
use super::{
- collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_section,
- notable_traits_decl, render_assoc_item, render_assoc_items, render_attributes_in_code,
- render_attributes_in_pre, render_impl, render_stability_since_raw, write_srclink,
- AssocItemLink, Context, ImplRenderingParameters,
+ collect_paths_for_type, document, ensure_trailing_slash, get_filtered_impls_for_reference,
+ item_ty_to_section, notable_traits_decl, render_all_impls, render_assoc_item,
+ render_assoc_items, render_attributes_in_code, render_attributes_in_pre, render_impl,
+ render_rightside, render_stability_since_raw, AssocItemLink, Context, ImplRenderingParameters,
};
use crate::clean;
use crate::config::ModuleSorting;
@@ -371,16 +371,21 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
}
clean::ImportKind::Glob => String::new(),
};
+ let stab_tags = stab_tags.unwrap_or_default();
+ let (stab_tags_before, stab_tags_after) = if stab_tags.is_empty() {
+ ("", "")
+ } else {
+ ("<div class=\"item-right docblock-short\">", "</div>")
+ };
write!(
w,
"<div class=\"item-left {stab}{add}import-item\"{id}>\
<code>{vis}{imp}</code>\
</div>\
- <div class=\"item-right docblock-short\">{stab_tags}</div>",
+ {stab_tags_before}{stab_tags}{stab_tags_after}",
stab = stab.unwrap_or_default(),
vis = myitem.visibility.print_with_space(myitem.item_id, cx),
imp = import.print(cx),
- stab_tags = stab_tags.unwrap_or_default(),
);
w.write_str(ITEM_TABLE_ROW_CLOSE);
}
@@ -395,7 +400,7 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
if myitem.fn_header(cx.tcx()).unwrap().unsafety
== hir::Unsafety::Unsafe =>
{
- "<a title=\"unsafe function\" href=\"#\"><sup>âš </sup></a>"
+ "<sup title=\"unsafe function\">âš </sup>"
}
_ => "",
};
@@ -412,6 +417,12 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
let doc_value = myitem.doc_value().unwrap_or_default();
w.write_str(ITEM_TABLE_ROW_OPEN);
+ let docs = MarkdownSummaryLine(&doc_value, &myitem.links(cx)).into_string();
+ let (docs_before, docs_after) = if docs.is_empty() {
+ ("", "")
+ } else {
+ ("<div class=\"item-right docblock-short\">", "</div>")
+ };
write!(
w,
"<div class=\"item-left {stab}{add}module-item\">\
@@ -420,11 +431,10 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
{unsafety_flag}\
{stab_tags}\
</div>\
- <div class=\"item-right docblock-short\">{docs}</div>",
+ {docs_before}{docs}{docs_after}",
name = myitem.name.unwrap(),
visibility_emoji = visibility_emoji,
stab_tags = extra_info_tags(myitem, item, cx.tcx()),
- docs = MarkdownSummaryLine(&doc_value, &myitem.links(cx)).into_string(),
class = myitem.type_(),
add = add,
stab = stab.unwrap_or_default(),
@@ -477,7 +487,7 @@ fn extra_info_tags(item: &clean::Item, parent: &clean::Item, tcx: TyCtxt<'_>) ->
(cfg, _) => cfg.as_deref().cloned(),
};
- debug!("Portability {:?} - {:?} = {:?}", item.cfg, parent.cfg, cfg);
+ debug!("Portability name={:?} {:?} - {:?} = {:?}", item.name, item.cfg, parent.cfg, cfg);
if let Some(ref cfg) = cfg {
tags += &tag_html("portability", &cfg.render_long_plain(), &cfg.render_short_html());
}
@@ -504,7 +514,7 @@ fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &cle
+ name.as_str().len()
+ generics_len;
- wrap_into_docblock(w, |w| {
+ wrap_into_item_decl(w, |w| {
wrap_item(w, "fn", |w| {
render_attributes_in_pre(w, it, "");
w.reserve(header_len);
@@ -520,7 +530,7 @@ fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &cle
name = name,
generics = f.generics.print(cx),
where_clause = print_where_clause(&f.generics, cx, 0, Ending::Newline),
- decl = f.decl.full_print(header_len, 0, header.asyncness, cx),
+ decl = f.decl.full_print(header_len, 0, cx),
notable_traits = notable_traits_decl(&f.decl, cx),
);
});
@@ -543,7 +553,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
cx.tcx().trait_def(t.def_id).must_implement_one_of.clone();
// Output the trait definition
- wrap_into_docblock(w, |w| {
+ wrap_into_item_decl(w, |w| {
wrap_item(w, "trait", |w| {
render_attributes_in_pre(w, it, "");
write!(
@@ -706,17 +716,10 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
document(&mut content, cx, m, Some(t), HeadingOffset::H5);
let toggled = !content.is_empty();
if toggled {
- write!(w, "<details class=\"rustdoc-toggle\" open><summary>");
- }
- write!(w, "<div id=\"{}\" class=\"method has-srclink\">", id);
- write!(w, "<div class=\"rightside\">");
-
- let has_stability = render_stability_since(w, m, t, cx.tcx());
- if has_stability {
- w.write_str(" · ");
+ write!(w, "<details class=\"rustdoc-toggle method-toggle\" open><summary>");
}
- write_srclink(cx, m, w);
- write!(w, "</div>");
+ write!(w, "<section id=\"{}\" class=\"method has-srclink\">", id);
+ render_rightside(w, cx, m, t, RenderMode::Normal);
write!(w, "<h4 class=\"code-header\">");
render_assoc_item(
w,
@@ -727,7 +730,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
RenderMode::Normal,
);
w.write_str("</h4>");
- w.write_str("</div>");
+ w.write_str("</section>");
if toggled {
write!(w, "</summary>");
w.push_buffer(content);
@@ -887,7 +890,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
w,
"implementors",
"Implementors",
- "<div class=\"item-list\" id=\"implementors-list\">",
+ "<div id=\"implementors-list\">",
);
for implementor in concrete {
render_implementor(cx, implementor, it, w, &implementor_dups, &[]);
@@ -899,7 +902,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
w,
"synthetic-implementors",
"Auto implementors",
- "<div class=\"item-list\" id=\"synthetic-implementors-list\">",
+ "<div id=\"synthetic-implementors-list\">",
);
for implementor in synthetic {
render_implementor(
@@ -920,7 +923,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
w,
"implementors",
"Implementors",
- "<div class=\"item-list\" id=\"implementors-list\"></div>",
+ "<div id=\"implementors-list\"></div>",
);
if t.is_auto(cx.tcx()) {
@@ -928,7 +931,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
w,
"synthetic-implementors",
"Auto implementors",
- "<div class=\"item-list\" id=\"synthetic-implementors-list\"></div>",
+ "<div id=\"synthetic-implementors-list\"></div>",
);
}
}
@@ -994,7 +997,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
// So C's HTML will have something like this:
//
// ```html
- // <script type="text/javascript" src="/implementors/A/trait.Foo.js"
+ // <script src="/implementors/A/trait.Foo.js"
// data-ignore-extern-crates="A,B" async></script>
// ```
//
@@ -1020,15 +1023,17 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
.map(|cnum| cx.shared.tcx.crate_name(cnum).to_string())
.collect::<Vec<_>>()
.join(",");
+ let (extern_before, extern_after) =
+ if extern_crates.is_empty() { ("", "") } else { (" data-ignore-extern-crates=\"", "\"") };
write!(
w,
- "<script type=\"text/javascript\" src=\"{src}\" data-ignore-extern-crates=\"{extern_crates}\" async></script>",
+ "<script src=\"{src}\"{extern_before}{extern_crates}{extern_after} async></script>",
src = js_src_path.finish(),
);
}
fn item_trait_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::TraitAlias) {
- wrap_into_docblock(w, |w| {
+ wrap_into_item_decl(w, |w| {
wrap_item(w, "trait-alias", |w| {
render_attributes_in_pre(w, it, "");
write!(
@@ -1052,7 +1057,7 @@ fn item_trait_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &
}
fn item_opaque_ty(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::OpaqueTy) {
- wrap_into_docblock(w, |w| {
+ wrap_into_item_decl(w, |w| {
wrap_item(w, "opaque", |w| {
render_attributes_in_pre(w, it, "");
write!(
@@ -1091,7 +1096,7 @@ fn item_typedef(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clea
});
}
- wrap_into_docblock(w, |w| write_content(w, cx, it, t));
+ wrap_into_item_decl(w, |w| write_content(w, cx, it, t));
document(w, cx, it, None, HeadingOffset::H2);
@@ -1105,7 +1110,7 @@ fn item_typedef(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clea
}
fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Union) {
- wrap_into_docblock(w, |w| {
+ wrap_into_item_decl(w, |w| {
wrap_item(w, "union", |w| {
render_attributes_in_pre(w, it, "");
render_union(w, it, Some(&s.generics), &s.fields, "", cx);
@@ -1169,7 +1174,7 @@ fn print_tuple_struct_fields(w: &mut Buffer, cx: &Context<'_>, s: &[clean::Item]
fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::Enum) {
let count_variants = e.variants().count();
- wrap_into_docblock(w, |w| {
+ wrap_into_item_decl(w, |w| {
wrap_item(w, "enum", |w| {
render_attributes_in_pre(w, it, "");
write!(
@@ -1198,7 +1203,8 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
let name = v.name.unwrap();
match *v.kind {
clean::VariantItem(ref var) => match var {
- clean::Variant::CLike => write!(w, "{}", name),
+ // FIXME(#101337): Show discriminant
+ clean::Variant::CLike(..) => write!(w, "{}", name),
clean::Variant::Tuple(ref s) => {
write!(w, "{}(", name);
print_tuple_struct_fields(w, cx, s);
@@ -1260,7 +1266,13 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
w.write_str(")");
}
w.write_str("</code>");
- render_stability_since(w, variant, it, cx.tcx());
+ render_stability_since_raw(
+ w,
+ variant.stable_since(cx.tcx()),
+ variant.const_stability(cx.tcx()),
+ it.stable_since(cx.tcx()),
+ it.const_stable_since(cx.tcx()),
+ );
w.write_str("</h3>");
use crate::clean::Variant;
@@ -1321,24 +1333,14 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
}
fn item_macro(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::Macro) {
- wrap_into_docblock(w, |w| {
- highlight::render_with_highlighting(
- &t.source,
- w,
- Some("macro"),
- None,
- None,
- it.span(cx.tcx()).inner().edition(),
- None,
- None,
- None,
- );
+ wrap_into_item_decl(w, |w| {
+ highlight::render_macro_with_highlighting(&t.source, w);
});
document(w, cx, it, None, HeadingOffset::H2)
}
fn item_proc_macro(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, m: &clean::ProcMacro) {
- wrap_into_docblock(w, |w| {
+ wrap_into_item_decl(w, |w| {
let name = it.name.expect("proc-macros always have names");
match m.kind {
MacroKind::Bang => {
@@ -1370,12 +1372,22 @@ fn item_proc_macro(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, m: &c
}
fn item_primitive(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item) {
+ let def_id = it.item_id.expect_def_id();
document(w, cx, it, None, HeadingOffset::H2);
- render_assoc_items(w, cx, it, it.item_id.expect_def_id(), AssocItemRender::All)
+ if it.name.map(|n| n.as_str() != "reference").unwrap_or(false) {
+ render_assoc_items(w, cx, it, def_id, AssocItemRender::All);
+ } else {
+ // We handle the "reference" primitive type on its own because we only want to list
+ // implementations on generic types.
+ let shared = Rc::clone(&cx.shared);
+ let (concrete, synthetic, blanket_impl) = get_filtered_impls_for_reference(&shared, it);
+
+ render_all_impls(w, cx, it, &concrete, &synthetic, &blanket_impl);
+ }
}
fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &clean::Constant) {
- wrap_into_docblock(w, |w| {
+ wrap_into_item_decl(w, |w| {
wrap_item(w, "const", |w| {
render_attributes_in_code(w, it);
@@ -1424,7 +1436,7 @@ fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &cle
}
fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Struct) {
- wrap_into_docblock(w, |w| {
+ wrap_into_item_decl(w, |w| {
wrap_item(w, "struct", |w| {
render_attributes_in_code(w, it);
render_struct(w, it, Some(&s.generics), s.struct_type, &s.fields, "", true, cx);
@@ -1477,7 +1489,7 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
}
fn item_static(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Static) {
- wrap_into_docblock(w, |w| {
+ wrap_into_item_decl(w, |w| {
wrap_item(w, "static", |w| {
render_attributes_in_code(w, it);
write!(
@@ -1494,7 +1506,7 @@ fn item_static(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
}
fn item_foreign_type(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item) {
- wrap_into_docblock(w, |w| {
+ wrap_into_item_decl(w, |w| {
wrap_item(w, "foreigntype", |w| {
w.write_str("extern {\n");
render_attributes_in_code(w, it);
@@ -1583,11 +1595,11 @@ fn bounds(t_bounds: &[clean::GenericBound], trait_alias: bool, cx: &Context<'_>)
bounds
}
-fn wrap_into_docblock<F>(w: &mut Buffer, f: F)
+fn wrap_into_item_decl<F>(w: &mut Buffer, f: F)
where
F: FnOnce(&mut Buffer),
{
- w.write_str("<div class=\"docblock item-decl\">");
+ w.write_str("<div class=\"item-decl\">");
f(w);
w.write_str("</div>")
}
@@ -1601,21 +1613,6 @@ where
w.write_str("</code></pre>");
}
-fn render_stability_since(
- w: &mut Buffer,
- item: &clean::Item,
- containing_item: &clean::Item,
- tcx: TyCtxt<'_>,
-) -> bool {
- render_stability_since_raw(
- w,
- item.stable_since(tcx),
- item.const_stability(tcx),
- containing_item.stable_since(tcx),
- containing_item.const_stable_since(tcx),
- )
-}
-
fn compare_impl<'a, 'b>(lhs: &'a &&Impl, rhs: &'b &&Impl, cx: &Context<'_>) -> Ordering {
let lhss = format!("{}", lhs.inner_impl().print(false, cx));
let rhss = format!("{}", rhs.inner_impl().print(false, cx));
diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs
index d672f0bb5..8eb9c07f8 100644
--- a/src/librustdoc/html/render/search_index.rs
+++ b/src/librustdoc/html/render/search_index.rs
@@ -544,10 +544,15 @@ fn get_fn_inputs_and_outputs<'tcx>(
(true, _) => (Some(impl_self), &func.generics),
(_, true) => (Some(impl_self), impl_generics),
(false, false) => {
- let mut params = func.generics.params.clone();
- params.extend(impl_generics.params.clone());
- let mut where_predicates = func.generics.where_predicates.clone();
- where_predicates.extend(impl_generics.where_predicates.clone());
+ let params =
+ func.generics.params.iter().chain(&impl_generics.params).cloned().collect();
+ let where_predicates = func
+ .generics
+ .where_predicates
+ .iter()
+ .chain(&impl_generics.where_predicates)
+ .cloned()
+ .collect();
combined_generics = clean::Generics { params, where_predicates };
(Some(impl_self), &combined_generics)
}
diff --git a/src/librustdoc/html/render/span_map.rs b/src/librustdoc/html/render/span_map.rs
index 34d590fb2..151ec2b28 100644
--- a/src/librustdoc/html/render/span_map.rs
+++ b/src/librustdoc/html/render/span_map.rs
@@ -166,25 +166,23 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
fn visit_expr(&mut self, expr: &'tcx rustc_hir::Expr<'tcx>) {
if let ExprKind::MethodCall(segment, ..) = expr.kind {
- if let Some(hir_id) = segment.hir_id {
- let hir = self.tcx.hir();
- let body_id = hir.enclosing_body_owner(hir_id);
- // FIXME: this is showing error messages for parts of the code that are not
- // compiled (because of cfg)!
- //
- // See discussion in https://github.com/rust-lang/rust/issues/69426#issuecomment-1019412352
- let typeck_results = self.tcx.typeck_body(
- hir.maybe_body_owned_by(body_id).expect("a body which isn't a body"),
+ let hir = self.tcx.hir();
+ let body_id = hir.enclosing_body_owner(segment.hir_id);
+ // FIXME: this is showing error messages for parts of the code that are not
+ // compiled (because of cfg)!
+ //
+ // See discussion in https://github.com/rust-lang/rust/issues/69426#issuecomment-1019412352
+ let typeck_results = self
+ .tcx
+ .typeck_body(hir.maybe_body_owned_by(body_id).expect("a body which isn't a body"));
+ if let Some(def_id) = typeck_results.type_dependent_def_id(expr.hir_id) {
+ self.matches.insert(
+ segment.ident.span,
+ match hir.span_if_local(def_id) {
+ Some(span) => LinkFromSrc::Local(clean::Span::new(span)),
+ None => LinkFromSrc::External(def_id),
+ },
);
- if let Some(def_id) = typeck_results.type_dependent_def_id(expr.hir_id) {
- self.matches.insert(
- segment.ident.span,
- match hir.span_if_local(def_id) {
- Some(span) => LinkFromSrc::Local(clean::Span::new(span)),
- None => LinkFromSrc::External(def_id),
- },
- );
- }
}
} else if self.handle_macro(expr.span) {
// We don't want to go deeper into the macro.
diff --git a/src/librustdoc/html/render/write_shared.rs b/src/librustdoc/html/render/write_shared.rs
index 6fb41ff32..85f63c985 100644
--- a/src/librustdoc/html/render/write_shared.rs
+++ b/src/librustdoc/html/render/write_shared.rs
@@ -1,5 +1,4 @@
use std::ffi::OsStr;
-use std::fmt::Write;
use std::fs::{self, File};
use std::io::prelude::*;
use std::io::{self, BufReader};
@@ -10,7 +9,8 @@ use std::sync::LazyLock as Lazy;
use itertools::Itertools;
use rustc_data_structures::flock;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
-use serde::Serialize;
+use serde::ser::SerializeSeq;
+use serde::{Serialize, Serializer};
use super::{collect_paths_for_type, ensure_trailing_slash, Context, BASIC_KEYWORDS};
use crate::clean::Crate;
@@ -284,25 +284,43 @@ pub(super) fn write_shared(
cx.write_shared(SharedResource::Unversioned { name }, contents, &options.emit)?;
}
- fn collect(path: &Path, krate: &str, key: &str) -> io::Result<(Vec<String>, Vec<String>)> {
+ /// Read a file and return all lines that match the `"{crate}":{data},` format,
+ /// and return a tuple `(Vec<DataString>, Vec<CrateNameString>)`.
+ ///
+ /// This forms the payload of files that look like this:
+ ///
+ /// ```javascript
+ /// var data = {
+ /// "{crate1}":{data},
+ /// "{crate2}":{data}
+ /// };
+ /// use_data(data);
+ /// ```
+ ///
+ /// The file needs to be formatted so that *only crate data lines start with `"`*.
+ fn collect(path: &Path, krate: &str) -> io::Result<(Vec<String>, Vec<String>)> {
let mut ret = Vec::new();
let mut krates = Vec::new();
if path.exists() {
- let prefix = format!(r#"{}["{}"]"#, key, krate);
+ let prefix = format!("\"{}\"", krate);
for line in BufReader::new(File::open(path)?).lines() {
let line = line?;
- if !line.starts_with(key) {
+ if !line.starts_with('"') {
continue;
}
if line.starts_with(&prefix) {
continue;
}
- ret.push(line.to_string());
+ if line.ends_with(',') {
+ ret.push(line[..line.len() - 1].to_string());
+ } else {
+ // No comma (it's the case for the last added crate line)
+ ret.push(line.to_string());
+ }
krates.push(
- line[key.len() + 2..]
- .split('"')
- .next()
+ line.split('"')
+ .find(|s| !s.is_empty())
.map(|s| s.to_owned())
.unwrap_or_else(String::new),
);
@@ -311,6 +329,20 @@ pub(super) fn write_shared(
Ok((ret, krates))
}
+ /// Read a file and return all lines that match the <code>"{crate}":{data},\</code> format,
+ /// and return a tuple `(Vec<DataString>, Vec<CrateNameString>)`.
+ ///
+ /// This forms the payload of files that look like this:
+ ///
+ /// ```javascript
+ /// var data = JSON.parse('{\
+ /// "{crate1}":{data},\
+ /// "{crate2}":{data}\
+ /// }');
+ /// use_data(data);
+ /// ```
+ ///
+ /// The file needs to be formatted so that *only crate data lines start with `"`*.
fn collect_json(path: &Path, krate: &str) -> io::Result<(Vec<String>, Vec<String>)> {
let mut ret = Vec::new();
let mut krates = Vec::new();
@@ -485,14 +517,12 @@ if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
};
let content = format!(
- "<h1 class=\"fqn\">\
- <span class=\"in-band\">List of all crates</span>\
- </h1><ul class=\"crate mod\">{}</ul>",
+ "<h1 class=\"fqn\">List of all crates</h1><ul class=\"all-items\">{}</ul>",
krates
.iter()
.map(|s| {
format!(
- "<li><a class=\"crate mod\" href=\"{}index.html\">{}</a></li>",
+ "<li><a href=\"{}index.html\">{}</a></li>",
ensure_trailing_slash(s),
s
)
@@ -526,13 +556,27 @@ if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
},
};
- #[derive(Serialize)]
struct Implementor {
text: String,
synthetic: bool,
types: Vec<String>,
}
+ impl Serialize for Implementor {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ let mut seq = serializer.serialize_seq(None)?;
+ seq.serialize_element(&self.text)?;
+ if self.synthetic {
+ seq.serialize_element(&1)?;
+ seq.serialize_element(&self.types)?;
+ }
+ seq.end()
+ }
+ }
+
let implementors = imps
.iter()
.filter_map(|imp| {
@@ -563,9 +607,9 @@ if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
}
let implementors = format!(
- r#"implementors["{}"] = {};"#,
+ r#""{}":{}"#,
krate.name(cx.tcx()),
- serde_json::to_string(&implementors).unwrap()
+ serde_json::to_string(&implementors).expect("failed serde conversion"),
);
let mut mydst = dst.clone();
@@ -576,16 +620,15 @@ if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
mydst.push(&format!("{}.{}.js", remote_item_type, remote_path[remote_path.len() - 1]));
let (mut all_implementors, _) =
- try_err!(collect(&mydst, krate.name(cx.tcx()).as_str(), "implementors"), &mydst);
+ try_err!(collect(&mydst, krate.name(cx.tcx()).as_str()), &mydst);
all_implementors.push(implementors);
// Sort the implementors by crate so the file will be generated
// identically even with rustdoc running in parallel.
all_implementors.sort();
- let mut v = String::from("(function() {var implementors = {};\n");
- for implementor in &all_implementors {
- writeln!(v, "{}", *implementor).unwrap();
- }
+ let mut v = String::from("(function() {var implementors = {\n");
+ v.push_str(&all_implementors.join(",\n"));
+ v.push_str("\n};");
v.push_str(
"if (window.register_implementors) {\
window.register_implementors(implementors);\
diff --git a/src/librustdoc/html/sources.rs b/src/librustdoc/html/sources.rs
index d0fd637ba..7ab65bff3 100644
--- a/src/librustdoc/html/sources.rs
+++ b/src/librustdoc/html/sources.rs
@@ -11,7 +11,6 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
-use rustc_span::edition::Edition;
use rustc_span::source_map::FileName;
use std::ffi::OsStr;
@@ -54,6 +53,7 @@ impl LocalSourcesCollector<'_, '_> {
fn add_local_source(&mut self, item: &clean::Item) {
let sess = self.tcx.sess;
let span = item.span(self.tcx);
+ let Some(span) = span else { return };
// skip all synthetic "files"
if !is_real_and_local(span, sess) {
return;
@@ -110,6 +110,7 @@ impl DocVisitor for SourceCollector<'_, '_> {
let tcx = self.cx.tcx();
let span = item.span(tcx);
+ let Some(span) = span else { return };
let sess = tcx.sess;
// If we're not rendering sources, there's nothing to do.
@@ -213,11 +214,10 @@ impl SourceCollector<'_, '_> {
print_src(
buf,
contents,
- cx.shared.edition(),
file_span,
cx,
&root_path,
- None,
+ highlight::DecorationInfo::default(),
SourceContext::Standalone,
)
},
@@ -266,16 +266,15 @@ pub(crate) enum SourceContext {
pub(crate) fn print_src(
buf: &mut Buffer,
s: &str,
- edition: Edition,
file_span: rustc_span::Span,
context: &Context<'_>,
root_path: &str,
- decoration_info: Option<highlight::DecorationInfo>,
+ decoration_info: highlight::DecorationInfo,
source_context: SourceContext,
) {
let lines = s.lines().count();
let mut line_numbers = Buffer::empty_from(buf);
- line_numbers.write_str("<pre class=\"line-numbers\">");
+ line_numbers.write_str("<pre class=\"src-line-numbers\">");
match source_context {
SourceContext::Standalone => {
for line in 1..=lines {
@@ -289,15 +288,14 @@ pub(crate) fn print_src(
}
}
line_numbers.write_str("</pre>");
- highlight::render_with_highlighting(
+ let current_href = &context
+ .href_from_span(clean::Span::new(file_span), false)
+ .expect("only local crates should have sources emitted");
+ highlight::render_source_with_highlighting(
s,
buf,
- None,
- None,
- None,
- edition,
- Some(line_numbers),
- Some(highlight::HrefContext { context, file_span, root_path }),
+ line_numbers,
+ highlight::HrefContext { context, file_span, root_path, current_href },
decoration_info,
);
}
diff --git a/src/librustdoc/html/static/css/noscript.css b/src/librustdoc/html/static/css/noscript.css
index 0a19a99ab..301f03a16 100644
--- a/src/librustdoc/html/static/css/noscript.css
+++ b/src/librustdoc/html/static/css/noscript.css
@@ -14,7 +14,11 @@ rules.
display: none;
}
-.sub {
+nav.sub {
/* The search bar and related controls don't work without JS */
display: none;
}
+
+.source .sidebar {
+ display: none;
+}
diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css
index 83fe14550..1cc954a98 100644
--- a/src/librustdoc/html/static/css/rustdoc.css
+++ b/src/librustdoc/html/static/css/rustdoc.css
@@ -132,19 +132,29 @@ h1, h2, h3, h4, h5, h6 {
font-weight: 500;
}
h1, h2, h3, h4 {
- margin: 20px 0 15px 0;
+ margin: 25px 0 15px 0;
padding-bottom: 6px;
}
.docblock h3, .docblock h4, h5, h6 {
margin: 15px 0 5px 0;
}
+.docblock > h2:first-child,
+.docblock > h3:first-child,
+.docblock > h4:first-child,
+.docblock > h5:first-child,
+.docblock > h6:first-child {
+ margin-top: 0;
+}
h1.fqn {
margin: 0;
padding: 0;
- border-bottom-color: var(--headings-border-bottom-color);
-}
-h2, h3, h4 {
- border-bottom-color: var(--headings-border-bottom-color);
+ flex-grow: 1;
+ /* We use overflow-wrap: break-word for Safari, which doesn't recognize
+ `anywhere`: https://developer.mozilla.org/en-US/docs/Web/CSS/overflow-wrap */
+ overflow-wrap: break-word;
+ /* Then override it with `anywhere`, which is required to make non-Safari browsers break
+ more aggressively when we want them to. */
+ overflow-wrap: anywhere;
}
.main-heading {
display: flex;
@@ -153,9 +163,6 @@ h2, h3, h4 {
padding-bottom: 6px;
margin-bottom: 15px;
}
-.main-heading a:hover {
- text-decoration: underline;
-}
#toggle-all-docs {
text-decoration: none;
}
@@ -164,7 +171,7 @@ h2, h3, h4 {
Rustdoc-generated h2 section headings (e.g. "Implementations", "Required Methods", etc)
Underlines elsewhere in the documentation break up visual flow and tend to invert
section hierarchies. */
-h2,
+.content h2,
.top-doc .docblock > h3,
.top-doc .docblock > h4 {
border-bottom: 1px solid var(--headings-border-bottom-color);
@@ -177,60 +184,31 @@ h4.code-header {
}
.code-header {
font-weight: 600;
- border-bottom-style: none;
margin: 0;
padding: 0;
- margin-top: 0.6em;
- margin-bottom: 0.4em;
-}
-.impl,
-.impl-items .method,
-.methods .method,
-.impl-items .type,
-.methods .type,
-.impl-items .associatedconstant,
-.methods .associatedconstant,
-.impl-items .associatedtype,
-.methods .associatedtype {
- flex-basis: 100%;
- font-weight: 600;
- position: relative;
-}
-
-div.impl-items > div {
- padding-left: 0;
}
+#crate-search,
h1, h2, h3, h4, h5, h6,
.sidebar,
.mobile-topbar,
-a.source,
.search-input,
.search-results .result-name,
-.content table td:first-child > a,
.item-left > a,
.out-of-band,
span.since,
-#source-sidebar, #sidebar-toggle,
-details.rustdoc-toggle > summary::before,
-div.impl-items > div:not(.docblock):not(.item-info),
-.content ul.crate a.crate,
a.srclink,
-#main-content > .since,
-#help-button > button,
+#help-button > a,
details.rustdoc-toggle.top-doc > summary,
-details.rustdoc-toggle.top-doc > summary::before,
details.rustdoc-toggle.non-exhaustive > summary,
-details.rustdoc-toggle.non-exhaustive > summary::before,
.scraped-example-title,
.more-examples-toggle summary, .more-examples-toggle .hide-more,
.example-links a,
/* This selector is for the items listed in the "all items" page. */
-#main-content > ul.docblock > li > a {
+ul.all-items {
font-family: "Fira Sans", Arial, NanumBarunGothic, sans-serif;
}
-h1, h2, h3, h4,
a#toggle-all-docs,
a.anchor,
.small-section-header a,
@@ -239,17 +217,62 @@ pre.rust a,
.sidebar h2 a,
.sidebar h3 a,
.mobile-topbar h2 a,
-.in-band a,
+h1 a,
.search-results a,
.module-item .stab,
.import-item .stab,
.result-name .primitive > i, .result-name .keyword > i,
-.content .method .where,
-.content .fn .where,
-.content .where.fmt-newline {
+.method .where,
+.fn .where,
+.where.fmt-newline {
color: var(--main-color);
}
+.content span.enum, .content a.enum,
+.content span.struct, .content a.struct,
+.content span.union, .content a.union,
+.content span.primitive, .content a.primitive,
+.content span.type, .content a.type,
+.content span.foreigntype, .content a.foreigntype {
+ color: var(--type-link-color);
+}
+
+.content span.trait, .content a.trait,
+.content span.traitalias, .content a.traitalias {
+ color: var(--trait-link-color);
+}
+
+.content span.associatedtype, .content a.associatedtype,
+.content span.constant, .content a.constant,
+.content span.static, .content a.static {
+ color: var(--assoc-item-link-color);
+}
+
+.content span.fn, .content a.fn,
+.content .fnname,
+.content span.method, .content a.method,
+.content span.tymethod, .content a.tymethod {
+ color: var(--function-link-color);
+}
+
+.content span.attr, .content a.attr,
+.content span.derive, .content a.derive,
+.content span.macro, .content a.macro {
+ color: var(--macro-link-color);
+}
+
+.content span.mod, .content a.mod {
+ color: var(--mod-link-color);
+}
+
+.content span.keyword, .content a.keyword {
+ color: var(--keyword-link-color);
+}
+
+a {
+ color: var(--link-color);
+}
+
ol, ul {
padding-left: 24px;
}
@@ -263,32 +286,14 @@ p {
https://www.w3.org/WAI/WCAG21/Understanding/visual-presentation.html */
margin: 0 0 .75em 0;
}
-
-summary {
- outline: none;
+/* For the last child of a div, the margin will be taken care of
+ by the margin-top of the next item. */
+p:last-child {
+ margin: 0;
}
/* Fix some style changes due to normalize.css 8 */
-td,
-th {
- padding: 0;
-}
-
-table {
- border-collapse: collapse;
-}
-
-button,
-input,
-optgroup,
-select,
-textarea {
- color: inherit;
- font: inherit;
- margin: 0;
-}
-
button {
/* Buttons on Safari have different default padding than other platforms. Make them the same. */
padding: 1px 6px;
@@ -339,9 +344,6 @@ code, pre, a.test-arrow, .code-header {
pre {
padding: 14px;
}
-.docblock.item-decl {
- margin-left: 0;
-}
.item-decl pre {
overflow-x: auto;
}
@@ -354,37 +356,15 @@ img {
max-width: 100%;
}
-li {
- position: relative;
-}
-
.source .content {
- max-width: none;
overflow: visible;
- margin-left: 0px;
-}
-
-nav.sub {
- position: relative;
- font-size: 1rem;
-}
-
-.sub-container {
- display: flex;
- flex-direction: row;
- flex-wrap: nowrap;
}
.sub-logo-container {
- display: none;
- margin-right: 20px;
+ line-height: 0;
}
-.source .sub-logo-container {
- display: block;
-}
-
-.source .sub-logo-container > img {
+.sub-logo-container > img {
height: 60px;
width: 60px;
object-fit: contain;
@@ -396,7 +376,7 @@ nav.sub {
.sidebar {
font-size: 0.875rem;
- width: 250px;
+ width: 200px;
min-width: 200px;
overflow-y: scroll;
position: sticky;
@@ -405,15 +385,6 @@ nav.sub {
left: 0;
}
-.sidebar-elems,
-.sidebar > .location {
- padding-left: 24px;
-}
-
-.sidebar .location {
- overflow-wrap: anywhere;
-}
-
.rustdoc.source .sidebar {
width: 50px;
min-width: 0px;
@@ -427,10 +398,6 @@ nav.sub {
overflow-y: hidden;
}
-.rustdoc.source .sidebar .sidebar-logo {
- display: none;
-}
-
.source .sidebar, #sidebar-toggle, #source-sidebar {
background-color: var(--sidebar-background-color);
}
@@ -440,16 +407,15 @@ nav.sub {
}
.source .sidebar > *:not(#sidebar-toggle) {
- opacity: 0;
visibility: hidden;
}
.source-sidebar-expanded .source .sidebar {
overflow-y: auto;
+ width: 300px;
}
.source-sidebar-expanded .source .sidebar > *:not(#sidebar-toggle) {
- opacity: 1;
visibility: visible;
}
@@ -507,25 +473,15 @@ nav.sub {
width: 100px;
}
-.location:empty {
- border: none;
-}
-
-.location a:first-of-type {
- font-weight: 500;
-}
-
-.block {
- padding: 0;
-}
-.block ul, .block li {
+ul.block, .block li {
padding: 0;
margin: 0;
list-style: none;
}
.block a,
-h2.location a {
+.sidebar h2 a,
+.sidebar h3 a {
display: block;
padding: 0.25rem;
margin-left: -0.25rem;
@@ -535,8 +491,7 @@ h2.location a {
}
.sidebar h2 {
- border-bottom: none;
- font-weight: 500;
+ overflow-wrap: anywhere;
padding: 0;
margin: 0;
margin-top: 0.7rem;
@@ -545,11 +500,23 @@ h2.location a {
.sidebar h3 {
font-size: 1.125rem; /* 18px */
- font-weight: 500;
padding: 0;
margin: 0;
}
+.sidebar-elems,
+.sidebar > h2 {
+ padding-left: 24px;
+}
+
+.sidebar a, .sidebar .current {
+ color: var(--sidebar-link-color);
+}
+.sidebar .current,
+.sidebar a:hover {
+ background-color: var(--sidebar-current-link-background-color);
+}
+
.sidebar-elems .block {
margin-bottom: 2em;
}
@@ -563,91 +530,70 @@ h2.location a {
}
.source .content pre.rust {
- white-space: pre;
overflow: auto;
padding-left: 0;
}
.rustdoc .example-wrap {
- display: inline-flex;
+ display: flex;
+ position: relative;
margin-bottom: 10px;
}
+/* For the last child of a div, the margin will be taken care of
+ by the margin-top of the next item. */
+.rustdoc .example-wrap:last-child {
+ margin-bottom: 0px;
+}
-.example-wrap {
- position: relative;
- width: 100%;
+.rustdoc .example-wrap > pre {
+ margin: 0;
+ flex-grow: 1;
+ overflow-x: auto;
}
-.example-wrap > pre.line-number {
+.rustdoc .example-wrap > pre.example-line-numbers,
+.rustdoc .example-wrap > pre.src-line-numbers {
+ flex-grow: 0;
overflow: initial;
+ text-align: right;
+ -webkit-user-select: none;
+ -moz-user-select: none;
+ -ms-user-select: none;
+ user-select: none;
+}
+
+.example-line-numbers {
border: 1px solid;
padding: 13px 8px;
- text-align: right;
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
+ border-color: var(--example-line-numbers-border-color);
}
-.example-wrap > pre.rust a:hover {
- text-decoration: underline;
-}
-
-.line-numbers {
- text-align: right;
-}
-.rustdoc:not(.source) .example-wrap > pre:not(.line-number) {
- width: 100%;
- overflow-x: auto;
+.src-line-numbers span {
+ cursor: pointer;
+ color: var(--src-line-numbers-span-color);
}
-
-.rustdoc:not(.source) .example-wrap > pre.line-numbers {
- width: auto;
- overflow-x: visible;
+.src-line-numbers .line-highlighted {
+ background-color: var(--src-line-number-highlighted-background-color);
}
-
-.rustdoc .example-wrap > pre {
- margin: 0;
-}
-
-#search {
- position: relative;
+.src-line-numbers :target {
+ background-color: transparent;
}
.search-loading {
text-align: center;
}
-#results > table {
- width: 100%;
- table-layout: fixed;
-}
-
-.content > .example-wrap pre.line-numbers {
- position: relative;
- -webkit-user-select: none;
- -moz-user-select: none;
- -ms-user-select: none;
- user-select: none;
-}
-.line-numbers span {
- cursor: pointer;
-}
-
.docblock-short {
overflow-wrap: break-word;
overflow-wrap: anywhere;
-}
-.docblock-short p {
- display: inline;
-}
-
-.docblock-short p {
overflow: hidden;
text-overflow: ellipsis;
- margin: 0;
}
/* Wrap non-pre code blocks (`text`) but not (```text```). */
.docblock > :not(pre) > code,
-.docblock-short > :not(pre) > code {
+.docblock-short > code {
white-space: pre-wrap;
}
@@ -663,42 +609,20 @@ h2.location a {
.docblock h5 { font-size: 1rem; }
.docblock h6 { font-size: 0.875rem; }
-.docblock h1, .docblock h2, .docblock h3, .docblock h4, .docblock h5, .docblock h6 {
- border-bottom-color: var(--headings-border-bottom-color);
-}
.docblock {
margin-left: 24px;
position: relative;
}
-.docblock > :not(.information):not(.more-examples-toggle) {
+.docblock > :not(.more-examples-toggle):not(.example-wrap) {
max-width: 100%;
overflow-x: auto;
}
-.content .out-of-band {
+.out-of-band {
flex-grow: 0;
font-size: 1.125rem;
- font-weight: normal;
- float: right;
-}
-
-.method > .code-header, .trait-impl > .code-header {
- max-width: calc(100% - 41px);
- display: block;
-}
-
-.content .in-band {
- flex-grow: 1;
- margin: 0px;
- padding: 0px;
- overflow-wrap: break-word;
- overflow-wrap: anywhere;
-}
-
-.in-band > code, .in-band > .code-header {
- display: inline-block;
}
.docblock code, .docblock-short code,
@@ -709,118 +633,42 @@ pre, .rustdoc.source .example-wrap {
#main-content {
position: relative;
}
-#main-content > .since {
- top: inherit;
-}
-
-.content table:not(.table-display) {
- border-spacing: 0 5px;
-}
-.content td { vertical-align: top; }
-.content td:first-child { padding-right: 20px; }
-.content td p:first-child { margin-top: 0; }
-.content td h1, .content td h2 { margin-left: 0; font-size: 1.125rem; }
-.content tr:first-child td { border-top: 0; }
.docblock table {
margin: .5em 0;
width: calc(100% - 2px);
overflow-x: auto;
display: block;
+ border-collapse: collapse;
}
.docblock table td {
padding: .5em;
- border: 1px dashed;
+ border: 1px dashed var(--border-color);
+ vertical-align: top;
}
.docblock table th {
padding: .5em;
text-align: left;
- border: 1px solid;
-}
-
-.fields + table {
- margin-bottom: 1em;
-}
-
-.content .item-list {
- list-style-type: none;
- padding: 0;
+ border: 1px solid var(--border-color);
}
-.content .multi-column {
- -moz-column-count: 5;
- -moz-column-gap: 2.5em;
- -webkit-column-count: 5;
- -webkit-column-gap: 2.5em;
- column-count: 5;
- column-gap: 2.5em;
-}
-.content .multi-column li { width: 100%; display: inline-block; }
-
-.content > .methods > .method {
- font-size: 1rem;
- position: relative;
-}
/* Shift "where ..." part of method or fn definition down a line */
-.content .method .where,
-.content .fn .where,
-.content .where.fmt-newline {
+.method .where,
+.fn .where,
+.where.fmt-newline {
display: block;
font-size: 0.875rem;
}
-.content .methods > div:not(.notable-traits):not(.method) {
- margin-left: 40px;
- margin-bottom: 15px;
-}
-
-.content .docblock > .impl-items {
- margin-left: 20px;
- margin-top: -34px;
-}
-.content .docblock >.impl-items .table-display {
- margin: 0;
-}
-.content .docblock >.impl-items table td {
- padding: 0;
-}
-.content .docblock > .impl-items .table-display, .impl-items table td {
- border: none;
-}
-
.item-info {
display: block;
-}
-
-.content .item-info code {
- font-size: 0.875rem;
-}
-
-.content .item-info {
- position: relative;
margin-left: 24px;
}
-.sub-variant > div > .item-info {
- margin-top: initial;
-}
-
-.content .impl-items .docblock, .content .impl-items .item-info {
- margin-bottom: .6em;
-}
-
-.content .impl-items > .item-info {
- margin-left: 40px;
-}
-
-.methods > .item-info, .content .impl-items > .item-info {
- margin-top: -8px;
-}
-
-.impl-items {
- flex-basis: 100%;
+.item-info code {
+ font-size: 0.875rem;
}
#main-content > .item-info {
@@ -830,31 +678,26 @@ pre, .rustdoc.source .example-wrap {
nav.sub {
flex-grow: 1;
- margin-bottom: 25px;
-}
-.source nav.sub {
- margin-left: 32px;
+ flex-flow: row nowrap;
+ margin: 4px 0 25px 0;
+ display: flex;
+ align-items: center;
}
-nav.main {
- padding: 20px 0;
- text-align: center;
+.search-form {
+ position: relative;
+ display: flex;
+ height: 34px;
+ flex-grow: 1;
}
-nav.main .current {
- border-top: 1px solid;
- border-bottom: 1px solid;
+.source nav.sub {
+ margin: 0 0 15px 0;
}
-nav.main .separator {
- border: 1px solid;
- display: inline-block;
- height: 23px;
- margin: 0 20px;
+.source .search-form {
+ margin-left: 32px;
}
-nav.sum { text-align: right; }
-nav.sub form { display: inline; }
a {
text-decoration: none;
- background: transparent;
}
.small-section-header {
@@ -867,9 +710,7 @@ a {
display: initial;
}
-.in-band:hover > .anchor, .impl:hover > .anchor, .method.trait-impl:hover > .anchor,
-.type.trait-impl:hover > .anchor, .associatedconstant.trait-impl:hover > .anchor,
-.associatedtype.trait-impl:hover > .anchor {
+.impl:hover > .anchor, .trait-impl:hover > .anchor {
display: inline-block;
position: absolute;
}
@@ -893,12 +734,16 @@ h2.small-section-header > .anchor {
content: '§';
}
-.docblock a:not(.srclink):not(.test-arrow):not(.scrape-help):hover,
-.docblock-short a:not(.srclink):not(.test-arrow):not(.scrape-help):hover, .item-info a {
+.main-heading a:hover,
+.example-wrap > pre.rust a:hover,
+.all-items a:hover,
+.docblock a:not(.test-arrow):not(.scrape-help):hover,
+.docblock-short a:not(.test-arrow):not(.scrape-help):hover,
+.item-info a {
text-decoration: underline;
}
-.block a.current.crate { font-weight: 500; }
+.crate.block a.current { font-weight: 500; }
/* In most contexts we use `overflow-wrap: anywhere` to ensure that we can wrap
as much as needed on mobile (see
@@ -938,61 +783,92 @@ table,
padding-right: 1.25rem;
}
-.search-container {
- position: relative;
- display: flex;
- height: 34px;
-}
-.search-container > * {
- height: 100%;
-}
.search-results-title {
- display: inline;
+ margin-top: 0;
+ white-space: nowrap;
+ /* flex layout allows shrinking the <select> appropriately if it becomes too large */
+ display: inline-flex;
+ max-width: 100%;
+ /* make things look like in a line, despite the fact that we're using a layout
+ with boxes (i.e. from the flex layout) */
+ align-items: baseline;
}
-#search-settings {
- font-size: 1.5rem;
- font-weight: 500;
- margin-bottom: 20px;
+#crate-search-div {
+ display: inline-block;
+ /* ensures that 100% in properties of #crate-search-div:after
+ are relative to the size of this div */
+ position: relative;
+ /* allows this div (and with it the <select>-element "#crate-search") to be shrunk */
+ min-width: 5em;
}
#crate-search {
min-width: 115px;
- margin-top: 5px;
- padding-left: 0.15em;
+ padding: 0;
+ /* keep these two in sync with "@-moz-document url-prefix()" below */
+ padding-left: 4px;
padding-right: 23px;
- border: 1px solid;
+ /* prevents the <select> from overflowing the containing div in case it's shrunk */
+ max-width: 100%;
+ /* contents can overflow because of max-width limit, then show ellipsis */
+ text-overflow: ellipsis;
+ border: 1px solid var(--border-color);
border-radius: 4px;
outline: none;
cursor: pointer;
-moz-appearance: none;
-webkit-appearance: none;
/* Removes default arrow from firefox */
+ text-indent: 0.01px;
+ background-color: var(--main-background-color);
+ color: inherit;
+ line-height: 1.5;
+ font-weight: 500;
+}
+/* cancel stylistic differences in padding in firefox
+for "appearance: none"-style (or equivalent) <select>s */
+@-moz-document url-prefix() {
+ #crate-search {
+ padding-left: 0px; /* == 4px - 4px */
+ padding-right: 19px; /* == 23px - 4px */
+ }
+}
+/* pseudo-element for holding the dropdown-arrow image; needs to be a separate thing
+so that we can apply CSS-filters to change the arrow color in themes */
+#crate-search-div::after {
+ /* lets clicks through! */
+ pointer-events: none;
+ /* completely covers the underlying div */
+ width: 100%;
+ height: 100%;
+ position: absolute;
+ top: 0;
+ left: 0;
+ content: "";
background-repeat: no-repeat;
- background-color: transparent;
background-size: 20px;
- background-position: calc(100% - 1px) 56%;
+ background-position: calc(100% - 2px) 56%;
+ /* image is black color, themes should apply a "filter" property to change the color */
background-image: /* AUTOREPLACE: */url("down-arrow.svg");
- max-width: 100%;
- text-overflow: ellipsis;
}
-.search-container {
- margin-top: 4px;
+#crate-search > option {
+ font-size: 1rem;
}
.search-input {
/* Override Normalize.css: it has a rule that sets
-webkit-appearance: textfield for search inputs. That
causes rounded corners and no border on iOS Safari. */
-webkit-appearance: none;
- /* Override Normalize.css: we have margins and do
- not want to overflow - the `moz` attribute is necessary
- until Firefox 29, too early to drop at this point */
- -moz-box-sizing: border-box !important;
- box-sizing: border-box !important;
outline: none;
- border: 1px solid;
+ border: 1px solid var(--border-color);
border-radius: 2px;
padding: 8px;
font-size: 1rem;
- width: 100%;
+ flex-grow: 1;
+ background-color: var(--button-background-color);
+ color: var(--search-color);
+}
+.search-input:focus {
+ border-color: var(--search-input-focused-border-color);
}
.search-results {
@@ -1015,7 +891,6 @@ table,
.search-results > a {
display: block;
- width: 100%;
/* A little margin ensures the browser's outlining of focused links has room to display. */
margin-left: 2px;
margin-right: 2px;
@@ -1027,17 +902,16 @@ table,
flex-flow: row wrap;
}
-.search-results .result-name, .search-results div.desc, .search-results .result-description {
+.search-results .result-name, .search-results div.desc {
width: 50%;
}
.search-results .result-name {
padding-right: 1em;
}
-.search-results .result-name > span {
- display: inline-block;
- margin: 0;
- font-weight: normal;
+.search-results a:hover,
+.search-results a:focus {
+ background-color: var(--search-result-link-focus-background-color);
}
.popover {
@@ -1048,7 +922,7 @@ table,
display: block;
margin-top: 7px;
border-radius: 3px;
- border: 1px solid;
+ border: 1px solid var(--border-color);
font-size: 1rem;
}
@@ -1057,7 +931,7 @@ table,
content: '';
position: absolute;
right: 11px;
- border: solid;
+ border: solid var(--border-color);
border-width: 1px 1px 0 0;
display: inline-block;
padding: 4px;
@@ -1070,39 +944,35 @@ table,
color: var(--main-color);
}
-#help-button .popover {
+/* use larger max-width for help popover, but not for help.html */
+#help.popover {
max-width: 600px;
}
-#help-button .popover::before {
+#help.popover::before {
right: 48px;
}
-#help-button dt {
+#help dt {
float: left;
clear: left;
display: block;
margin-right: 0.5rem;
}
-#help-button span.top, #help-button span.bottom {
+#help span.top, #help span.bottom {
text-align: center;
display: block;
font-size: 1.125rem;
}
-#help-button span.top {
- text-align: center;
- display: block;
+#help span.top {
margin: 10px 0;
- border-bottom: 1px solid;
+ border-bottom: 1px solid var(--border-color);
padding-bottom: 4px;
margin-bottom: 6px;
}
-#help-button span.bottom {
+#help span.bottom {
clear: both;
- border-top: 1px solid;
-}
-.side-by-side {
- text-align: initial;
+ border-top: 1px solid var(--border-color);
}
.side-by-side > div {
width: 50%;
@@ -1125,14 +995,24 @@ table,
margin-bottom: 5px;
font-size: 0.875rem;
font-weight: normal;
+ color: var(--main-color);
+ background-color: var(--stab-background-color);
}
-.stab p {
- display: inline;
- margin: 0;
+
+.stab.portability > code {
+ background: none;
+ color: var(--stab-code-color);
}
.stab .emoji {
font-size: 1.25rem;
+ margin-right: 0.3rem;
+}
+
+/* This is to prevent the `.stab` elements to overflow the .docblock elements. */
+.docblock .stab {
+ padding: 0 0.125em;
+ margin-bottom: 0;
}
/* Black one-pixel outline around emoji shapes */
@@ -1169,37 +1049,157 @@ table,
.rightside {
padding-left: 12px;
padding-right: 2px;
- position: initial;
+ float: right;
}
-.impl-items .srclink, .impl .srclink, .methods .srclink {
- /* Override header settings otherwise it's too bold */
- font-weight: normal;
- font-size: 1rem;
+.rightside:not(a),
+.out-of-band {
+ color: var(--right-side-color);
}
-.rightside {
- float: right;
+pre.rust {
+ tab-size: 4;
+ -moz-tab-size: 4;
}
-.variants_table {
- width: 100%;
+/* Code highlighting */
+pre.rust .kw {
+ color: var(--code-highlight-kw-color);
+}
+pre.rust .kw-2 {
+ color: var(--code-highlight-kw-2-color);
+}
+pre.rust .lifetime {
+ color: var(--code-highlight-lifetime-color);
+}
+pre.rust .prelude-ty {
+ color: var(--code-highlight-prelude-color);
+}
+pre.rust .prelude-val {
+ color: var(--code-highlight-prelude-val-color);
+}
+pre.rust .string {
+ color: var(--code-highlight-string-color);
+}
+pre.rust .number {
+ color: var(--code-highlight-number-color);
+}
+pre.rust .bool-val {
+ color: var(--code-highlight-literal-color);
+}
+pre.rust .self {
+ color: var(--code-highlight-self-color);
+}
+pre.rust .attribute {
+ color: var(--code-highlight-attribute-color);
+}
+pre.rust .macro,
+pre.rust .macro-nonterminal {
+ color: var(--code-highlight-macro-color);
+}
+pre.rust .question-mark {
+ font-weight: bold;
+ color: var(--code-highlight-question-mark-color);
+}
+pre.rust .comment {
+ color: var(--code-highlight-comment-color);
+}
+pre.rust .doccomment {
+ color: var(--code-highlight-doc-comment-color);
}
-.variants_table tbody tr td:first-child {
- width: 1%; /* make the variant name as small as possible */
+.example-wrap.compile_fail,
+.example-wrap.should_panic {
+ border-left: 2px solid var(--codeblock-error-color);
}
-td.summary-column {
- width: 100%;
+.ignore.example-wrap {
+ border-left: 2px solid var(--codeblock-ignore-color);
}
-.summary {
- padding-right: 0px;
+.example-wrap.compile_fail:hover,
+.example-wrap.should_panic:hover {
+ border-left: 2px solid var(--codeblock-error-hover-color);
}
-pre.rust .question-mark {
+.example-wrap.ignore:hover {
+ border-left: 2px solid var(--codeblock-ignore-hover-color);
+}
+
+.example-wrap.compile_fail .tooltip,
+.example-wrap.should_panic .tooltip {
+ color: var(--codeblock-error-color);
+}
+
+.example-wrap.ignore .tooltip {
+ color: var(--codeblock-ignore-color);
+}
+
+.example-wrap.compile_fail:hover .tooltip,
+.example-wrap.should_panic:hover .tooltip {
+ color: var(--codeblock-error-hover-color);
+}
+
+.example-wrap.ignore:hover .tooltip {
+ color: var(--codeblock-ignore-hover-color);
+}
+
+.example-wrap .tooltip {
+ position: absolute;
+ display: block;
+ cursor: pointer;
+ left: -25px;
+ top: 5px;
+}
+
+.example-wrap .tooltip::after {
+ display: none;
+ text-align: center;
+ padding: 5px 3px 3px 3px;
+ border-radius: 6px;
+ margin-left: 5px;
+ font-size: 1rem;
+ border: 1px solid var(--border-color);
+ position: absolute;
+ width: max-content;
+ top: -2px;
+ z-index: 1;
+}
+
+.example-wrap .tooltip::before {
+ content: " ";
+ position: absolute;
+ top: 50%;
+ left: 16px;
+ margin-top: -5px;
+ border-width: 5px;
+ border-style: solid;
+ display: none;
+ z-index: 1;
+}
+
+.example-wrap.ignore .tooltip::after {
+ content: "This example is not tested";
+}
+.example-wrap.compile_fail .tooltip::after {
+ content: "This example deliberately fails to compile";
+}
+.example-wrap.should_panic .tooltip::after {
+ content: "This example panics";
+}
+.example-wrap.edition .tooltip::after {
+ content: "This code runs with edition " attr(data-edition);
+}
+
+.example-wrap .tooltip:hover::before, .example-wrap .tooltip:hover::after {
+ display: inline;
+}
+
+.example-wrap.compile_fail .tooltip,
+.example-wrap.should_panic .tooltip,
+.example-wrap.ignore .tooltip {
font-weight: bold;
+ font-size: 1.25rem;
}
a.test-arrow {
@@ -1216,12 +1216,10 @@ a.test-arrow {
.example-wrap:hover .test-arrow {
visibility: visible;
}
-a.test-arrow:hover{
- text-decoration: none;
-}
.code-attribute {
font-weight: 300;
+ color: var(--code-attribute-color);
}
.item-spacer {
@@ -1230,7 +1228,6 @@ a.test-arrow:hover{
}
.out-of-band > span.since {
- position: initial;
font-size: 1.25rem;
}
@@ -1238,13 +1235,11 @@ h3.variant {
font-weight: 600;
font-size: 1.125rem;
margin-bottom: 10px;
- border-bottom: none;
}
.sub-variant h4 {
font-size: 1rem;
font-weight: 400;
- border-bottom: none;
margin-top: 0;
margin-bottom: 0;
}
@@ -1258,12 +1253,6 @@ h3.variant {
margin-left: 24px;
}
-.toggle-label {
- display: inline-block;
- margin-left: 4px;
- margin-top: 3px;
-}
-
:target > code, :target > .code-header {
opacity: 1;
}
@@ -1272,61 +1261,6 @@ h3.variant {
padding-right: 3px;
}
-.information {
- position: absolute;
- left: -25px;
- margin-top: 7px;
- z-index: 1;
-}
-
-.tooltip {
- position: relative;
- display: inline-block;
- cursor: pointer;
-}
-
-.tooltip::after {
- display: none;
- text-align: center;
- padding: 5px 3px 3px 3px;
- border-radius: 6px;
- margin-left: 5px;
- font-size: 1rem;
-}
-
-.tooltip.ignore::after {
- content: "This example is not tested";
-}
-.tooltip.compile_fail::after {
- content: "This example deliberately fails to compile";
-}
-.tooltip.should_panic::after {
- content: "This example panics";
-}
-.tooltip.edition::after {
- content: "This code runs with edition " attr(data-edition);
-}
-
-.tooltip::before {
- content: " ";
- position: absolute;
- top: 50%;
- left: 16px;
- margin-top: -5px;
- border-width: 5px;
- border-style: solid;
- display: none;
-}
-
-.tooltip:hover::before, .tooltip:hover::after {
- display: inline;
-}
-
-.tooltip.compile_fail, .tooltip.should_panic, .tooltip.ignore {
- font-weight: bold;
- font-size: 1.25rem;
-}
-
.notable-traits-tooltip {
display: inline-block;
cursor: pointer;
@@ -1356,7 +1290,7 @@ h3.variant {
content: "\00a0\00a0\00a0";
}
-.notable-traits .notable, .notable-traits .docblock {
+.notable-traits .docblock {
margin: 0;
}
@@ -1368,22 +1302,12 @@ h3.variant {
display: block;
}
-.notable-traits .docblock code.content{
+.notable-traits .docblock code.content {
margin: 0;
padding: 0;
font-size: 1.25rem;
}
-/* Example code has the "Run" button that needs to be positioned relative to the pre */
-pre.rust.rust-example-rendered {
- position: relative;
-}
-
-pre.rust {
- tab-size: 4;
- -moz-tab-size: 4;
-}
-
.search-failed {
text-align: center;
margin-top: 20px;
@@ -1402,27 +1326,21 @@ pre.rust {
}
#titles {
- height: 35px;
+ display: flex;
+ flex-direction: row;
+ gap: 1px;
+ margin-bottom: 4px;
}
#titles > button {
- float: left;
- width: 33.3%;
text-align: center;
font-size: 1.125rem;
cursor: pointer;
border: 0;
border-top: 2px solid;
-}
-
-#titles > button:first-child:last-child {
- margin-right: 1px;
- width: calc(100% - 1px);
-}
-
-#titles > button:not(:last-child) {
- margin-right: 1px;
- width: calc(33.3% - 1px);
+ flex: 1;
+ line-height: 1.5;
+ color: inherit;
}
#titles > button > div.count {
@@ -1440,7 +1358,6 @@ pre.rust {
position: sticky;
top: 0;
left: 0;
- font-weight: bold;
font-size: 1.25rem;
border-bottom: 1px solid;
display: flex;
@@ -1451,16 +1368,17 @@ pre.rust {
}
#source-sidebar {
width: 100%;
- z-index: 1;
overflow: auto;
}
#source-sidebar > .title {
font-size: 1.5rem;
text-align: center;
- border-bottom: 1px solid;
+ border-bottom: 1px solid var(--border-color);
margin-bottom: 6px;
}
#sidebar-toggle > button {
+ font-size: inherit;
+ font-weight: bold;
background: none;
color: inherit;
cursor: pointer;
@@ -1483,23 +1401,35 @@ pre.rust {
outline: none;
}
-#copy-path {
- height: 34px;
-}
-#settings-menu > a, #help-button > button, #copy-path {
- padding: 5px;
+#settings-menu > a, #help-button > a, #copy-path {
width: 33px;
- border: 1px solid;
- border-radius: 2px;
cursor: pointer;
+ line-height: 1.5;
}
-#settings-menu {
- padding: 0;
-}
-#settings-menu > a, #help-button > button {
+
+#settings-menu > a, #help-button > a {
padding: 5px;
height: 100%;
display: block;
+ background-color: var(--button-background-color);
+ border: 1px solid var(--border-color);
+ border-radius: 2px;
+}
+
+#copy-path {
+ color: var(--copy-path-button-color);
+ background: var(--main-background-color);
+ height: 34px;
+ margin-left: 10px;
+ padding: 0;
+ padding-left: 2px;
+ border: 0;
+}
+#copy-path > img {
+ filter: var(--copy-path-img-filter);
+}
+#copy-path:hover > img {
+ filter: var(--copy-path-img-hover-filter);
}
@keyframes rotating {
@@ -1514,26 +1444,7 @@ pre.rust {
animation: rotating 2s linear infinite;
}
-.setting-line .radio-line input:checked {
- box-shadow: inset 0 0 0 3px var(--main-background-color);
- background-color: var(--settings-input-color);
-}
-.setting-line .radio-line input:focus {
- box-shadow: 0 0 1px 1px var(--settings-input-color);
-}
-/* In here we combine both `:focus` and `:checked` properties. */
-.setting-line .radio-line input:checked:focus {
- box-shadow: inset 0 0 0 3px var(--main-background-color),
- 0 0 2px 2px var(--settings-input-color);
-}
-.setting-line .radio-line input:hover {
- border-color: var(--settings-input-color) !important;
-}
-input:checked + .slider {
- background-color: var(--settings-input-color);
-}
-
-#help-button > button {
+#help-button > a {
text-align: center;
/* Rare exception to specifying font sizes in rem. Since this is acting
as an icon, it's okay to specify their sizes in pixels. */
@@ -1541,105 +1452,50 @@ input:checked + .slider {
padding-top: 2px;
}
-#copy-path {
- background: initial;
- margin-left: 10px;
- padding: 0;
- padding-left: 2px;
- border: 0;
-}
-
-#theme-choices {
- display: none;
- position: absolute;
- left: 0;
- top: 28px;
- border: 1px solid;
- border-radius: 3px;
- z-index: 1;
- cursor: pointer;
-}
-
-#theme-choices > button {
- border: none;
- width: 100%;
- padding: 4px 8px;
- text-align: center;
- background: rgba(0,0,0,0);
- overflow-wrap: normal;
-}
-
-#theme-choices > button:not(:first-child) {
- border-top: 1px solid;
-}
-
kbd {
display: inline-block;
padding: 3px 5px;
font: 15px monospace;
line-height: 10px;
vertical-align: middle;
- border: solid 1px;
+ border: solid 1px var(--border-color);
border-radius: 3px;
cursor: default;
}
-.hidden-by-impl-hider,
-.hidden-by-usual-hider {
- /* important because of conflicting rule for small screens */
- display: none !important;
-}
-
-#implementations-list > h3 > span.in-band {
- width: 100%;
-}
-
-.table-display {
- width: 100%;
- border: 0;
- border-collapse: collapse;
- border-spacing: 0;
- font-size: 1rem;
-}
-
-.table-display tr td:first-child {
- padding-right: 0;
-}
-
-.table-display tr td:last-child {
- float: right;
-}
-.table-display .out-of-band {
- position: relative;
- font-size: 1.125rem;
- display: block;
+ul.all-items > li {
+ list-style: none;
}
-.table-display td:hover .anchor {
- display: block;
- top: 2px;
- left: -5px;
+details.dir-entry {
+ padding-left: 4px;
}
-#main-content > ul {
- padding-left: 10px;
-}
-#main-content > ul > li {
- list-style: none;
+details.dir-entry > summary::after {
+ content: " â–º";
+ position: absolute;
+ left: -15px;
+ top: 0px;
+ font-size: 80%;
+ padding: 2px 0px;
+ /* set width to cover gap between arrow and text */
+ width: 25px;
}
-.non-exhaustive {
- margin-bottom: 1em;
+details[open].dir-entry > summary::after {
+ content: " â–¼";
}
-details.dir-entry {
- padding-left: 4px;
+details.dir-entry > summary::-webkit-details-marker,
+details.dir-entry > summary::marker {
+ display: none;
}
details.dir-entry > summary {
margin: 0 0 0 13px;
- list-style-position: outside;
+ list-style: none;
cursor: pointer;
+ position: relative;
}
details.dir-entry div.folders, details.dir-entry div.files {
@@ -1650,6 +1506,17 @@ details.dir-entry a {
display: block;
}
+/* We use CSS containment on the details elements because most sizeable elements
+ of the page are contained in one of these. This also makes re-rendering
+ faster on document changes (like closing and opening toggles).
+ Unfortunately we can't yet specify contain: content or contain: strict
+ because the [-]/[+] toggles extend past the boundaries of the <details>
+ https://developer.mozilla.org/en-US/docs/Web/CSS/contain */
+details.rustdoc-toggle {
+ contain: layout;
+ position: relative;
+}
+
/* The hideme class is used on summary tags that contain a span with
placeholder text shown only when the toggle is closed. For instance,
"Expand description" or "Show methods". */
@@ -1659,6 +1526,8 @@ details.rustdoc-toggle > summary.hideme {
details.rustdoc-toggle > summary {
list-style: none;
+ /* focus outline is shown on `::before` instead of this */
+ outline: none;
}
details.rustdoc-toggle > summary::-webkit-details-marker,
details.rustdoc-toggle > summary::marker {
@@ -1681,6 +1550,11 @@ details.rustdoc-toggle > summary::before {
opacity: .5;
}
+details.rustdoc-toggle > summary.hideme > span,
+.more-examples-toggle summary, .more-examples-toggle .hide-more {
+ color: var(--toggles-color);
+}
+
/* Screen readers see the text version at the end the line.
Visual readers see the icon at the start of the line, but small and transparent. */
details.rustdoc-toggle > summary::after {
@@ -1701,6 +1575,15 @@ details.rustdoc-toggle > summary:hover::before {
opacity: 1;
}
+details.rustdoc-toggle > summary:focus-visible::before {
+ /* The SVG is black, and gets turned white using a filter in the dark themes.
+ Do the same with the outline.
+ The dotted 1px style is copied from Firefox's focus ring style.
+ */
+ outline: 1px dotted #000;
+ outline-offset: 1px;
+}
+
details.rustdoc-toggle.top-doc > summary,
details.rustdoc-toggle.top-doc > summary::before,
details.rustdoc-toggle.non-exhaustive > summary,
@@ -1736,10 +1619,6 @@ details.rustdoc-toggle[open] > summary.hideme {
position: absolute;
}
-details.rustdoc-toggle {
- position: relative;
-}
-
details.rustdoc-toggle[open] > summary.hideme > span {
display: none;
}
@@ -1773,37 +1652,20 @@ details.rustdoc-toggle[open] > summary.hideme::after {
display: inline-block;
}
-/* Media Queries */
-
-/*
-WARNING: RUSTDOC_MOBILE_BREAKPOINT MEDIA QUERY;
-If you update this line, then you also need to update the line with the same warning
-in storage.js plus the media query with (max-width: 700px)
-*/
-@media (min-width: 701px) {
- /* In case there is no documentation before a code block, we need to add some margin at the top
- to prevent an overlay between the "collapse toggle" and the information tooltip.
- However, it's not needed with smaller screen width because the doc/code block is always put
- "one line" below. */
- .docblock > .information:first-child > .tooltip {
- margin-top: 16px;
- }
-
- /* When we expand the sidebar on the source code page, we hide the logo on the left of the
- search bar to have more space. */
- .source-sidebar-expanded .source .sidebar + main .width-limiter .sub-logo-container.rust-logo {
- display: none;
- }
-
- .source-sidebar-expanded .source .sidebar {
- width: 300px;
- }
+/* In case there is no documentation before a code block, we need to add some margin at the top
+to prevent an overlay between the "collapse toggle" and the information tooltip.
+However, it's not needed with smaller screen width because the doc/code block is always put
+"one line" below. */
+.docblock > .example-wrap:first-child .tooltip {
+ margin-top: 16px;
}
+/* Media Queries */
+
/*
WARNING: RUSTDOC_MOBILE_BREAKPOINT MEDIA QUERY
If you update this line, then you also need to update the line with the same warning
-in storage.js plus the media query with (min-width: 701px)
+in storage.js
*/
@media (max-width: 700px) {
/* When linking to an item with an `id` (for instance, by clicking a link in the sidebar,
@@ -1826,18 +1688,17 @@ in storage.js plus the media query with (min-width: 701px)
padding-top: 0px;
}
- .rustdoc,
.main-heading {
flex-direction: column;
}
- .content .out-of-band {
+ .out-of-band {
text-align: left;
margin-left: initial;
padding: initial;
}
- .content .out-of-band .since::before {
+ .out-of-band .since::before {
content: "Since ";
}
@@ -1852,10 +1713,6 @@ in storage.js plus the media query with (min-width: 701px)
display: none;
}
- .sidebar-elems {
- margin-top: 1em;
- }
-
.sidebar {
position: fixed;
top: 45px;
@@ -1887,24 +1744,13 @@ in storage.js plus the media query with (min-width: 701px)
}
.rustdoc.source > .sidebar {
- position: fixed;
- margin: 0;
- z-index: 11;
width: 0;
}
- .mobile-topbar .location a {
- padding: 0;
- margin: 0;
- }
-
- .mobile-topbar .location {
- border: none;
- padding: 0;
+ .mobile-topbar h2 {
+ padding-bottom: 0;
margin: auto 0.5em auto auto;
- text-overflow: ellipsis;
overflow: hidden;
- white-space: nowrap;
/* Rare exception to specifying font sizes in rem. Since the topbar
height is specified in pixels, this also has to be specified in
pixels to avoid overflowing the topbar when the user sets a bigger
@@ -1912,6 +1758,13 @@ in storage.js plus the media query with (min-width: 701px)
font-size: 24px;
}
+ .mobile-topbar h2 a {
+ display: block;
+ text-overflow: ellipsis;
+ overflow: hidden;
+ white-space: nowrap;
+ }
+
.mobile-topbar .logo-container {
max-height: 45px;
}
@@ -1936,56 +1789,30 @@ in storage.js plus the media query with (min-width: 701px)
top: 0;
}
- .source .mobile-topbar {
- display: none;
- }
-
.sidebar-menu-toggle {
width: 45px;
/* Rare exception to specifying font sizes in rem. Since this is acting
as an icon, it's okay to specify its sizes in pixels. */
font-size: 32px;
border: none;
+ color: var(--main-color);
}
.sidebar-elems {
+ margin-top: 1em;
background-color: var(--sidebar-background-color);
}
- .source nav:not(.sidebar).sub {
- margin-left: 32px;
- }
-
.content {
margin-left: 0px;
}
- .source .content {
- margin-top: 10px;
- }
-
- #search {
- margin-left: 0;
- padding: 0;
- }
-
.anchor {
display: none !important;
}
- .notable-traits {
- position: absolute;
- left: -22px;
- top: 24px;
- }
-
#titles > button > div.count {
- float: left;
- width: 100%;
- }
-
- #titles {
- height: 50px;
+ display: block;
}
/* Because of ios, we need to actually have a full height sidebar title so the
@@ -2020,7 +1847,6 @@ in storage.js plus the media query with (min-width: 701px)
border-top-right-radius: 3px;
border-bottom-right-radius: 3px;
cursor: pointer;
- font-weight: bold;
border: 1px solid;
border-left: 0;
}
@@ -2036,14 +1862,6 @@ in storage.js plus the media query with (min-width: 701px)
border-bottom: 1px solid;
}
- #source-sidebar {
- z-index: 11;
- }
-
- #main-content > .line-numbers {
- margin-top: 0;
- }
-
.notable-traits .notable-traits-tooltiptext {
left: 0;
top: 100%;
@@ -2071,10 +1889,10 @@ in storage.js plus the media query with (min-width: 701px)
border-bottom: 1px solid #aaa9;
padding: 5px 0px;
}
- .search-results .result-name, .search-results div.desc, .search-results .result-description {
+ .search-results .result-name, .search-results div.desc {
width: 100%;
}
- .search-results div.desc, .search-results .result-description, .item-right {
+ .search-results div.desc, .item-right {
padding-left: 2em;
}
@@ -2092,10 +1910,20 @@ in storage.js plus the media query with (min-width: 701px)
#main-content > div > details.rustdoc-toggle > summary::before {
left: -11px;
}
+
+ /* Align summary-nested and unnested item-info gizmos. */
+ .impl-items > .item-info {
+ margin-left: 34px;
+ }
+
+ .source nav.sub {
+ margin: 0;
+ padding: 8px;
+ }
}
@media print {
- nav.sidebar, nav.sub, .content .out-of-band, a.srclink, #copy-path,
+ nav.sidebar, nav.sub, .out-of-band, a.srclink, #copy-path,
details.rustdoc-toggle[open] > summary::before, details.rustdoc-toggle > summary::before,
details.rustdoc-toggle.top-doc > summary {
display: none;
@@ -2111,19 +1939,6 @@ in storage.js plus the media query with (min-width: 701px)
}
@media (max-width: 464px) {
- #titles, #titles > button {
- height: 73px;
- }
-
- #main-content > table:not(.table-display) td {
- word-break: break-word;
- width: 50%;
- }
-
- #crate-search {
- border-radius: 4px;
- }
-
.docblock {
margin-left: 12px;
}
@@ -2133,15 +1948,15 @@ in storage.js plus the media query with (min-width: 701px)
overflow-wrap: anywhere;
}
- .sub-container {
+ nav.sub {
flex-direction: column;
}
- .sub-logo-container {
- align-self: center;
+ .search-form {
+ align-self: stretch;
}
- .source .sub-logo-container > img {
+ .sub-logo-container > img {
height: 35px;
width: 35px;
}
@@ -2154,23 +1969,24 @@ in storage.js plus the media query with (min-width: 701px)
}
}
-.method-toggle summary,
-.implementors-toggle summary,
-.impl {
+.method-toggle > summary,
+.implementors-toggle > summary,
+.impl,
+#implementors-list > .docblock,
+.impl-items > section,
+.methods > section
+{
margin-bottom: 0.75em;
}
-.method-toggle[open] {
+.method-toggle[open]:not(:last-child),
+.implementors-toggle[open]:not(:last-child) {
margin-bottom: 2em;
}
-.implementors-toggle[open] {
- margin-bottom: 2em;
-}
-
-#trait-implementations-list .method-toggle,
-#synthetic-implementations-list .method-toggle,
-#blanket-implementations-list .method-toggle {
+#trait-implementations-list .method-toggle:not(:last-child),
+#synthetic-implementations-list .method-toggle:not(:last-child),
+#blanket-implementations-list .method-toggle:not(:last-child) {
margin-bottom: 1em;
}
@@ -2207,10 +2023,6 @@ in storage.js plus the media query with (min-width: 701px)
padding-bottom: 0;
}
-.scraped-example:not(.expanded) .code-wrapper pre.line-numbers {
- overflow-x: hidden;
-}
-
.scraped-example .code-wrapper .prev {
position: absolute;
top: 0.25em;
@@ -2253,12 +2065,12 @@ in storage.js plus the media query with (min-width: 701px)
bottom: 0;
}
-.scraped-example .code-wrapper .line-numbers {
+.scraped-example .code-wrapper .src-line-numbers {
margin: 0;
padding: 14px 0;
}
-.scraped-example .code-wrapper .line-numbers span {
+.scraped-example .code-wrapper .src-line-numbers span {
padding: 0 14px;
}
diff --git a/src/librustdoc/html/static/css/settings.css b/src/librustdoc/html/static/css/settings.css
index e82ec0426..83939f63b 100644
--- a/src/librustdoc/html/static/css/settings.css
+++ b/src/librustdoc/html/static/css/settings.css
@@ -12,7 +12,8 @@
margin-right: 0.3em;
height: 1.2rem;
width: 1.2rem;
- border: 1px solid;
+ color: inherit;
+ border: 1px solid currentColor;
outline: none;
-webkit-appearance: none;
cursor: pointer;
@@ -88,3 +89,22 @@ input:checked + .slider:before {
#settings .setting-line {
margin: 1.2em 0.6em;
}
+
+.setting-line .radio-line input:checked {
+ box-shadow: inset 0 0 0 3px var(--main-background-color);
+ background-color: var(--settings-input-color);
+}
+.setting-line .radio-line input:focus {
+ box-shadow: 0 0 1px 1px var(--settings-input-color);
+}
+/* In here we combine both `:focus` and `:checked` properties. */
+.setting-line .radio-line input:checked:focus {
+ box-shadow: inset 0 0 0 3px var(--main-background-color),
+ 0 0 2px 2px var(--settings-input-color);
+}
+.setting-line .radio-line input:hover {
+ border-color: var(--settings-input-color) !important;
+}
+input:checked + .slider {
+ background-color: var(--settings-input-color);
+}
diff --git a/src/librustdoc/html/static/css/themes/ayu.css b/src/librustdoc/html/static/css/themes/ayu.css
index c42cac59b..fdfdb3e19 100644
--- a/src/librustdoc/html/static/css/themes/ayu.css
+++ b/src/librustdoc/html/static/css/themes/ayu.css
@@ -14,6 +14,50 @@ Original by Dempfi (https://github.com/dempfi/ayu)
--scrollbar-thumb-background-color: #5c6773;
--scrollbar-color: #5c6773 #24292f;
--headings-border-bottom-color: #5c6773;
+ --border-color: #5c6773;
+ --button-background-color: #141920;
+ --right-side-color: grey;
+ --code-attribute-color: #999;
+ --toggles-color: #999;
+ --search-input-focused-border-color: #5c6773; /* Same as `--border-color`. */
+ --copy-path-button-color: #fff;
+ --copy-path-img-filter: invert(70%);
+ --copy-path-img-hover-filter: invert(100%);
+ --codeblock-error-hover-color: rgb(255, 0, 0);
+ --codeblock-error-color: rgba(255, 0, 0, .5);
+ --codeblock-ignore-hover-color: rgb(255, 142, 0);
+ --codeblock-ignore-color: rgba(255, 142, 0, .6);
+ --type-link-color: #ffa0a5;
+ --trait-link-color: #39afd7;
+ --assoc-item-link-color: #39afd7;
+ --function-link-color: #fdd687;
+ --macro-link-color: #a37acc;
+ --keyword-link-color: #39afd7;
+ --mod-link-color: #39afd7;
+ --link-color: #39afd7;
+ --sidebar-link-color: #53b1db;
+ --sidebar-current-link-background-color: transparent;
+ --search-result-link-focus-background-color: #3c3c3c;
+ --stab-background-color: #314559;
+ --stab-code-color: #e6e1cf;
+ --search-color: #fff;
+ --code-highlight-kw-color: #ff7733;
+ --code-highlight-kw-2-color: #ff7733;
+ --code-highlight-lifetime-color: #ff7733;
+ --code-highlight-prelude-color: #69f2df;
+ --code-highlight-prelude-val-color: #ff7733;
+ --code-highlight-number-color: #b8cc52;
+ --code-highlight-string-color: #b8cc52;
+ --code-highlight-literal-color: #ff7733;
+ --code-highlight-attribute-color: #e6e1cf;
+ --code-highlight-self-color: #36a3d9;
+ --code-highlight-macro-color: #a37acc;
+ --code-highlight-question-mark-color: #ff9011;
+ --code-highlight-comment-color: #788797;
+ --code-highlight-doc-comment-color: #a1ac88;
+ --example-line-numbers-border-color: none;
+ --src-line-numbers-span-color: #5c6773;
+ --src-line-number-highlighted-background-color: rgba(255, 236, 164, 0.06);
}
.slider {
@@ -29,17 +73,13 @@ input:focus + .slider {
h1, h2, h3, h4 {
color: white;
}
-h1.fqn a {
+h1 a {
color: #fff;
}
h4 {
border: none;
}
-.in-band {
- background-color: #0f1419;
-}
-
.docblock code {
color: #ffb454;
}
@@ -49,7 +89,7 @@ h4 {
.docblock pre > code, pre > code {
color: #e6e1cf;
}
-span code {
+.item-info code {
color: #e6e1cf;
}
.docblock a > code {
@@ -68,7 +108,6 @@ pre, .rustdoc.source .example-wrap {
.sidebar .current,
.sidebar a:hover {
- background-color: transparent;
color: #ffb44c;
}
@@ -76,25 +115,20 @@ pre, .rustdoc.source .example-wrap {
color: #ff7733;
}
-.line-numbers span { color: #5c6773; }
-.line-numbers .line-highlighted {
+.src-line-numbers .line-highlighted {
color: #708090;
- background-color: rgba(255, 236, 164, 0.06);
padding-right: 4px;
border-right: 1px solid #ffb44c;
}
-.docblock table td, .docblock table th {
- border-color: #5c6773;
-}
-
.search-results a:hover {
- background-color: #777;
+ color: #fff !important;
+ background-color: #3c3c3c;
}
.search-results a:focus {
- color: #000 !important;
- background-color: #c6afb3;
+ color: #fff !important;
+ background-color: #3c3c3c;
}
.search-results a {
color: #0096cf;
@@ -105,97 +139,27 @@ pre, .rustdoc.source .example-wrap {
.content .item-info::before { color: #ccc; }
-.content span.foreigntype, .content a.foreigntype { color: #ffa0a5; }
-.content span.union, .content a.union { color: #ffa0a5; }
-.content span.constant, .content a.constant,
-.content span.static, .content a.static { color: #39AFD7; }
-.content span.primitive, .content a.primitive { color: #ffa0a5; }
-.content span.traitalias, .content a.traitalias { color: #39AFD7; }
-.content span.keyword, .content a.keyword { color: #39AFD7; }
-
-.content span.externcrate, .content span.mod, .content a.mod {
- color: #39AFD7;
-}
-.content span.struct, .content a.struct {
- color: #ffa0a5;
-}
-.content span.enum, .content a.enum {
- color: #ffa0a5;
-}
-.content span.trait, .content a.trait {
- color: #39AFD7;
-}
-.content span.type, .content a.type {
- color: #39AFD7;
-}
-.content span.type,
-.content a.type,
-.block a.current.type { color: #39AFD7; }
-.content span.associatedtype,
-.content a.associatedtype,
-.block a.current.associatedtype { color: #39AFD7; }
-.content span.fn, .content a.fn, .content span.method,
-.content a.method, .content span.tymethod,
-.content a.tymethod, .content .fnname {
- color: #fdd687;
-}
-.content span.attr, .content a.attr, .content span.derive,
-.content a.derive, .content span.macro, .content a.macro {
- color: #a37acc;
-}
-
-.sidebar a { color: #53b1db; }
-.sidebar a.current.type { color: #53b1db; }
-.sidebar a.current.associatedtype { color: #53b1db; }
-
-pre.rust .comment { color: #788797; }
-pre.rust .doccomment { color: #a1ac88; }
-
-nav.main .current {
- border-top-color: #5c6773;
- border-bottom-color: #5c6773;
-}
-nav.main .separator {
- border: 1px solid #5c6773;
-}
-a {
- color: #39AFD7;
-}
-
.sidebar h2 a,
.sidebar h3 a {
color: white;
}
-.search-results a {
- color: #0096cf;
-}
body.source .example-wrap pre.rust a {
background: #333;
}
-details.rustdoc-toggle > summary.hideme > span,
-details.rustdoc-toggle > summary::before {
- color: #999;
-}
-
details.rustdoc-toggle > summary::before {
filter: invert(100%);
}
-#crate-search, .search-input {
- background-color: #141920;
- border-color: #424c57;
+#crate-search-div::after {
+ /* match border-color; uses https://codepen.io/sosuke/pen/Pjoqqp */
+ filter: invert(41%) sepia(12%) saturate(487%) hue-rotate(171deg) brightness(94%) contrast(94%);
}
-
-#crate-search {
- /* Without the `!important`, the border-color is ignored for `<select>`...
- It cannot be in the group above because `.search-input` has a different border color on
- hover. */
- border-color: #424c57 !important;
+#crate-search:hover, #crate-search:focus {
+ border-color: #e0e0e0 !important;
}
-
-.search-input {
- color: #ffffff;
+#crate-search-div:hover::after, #crate-search-div:focus-within::after {
+ filter: invert(98%) sepia(12%) saturate(81%) hue-rotate(343deg) brightness(113%) contrast(76%);
}
.module-item .stab,
@@ -203,63 +167,10 @@ details.rustdoc-toggle > summary::before {
color: #000;
}
-/* Created this empty rule to satisfy the theme checks. */
-.stab.empty-impl {}
-.stab.must_implement {}
-
-.stab.unstable,
-.stab.deprecated,
-.stab.portability,
-.stab.empty-impl,
-.stab.must_implement {
- color: #c5c5c5;
- background: #314559 !important;
- border-style: none !important;
- border-radius: 4px;
- padding: 3px 6px 3px 6px;
-}
-
-.stab.portability > code {
- color: #e6e1cf;
- background: none;
-}
-
-.rightside,
-.out-of-band {
- color: grey;
-}
-
.result-name .primitive > i, .result-name .keyword > i {
color: #788797;
}
-.line-numbers :target { background-color: transparent; }
-
-/* Code highlighting */
-pre.rust .number, pre.rust .string { color: #b8cc52; }
-pre.rust .kw, pre.rust .kw-2, pre.rust .prelude-ty,
-pre.rust .bool-val, pre.rust .prelude-val,
-pre.rust .op, pre.rust .lifetime { color: #ff7733; }
-pre.rust .macro, pre.rust .macro-nonterminal { color: #a37acc; }
-pre.rust .question-mark {
- color: #ff9011;
-}
-pre.rust .self {
- color: #36a3d9;
- font-style: italic;
-}
-pre.rust .attribute {
- color: #e6e1cf;
-}
-pre.rust .attribute .ident, pre.rust .attribute .op {
- color: #e6e1cf;
-}
-
-.example-wrap > pre.line-number {
- color: #5c67736e;
- border: none;
-}
-
a.test-arrow {
font-size: 100%;
color: #788797;
@@ -272,64 +183,11 @@ a.test-arrow:hover {
color: #c5c5c5;
}
-.toggle-label,
-.code-attribute {
- color: #999;
-}
-
:target {
background: rgba(255, 236, 164, 0.06);
border-right: 3px solid rgba(255, 180, 76, 0.85);
}
-pre.compile_fail {
- border-left: 2px solid rgba(255,0,0,.4);
-}
-
-pre.compile_fail:hover, .information:hover + pre.compile_fail {
- border-left: 2px solid #f00;
-}
-
-pre.should_panic {
- border-left: 2px solid rgba(255,0,0,.4);
-}
-
-pre.should_panic:hover, .information:hover + pre.should_panic {
- border-left: 2px solid #f00;
-}
-
-pre.ignore {
- border-left: 2px solid rgba(255,142,0,.6);
-}
-
-pre.ignore:hover, .information:hover + pre.ignore {
- border-left: 2px solid #ff9200;
-}
-
-.tooltip.compile_fail {
- color: rgba(255,0,0,.5);
-}
-
-.information > .compile_fail:hover {
- color: #f00;
-}
-
-.tooltip.should_panic {
- color: rgba(255,0,0,.5);
-}
-
-.information > .should_panic:hover {
- color: #f00;
-}
-
-.tooltip.ignore {
- color: rgba(255,142,0,.6);
-}
-
-.information > .ignore:hover {
- color: #ff9200;
-}
-
.search-failed a {
color: #39AFD7;
}
@@ -337,7 +195,6 @@ pre.ignore:hover, .information:hover + pre.ignore {
.tooltip::after {
background-color: #314559;
color: #c5c5c5;
- border: 1px solid #5c6773;
}
.tooltip::before {
@@ -346,11 +203,6 @@ pre.ignore:hover, .information:hover + pre.ignore {
.notable-traits-tooltiptext {
background-color: #314559;
- border-color: #5c6773;
-}
-
-.notable-traits-tooltiptext .notable {
- border-bottom-color: #5c6773;
}
#titles > button.selected {
@@ -378,99 +230,19 @@ individually rather than as a group) */
/* FIXME: these rules should be at the bottom of the file but currently must be
above the `@media (max-width: 700px)` rules due to a bug in the css checker */
/* see https://github.com/rust-lang/rust/pull/71237#issuecomment-618170143 */
-.search-input:focus {}
-.content span.attr,.content a.attr,.block a.current.attr,.content span.derive,.content a.derive,
-.block a.current.derive,.content span.macro,.content a.macro,.block a.current.macro {}
-.content span.struct,.content a.struct,.block a.current.struct {}
-#titles>button:hover,#titles>button.selected {}
-.content span.typedef,.content a.typedef,.block a.current.typedef {}
-.content span.union,.content a.union,.block a.current.union {}
pre.rust .lifetime {}
-.stab.unstable {}
-h2,
-h3:not(.impl):not(.method):not(.type):not(.tymethod), h4:not(.method):not(.type):not(.tymethod) {}
-.content span.enum,.content a.enum,.block a.current.enum {}
-.content span.constant,.content a.constant,.block a.current.constant,.content span.static,
-.content a.static, .block a.current.static {}
-.content span.keyword,.content a.keyword,.block a.current.keyword {}
-pre.rust .comment {}
-.content span.traitalias,.content a.traitalias,.block a.current.traitalias {}
-.content span.fn,.content a.fn,.block a.current.fn,.content span.method,.content a.method,
-.block a.current.method,.content span.tymethod,.content a.tymethod,.block a.current.tymethod,
-.content .fnname {}
pre.rust .kw {}
-pre.rust .self,pre.rust .bool-val,pre.rust .prelude-val,pre.rust .attribute,
-pre.rust .attribute .ident {}
-.content span.foreigntype,.content a.foreigntype,.block a.current.foreigntype {}
-pre.rust .doccomment {}
-.stab.deprecated {}
-.content a.attr,.content a.derive,.content a.macro {}
-.stab.portability {}
-.content span.primitive,.content a.primitive,.block a.current.primitive {}
-.content span.externcrate,.content span.mod,.content a.mod,.block a.current.mod {}
-pre.rust .kw-2,pre.rust .prelude-ty {}
-.content span.trait,.content a.trait,.block a.current.trait {}
-
-.search-results a:focus span {}
-a.result-trait:focus {}
-a.result-traitalias:focus {}
-a.result-mod:focus,
-a.result-externcrate:focus {}
-a.result-mod:focus {}
-a.result-externcrate:focus {}
-a.result-enum:focus {}
-a.result-struct:focus {}
-a.result-union:focus {}
-a.result-fn:focus,
-a.result-method:focus,
-a.result-tymethod:focus {}
-a.result-type:focus {}
-a.result-associatedtype:focus {}
-a.result-foreigntype:focus {}
-a.result-attr:focus,
-a.result-derive:focus,
-a.result-macro:focus {}
-a.result-constant:focus,
-a.result-static:focus {}
-a.result-primitive:focus {}
-a.result-keyword:focus {}
-
-.sidebar a.current.enum {}
-.sidebar a.current.struct {}
-.sidebar a.current.foreigntype {}
-.sidebar a.current.attr,
-.sidebar a.current.derive,
-.sidebar a.current.macro {}
-.sidebar a.current.union {}
-.sidebar a.current.constant
-.sidebar a.current.static {}
-.sidebar a.current.primitive {}
-.sidebar a.current.externcrate
-.sidebar a.current.mod {}
-.sidebar a.current.trait {}
-.sidebar a.current.traitalias {}
-.sidebar a.current.fn,
-.sidebar a.current.method,
-.sidebar a.current.tymethod {}
-.sidebar a.current.keyword {}
-
-@media (max-width: 700px) {
- .sidebar-elems {
- border-right-color: #5c6773;
- }
-}
+#titles > button:hover, #titles > button.selected {}
+pre.rust .self, pre.rust .bool-val, pre.rust .prelude-val, pre.rust .attribute {}
+pre.rust .kw-2, pre.rust .prelude-ty {}
kbd {
color: #c5c5c5;
background-color: #314559;
- border-color: #5c6773;
- border-bottom-color: #5c6773;
box-shadow: inset 0 -1px 0 #5c6773;
}
-#settings-menu > a, #help-button > button {
- border-color: #5c6773;
- background-color: #0f1419;
+#settings-menu > a, #help-button > a {
color: #fff;
}
@@ -478,39 +250,11 @@ kbd {
filter: invert(100);
}
-.popover, .popover::before,
-#help-button span.top, #help-button span.bottom {
- border-color: #5c6773;
-}
-
-#copy-path {
- color: #fff;
-}
-#copy-path > img {
- filter: invert(70%);
-}
-#copy-path:hover > img {
- filter: invert(100%);
-}
-
#settings-menu > a:hover, #settings-menu > a:focus,
-#help-button > button:hover, #help-button > button:focus {
+#help-button > a:hover, #help-button > a:focus {
border-color: #e0e0e0;
}
-#theme-choices {
- border-color: #5c6773;
- background-color: #0f1419;
-}
-
-#theme-choices > button:not(:first-child) {
- border-top-color: #5c6773;
-}
-
-#theme-choices > button:hover, #theme-choices > button:focus {
- background-color: rgba(110, 110, 110, 0.33);
-}
-
.search-results .result-name span.alias {
color: #c5c5c5;
}
@@ -520,7 +264,6 @@ kbd {
#source-sidebar > .title {
color: #fff;
- border-bottom-color: #5c6773;
}
#source-sidebar div.files > a:hover, details.dir-entry summary:hover,
#source-sidebar div.files > a:focus, details.dir-entry summary:focus {
@@ -540,9 +283,6 @@ kbd {
border-color: white;
color: white;
}
-.more-examples-toggle summary, .more-examples-toggle .hide-more {
- color: #999;
-}
.scraped-example .example-wrap .rust span.highlight {
background: rgb(91, 59, 1);
}
diff --git a/src/librustdoc/html/static/css/themes/dark.css b/src/librustdoc/html/static/css/themes/dark.css
index a550eb1c1..361d3d4a2 100644
--- a/src/librustdoc/html/static/css/themes/dark.css
+++ b/src/librustdoc/html/static/css/themes/dark.css
@@ -9,6 +9,50 @@
--scrollbar-thumb-background-color: rgba(32, 34, 37, .6);
--scrollbar-color: rgba(32,34,37,.6) #5a5a5a;
--headings-border-bottom-color: #d2d2d2;
+ --border-color: #e0e0e0;
+ --button-background-color: #f0f0f0;
+ --right-side-color: grey;
+ --code-attribute-color: #999;
+ --toggles-color: #999;
+ --search-input-focused-border-color: #008dfd;
+ --copy-path-button-color: #999;
+ --copy-path-img-filter: invert(50%);
+ --copy-path-img-hover-filter: invert(65%);
+ --codeblock-error-hover-color: rgb(255, 0, 0);
+ --codeblock-error-color: rgba(255, 0, 0, .5);
+ --codeblock-ignore-hover-color: rgb(255, 142, 0);
+ --codeblock-ignore-color: rgba(255, 142, 0, .6);
+ --type-link-color: #2dbfb8;
+ --trait-link-color: #b78cf2;
+ --assoc-item-link-color: #d2991d;
+ --function-link-color: #2bab63;
+ --macro-link-color: #09bd00;
+ --keyword-link-color: #d2991d;
+ --mod-link-color: #d2991d;
+ --link-color: #d2991d;
+ --sidebar-link-color: #fdbf35;
+ --sidebar-current-link-background-color: #444;
+ --search-result-link-focus-background-color: #616161;
+ --stab-background-color: #314559;
+ --stab-code-color: #e6e1cf;
+ --search-color: #111;
+ --code-highlight-kw-color: #ab8ac1;
+ --code-highlight-kw-2-color: #769acb;
+ --code-highlight-lifetime-color: #d97f26;
+ --code-highlight-prelude-color: #769acb;
+ --code-highlight-prelude-val-color: #ee6868;
+ --code-highlight-number-color: #83a300;
+ --code-highlight-string-color: #83a300;
+ --code-highlight-literal-color: #ee6868;
+ --code-highlight-attribute-color: #ee6868;
+ --code-highlight-self-color: #ee6868;
+ --code-highlight-macro-color: #3e999f;
+ --code-highlight-question-mark-color: #ff9011;
+ --code-highlight-comment-color: #8d8d8b;
+ --code-highlight-doc-comment-color: #8ca375;
+ --example-line-numbers-border-color: #4a4949;
+ --src-line-numbers-span-color: #3b91e2;
+ --src-line-number-highlighted-background-color: #0a042f;
}
.slider {
@@ -21,10 +65,6 @@ input:focus + .slider {
box-shadow: 0 0 0 2px #0a84ff, 0 0 0 6px rgba(10, 132, 255, 0.3);
}
-.in-band {
- background-color: #353535;
-}
-
.rust-logo {
filter: drop-shadow(1px 0 0px #fff)
drop-shadow(0 1px 0 #fff)
@@ -32,175 +72,25 @@ input:focus + .slider {
drop-shadow(0 -1px 0 #fff)
}
-.sidebar .current,
-.sidebar a:hover {
- background: #444;
-}
-
-.line-numbers span { color: #3B91E2; }
-.line-numbers .line-highlighted {
- background-color: #0a042f !important;
-}
-
-.docblock table td, .docblock table th {
- border-color: #ddd;
-}
-
-.search-results a:hover {
- background-color: #777;
-}
-
-.search-results a:focus {
- color: #eee !important;
- background-color: #616161;
-}
-.search-results a:focus span { color: #eee !important; }
-a.result-trait:focus { background-color: #013191; }
-a.result-traitalias:focus { background-color: #013191; }
-a.result-mod:focus,
-a.result-externcrate:focus { background-color: #884719; }
-a.result-enum:focus { background-color: #194e9f; }
-a.result-struct:focus { background-color: #194e9f; }
-a.result-union:focus { background-color: #194e9f; }
-a.result-fn:focus,
-a.result-method:focus,
-a.result-tymethod:focus { background-color: #4950ed; }
-a.result-type:focus { background-color: #194e9f; }
-a.result-associatedtype:focus { background-color: #884719; }
-a.result-foreigntype:focus { background-color: #194e9f; }
-a.result-attr:focus,
-a.result-derive:focus,
-a.result-macro:focus { background-color: #217d1c; }
-a.result-constant:focus,
-a.result-static:focus { background-color: #884719; }
-a.result-primitive:focus { background-color: #194e9f; }
-a.result-keyword:focus { background-color: #884719; }
-
.content .item-info::before { color: #ccc; }
-.content span.enum, .content a.enum, .block a.current.enum { color: #2dbfb8; }
-.content span.struct, .content a.struct, .block a.current.struct { color: #2dbfb8; }
-.content span.type, .content a.type, .block a.current.type { color: #2dbfb8; }
-.content span.associatedtype,
-.content a.associatedtype,
-.block a.current.associatedtype { color: #D2991D; }
-.content span.foreigntype, .content a.foreigntype, .block a.current.foreigntype { color: #2dbfb8; }
-.content span.attr, .content a.attr, .block a.current.attr,
-.content span.derive, .content a.derive, .block a.current.derive,
-.content span.macro, .content a.macro, .block a.current.macro { color: #09bd00; }
-.content span.union, .content a.union, .block a.current.union { color: #2dbfb8; }
-.content span.constant, .content a.constant, .block a.current.constant,
-.content span.static, .content a.static, .block a.current.static { color: #D2991D; }
-.content span.primitive, .content a.primitive, .block a.current.primitive { color: #2dbfb8; }
-.content span.externcrate,
-.content span.mod, .content a.mod, .block a.current.mod { color: #D2991D; }
-.content span.trait, .content a.trait, .block a.current.trait { color: #b78cf2; }
-.content span.traitalias, .content a.traitalias, .block a.current.traitalias { color: #b78cf2; }
-.content span.fn, .content a.fn, .block a.current.fn,
-.content span.method, .content a.method, .block a.current.method,
-.content span.tymethod, .content a.tymethod, .block a.current.tymethod,
-.content .fnname{ color: #2BAB63; }
-.content span.keyword, .content a.keyword, .block a.current.keyword { color: #D2991D; }
-
-.sidebar a { color: #fdbf35; }
-.sidebar a.current.enum { color: #12ece2; }
-.sidebar a.current.struct { color: #12ece2; }
-.sidebar a.current.type { color: #12ece2; }
-.sidebar a.current.associatedtype { color: #fdbf35; }
-.sidebar a.current.foreigntype { color: #12ece2; }
-.sidebar a.current.attr,
-.sidebar a.current.derive,
-.sidebar a.current.macro { color: #0be900; }
-.sidebar a.current.union { color: #12ece2; }
-.sidebar a.current.constant
-.sidebar a.current.static { color: #fdbf35; }
-.sidebar a.current.primitive { color: #12ece2; }
-.sidebar a.current.externcrate
-.sidebar a.current.mod { color: #fdbf35; }
-.sidebar a.current.trait { color: #cca7ff; }
-.sidebar a.current.traitalias { color: #cca7ff; }
-.sidebar a.current.fn,
-.sidebar a.current.method,
-.sidebar a.current.tymethod { color: #32d479; }
-.sidebar a.current.keyword { color: #fdbf35; }
-
-pre.rust .comment { color: #8d8d8b; }
-pre.rust .doccomment { color: #8ca375; }
-
-nav.main .current {
- border-top-color: #eee;
- border-bottom-color: #eee;
-}
-nav.main .separator {
- border-color: #eee;
-}
-
-a {
- color: #D2991D;
-}
-
body.source .example-wrap pre.rust a {
background: #333;
}
-details.rustdoc-toggle > summary.hideme > span,
-details.rustdoc-toggle > summary::before {
- color: #999;
-}
-
details.rustdoc-toggle > summary::before {
filter: invert(100%);
}
-#crate-search, .search-input {
- color: #111;
- background-color: #f0f0f0;
- border-color: #f0f0f0;
-}
-
-#crate-search {
- /* Without the `!important`, the border-color is ignored for `<select>`...
- It cannot be in the group above because `.search-input` has a different border color on
- hover. */
- border-color: #f0f0f0 !important;
-}
-
-.search-input {
- border-color: #e0e0e0;
-}
-
-.search-input:focus {
- border-color: #008dfd;
-}
-
-.stab.empty-impl { background: #FFF5D6; border-color: #FFC600; color: #2f2f2f; }
-.stab.unstable { background: #FFF5D6; border-color: #FFC600; color: #2f2f2f; }
-.stab.deprecated { background: #ffc4c4; border-color: #db7b7b; color: #2f2f2f; }
-.stab.must_implement { background: #F3DFFF; border-color: #b07bdb; color: #2f2f2f; }
-.stab.portability { background: #F3DFFF; border-color: #b07bdb; color: #2f2f2f; }
-.stab.portability > code { background: none; }
-
-.rightside,
-.out-of-band {
- color: grey;
+#crate-search-div::after {
+ /* match border-color; uses https://codepen.io/sosuke/pen/Pjoqqp */
+ filter: invert(94%) sepia(0%) saturate(721%) hue-rotate(255deg) brightness(90%) contrast(90%);
}
-
-.line-numbers :target { background-color: transparent; }
-
-/* Code highlighting */
-pre.rust .kw { color: #ab8ac1; }
-pre.rust .kw-2, pre.rust .prelude-ty { color: #769acb; }
-pre.rust .number, pre.rust .string { color: #83a300; }
-pre.rust .self, pre.rust .bool-val, pre.rust .prelude-val,
-pre.rust .attribute, pre.rust .attribute .ident { color: #ee6868; }
-pre.rust .macro, pre.rust .macro-nonterminal { color: #3E999F; }
-pre.rust .lifetime { color: #d97f26; }
-pre.rust .question-mark {
- color: #ff9011;
+#crate-search:hover, #crate-search:focus {
+ border-color: #2196f3 !important;
}
-
-.example-wrap > pre.line-number {
- border-color: #4a4949;
+#crate-search-div:hover::after, #crate-search-div:focus-within::after {
+ filter: invert(69%) sepia(60%) saturate(6613%) hue-rotate(184deg) brightness(100%) contrast(91%);
}
a.test-arrow {
@@ -212,64 +102,11 @@ a.test-arrow:hover{
background-color: #4e8bca;
}
-.toggle-label,
-.code-attribute {
- color: #999;
-}
-
:target {
background-color: #494a3d;
border-right: 3px solid #bb7410;
}
-pre.compile_fail {
- border-left: 2px solid rgba(255,0,0,.8);
-}
-
-pre.compile_fail:hover, .information:hover + pre.compile_fail {
- border-left: 2px solid #f00;
-}
-
-pre.should_panic {
- border-left: 2px solid rgba(255,0,0,.8);
-}
-
-pre.should_panic:hover, .information:hover + pre.should_panic {
- border-left: 2px solid #f00;
-}
-
-pre.ignore {
- border-left: 2px solid rgba(255,142,0,.6);
-}
-
-pre.ignore:hover, .information:hover + pre.ignore {
- border-left: 2px solid #ff9200;
-}
-
-.tooltip.compile_fail {
- color: rgba(255,0,0,.8);
-}
-
-.information > .compile_fail:hover {
- color: #f00;
-}
-
-.tooltip.should_panic {
- color: rgba(255,0,0,.8);
-}
-
-.information > .should_panic:hover {
- color: #f00;
-}
-
-.tooltip.ignore {
- color: rgba(255,142,0,.6);
-}
-
-.information > .ignore:hover {
- color: #ff9200;
-}
-
.search-failed a {
color: #0089ff;
}
@@ -286,11 +123,6 @@ pre.ignore:hover, .information:hover + pre.ignore {
.notable-traits-tooltiptext {
background-color: #111;
- border-color: #777;
-}
-
-.notable-traits-tooltiptext .notable {
- border-bottom-color: #d2d2d2;
}
#titles > button:not(.selected) {
@@ -307,59 +139,21 @@ pre.ignore:hover, .information:hover + pre.ignore {
color: #888;
}
-@media (max-width: 700px) {
- .sidebar-elems {
- border-right-color: #000;
- }
-}
-
kbd {
color: #000;
background-color: #fafbfc;
- border-color: #d1d5da;
- border-bottom-color: #c6cbd1;
box-shadow: inset 0 -1px 0 #c6cbd1;
}
-#settings-menu > a, #help-button > button {
- border-color: #e0e0e0;
- background: #f0f0f0;
+#settings-menu > a, #help-button > a {
color: #000;
}
#settings-menu > a:hover, #settings-menu > a:focus,
-#help-button > button:hover, #help-button > button:focus {
+#help-button > a:hover, #help-button > a:focus {
border-color: #ffb900;
}
-.popover, .popover::before,
-#help-button span.top, #help-button span.bottom {
- border-color: #d2d2d2;
-}
-
-#copy-path {
- color: #999;
-}
-#copy-path > img {
- filter: invert(50%);
-}
-#copy-path:hover > img {
- filter: invert(65%);
-}
-
-#theme-choices {
- border-color: #e0e0e0;
- background-color: #353535;
-}
-
-#theme-choices > button:not(:first-child) {
- border-top-color: #e0e0e0;
-}
-
-#theme-choices > button:hover, #theme-choices > button:focus {
- background-color: #4e4e4e;
-}
-
.search-results .result-name span.alias {
color: #fff;
}
@@ -367,9 +161,6 @@ kbd {
color: #ccc;
}
-#source-sidebar > .title {
- border-bottom-color: #ccc;
-}
#source-sidebar div.files > a:hover, details.dir-entry summary:hover,
#source-sidebar div.files > a:focus, details.dir-entry summary:focus {
background-color: #444;
@@ -386,9 +177,6 @@ kbd {
border-color: white;
color: white;
}
-.more-examples-toggle summary, .more-examples-toggle .hide-more {
- color: #999;
-}
.scraped-example .example-wrap .rust span.highlight {
background: rgb(91, 59, 1);
}
diff --git a/src/librustdoc/html/static/css/themes/light.css b/src/librustdoc/html/static/css/themes/light.css
index b751acff1..5eb4bbcf8 100644
--- a/src/librustdoc/html/static/css/themes/light.css
+++ b/src/librustdoc/html/static/css/themes/light.css
@@ -9,6 +9,50 @@
--scrollbar-thumb-background-color: rgba(36, 37, 39, 0.6);
--scrollbar-color: rgba(36, 37, 39, 0.6) #d9d9d9;
--headings-border-bottom-color: #ddd;
+ --border-color: #e0e0e0;
+ --button-background-color: #fff;
+ --right-side-color: grey;
+ --code-attribute-color: #999;
+ --toggles-color: #999;
+ --search-input-focused-border-color: #66afe9;
+ --copy-path-button-color: #999;
+ --copy-path-img-filter: invert(50%);
+ --copy-path-img-hover-filter: invert(35%);
+ --codeblock-error-hover-color: rgb(255, 0, 0);
+ --codeblock-error-color: rgba(255, 0, 0, .5);
+ --codeblock-ignore-hover-color: rgb(255, 142, 0);
+ --codeblock-ignore-color: rgba(255, 142, 0, .6);
+ --type-link-color: #ad378a;
+ --trait-link-color: #6e4fc9;
+ --assoc-item-link-color: #3873ad;
+ --function-link-color: #ad7c37;
+ --macro-link-color: #068000;
+ --keyword-link-color: #3873ad;
+ --mod-link-color: #3873ad;
+ --link-color: #3873ad;
+ --sidebar-link-color: #356da4;
+ --sidebar-current-link-background-color: #fff;
+ --search-result-link-focus-background-color: #ccc;
+ --stab-background-color: #fff5d6;
+ --stab-code-color: #000;
+ --search-color: #000;
+ --code-highlight-kw-color: #8959a8;
+ --code-highlight-kw-2-color: #4271ae;
+ --code-highlight-lifetime-color: #b76514;
+ --code-highlight-prelude-color: #4271ae;
+ --code-highlight-prelude-val-color: #c82829;
+ --code-highlight-number-color: #718c00;
+ --code-highlight-string-color: #718c00;
+ --code-highlight-literal-color: #c82829;
+ --code-highlight-attribute-color: #c82829;
+ --code-highlight-self-color: #c82829;
+ --code-highlight-macro-color: #3e999f;
+ --code-highlight-question-mark-color: #ff9011;
+ --code-highlight-comment-color: #8e908c;
+ --code-highlight-doc-comment-color: #4d4d4c;
+ --example-line-numbers-border-color: #c7c7c7;
+ --src-line-numbers-span-color: #c67e2d;
+ --src-line-number-highlighted-background-color: #fdffd3;
}
.slider {
@@ -21,175 +65,27 @@ input:focus + .slider {
box-shadow: 0 0 0 2px #0a84ff, 0 0 0 6px rgba(10, 132, 255, 0.3);
}
-.in-band {
- background-color: white;
-}
-
.rust-logo {
/* This rule exists to force other themes to explicitly style the logo.
* Rustdoc has a custom linter for this purpose.
*/
}
-.sidebar .current,
-.sidebar a:hover {
- background-color: #fff;
-}
-
-.line-numbers span { color: #c67e2d; }
-.line-numbers .line-highlighted {
- background-color: #FDFFD3 !important;
-}
-
-.docblock table td, .docblock table th {
- border-color: #ddd;
-}
-
-.search-results a:hover {
- background-color: #ddd;
-}
-
-.search-results a:focus {
- color: #000 !important;
- background-color: #ccc;
-}
-.search-results a:focus span { color: #000 !important; }
-a.result-trait:focus { background-color: #c7b6ff; }
-a.result-traitalias:focus { background-color: #c7b6ff; }
-a.result-mod:focus,
-a.result-externcrate:focus { background-color: #afc6e4; }
-a.result-enum:focus { background-color: #e7b1a0; }
-a.result-struct:focus { background-color: #e7b1a0; }
-a.result-union:focus { background-color: #e7b1a0; }
-a.result-fn:focus,
-a.result-method:focus,
-a.result-tymethod:focus { background-color: #c6afb3; }
-a.result-type:focus { background-color: #e7b1a0; }
-a.result-associatedtype:focus { background-color: #afc6e4; }
-a.result-foreigntype:focus { background-color: #e7b1a0; }
-a.result-attr:focus,
-a.result-derive:focus,
-a.result-macro:focus { background-color: #8ce488; }
-a.result-constant:focus,
-a.result-static:focus { background-color: #afc6e4; }
-a.result-primitive:focus { background-color: #e7b1a0; }
-a.result-keyword:focus { background-color: #afc6e4; }
-
.content .item-info::before { color: #ccc; }
-.content span.enum, .content a.enum, .block a.current.enum { color: #AD378A; }
-.content span.struct, .content a.struct, .block a.current.struct { color: #AD378A; }
-.content span.type, .content a.type, .block a.current.type { color: #AD378A; }
-.content span.foreigntype, .content a.foreigntype, .block a.current.foreigntype { color: #3873AD; }
-.content span.associatedtype,
-.content a.associatedtype,
-.block a.current.associatedtype { color: #3873AD; }
-.content span.attr, .content a.attr, .block a.current.attr,
-.content span.derive, .content a.derive, .block a.current.derive,
-.content span.macro, .content a.macro, .block a.current.macro { color: #068000; }
-.content span.union, .content a.union, .block a.current.union { color: #AD378A; }
-.content span.constant, .content a.constant, .block a.current.constant,
-.content span.static, .content a.static, .block a.current.static { color: #3873AD; }
-.content span.primitive, .content a.primitive, .block a.current.primitive { color: #AD378A; }
-.content span.externcrate,
-.content span.mod, .content a.mod, .block a.current.mod { color: #3873AD; }
-.content span.trait, .content a.trait, .block a.current.trait { color: #6E4FC9; }
-.content span.traitalias, .content a.traitalias, .block a.current.traitalias { color: #5137AD; }
-.content span.fn, .content a.fn, .block a.current.fn,
-.content span.method, .content a.method, .block a.current.method,
-.content span.tymethod, .content a.tymethod, .block a.current.tymethod,
-.content .fnname { color: #AD7C37; }
-.content span.keyword, .content a.keyword, .block a.current.keyword { color: #3873AD; }
-
-.sidebar a { color: #356da4; }
-.sidebar a.current.enum { color: #a63283; }
-.sidebar a.current.struct { color: #a63283; }
-.sidebar a.current.type { color: #a63283; }
-.sidebar a.current.associatedtype { color: #356da4; }
-.sidebar a.current.foreigntype { color: #356da4; }
-.sidebar a.current.attr,
-.sidebar a.current.derive,
-.sidebar a.current.macro { color: #067901; }
-.sidebar a.current.union { color: #a63283; }
-.sidebar a.current.constant
-.sidebar a.current.static { color: #356da4; }
-.sidebar a.current.primitive { color: #a63283; }
-.sidebar a.current.externcrate
-.sidebar a.current.mod { color: #356da4; }
-.sidebar a.current.trait { color: #6849c3; }
-.sidebar a.current.traitalias { color: #4b349e; }
-.sidebar a.current.fn,
-.sidebar a.current.method,
-.sidebar a.current.tymethod { color: #a67736; }
-.sidebar a.current.keyword { color: #356da4; }
-
-nav.main .current {
- border-top-color: #000;
- border-bottom-color: #000;
-}
-nav.main .separator {
- border: 1px solid #000;
-}
-
-a {
- color: #3873AD;
-}
-
body.source .example-wrap pre.rust a {
background: #eee;
}
-details.rustdoc-toggle > summary.hideme > span,
-details.rustdoc-toggle > summary::before {
- color: #999;
-}
-
-#crate-search, .search-input {
- background-color: white;
- border-color: #e0e0e0;
-}
-
-#crate-search {
- /* Without the `!important`, the border-color is ignored for `<select>`...
- It cannot be in the group above because `.search-input` has a different border color on
- hover. */
- border-color: #e0e0e0 !important;
-}
-
-.search-input:focus {
- border-color: #66afe9;
-}
-
-.stab.empty-impl { background: #FFF5D6; border-color: #FFC600; }
-.stab.unstable { background: #FFF5D6; border-color: #FFC600; }
-.stab.deprecated { background: #ffc4c4; border-color: #db7b7b; }
-.stab.must_implement { background: #F3DFFF; border-color: #b07bdb; }
-.stab.portability { background: #F3DFFF; border-color: #b07bdb; }
-.stab.portability > code { background: none; }
-
-.rightside,
-.out-of-band {
- color: grey;
+#crate-search-div::after {
+ /* match border-color; uses https://codepen.io/sosuke/pen/Pjoqqp */
+ filter: invert(100%) sepia(0%) saturate(4223%) hue-rotate(289deg) brightness(114%) contrast(76%);
}
-
-.line-numbers :target { background-color: transparent; }
-
-/* Code highlighting */
-pre.rust .kw { color: #8959A8; }
-pre.rust .kw-2, pre.rust .prelude-ty { color: #4271AE; }
-pre.rust .number, pre.rust .string { color: #718C00; }
-pre.rust .self, pre.rust .bool-val, pre.rust .prelude-val,
-pre.rust .attribute, pre.rust .attribute .ident { color: #C82829; }
-pre.rust .comment { color: #8E908C; }
-pre.rust .doccomment { color: #4D4D4C; }
-pre.rust .macro, pre.rust .macro-nonterminal { color: #3E999F; }
-pre.rust .lifetime { color: #B76514; }
-pre.rust .question-mark {
- color: #ff9011;
+#crate-search:hover, #crate-search:focus {
+ border-color: #717171 !important;
}
-
-.example-wrap > pre.line-number {
- border-color: #c7c7c7;
+#crate-search-div:hover::after, #crate-search-div:focus-within::after {
+ filter: invert(44%) sepia(18%) saturate(23%) hue-rotate(317deg) brightness(96%) contrast(93%);
}
a.test-arrow {
@@ -201,64 +97,11 @@ a.test-arrow:hover{
background-color: #4e8bca;
}
-.toggle-label,
-.code-attribute {
- color: #999;
-}
-
:target {
background: #FDFFD3;
border-right: 3px solid #AD7C37;
}
-pre.compile_fail {
- border-left: 2px solid rgba(255,0,0,.5);
-}
-
-pre.compile_fail:hover, .information:hover + pre.compile_fail {
- border-left: 2px solid #f00;
-}
-
-pre.should_panic {
- border-left: 2px solid rgba(255,0,0,.5);
-}
-
-pre.should_panic:hover, .information:hover + pre.should_panic {
- border-left: 2px solid #f00;
-}
-
-pre.ignore {
- border-left: 2px solid rgba(255,142,0,.6);
-}
-
-pre.ignore:hover, .information:hover + pre.ignore {
- border-left: 2px solid #ff9200;
-}
-
-.tooltip.compile_fail {
- color: rgba(255,0,0,.5);
-}
-
-.information > .compile_fail:hover {
- color: #f00;
-}
-
-.tooltip.should_panic {
- color: rgba(255,0,0,.5);
-}
-
-.information > .should_panic:hover {
- color: #f00;
-}
-
-.tooltip.ignore {
- color: rgba(255,142,0,.6);
-}
-
-.information > .ignore:hover {
- color: #ff9200;
-}
-
.search-failed a {
color: #3873AD;
}
@@ -274,11 +117,6 @@ pre.ignore:hover, .information:hover + pre.ignore {
.notable-traits-tooltiptext {
background-color: #eee;
- border-color: #999;
-}
-
-.notable-traits-tooltiptext .notable {
- border-bottom-color: #DDDDDD;
}
#titles > button:not(.selected) {
@@ -295,58 +133,21 @@ pre.ignore:hover, .information:hover + pre.ignore {
color: #888;
}
-@media (max-width: 700px) {
- .sidebar-elems {
- border-right-color: #000;
- }
-}
-
kbd {
color: #000;
background-color: #fafbfc;
- border-color: #d1d5da;
- border-bottom-color: #c6cbd1;
box-shadow: inset 0 -1px 0 #c6cbd1;
}
-#settings-menu > a, #help-button > button {
- border-color: #e0e0e0;
- background-color: #fff;
+#settings-menu > a, #help-button > a {
+ color: #000;
}
#settings-menu > a:hover, #settings-menu > a:focus,
-#help-button > button:hover, #help-button > button:focus {
+#help-button > a:hover, #help-button > a:focus {
border-color: #717171;
}
-.popover, .popover::before,
-#help-button span.top, #help-button span.bottom {
- border-color: #DDDDDD;
-}
-
-#copy-path {
- color: #999;
-}
-#copy-path > img {
- filter: invert(50%);
-}
-#copy-path:hover > img {
- filter: invert(35%);
-}
-
-#theme-choices {
- border-color: #ccc;
- background-color: #fff;
-}
-
-#theme-choices > button:not(:first-child) {
- border-top-color: #e0e0e0;
-}
-
-#theme-choices > button:hover, #theme-choices > button:focus {
- background-color: #eee;
-}
-
.search-results .result-name span.alias {
color: #000;
}
@@ -354,9 +155,6 @@ kbd {
color: #999;
}
-#source-sidebar > .title {
- border-bottom-color: #ccc;
-}
#source-sidebar div.files > a:hover, details.dir-entry summary:hover,
#source-sidebar div.files > a:focus, details.dir-entry summary:focus {
background-color: #E0E0E0;
@@ -372,9 +170,6 @@ kbd {
border-color: black;
color: black;
}
-.more-examples-toggle summary, .more-examples-toggle .hide-more {
- color: #999;
-}
.scraped-example .example-wrap .rust span.highlight {
background: #fcffd6;
}
diff --git a/src/librustdoc/html/static/images/down-arrow.svg b/src/librustdoc/html/static/images/down-arrow.svg
index 35437e77a..5d76a64e9 100644
--- a/src/librustdoc/html/static/images/down-arrow.svg
+++ b/src/librustdoc/html/static/images/down-arrow.svg
@@ -1 +1 @@
-<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="Layer_1" width="128" height="128" enable-background="new 0 0 128 128" version="1.1" viewBox="-30 -20 176 176" xml:space="preserve"><g><line x1="111" x2="64" y1="40.5" y2="87.499" fill="none" stroke="#2F3435" stroke-linecap="square" stroke-miterlimit="10" stroke-width="12"/><line x1="64" x2="17" y1="87.499" y2="40.5" fill="none" stroke="#2F3435" stroke-linecap="square" stroke-miterlimit="10" stroke-width="12"/></g></svg> \ No newline at end of file
+<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="Layer_1" width="128" height="128" enable-background="new 0 0 128 128" version="1.1" viewBox="-30 -20 176 176" xml:space="preserve"><g><line x1="111" x2="64" y1="40.5" y2="87.499" fill="none" stroke="#000000" stroke-linecap="square" stroke-miterlimit="10" stroke-width="12"/><line x1="64" x2="17" y1="87.499" y2="40.5" fill="none" stroke="#000000" stroke-linecap="square" stroke-miterlimit="10" stroke-width="12"/></g></svg> \ No newline at end of file
diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js
index 0702b2b0b..33480fa41 100644
--- a/src/librustdoc/html/static/js/main.js
+++ b/src/librustdoc/html/static/js/main.js
@@ -55,7 +55,7 @@ function blurHandler(event, parentElem, hideCallback) {
function setMobileTopbar() {
// FIXME: It would be nicer to generate this text content directly in HTML,
// but with the current code it's hard to get the right information in the right place.
- const mobileLocationTitle = document.querySelector(".mobile-topbar h2.location");
+ const mobileLocationTitle = document.querySelector(".mobile-topbar h2");
const locationTitle = document.querySelector(".sidebar h2.location");
if (mobileLocationTitle && locationTitle) {
mobileLocationTitle.innerHTML = locationTitle.innerHTML;
@@ -192,6 +192,8 @@ function loadCss(cssFileName) {
}
(function() {
+ const isHelpPage = window.location.pathname.endsWith("/help.html");
+
function loadScript(url) {
const script = document.createElement("script");
script.src = url;
@@ -199,6 +201,9 @@ function loadCss(cssFileName) {
}
getSettingsButton().onclick = event => {
+ if (event.ctrlKey || event.altKey || event.metaKey) {
+ return;
+ }
addClass(getSettingsButton(), "rotate");
event.preventDefault();
// Sending request for the CSS and the JS files at the same time so it will
@@ -348,8 +353,7 @@ function loadCss(cssFileName) {
function onHashChange(ev) {
// If we're in mobile mode, we should hide the sidebar in any case.
- const sidebar = document.getElementsByClassName("sidebar")[0];
- removeClass(sidebar, "shown");
+ hideSidebar();
handleHashes(ev);
}
@@ -405,9 +409,12 @@ function loadCss(cssFileName) {
break;
case "+":
+ ev.preventDefault();
+ expandAllDocs();
+ break;
case "-":
ev.preventDefault();
- toggleAllDocs();
+ collapseAllDocs();
break;
case "?":
@@ -443,18 +450,15 @@ function loadCss(cssFileName) {
return;
}
- const div = document.createElement("div");
- div.className = "block " + shortty;
const h3 = document.createElement("h3");
h3.innerHTML = `<a href="index.html#${id}">${longty}</a>`;
- div.appendChild(h3);
const ul = document.createElement("ul");
+ ul.className = "block " + shortty;
for (const item of filtered) {
const name = item[0];
const desc = item[1]; // can be null
- let klass = shortty;
let path;
if (shortty === "mod") {
path = name + "/index.html";
@@ -462,20 +466,19 @@ function loadCss(cssFileName) {
path = shortty + "." + name + ".html";
}
const current_page = document.location.href.split("/").pop();
- if (path === current_page) {
- klass += " current";
- }
const link = document.createElement("a");
link.href = path;
link.title = desc;
- link.className = klass;
+ if (path === current_page) {
+ link.className = "current";
+ }
link.textContent = name;
const li = document.createElement("li");
li.appendChild(link);
ul.appendChild(li);
}
- div.appendChild(ul);
- sidebar.appendChild(div);
+ sidebar.appendChild(h3);
+ sidebar.appendChild(ul);
}
if (sidebar) {
@@ -501,6 +504,10 @@ function loadCss(cssFileName) {
const synthetic_implementors = document.getElementById("synthetic-implementors-list");
const inlined_types = new Set();
+ const TEXT_IDX = 0;
+ const SYNTHETIC_IDX = 1;
+ const TYPES_IDX = 2;
+
if (synthetic_implementors) {
// This `inlined_types` variable is used to avoid having the same implementation
// showing up twice. For example "String" in the "Sync" doc page.
@@ -519,15 +526,15 @@ function loadCss(cssFileName) {
}
let currentNbImpls = implementors.getElementsByClassName("impl").length;
- const traitName = document.querySelector("h1.fqn > .in-band > .trait").textContent;
+ const traitName = document.querySelector("h1.fqn > .trait").textContent;
const baseIdName = "impl-" + traitName + "-";
const libs = Object.getOwnPropertyNames(imp);
// We don't want to include impls from this JS file, when the HTML already has them.
// The current crate should always be ignored. Other crates that should also be
// ignored are included in the attribute `data-ignore-extern-crates`.
- const ignoreExternCrates = document
- .querySelector("script[data-ignore-extern-crates]")
- .getAttribute("data-ignore-extern-crates");
+ const script = document
+ .querySelector("script[data-ignore-extern-crates]");
+ const ignoreExternCrates = script ? script.getAttribute("data-ignore-extern-crates") : "";
for (const lib of libs) {
if (lib === window.currentCrate || ignoreExternCrates.indexOf(lib) !== -1) {
continue;
@@ -536,10 +543,12 @@ function loadCss(cssFileName) {
struct_loop:
for (const struct of structs) {
- const list = struct.synthetic ? synthetic_implementors : implementors;
+ const list = struct[SYNTHETIC_IDX] ? synthetic_implementors : implementors;
- if (struct.synthetic) {
- for (const struct_type of struct.types) {
+ // The types list is only used for synthetic impls.
+ // If this changes, `main.js` and `write_shared.rs` both need changed.
+ if (struct[SYNTHETIC_IDX]) {
+ for (const struct_type of struct[TYPES_IDX]) {
if (inlined_types.has(struct_type)) {
continue struct_loop;
}
@@ -548,9 +557,8 @@ function loadCss(cssFileName) {
}
const code = document.createElement("h3");
- code.innerHTML = struct.text;
+ code.innerHTML = struct[TEXT_IDX];
addClass(code, "code-header");
- addClass(code, "in-band");
onEachLazy(code.getElementsByTagName("a"), elem => {
const href = elem.getAttribute("href");
@@ -588,38 +596,52 @@ function loadCss(cssFileName) {
return;
}
// Draw a convenient sidebar of known crates if we have a listing
- const div = document.createElement("div");
- div.className = "block crate";
- div.innerHTML = "<h3>Crates</h3>";
+ const h3 = document.createElement("h3");
+ h3.innerHTML = "Crates";
const ul = document.createElement("ul");
- div.appendChild(ul);
+ ul.className = "block crate";
for (const crate of window.ALL_CRATES) {
- let klass = "crate";
- if (window.rootPath !== "./" && crate === window.currentCrate) {
- klass += " current";
- }
const link = document.createElement("a");
link.href = window.rootPath + crate + "/index.html";
- link.className = klass;
+ if (window.rootPath !== "./" && crate === window.currentCrate) {
+ link.className = "current";
+ }
link.textContent = crate;
const li = document.createElement("li");
li.appendChild(link);
ul.appendChild(li);
}
- sidebarElems.appendChild(div);
+ sidebarElems.appendChild(h3);
+ sidebarElems.appendChild(ul);
}
+ function expandAllDocs() {
+ const innerToggle = document.getElementById(toggleAllDocsId);
+ removeClass(innerToggle, "will-expand");
+ onEachLazy(document.getElementsByClassName("rustdoc-toggle"), e => {
+ if (!hasClass(e, "type-contents-toggle")) {
+ e.open = true;
+ }
+ });
+ innerToggle.title = "collapse all docs";
+ innerToggle.children[0].innerText = "\u2212"; // "\u2212" is "−" minus sign
+ }
- function labelForToggleButton(sectionIsCollapsed) {
- if (sectionIsCollapsed) {
- // button will expand the section
- return "+";
- }
- // button will collapse the section
- // note that this text is also set in the HTML template in ../render/mod.rs
- return "\u2212"; // "\u2212" is "−" minus sign
+ function collapseAllDocs() {
+ const innerToggle = document.getElementById(toggleAllDocsId);
+ addClass(innerToggle, "will-expand");
+ onEachLazy(document.getElementsByClassName("rustdoc-toggle"), e => {
+ if (e.parentNode.id !== "implementations-list" ||
+ (!hasClass(e, "implementors-toggle") &&
+ !hasClass(e, "type-contents-toggle"))
+ ) {
+ e.open = false;
+ }
+ });
+ innerToggle.title = "expand all docs";
+ innerToggle.children[0].innerText = "+";
}
function toggleAllDocs() {
@@ -627,29 +649,11 @@ function loadCss(cssFileName) {
if (!innerToggle) {
return;
}
- let sectionIsCollapsed = false;
if (hasClass(innerToggle, "will-expand")) {
- removeClass(innerToggle, "will-expand");
- onEachLazy(document.getElementsByClassName("rustdoc-toggle"), e => {
- if (!hasClass(e, "type-contents-toggle")) {
- e.open = true;
- }
- });
- innerToggle.title = "collapse all docs";
+ expandAllDocs();
} else {
- addClass(innerToggle, "will-expand");
- onEachLazy(document.getElementsByClassName("rustdoc-toggle"), e => {
- if (e.parentNode.id !== "implementations-list" ||
- (!hasClass(e, "implementors-toggle") &&
- !hasClass(e, "type-contents-toggle"))
- ) {
- e.open = false;
- }
- });
- sectionIsCollapsed = true;
- innerToggle.title = "expand all docs";
+ collapseAllDocs();
}
- innerToggle.children[0].innerText = labelForToggleButton(sectionIsCollapsed);
}
(function() {
@@ -692,47 +696,102 @@ function loadCss(cssFileName) {
}
}());
+ window.rustdoc_add_line_numbers_to_examples = () => {
+ onEachLazy(document.getElementsByClassName("rust-example-rendered"), x => {
+ const parent = x.parentNode;
+ const line_numbers = parent.querySelectorAll(".example-line-numbers");
+ if (line_numbers.length > 0) {
+ return;
+ }
+ const count = x.textContent.split("\n").length;
+ const elems = [];
+ for (let i = 0; i < count; ++i) {
+ elems.push(i + 1);
+ }
+ const node = document.createElement("pre");
+ addClass(node, "example-line-numbers");
+ node.innerHTML = elems.join("\n");
+ parent.insertBefore(node, x);
+ });
+ };
+
+ window.rustdoc_remove_line_numbers_from_examples = () => {
+ onEachLazy(document.getElementsByClassName("rust-example-rendered"), x => {
+ const parent = x.parentNode;
+ const line_numbers = parent.querySelectorAll(".example-line-numbers");
+ for (const node of line_numbers) {
+ parent.removeChild(node);
+ }
+ });
+ };
+
(function() {
// To avoid checking on "rustdoc-line-numbers" value on every loop...
- let lineNumbersFunc = () => {};
if (getSettingValue("line-numbers") === "true") {
- lineNumbersFunc = x => {
- const count = x.textContent.split("\n").length;
- const elems = [];
- for (let i = 0; i < count; ++i) {
- elems.push(i + 1);
- }
- const node = document.createElement("pre");
- addClass(node, "line-number");
- node.innerHTML = elems.join("\n");
- x.parentNode.insertBefore(node, x);
- };
+ window.rustdoc_add_line_numbers_to_examples();
}
- onEachLazy(document.getElementsByClassName("rust-example-rendered"), e => {
- if (hasClass(e, "compile_fail")) {
- e.addEventListener("mouseover", function() {
- this.parentElement.previousElementSibling.childNodes[0].style.color = "#f00";
- });
- e.addEventListener("mouseout", function() {
- this.parentElement.previousElementSibling.childNodes[0].style.color = "";
- });
- } else if (hasClass(e, "ignore")) {
- e.addEventListener("mouseover", function() {
- this.parentElement.previousElementSibling.childNodes[0].style.color = "#ff9200";
- });
- e.addEventListener("mouseout", function() {
- this.parentElement.previousElementSibling.childNodes[0].style.color = "";
- });
- }
- lineNumbersFunc(e);
- });
}());
+ let oldSidebarScrollPosition = null;
+
+ // Scroll locking used both here and in source-script.js
+
+ window.rustdocMobileScrollLock = function() {
+ const mobile_topbar = document.querySelector(".mobile-topbar");
+ if (window.innerWidth <= window.RUSTDOC_MOBILE_BREAKPOINT) {
+ // This is to keep the scroll position on mobile.
+ oldSidebarScrollPosition = window.scrollY;
+ document.body.style.width = `${document.body.offsetWidth}px`;
+ document.body.style.position = "fixed";
+ document.body.style.top = `-${oldSidebarScrollPosition}px`;
+ if (mobile_topbar) {
+ mobile_topbar.style.top = `${oldSidebarScrollPosition}px`;
+ mobile_topbar.style.position = "relative";
+ }
+ } else {
+ oldSidebarScrollPosition = null;
+ }
+ };
+
+ window.rustdocMobileScrollUnlock = function() {
+ const mobile_topbar = document.querySelector(".mobile-topbar");
+ if (oldSidebarScrollPosition !== null) {
+ // This is to keep the scroll position on mobile.
+ document.body.style.width = "";
+ document.body.style.position = "";
+ document.body.style.top = "";
+ if (mobile_topbar) {
+ mobile_topbar.style.top = "";
+ mobile_topbar.style.position = "";
+ }
+ // The scroll position is lost when resetting the style, hence why we store it in
+ // `oldSidebarScrollPosition`.
+ window.scrollTo(0, oldSidebarScrollPosition);
+ oldSidebarScrollPosition = null;
+ }
+ };
+
+ function showSidebar() {
+ window.rustdocMobileScrollLock();
+ const sidebar = document.getElementsByClassName("sidebar")[0];
+ addClass(sidebar, "shown");
+ }
+
function hideSidebar() {
+ window.rustdocMobileScrollUnlock();
const sidebar = document.getElementsByClassName("sidebar")[0];
removeClass(sidebar, "shown");
}
+ window.addEventListener("resize", () => {
+ if (window.innerWidth > window.RUSTDOC_MOBILE_BREAKPOINT &&
+ oldSidebarScrollPosition !== null) {
+ // If the user opens the sidebar in "mobile" mode, and then grows the browser window,
+ // we need to switch away from mobile mode and make the main content area scrollable.
+ hideSidebar();
+ }
+ });
+
function handleClick(id, f) {
const elem = document.getElementById(id);
if (elem) {
@@ -775,9 +834,9 @@ function loadCss(cssFileName) {
sidebar_menu_toggle.addEventListener("click", () => {
const sidebar = document.getElementsByClassName("sidebar")[0];
if (!hasClass(sidebar, "shown")) {
- addClass(sidebar, "shown");
+ showSidebar();
} else {
- removeClass(sidebar, "shown");
+ hideSidebar();
}
});
}
@@ -834,7 +893,10 @@ function loadCss(cssFileName) {
rustdoc_version.appendChild(rustdoc_version_code);
const container = document.createElement("div");
- container.className = "popover";
+ if (!isHelpPage) {
+ container.className = "popover";
+ }
+ container.id = "help";
container.style.display = "none";
const side_by_side = document.createElement("div");
@@ -846,15 +908,22 @@ function loadCss(cssFileName) {
container.appendChild(side_by_side);
container.appendChild(rustdoc_version);
- const help_button = getHelpButton();
- help_button.appendChild(container);
+ if (isHelpPage) {
+ const help_section = document.createElement("section");
+ help_section.appendChild(container);
+ document.getElementById("main-content").appendChild(help_section);
+ container.style.display = "block";
+ } else {
+ const help_button = getHelpButton();
+ help_button.appendChild(container);
- container.onblur = helpBlurHandler;
- container.onclick = event => {
- event.preventDefault();
- };
- help_button.onblur = helpBlurHandler;
- help_button.children[0].onblur = helpBlurHandler;
+ container.onblur = helpBlurHandler;
+ container.onclick = event => {
+ event.preventDefault();
+ };
+ help_button.onblur = helpBlurHandler;
+ help_button.children[0].onblur = helpBlurHandler;
+ }
return container;
}
@@ -863,7 +932,7 @@ function loadCss(cssFileName) {
* Hide all the popover menus.
*/
window.hidePopoverMenus = function() {
- onEachLazy(document.querySelectorAll(".search-container .popover"), elem => {
+ onEachLazy(document.querySelectorAll(".search-form .popover"), elem => {
elem.style.display = "none";
});
};
@@ -895,19 +964,43 @@ function loadCss(cssFileName) {
}
}
- document.querySelector(`#${HELP_BUTTON_ID} > button`).addEventListener("click", event => {
- const target = event.target;
- if (target.tagName !== "BUTTON" || target.parentElement.id !== HELP_BUTTON_ID) {
- return;
- }
- const menu = getHelpMenu(true);
- const shouldShowHelp = menu.style.display === "none";
- if (shouldShowHelp) {
- showHelp();
- } else {
- window.hidePopoverMenus();
- }
- });
+ if (isHelpPage) {
+ showHelp();
+ document.querySelector(`#${HELP_BUTTON_ID} > a`).addEventListener("click", event => {
+ // Already on the help page, make help button a no-op.
+ const target = event.target;
+ if (target.tagName !== "A" ||
+ target.parentElement.id !== HELP_BUTTON_ID ||
+ event.ctrlKey ||
+ event.altKey ||
+ event.metaKey) {
+ return;
+ }
+ event.preventDefault();
+ });
+ } else {
+ document.querySelector(`#${HELP_BUTTON_ID} > a`).addEventListener("click", event => {
+ // By default, have help button open docs in a popover.
+ // If user clicks with a moderator, though, use default browser behavior,
+ // probably opening in a new window or tab.
+ const target = event.target;
+ if (target.tagName !== "A" ||
+ target.parentElement.id !== HELP_BUTTON_ID ||
+ event.ctrlKey ||
+ event.altKey ||
+ event.metaKey) {
+ return;
+ }
+ event.preventDefault();
+ const menu = getHelpMenu(true);
+ const shouldShowHelp = menu.style.display === "none";
+ if (shouldShowHelp) {
+ showHelp();
+ } else {
+ window.hidePopoverMenus();
+ }
+ });
+ }
setMobileTopbar();
addSidebarItems();
diff --git a/src/librustdoc/html/static/js/scrape-examples.js b/src/librustdoc/html/static/js/scrape-examples.js
index fd7a14497..d0fd115fd 100644
--- a/src/librustdoc/html/static/js/scrape-examples.js
+++ b/src/librustdoc/html/static/js/scrape-examples.js
@@ -8,7 +8,7 @@
// Scroll code block to the given code location
function scrollToLoc(elt, loc) {
- const lines = elt.querySelector(".line-numbers");
+ const lines = elt.querySelector(".src-line-numbers");
let scrollOffset;
// If the block is greater than the size of the viewer,
diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js
index 75c7bd45a..d04ec357c 100644
--- a/src/librustdoc/html/static/js/search.js
+++ b/src/librustdoc/html/static/js/search.js
@@ -429,9 +429,9 @@ function initSearch(rawSearchIndex) {
}
const posBefore = parserState.pos;
getNextElem(query, parserState, elems, endChar === ">");
- // This case can be encountered if `getNextElem` encounted a "stop character" right from
- // the start. For example if you have `,,` or `<>`. In this case, we simply move up the
- // current position to continue the parsing.
+ // This case can be encountered if `getNextElem` encountered a "stop character" right
+ // from the start. For example if you have `,,` or `<>`. In this case, we simply move up
+ // the current position to continue the parsing.
if (posBefore === parserState.pos) {
parserState.pos += 1;
}
@@ -581,7 +581,7 @@ function initSearch(rawSearchIndex) {
const elem = document.getElementById("crate-search");
if (elem &&
- elem.value !== "All crates" &&
+ elem.value !== "all crates" &&
hasOwnPropertyRustdoc(rawSearchIndex, elem.value)
) {
return elem.value;
@@ -1551,12 +1551,6 @@ function initSearch(rawSearchIndex) {
return [displayPath, href];
}
- function escape(content) {
- const h1 = document.createElement("h1");
- h1.textContent = content;
- return h1.innerHTML;
- }
-
function pathSplitter(path) {
const tmp = "<span>" + path.replace(/::/g, "::</span><span>");
if (tmp.endsWith("<span>")) {
@@ -1710,22 +1704,15 @@ function initSearch(rawSearchIndex) {
let crates = "";
const crates_list = Object.keys(rawSearchIndex);
if (crates_list.length > 1) {
- crates = " in <select id=\"crate-search\"><option value=\"All crates\">" +
- "All crates</option>";
+ crates = " in&nbsp;<div id=\"crate-search-div\"><select id=\"crate-search\">" +
+ "<option value=\"all crates\">all crates</option>";
for (const c of crates_list) {
crates += `<option value="${c}" ${c === filterCrates && "selected"}>${c}</option>`;
}
- crates += "</select>";
- }
-
- let typeFilter = "";
- if (results.query.typeFilter !== NO_TYPE_FILTER) {
- typeFilter = " (type: " + escape(itemTypes[results.query.typeFilter]) + ")";
+ crates += "</select></div>";
}
- let output = "<div id=\"search-settings\">" +
- `<h1 class="search-results-title">Results for ${escape(results.query.userQuery)}` +
- `${typeFilter}</h1>${crates}</div>`;
+ let output = `<h1 class="search-results-title">Results${crates}</h1>`;
if (results.query.error !== null) {
output += `<h3>Query parser error: "${results.query.error}".</h3>`;
output += "<div id=\"titles\">" +
@@ -2245,7 +2232,7 @@ function initSearch(rawSearchIndex) {
}
function updateCrate(ev) {
- if (ev.target.value === "All crates") {
+ if (ev.target.value === "all crates") {
// If we don't remove it from the URL, it'll be picked up again by the search.
const params = searchState.getQueryStringParams();
const query = searchState.input.value.trim();
diff --git a/src/librustdoc/html/static/js/settings.js b/src/librustdoc/html/static/js/settings.js
index 797b931af..5e1c7e6f0 100644
--- a/src/librustdoc/html/static/js/settings.js
+++ b/src/librustdoc/html/static/js/settings.js
@@ -19,6 +19,13 @@
updateSystemTheme();
updateLightAndDark();
break;
+ case "line-numbers":
+ if (value === true) {
+ window.rustdoc_add_line_numbers_to_examples();
+ } else {
+ window.rustdoc_remove_line_numbers_from_examples();
+ }
+ break;
}
}
@@ -209,7 +216,9 @@
const innerHTML = `<div class="settings">${buildSettingsPageSections(settings)}</div>`;
const el = document.createElement(elementKind);
el.id = "settings";
- el.className = "popover";
+ if (!isSettingsPage) {
+ el.className = "popover";
+ }
el.innerHTML = innerHTML;
if (isSettingsPage) {
diff --git a/src/librustdoc/html/static/js/source-script.js b/src/librustdoc/html/static/js/source-script.js
index c45d61429..0b9368dd8 100644
--- a/src/librustdoc/html/static/js/source-script.js
+++ b/src/librustdoc/html/static/js/source-script.js
@@ -10,7 +10,6 @@
(function() {
const rootPath = document.getElementById("rustdoc-vars").attributes["data-root-path"].value;
-let oldScrollPosition = 0;
const NAME_OFFSET = 0;
const DIRS_OFFSET = 1;
@@ -70,24 +69,12 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) {
function toggleSidebar() {
const child = this.parentNode.children[0];
if (child.innerText === ">") {
- if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT) {
- // This is to keep the scroll position on mobile.
- oldScrollPosition = window.scrollY;
- document.body.style.position = "fixed";
- document.body.style.top = `-${oldScrollPosition}px`;
- }
+ window.rustdocMobileScrollLock();
addClass(document.documentElement, "source-sidebar-expanded");
child.innerText = "<";
updateLocalStorage("source-sidebar-show", "true");
} else {
- if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT) {
- // This is to keep the scroll position on mobile.
- document.body.style.position = "";
- document.body.style.top = "";
- // The scroll position is lost when resetting the style, hence why we store it in
- // `oldScroll`.
- window.scrollTo(0, oldScrollPosition);
- }
+ window.rustdocMobileScrollUnlock();
removeClass(document.documentElement, "source-sidebar-expanded");
child.innerText = ">";
updateLocalStorage("source-sidebar-show", "false");
@@ -111,7 +98,7 @@ function createSidebarToggle() {
return sidebarToggle;
}
-// This function is called from "source-files.js", generated in `html/render/mod.rs`.
+// This function is called from "source-files.js", generated in `html/render/write_shared.rs`.
// eslint-disable-next-line no-unused-vars
function createSourceSidebar() {
const container = document.querySelector("nav.sidebar");
@@ -169,7 +156,7 @@ function highlightSourceLines(match) {
if (x) {
x.scrollIntoView();
}
- onEachLazy(document.getElementsByClassName("line-numbers"), e => {
+ onEachLazy(document.getElementsByClassName("src-line-numbers"), e => {
onEachLazy(e.getElementsByTagName("span"), i_e => {
removeClass(i_e, "line-highlighted");
});
@@ -231,7 +218,7 @@ window.addEventListener("hashchange", () => {
}
});
-onEachLazy(document.getElementsByClassName("line-numbers"), el => {
+onEachLazy(document.getElementsByClassName("src-line-numbers"), el => {
el.addEventListener("click", handleSourceHighlight);
});
diff --git a/src/librustdoc/html/static/js/storage.js b/src/librustdoc/html/static/js/storage.js
index 0c5389d45..b462a2c50 100644
--- a/src/librustdoc/html/static/js/storage.js
+++ b/src/librustdoc/html/static/js/storage.js
@@ -10,9 +10,9 @@ window.currentTheme = document.getElementById("themeStyle");
window.mainTheme = document.getElementById("mainThemeStyle");
// WARNING: RUSTDOC_MOBILE_BREAKPOINT MEDIA QUERY
-// If you update this line, then you also need to update the two media queries with the same
+// If you update this line, then you also need to update the media query with the same
// warning in rustdoc.css
-window.RUSTDOC_MOBILE_BREAKPOINT = 701;
+window.RUSTDOC_MOBILE_BREAKPOINT = 700;
const settingsDataset = (function() {
const settingsElement = document.getElementById("default-settings");
diff --git a/src/librustdoc/html/templates/page.html b/src/librustdoc/html/templates/page.html
index 8e25f6764..c32386916 100644
--- a/src/librustdoc/html/templates/page.html
+++ b/src/librustdoc/html/templates/page.html
@@ -13,13 +13,13 @@
<link rel="preload" as="font" type="font/woff2" crossorigin href="{{static_root_path|safe}}SourceCodePro-Regular.ttf.woff2"> {#- -#}
<link rel="preload" as="font" type="font/woff2" crossorigin href="{{static_root_path|safe}}SourceSerif4-Bold.ttf.woff2"> {#- -#}
<link rel="preload" as="font" type="font/woff2" crossorigin href="{{static_root_path|safe}}SourceCodePro-Semibold.ttf.woff2"> {#- -#}
- <link rel="stylesheet" type="text/css" {# -#}
+ <link rel="stylesheet" {# -#}
href="{{static_root_path|safe}}normalize{{page.resource_suffix}}.css"> {#- -#}
- <link rel="stylesheet" type="text/css" {# -#}
+ <link rel="stylesheet" {# -#}
href="{{static_root_path|safe}}rustdoc{{page.resource_suffix}}.css" {# -#}
id="mainThemeStyle"> {#- -#}
{%- for theme in themes -%}
- <link rel="stylesheet" type="text/css" {# -#}
+ <link rel="stylesheet" {# -#}
href="{{static_root_path|safe}}{{theme}}{{page.resource_suffix}}.css" {# -#}
{%- if theme == "light" -%}
id="themeStyle"
@@ -51,7 +51,7 @@
href="{{static_root_path|safe}}noscript{{page.resource_suffix}}.css"> {#- -#}
</noscript> {#- -#}
{%- if layout.css_file_extension.is_some() -%}
- <link rel="stylesheet" type="text/css" {# -#}
+ <link rel="stylesheet" {# -#}
href="{{static_root_path|safe}}theme{{page.resource_suffix}}.css"> {#- -#}
{%- endif -%}
{%- if !layout.favicon.is_empty() -%}
@@ -73,6 +73,7 @@
</div> {#- -#}
<![endif]--> {#- -#}
{{- layout.external_html.before_content|safe -}}
+ {%- if page.css_class != "source" -%}
<nav class="mobile-topbar"> {#- -#}
<button class="sidebar-menu-toggle">&#9776;</button> {#- -#}
<a class="sidebar-logo" href="{{page.root_path|safe}}{{krate_with_trailing_slash|safe}}index.html"> {#- -#}
@@ -84,9 +85,11 @@
{%- endif -%}
</div> {#- -#}
</a> {#- -#}
- <h2 class="location"></h2> {#- -#}
+ <h2></h2> {#- -#}
</nav> {#- -#}
+ {%- endif -%}
<nav class="sidebar"> {#- -#}
+ {%- if page.css_class != "source" -%}
<a class="sidebar-logo" href="{{page.root_path|safe}}{{krate_with_trailing_slash|safe}}index.html"> {#- -#}
<div class="logo-container"> {#- -#}
{%- if !layout.logo.is_empty() %}
@@ -96,11 +99,13 @@
{%- endif -%}
</div> {#- -#}
</a> {#- -#}
+ {%- endif -%}
{{- sidebar|safe -}}
</nav> {#- -#}
<main> {#- -#}
<div class="width-limiter"> {#- -#}
- <div class="sub-container"> {#- -#}
+ <nav class="sub"> {#- -#}
+ {%- if page.css_class == "source" -%}
<a class="sub-logo-container" href="{{page.root_path|safe}}{{krate_with_trailing_slash|safe}}index.html"> {#- -#}
{%- if !layout.logo.is_empty() %}
<img src="{{layout.logo}}" alt="logo"> {#- -#}
@@ -108,30 +113,27 @@
<img class="rust-logo" src="{{static_root_path|safe}}rust-logo{{page.resource_suffix}}.svg" alt="logo"> {#- -#}
{%- endif -%}
</a> {#- -#}
- <nav class="sub"> {#- -#}
- <form class="search-form"> {#- -#}
- <div class="search-container"> {#- -#}
- <span></span> {#- This empty span is a hacky fix for Safari - See #93184 -#}
- <input {# -#}
- class="search-input" {# -#}
- name="search" {# -#}
- autocomplete="off" {# -#}
- spellcheck="false" {# -#}
- placeholder="Click or press ‘S’ to search, ‘?’ for more options…" {# -#}
- type="search"> {#- -#}
- <div id="help-button" title="help" tabindex="-1"> {#- -#}
- <button type="button">?</button> {#- -#}
- </div> {#- -#}
- <div id="settings-menu" tabindex="-1"> {#- -#}
- <a href="{{page.root_path|safe}}settings.html" title="settings"> {#- -#}
- <img width="22" height="22" alt="Change settings" {# -#}
- src="{{static_root_path|safe}}wheel{{page.resource_suffix}}.svg"> {#- -#}
- </a> {#- -#}
- </div> {#- -#}
- </div> {#- -#}
- </form> {#- -#}
- </nav> {#- -#}
- </div> {#- -#}
+ {%- endif -%}
+ <form class="search-form"> {#- -#}
+ <span></span> {#- This empty span is a hacky fix for Safari - See #93184 -#}
+ <input {# -#}
+ class="search-input" {# -#}
+ name="search" {# -#}
+ autocomplete="off" {# -#}
+ spellcheck="false" {# -#}
+ placeholder="Click or press ‘S’ to search, ‘?’ for more options…" {# -#}
+ type="search"> {#- -#}
+ <div id="help-button" title="help" tabindex="-1"> {#- -#}
+ <a href="{{page.root_path|safe}}help.html">?</a> {#- -#}
+ </div> {#- -#}
+ <div id="settings-menu" tabindex="-1"> {#- -#}
+ <a href="{{page.root_path|safe}}settings.html" title="settings"> {#- -#}
+ <img width="22" height="22" alt="Change settings" {# -#}
+ src="{{static_root_path|safe}}wheel{{page.resource_suffix}}.svg"> {#- -#}
+ </a> {#- -#}
+ </div> {#- -#}
+ </form> {#- -#}
+ </nav> {#- -#}
<section id="main-content" class="content">{{- content|safe -}}</section> {#- -#}
</div> {#- -#}
</main> {#- -#}
diff --git a/src/librustdoc/html/templates/print_item.html b/src/librustdoc/html/templates/print_item.html
index c755157d2..b6ce3ea3d 100644
--- a/src/librustdoc/html/templates/print_item.html
+++ b/src/librustdoc/html/templates/print_item.html
@@ -1,18 +1,16 @@
<div class="main-heading"> {#- -#}
<h1 class="fqn"> {#- -#}
- <span class="in-band"> {#- -#}
- {{-typ-}}
- {#- The breadcrumbs of the item path, like std::string -#}
- {%- for component in path_components -%}
- <a href="{{component.path|safe}}index.html">{{component.name}}</a>::<wbr>
- {%- endfor -%}
- <a class="{{item_type}}" href="#">{{name}}</a> {#- -#}
- <button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"> {#- -#}
- <img src="{{static_root_path|safe}}clipboard{{page.resource_suffix}}.svg" {# -#}
- width="19" height="18" {# -#}
- alt="Copy item path"> {#- -#}
- </button> {#- -#}
- </span> {#- -#}
+ {{-typ-}}
+ {#- The breadcrumbs of the item path, like std::string -#}
+ {%- for component in path_components -%}
+ <a href="{{component.path|safe}}index.html">{{component.name}}</a>::<wbr>
+ {%- endfor -%}
+ <a class="{{item_type}}" href="#">{{name}}</a> {#- -#}
+ <button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"> {#- -#}
+ <img src="{{static_root_path|safe}}clipboard{{page.resource_suffix}}.svg" {# -#}
+ width="19" height="18" {# -#}
+ alt="Copy item path"> {#- -#}
+ </button> {#- -#}
</h1> {#- -#}
<span class="out-of-band"> {#- -#}
{% if !stability_since_raw.is_empty() %}
diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs
index 716a4c9ea..cdf59cdd3 100644
--- a/src/librustdoc/json/conversions.rs
+++ b/src/librustdoc/json/conversions.rs
@@ -19,6 +19,7 @@ use crate::clean::utils::print_const_expr;
use crate::clean::{self, ItemId};
use crate::formats::item_type::ItemType;
use crate::json::JsonRenderer;
+use crate::passes::collect_intra_doc_links::UrlFragment;
impl JsonRenderer<'_> {
pub(super) fn convert_item(&self, item: clean::Item) -> Option<Item> {
@@ -29,8 +30,14 @@ impl JsonRenderer<'_> {
.get(&item.item_id)
.into_iter()
.flatten()
- .map(|clean::ItemLink { link, did, .. }| {
- (link.clone(), from_item_id((*did).into(), self.tcx))
+ .map(|clean::ItemLink { link, page_id, fragment, .. }| {
+ let id = match fragment {
+ Some(UrlFragment::Item(frag_id)) => *frag_id,
+ // FIXME: Pass the `UserWritten` segment to JSON consumer.
+ Some(UrlFragment::UserWritten(_)) | None => *page_id,
+ };
+
+ (link.clone(), from_item_id(id.into(), self.tcx))
})
.collect();
let docs = item.attrs.collapsed_doc_value();
@@ -46,10 +53,14 @@ impl JsonRenderer<'_> {
clean::KeywordItem => return None,
clean::StrippedItem(ref inner) => {
match &**inner {
- // We document non-empty stripped modules as with `Module::is_stripped` set to
+ // We document stripped modules as with `Module::is_stripped` set to
// `true`, to prevent contained items from being orphaned for downstream users,
// as JSON does no inlining.
- clean::ModuleItem(m) if !m.items.is_empty() => from_clean_item(item, self.tcx),
+ clean::ModuleItem(_)
+ if self.imported_items.contains(&item_id.expect_def_id()) =>
+ {
+ from_clean_item(item, self.tcx)
+ }
_ => return None,
}
}
@@ -59,7 +70,7 @@ impl JsonRenderer<'_> {
id: from_item_id_with_name(item_id, self.tcx, name),
crate_id: item_id.krate().as_u32(),
name: name.map(|sym| sym.to_string()),
- span: self.convert_span(span),
+ span: span.and_then(|span| self.convert_span(span)),
visibility: self.convert_visibility(visibility),
docs,
attrs,
@@ -119,6 +130,16 @@ where
}
}
+impl<I, T, U> FromWithTcx<I> for Vec<U>
+where
+ I: IntoIterator<Item = T>,
+ U: FromWithTcx<T>,
+{
+ fn from_tcx(f: I, tcx: TyCtxt<'_>) -> Vec<U> {
+ f.into_iter().map(|x| x.into_tcx(tcx)).collect()
+ }
+}
+
pub(crate) fn from_deprecation(deprecation: rustc_attr::Deprecation) -> Deprecation {
#[rustfmt::skip]
let rustc_attr::Deprecation { since, note, is_since_rustc_version: _, suggestion: _ } = deprecation;
@@ -130,11 +151,11 @@ impl FromWithTcx<clean::GenericArgs> for GenericArgs {
use clean::GenericArgs::*;
match args {
AngleBracketed { args, bindings } => GenericArgs::AngleBracketed {
- args: args.into_vec().into_iter().map(|a| a.into_tcx(tcx)).collect(),
- bindings: bindings.into_iter().map(|a| a.into_tcx(tcx)).collect(),
+ args: args.into_vec().into_tcx(tcx),
+ bindings: bindings.into_tcx(tcx),
},
Parenthesized { inputs, output } => GenericArgs::Parenthesized {
- inputs: inputs.into_vec().into_iter().map(|a| a.into_tcx(tcx)).collect(),
+ inputs: inputs.into_vec().into_tcx(tcx),
output: output.map(|a| (*a).into_tcx(tcx)),
},
}
@@ -145,7 +166,7 @@ impl FromWithTcx<clean::GenericArg> for GenericArg {
fn from_tcx(arg: clean::GenericArg, tcx: TyCtxt<'_>) -> Self {
use clean::GenericArg::*;
match arg {
- Lifetime(l) => GenericArg::Lifetime(l.0.to_string()),
+ Lifetime(l) => GenericArg::Lifetime(convert_lifetime(l)),
Type(t) => GenericArg::Type(t.into_tcx(tcx)),
Const(box c) => GenericArg::Const(c.into_tcx(tcx)),
Infer => GenericArg::Infer,
@@ -177,9 +198,7 @@ impl FromWithTcx<clean::TypeBindingKind> for TypeBindingKind {
use clean::TypeBindingKind::*;
match kind {
Equality { term } => TypeBindingKind::Equality(term.into_tcx(tcx)),
- Constraint { bounds } => {
- TypeBindingKind::Constraint(bounds.into_iter().map(|a| a.into_tcx(tcx)).collect())
- }
+ Constraint { bounds } => TypeBindingKind::Constraint(bounds.into_tcx(tcx)),
}
}
}
@@ -240,11 +259,11 @@ fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
VariantItem(v) => ItemEnum::Variant(v.into_tcx(tcx)),
FunctionItem(f) => ItemEnum::Function(from_function(f, header.unwrap(), tcx)),
ForeignFunctionItem(f) => ItemEnum::Function(from_function(f, header.unwrap(), tcx)),
- TraitItem(t) => ItemEnum::Trait(t.into_tcx(tcx)),
+ TraitItem(t) => ItemEnum::Trait((*t).into_tcx(tcx)),
TraitAliasItem(t) => ItemEnum::TraitAlias(t.into_tcx(tcx)),
MethodItem(m, _) => ItemEnum::Method(from_function_method(m, true, header.unwrap(), tcx)),
TyMethodItem(m) => ItemEnum::Method(from_function_method(m, false, header.unwrap(), tcx)),
- ImplItem(i) => ItemEnum::Impl(i.into_tcx(tcx)),
+ ImplItem(i) => ItemEnum::Impl((*i).into_tcx(tcx)),
StaticItem(s) => ItemEnum::Static(s.into_tcx(tcx)),
ForeignStaticItem(s) => ItemEnum::Static(s.into_tcx(tcx)),
ForeignTypeItem => ItemEnum::ForeignType,
@@ -253,19 +272,24 @@ fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
ConstantItem(c) => ItemEnum::Constant(c.into_tcx(tcx)),
MacroItem(m) => ItemEnum::Macro(m.source),
ProcMacroItem(m) => ItemEnum::ProcMacro(m.into_tcx(tcx)),
- PrimitiveItem(p) => ItemEnum::PrimitiveType(p.as_sym().to_string()),
+ PrimitiveItem(p) => {
+ ItemEnum::Primitive(Primitive {
+ name: p.as_sym().to_string(),
+ impls: Vec::new(), // Added in JsonRenderer::item
+ })
+ }
TyAssocConstItem(ty) => ItemEnum::AssocConst { type_: ty.into_tcx(tcx), default: None },
AssocConstItem(ty, default) => {
ItemEnum::AssocConst { type_: ty.into_tcx(tcx), default: Some(default.expr(tcx)) }
}
TyAssocTypeItem(g, b) => ItemEnum::AssocType {
generics: (*g).into_tcx(tcx),
- bounds: b.into_iter().map(|x| x.into_tcx(tcx)).collect(),
+ bounds: b.into_tcx(tcx),
default: None,
},
AssocTypeItem(t, b) => ItemEnum::AssocType {
generics: t.generics.into_tcx(tcx),
- bounds: b.into_iter().map(|x| x.into_tcx(tcx)).collect(),
+ bounds: b.into_tcx(tcx),
default: Some(t.item_type.unwrap_or(t.type_).into_tcx(tcx)),
},
// `convert_item` early returns `None` for stripped items and keywords.
@@ -292,11 +316,19 @@ impl FromWithTcx<clean::Struct> for Struct {
fn from_tcx(struct_: clean::Struct, tcx: TyCtxt<'_>) -> Self {
let fields_stripped = struct_.has_stripped_entries();
let clean::Struct { struct_type, generics, fields } = struct_;
+
+ let kind = match struct_type {
+ CtorKind::Fn => StructKind::Tuple(ids_keeping_stripped(fields, tcx)),
+ CtorKind::Const => {
+ assert!(fields.is_empty());
+ StructKind::Unit
+ }
+ CtorKind::Fictive => StructKind::Plain { fields: ids(fields, tcx), fields_stripped },
+ };
+
Struct {
- struct_type: from_ctor_kind(struct_type),
+ kind,
generics: generics.into_tcx(tcx),
- fields_stripped,
- fields: ids(fields, tcx),
impls: Vec::new(), // Added in JsonRenderer::item
}
}
@@ -315,14 +347,6 @@ impl FromWithTcx<clean::Union> for Union {
}
}
-pub(crate) fn from_ctor_kind(struct_type: CtorKind) -> StructType {
- match struct_type {
- CtorKind::Fictive => StructType::Plain,
- CtorKind::Fn => StructType::Tuple,
- CtorKind::Const => StructType::Unit,
- }
-}
-
pub(crate) fn from_fn_header(header: &rustc_hir::FnHeader) -> Header {
Header {
async_: header.is_async(),
@@ -347,15 +371,15 @@ fn convert_abi(a: RustcAbi) -> Abi {
}
}
+fn convert_lifetime(l: clean::Lifetime) -> String {
+ l.0.to_string()
+}
+
impl FromWithTcx<clean::Generics> for Generics {
fn from_tcx(generics: clean::Generics, tcx: TyCtxt<'_>) -> Self {
Generics {
- params: generics.params.into_iter().map(|x| x.into_tcx(tcx)).collect(),
- where_predicates: generics
- .where_predicates
- .into_iter()
- .map(|x| x.into_tcx(tcx))
- .collect(),
+ params: generics.params.into_tcx(tcx),
+ where_predicates: generics.where_predicates.into_tcx(tcx),
}
}
}
@@ -374,10 +398,10 @@ impl FromWithTcx<clean::GenericParamDefKind> for GenericParamDefKind {
use clean::GenericParamDefKind::*;
match kind {
Lifetime { outlives } => GenericParamDefKind::Lifetime {
- outlives: outlives.into_iter().map(|lt| lt.0.to_string()).collect(),
+ outlives: outlives.into_iter().map(convert_lifetime).collect(),
},
Type { did: _, bounds, default, synthetic } => GenericParamDefKind::Type {
- bounds: bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
+ bounds: bounds.into_tcx(tcx),
default: default.map(|x| (*x).into_tcx(tcx)),
synthetic,
},
@@ -395,7 +419,7 @@ impl FromWithTcx<clean::WherePredicate> for WherePredicate {
match predicate {
BoundPredicate { ty, bounds, bound_params } => WherePredicate::BoundPredicate {
type_: ty.into_tcx(tcx),
- bounds: bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
+ bounds: bounds.into_tcx(tcx),
generic_params: bound_params
.into_iter()
.map(|x| GenericParamDef {
@@ -405,11 +429,12 @@ impl FromWithTcx<clean::WherePredicate> for WherePredicate {
.collect(),
},
RegionPredicate { lifetime, bounds } => WherePredicate::RegionPredicate {
- lifetime: lifetime.0.to_string(),
- bounds: bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
+ lifetime: convert_lifetime(lifetime),
+ bounds: bounds.into_tcx(tcx),
},
- EqPredicate { lhs, rhs } => {
- WherePredicate::EqPredicate { lhs: lhs.into_tcx(tcx), rhs: rhs.into_tcx(tcx) }
+ // FIXME(fmease): Convert bound parameters as well.
+ EqPredicate { lhs, rhs, bound_params: _ } => {
+ WherePredicate::EqPredicate { lhs: (*lhs).into_tcx(tcx), rhs: (*rhs).into_tcx(tcx) }
}
}
}
@@ -420,15 +445,13 @@ impl FromWithTcx<clean::GenericBound> for GenericBound {
use clean::GenericBound::*;
match bound {
TraitBound(clean::PolyTrait { trait_, generic_params }, modifier) => {
- // FIXME: should `trait_` be a clean::Path equivalent in JSON?
- let trait_ = clean::Type::Path { path: trait_ }.into_tcx(tcx);
GenericBound::TraitBound {
- trait_,
- generic_params: generic_params.into_iter().map(|x| x.into_tcx(tcx)).collect(),
+ trait_: trait_.into_tcx(tcx),
+ generic_params: generic_params.into_tcx(tcx),
modifier: from_trait_bound_modifier(modifier),
}
}
- Outlives(lifetime) => GenericBound::Outlives(lifetime.0.to_string()),
+ Outlives(lifetime) => GenericBound::Outlives(convert_lifetime(lifetime)),
}
}
}
@@ -447,64 +470,49 @@ pub(crate) fn from_trait_bound_modifier(
impl FromWithTcx<clean::Type> for Type {
fn from_tcx(ty: clean::Type, tcx: TyCtxt<'_>) -> Self {
use clean::Type::{
- Array, BareFunction, BorrowedRef, DynTrait, Generic, ImplTrait, Infer, Primitive,
- QPath, RawPointer, Slice, Tuple,
+ Array, BareFunction, BorrowedRef, Generic, ImplTrait, Infer, Primitive, QPath,
+ RawPointer, Slice, Tuple,
};
match ty {
- clean::Type::Path { path } => Type::ResolvedPath {
- name: path.whole_name(),
- id: from_item_id(path.def_id().into(), tcx),
- args: path.segments.last().map(|args| Box::new(args.clone().args.into_tcx(tcx))),
- param_names: Vec::new(),
- },
- DynTrait(mut bounds, lt) => {
- let first_trait = bounds.remove(0).trait_;
-
- Type::ResolvedPath {
- name: first_trait.whole_name(),
- id: from_item_id(first_trait.def_id().into(), tcx),
- args: first_trait
- .segments
- .last()
- .map(|args| Box::new(args.clone().args.into_tcx(tcx))),
- param_names: bounds
- .into_iter()
- .map(|t| {
- clean::GenericBound::TraitBound(t, rustc_hir::TraitBoundModifier::None)
- })
- .chain(lt.map(clean::GenericBound::Outlives))
- .map(|bound| bound.into_tcx(tcx))
- .collect(),
- }
- }
+ clean::Type::Path { path } => Type::ResolvedPath(path.into_tcx(tcx)),
+ clean::Type::DynTrait(bounds, lt) => Type::DynTrait(DynTrait {
+ lifetime: lt.map(convert_lifetime),
+ traits: bounds.into_tcx(tcx),
+ }),
Generic(s) => Type::Generic(s.to_string()),
Primitive(p) => Type::Primitive(p.as_sym().to_string()),
BareFunction(f) => Type::FunctionPointer(Box::new((*f).into_tcx(tcx))),
- Tuple(t) => Type::Tuple(t.into_iter().map(|x| x.into_tcx(tcx)).collect()),
+ Tuple(t) => Type::Tuple(t.into_tcx(tcx)),
Slice(t) => Type::Slice(Box::new((*t).into_tcx(tcx))),
Array(t, s) => Type::Array { type_: Box::new((*t).into_tcx(tcx)), len: s },
- ImplTrait(g) => Type::ImplTrait(g.into_iter().map(|x| x.into_tcx(tcx)).collect()),
+ ImplTrait(g) => Type::ImplTrait(g.into_tcx(tcx)),
Infer => Type::Infer,
RawPointer(mutability, type_) => Type::RawPointer {
mutable: mutability == ast::Mutability::Mut,
type_: Box::new((*type_).into_tcx(tcx)),
},
BorrowedRef { lifetime, mutability, type_ } => Type::BorrowedRef {
- lifetime: lifetime.map(|l| l.0.to_string()),
+ lifetime: lifetime.map(convert_lifetime),
mutable: mutability == ast::Mutability::Mut,
type_: Box::new((*type_).into_tcx(tcx)),
},
- QPath { assoc, self_type, trait_, .. } => {
- // FIXME: should `trait_` be a clean::Path equivalent in JSON?
- let trait_ = clean::Type::Path { path: trait_ }.into_tcx(tcx);
- Type::QualifiedPath {
- name: assoc.name.to_string(),
- args: Box::new(assoc.args.clone().into_tcx(tcx)),
- self_type: Box::new((*self_type).into_tcx(tcx)),
- trait_: Box::new(trait_),
- }
- }
+ QPath(box clean::QPathData { assoc, self_type, trait_, .. }) => Type::QualifiedPath {
+ name: assoc.name.to_string(),
+ args: Box::new(assoc.args.into_tcx(tcx)),
+ self_type: Box::new(self_type.into_tcx(tcx)),
+ trait_: trait_.into_tcx(tcx),
+ },
+ }
+ }
+}
+
+impl FromWithTcx<clean::Path> for Path {
+ fn from_tcx(path: clean::Path, tcx: TyCtxt<'_>) -> Path {
+ Path {
+ name: path.whole_name(),
+ id: from_item_id(path.def_id().into(), tcx),
+ args: path.segments.last().map(|args| Box::new(args.clone().args.into_tcx(tcx))),
}
}
}
@@ -528,7 +536,7 @@ impl FromWithTcx<clean::BareFunctionDecl> for FunctionPointer {
async_: false,
abi: convert_abi(abi),
},
- generic_params: generic_params.into_iter().map(|x| x.into_tcx(tcx)).collect(),
+ generic_params: generic_params.into_tcx(tcx),
decl: decl.into_tcx(tcx),
}
}
@@ -562,18 +570,25 @@ impl FromWithTcx<clean::Trait> for Trait {
is_unsafe,
items: ids(items, tcx),
generics: generics.into_tcx(tcx),
- bounds: bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
+ bounds: bounds.into_tcx(tcx),
implementations: Vec::new(), // Added in JsonRenderer::item
}
}
}
-impl FromWithTcx<Box<clean::Impl>> for Impl {
- fn from_tcx(impl_: Box<clean::Impl>, tcx: TyCtxt<'_>) -> Self {
+impl FromWithTcx<clean::PolyTrait> for PolyTrait {
+ fn from_tcx(
+ clean::PolyTrait { trait_, generic_params }: clean::PolyTrait,
+ tcx: TyCtxt<'_>,
+ ) -> Self {
+ PolyTrait { trait_: trait_.into_tcx(tcx), generic_params: generic_params.into_tcx(tcx) }
+ }
+}
+
+impl FromWithTcx<clean::Impl> for Impl {
+ fn from_tcx(impl_: clean::Impl, tcx: TyCtxt<'_>) -> Self {
let provided_trait_methods = impl_.provided_trait_methods(tcx);
- let clean::Impl { unsafety, generics, trait_, for_, items, polarity, kind } = *impl_;
- // FIXME: should `trait_` be a clean::Path equivalent in JSON?
- let trait_ = trait_.map(|path| clean::Type::Path { path }.into_tcx(tcx));
+ let clean::Impl { unsafety, generics, trait_, for_, items, polarity, kind } = impl_;
// FIXME: use something like ImplKind in JSON?
let (synthetic, blanket_impl) = match kind {
clean::ImplKind::Normal | clean::ImplKind::FakeVaradic => (false, None),
@@ -591,7 +606,7 @@ impl FromWithTcx<Box<clean::Impl>> for Impl {
.into_iter()
.map(|x| x.to_string())
.collect(),
- trait_,
+ trait_: trait_.map(|path| path.into_tcx(tcx)),
for_: for_.into_tcx(tcx),
items: ids(items, tcx),
negative: negative_polarity,
@@ -642,38 +657,28 @@ impl FromWithTcx<clean::Enum> for Enum {
}
}
-impl FromWithTcx<clean::VariantStruct> for Struct {
- fn from_tcx(struct_: clean::VariantStruct, tcx: TyCtxt<'_>) -> Self {
- let fields_stripped = struct_.has_stripped_entries();
- let clean::VariantStruct { struct_type, fields } = struct_;
- Struct {
- struct_type: from_ctor_kind(struct_type),
- generics: Generics { params: vec![], where_predicates: vec![] },
- fields_stripped,
- fields: ids(fields, tcx),
- impls: Vec::new(),
- }
- }
-}
-
impl FromWithTcx<clean::Variant> for Variant {
fn from_tcx(variant: clean::Variant, tcx: TyCtxt<'_>) -> Self {
use clean::Variant::*;
match variant {
- CLike => Variant::Plain,
- Tuple(fields) => Variant::Tuple(
- fields
- .into_iter()
- .map(|f| {
- if let clean::StructFieldItem(ty) = *f.kind {
- ty.into_tcx(tcx)
- } else {
- unreachable!()
- }
- })
- .collect(),
- ),
- Struct(s) => Variant::Struct(ids(s.fields, tcx)),
+ CLike(disr) => Variant::Plain(disr.map(|disr| disr.into_tcx(tcx))),
+ Tuple(fields) => Variant::Tuple(ids_keeping_stripped(fields, tcx)),
+ Struct(s) => Variant::Struct {
+ fields_stripped: s.has_stripped_entries(),
+ fields: ids(s.fields, tcx),
+ },
+ }
+ }
+}
+
+impl FromWithTcx<clean::Discriminant> for Discriminant {
+ fn from_tcx(disr: clean::Discriminant, tcx: TyCtxt<'_>) -> Self {
+ Discriminant {
+ // expr is only none if going throught the inlineing path, which gets
+ // `rustc_middle` types, not `rustc_hir`, but because JSON never inlines
+ // the expr is always some.
+ expr: disr.expr(tcx).unwrap(),
+ value: disr.value(tcx),
}
}
}
@@ -681,24 +686,18 @@ impl FromWithTcx<clean::Variant> for Variant {
impl FromWithTcx<clean::Import> for Import {
fn from_tcx(import: clean::Import, tcx: TyCtxt<'_>) -> Self {
use clean::ImportKind::*;
- match import.kind {
- Simple(s) => Import {
- source: import.source.path.whole_name(),
- name: s.to_string(),
- id: import.source.did.map(ItemId::from).map(|i| from_item_id(i, tcx)),
- glob: false,
- },
- Glob => Import {
- source: import.source.path.whole_name(),
- name: import
- .source
- .path
- .last_opt()
- .unwrap_or_else(|| Symbol::intern("*"))
- .to_string(),
- id: import.source.did.map(ItemId::from).map(|i| from_item_id(i, tcx)),
- glob: true,
- },
+ let (name, glob) = match import.kind {
+ Simple(s) => (s.to_string(), false),
+ Glob => (
+ import.source.path.last_opt().unwrap_or_else(|| Symbol::intern("*")).to_string(),
+ true,
+ ),
+ };
+ Import {
+ source: import.source.path.whole_name(),
+ name,
+ id: import.source.did.map(ItemId::from).map(|i| from_item_id(i, tcx)),
+ glob,
}
}
}
@@ -730,10 +729,7 @@ impl FromWithTcx<Box<clean::Typedef>> for Typedef {
impl FromWithTcx<clean::OpaqueTy> for OpaqueTy {
fn from_tcx(opaque: clean::OpaqueTy, tcx: TyCtxt<'_>) -> Self {
- OpaqueTy {
- bounds: opaque.bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
- generics: opaque.generics.into_tcx(tcx),
- }
+ OpaqueTy { bounds: opaque.bounds.into_tcx(tcx), generics: opaque.generics.into_tcx(tcx) }
}
}
@@ -749,10 +745,7 @@ impl FromWithTcx<clean::Static> for Static {
impl FromWithTcx<clean::TraitAlias> for TraitAlias {
fn from_tcx(alias: clean::TraitAlias, tcx: TyCtxt<'_>) -> Self {
- TraitAlias {
- generics: alias.generics.into_tcx(tcx),
- params: alias.bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
- }
+ TraitAlias { generics: alias.generics.into_tcx(tcx), params: alias.bounds.into_tcx(tcx) }
}
}
@@ -796,3 +789,19 @@ fn ids(items: impl IntoIterator<Item = clean::Item>, tcx: TyCtxt<'_>) -> Vec<Id>
.map(|i| from_item_id_with_name(i.item_id, tcx, i.name))
.collect()
}
+
+fn ids_keeping_stripped(
+ items: impl IntoIterator<Item = clean::Item>,
+ tcx: TyCtxt<'_>,
+) -> Vec<Option<Id>> {
+ items
+ .into_iter()
+ .map(|i| {
+ if !i.is_stripped() && !i.is_keyword() {
+ Some(from_item_id_with_name(i.item_id, tcx, i.name))
+ } else {
+ None
+ }
+ })
+ .collect()
+}
diff --git a/src/librustdoc/json/import_finder.rs b/src/librustdoc/json/import_finder.rs
new file mode 100644
index 000000000..c5c687df7
--- /dev/null
+++ b/src/librustdoc/json/import_finder.rs
@@ -0,0 +1,38 @@
+use rustc_data_structures::fx::FxHashSet;
+use rustc_hir::def_id::DefId;
+
+use crate::{
+ clean::{self, Import, ImportSource, Item},
+ fold::DocFolder,
+};
+
+/// Get the id's of all items that are `pub use`d in the crate.
+///
+/// We need this to know if a stripped module is `pub use mod::*`, to decide
+/// if it needs to be kept in the index, despite being stripped.
+///
+/// See [#100973](https://github.com/rust-lang/rust/issues/100973) and
+/// [#101103](https://github.com/rust-lang/rust/issues/101103) for times when
+/// this information is needed.
+pub(crate) fn get_imports(krate: clean::Crate) -> (clean::Crate, FxHashSet<DefId>) {
+ let mut finder = ImportFinder { imported: FxHashSet::default() };
+ let krate = finder.fold_crate(krate);
+ (krate, finder.imported)
+}
+
+struct ImportFinder {
+ imported: FxHashSet<DefId>,
+}
+
+impl DocFolder for ImportFinder {
+ fn fold_item(&mut self, i: Item) -> Option<Item> {
+ match *i.kind {
+ clean::ImportItem(Import { source: ImportSource { did: Some(did), .. }, .. }) => {
+ self.imported.insert(did);
+ Some(i)
+ }
+
+ _ => Some(self.fold_item_recur(i)),
+ }
+ }
+}
diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs
index 6364d00d0..d13efe6c1 100644
--- a/src/librustdoc/json/mod.rs
+++ b/src/librustdoc/json/mod.rs
@@ -5,6 +5,7 @@
//! docs for usage and details.
mod conversions;
+mod import_finder;
use std::cell::RefCell;
use std::fs::{create_dir_all, File};
@@ -12,7 +13,7 @@ use std::io::{BufWriter, Write};
use std::path::PathBuf;
use std::rc::Rc;
-use rustc_data_structures::fx::FxHashMap;
+use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::def_id::DefId;
use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
@@ -39,6 +40,7 @@ pub(crate) struct JsonRenderer<'tcx> {
/// The directory where the blob will be written to.
out_path: PathBuf,
cache: Rc<Cache>,
+ imported_items: FxHashSet<DefId>,
}
impl<'tcx> JsonRenderer<'tcx> {
@@ -99,14 +101,17 @@ impl<'tcx> JsonRenderer<'tcx> {
}
fn get_trait_items(&mut self) -> Vec<(types::Id, types::Item)> {
+ debug!("Adding foreign trait items");
Rc::clone(&self.cache)
.traits
.iter()
.filter_map(|(&id, trait_item)| {
// only need to synthesize items for external traits
if !id.is_local() {
- let trait_item = &trait_item.trait_;
- trait_item.items.clone().into_iter().for_each(|i| self.item(i).unwrap());
+ for item in &trait_item.items {
+ trace!("Adding subitem to {id:?}: {:?}", item.item_id);
+ self.item(item.clone()).unwrap();
+ }
let item_id = from_item_id(id.into(), self.tcx);
Some((
item_id.clone(),
@@ -157,12 +162,16 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
tcx: TyCtxt<'tcx>,
) -> Result<(Self, clean::Crate), Error> {
debug!("Initializing json renderer");
+
+ let (krate, imported_items) = import_finder::get_imports(krate);
+
Ok((
JsonRenderer {
tcx,
index: Rc::new(RefCell::new(FxHashMap::default())),
out_path: options.output,
cache: Rc::new(cache),
+ imported_items,
},
krate,
))
@@ -176,7 +185,9 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
/// the hashmap because certain items (traits and types) need to have their mappings for trait
/// implementations filled out before they're inserted.
fn item(&mut self, item: clean::Item) -> Result<(), Error> {
- trace!("rendering {} {:?}", item.type_(), item.name);
+ let item_type = item.type_();
+ let item_name = item.name;
+ trace!("rendering {} {:?}", item_type, item_name);
// Flatten items that recursively store other items. We include orphaned items from
// stripped modules and etc that are otherwise reachable.
@@ -207,13 +218,16 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
u.impls = self.get_impls(item_id.expect_def_id());
false
}
+ types::ItemEnum::Primitive(ref mut p) => {
+ p.impls = self.get_impls(item_id.expect_def_id());
+ false
+ }
types::ItemEnum::Method(_)
+ | types::ItemEnum::Module(_)
| types::ItemEnum::AssocConst { .. }
- | types::ItemEnum::AssocType { .. }
- | types::ItemEnum::PrimitiveType(_) => true,
- types::ItemEnum::Module(_)
- | types::ItemEnum::ExternCrate { .. }
+ | types::ItemEnum::AssocType { .. } => true,
+ types::ItemEnum::ExternCrate { .. }
| types::ItemEnum::Import(_)
| types::ItemEnum::StructField(_)
| types::ItemEnum::Variant(_)
@@ -245,6 +259,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
}
}
+ trace!("done rendering {} {:?}", item_type, item_name);
Ok(())
}
@@ -255,14 +270,20 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
fn after_krate(&mut self) -> Result<(), Error> {
debug!("Done with crate");
+ debug!("Adding Primitve impls");
for primitive in Rc::clone(&self.cache).primitive_locations.values() {
self.get_impls(*primitive);
}
let e = ExternalCrate { crate_num: LOCAL_CRATE };
+ // FIXME(adotinthevoid): Remove this, as it's not consistant with not
+ // inlining foreign items.
+ let foreign_trait_items = self.get_trait_items();
let mut index = (*self.index).clone().into_inner();
- index.extend(self.get_trait_items());
+ index.extend(foreign_trait_items);
+
+ debug!("Constructing Output");
// This needs to be the default HashMap for compatibility with the public interface for
// rustdoc-json-types
#[allow(rustc::default_hash_types)]
@@ -274,10 +295,9 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
paths: self
.cache
.paths
- .clone()
- .into_iter()
- .chain(self.cache.external_paths.clone().into_iter())
- .map(|(k, (path, kind))| {
+ .iter()
+ .chain(&self.cache.external_paths)
+ .map(|(&k, &(ref path, kind))| {
(
from_item_id(k.into(), self.tcx),
types::ItemSummary {
diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs
index 92380d124..4cf9435d9 100644
--- a/src/librustdoc/lib.rs
+++ b/src/librustdoc/lib.rs
@@ -9,14 +9,12 @@
#![feature(control_flow_enum)]
#![feature(drain_filter)]
#![feature(let_chains)]
-#![feature(let_else)]
#![feature(test)]
#![feature(never_type)]
#![feature(once_cell)]
#![feature(type_ascription)]
#![feature(iter_intersperse)]
#![feature(type_alias_impl_trait)]
-#![feature(generic_associated_types)]
#![recursion_limit = "256"]
#![warn(rustc::internal)]
#![allow(clippy::collapsible_if, clippy::collapsible_else_if)]
@@ -43,6 +41,7 @@ extern crate rustc_errors;
extern crate rustc_expand;
extern crate rustc_feature;
extern crate rustc_hir;
+extern crate rustc_hir_analysis;
extern crate rustc_hir_pretty;
extern crate rustc_index;
extern crate rustc_infer;
@@ -61,7 +60,6 @@ extern crate rustc_session;
extern crate rustc_span;
extern crate rustc_target;
extern crate rustc_trait_selection;
-extern crate rustc_typeck;
extern crate test;
// See docs in https://github.com/rust-lang/rust/blob/master/compiler/rustc/src/main.rs
@@ -156,7 +154,6 @@ pub fn main() {
}
}
- rustc_driver::set_sigpipe_handler();
rustc_driver::install_ice_hook();
// When using CI artifacts (with `download_stage1 = true`), tracing is unconditionally built
@@ -461,7 +458,7 @@ fn opts() -> Vec<RustcOptGroup> {
"human|json|short",
)
}),
- unstable("diagnostic-width", |o| {
+ stable("diagnostic-width", |o| {
o.optopt(
"",
"diagnostic-width",
@@ -676,39 +673,6 @@ fn usage(argv0: &str) {
/// A result type used by several functions under `main()`.
type MainResult = Result<(), ErrorGuaranteed>;
-fn main_args(at_args: &[String]) -> MainResult {
- let args = rustc_driver::args::arg_expand_all(at_args);
-
- let mut options = getopts::Options::new();
- for option in opts() {
- (option.apply)(&mut options);
- }
- let matches = match options.parse(&args[1..]) {
- Ok(m) => m,
- Err(err) => {
- early_error(ErrorOutputType::default(), &err.to_string());
- }
- };
-
- // Note that we discard any distinction between different non-zero exit
- // codes from `from_matches` here.
- let options = match config::Options::from_matches(&matches, args) {
- Ok(opts) => opts,
- Err(code) => {
- return if code == 0 {
- Ok(())
- } else {
- Err(ErrorGuaranteed::unchecked_claim_error_was_emitted())
- };
- }
- };
- rustc_interface::util::run_in_thread_pool_with_globals(
- options.edition,
- 1, // this runs single-threaded, even in a parallel compiler
- move || main_options(options),
- )
-}
-
fn wrap_return(diag: &rustc_errors::Handler, res: Result<(), String>) -> MainResult {
match res {
Ok(()) => Ok(()),
@@ -739,7 +703,33 @@ fn run_renderer<'tcx, T: formats::FormatRenderer<'tcx>>(
}
}
-fn main_options(options: config::Options) -> MainResult {
+fn main_args(at_args: &[String]) -> MainResult {
+ let args = rustc_driver::args::arg_expand_all(at_args);
+
+ let mut options = getopts::Options::new();
+ for option in opts() {
+ (option.apply)(&mut options);
+ }
+ let matches = match options.parse(&args[1..]) {
+ Ok(m) => m,
+ Err(err) => {
+ early_error(ErrorOutputType::default(), &err.to_string());
+ }
+ };
+
+ // Note that we discard any distinction between different non-zero exit
+ // codes from `from_matches` here.
+ let (options, render_options) = match config::Options::from_matches(&matches, args) {
+ Ok(opts) => opts,
+ Err(code) => {
+ return if code == 0 {
+ Ok(())
+ } else {
+ Err(ErrorGuaranteed::unchecked_claim_error_was_emitted())
+ };
+ }
+ };
+
let diag = core::new_handler(
options.error_format,
None,
@@ -751,9 +741,18 @@ fn main_options(options: config::Options) -> MainResult {
(true, true) => return wrap_return(&diag, markdown::test(options)),
(true, false) => return doctest::run(options),
(false, true) => {
+ let input = options.input.clone();
+ let edition = options.edition;
+ let config = core::create_config(options);
+
+ // `markdown::render` can invoke `doctest::make_test`, which
+ // requires session globals and a thread pool, so we use
+ // `run_compiler`.
return wrap_return(
&diag,
- markdown::render(&options.input, options.render_options, options.edition),
+ interface::run_compiler(config, |_compiler| {
+ markdown::render(&input, render_options, edition)
+ }),
);
}
(false, false) => {}
@@ -774,14 +773,12 @@ fn main_options(options: config::Options) -> MainResult {
let crate_version = options.crate_version.clone();
let output_format = options.output_format;
- // FIXME: fix this clone (especially render_options)
let externs = options.externs.clone();
- let render_options = options.render_options.clone();
let scrape_examples_options = options.scrape_examples_options.clone();
- let document_private = options.render_options.document_private;
+
let config = core::create_config(options);
- interface::create_compiler_and_run(config, |compiler| {
+ interface::run_compiler(config, |compiler| {
let sess = compiler.session();
if sess.opts.describe_lints {
@@ -813,7 +810,7 @@ fn main_options(options: config::Options) -> MainResult {
sess,
krate,
externs,
- document_private,
+ render_options.document_private,
)
});
(resolver.clone(), resolver_caches)
diff --git a/src/librustdoc/lint.rs b/src/librustdoc/lint.rs
index 240aec52c..3aad97bc2 100644
--- a/src/librustdoc/lint.rs
+++ b/src/librustdoc/lint.rs
@@ -64,9 +64,13 @@ where
}
macro_rules! declare_rustdoc_lint {
- ($(#[$attr:meta])* $name: ident, $level: ident, $descr: literal $(,)?) => {
+ (
+ $(#[$attr:meta])* $name: ident, $level: ident, $descr: literal $(,)?
+ $(@feature_gate = $gate:expr;)?
+ ) => {
declare_tool_lint! {
$(#[$attr])* pub rustdoc::$name, $level, $descr
+ $(, @feature_gate = $gate;)?
}
}
}
@@ -123,7 +127,8 @@ declare_rustdoc_lint! {
/// [rustdoc book]: ../../../rustdoc/lints.html#missing_doc_code_examples
MISSING_DOC_CODE_EXAMPLES,
Allow,
- "detects publicly-exported items without code samples in their documentation"
+ "detects publicly-exported items without code samples in their documentation",
+ @feature_gate = rustc_span::symbol::sym::rustdoc_missing_doc_code_examples;
}
declare_rustdoc_lint! {
@@ -143,7 +148,7 @@ declare_rustdoc_lint! {
///
/// [rustdoc book]: ../../../rustdoc/lints.html#invalid_html_tags
INVALID_HTML_TAGS,
- Allow,
+ Warn,
"detects invalid HTML tags in doc comments"
}
diff --git a/src/librustdoc/markdown.rs b/src/librustdoc/markdown.rs
index 0b557ef24..044e05144 100644
--- a/src/librustdoc/markdown.rs
+++ b/src/librustdoc/markdown.rs
@@ -5,7 +5,6 @@ use std::path::Path;
use rustc_span::edition::Edition;
use rustc_span::source_map::DUMMY_SP;
-use rustc_span::Symbol;
use crate::config::{Options, RenderOptions};
use crate::doctest::{Collector, GlobalTestOptions};
@@ -36,6 +35,8 @@ fn extract_leading_metadata(s: &str) -> (Vec<&str>, &str) {
/// Render `input` (e.g., "foo.md") into an HTML file in `output`
/// (e.g., output = "bar" => "bar/foo.html").
+///
+/// Requires session globals to be available, for symbol interning.
pub(crate) fn render<P: AsRef<Path>>(
input: P,
options: RenderOptions,
@@ -133,7 +134,7 @@ pub(crate) fn test(options: Options) -> Result<(), String> {
let mut opts = GlobalTestOptions::default();
opts.no_crate_inject = true;
let mut collector = Collector::new(
- Symbol::intern(&options.input.display().to_string()),
+ options.input.display().to_string(),
options.clone(),
true,
opts,
@@ -142,7 +143,7 @@ pub(crate) fn test(options: Options) -> Result<(), String> {
options.enable_per_target_ignores,
);
collector.set_position(DUMMY_SP);
- let codes = ErrorCodes::from(options.render_options.unstable_features.is_nightly_build());
+ let codes = ErrorCodes::from(options.unstable_features.is_nightly_build());
find_testable_code(&input_str, &mut collector, codes, options.enable_per_target_ignores, None);
diff --git a/src/librustdoc/passes/bare_urls.rs b/src/librustdoc/passes/bare_urls.rs
index 392e26ea6..7ff3ccef9 100644
--- a/src/librustdoc/passes/bare_urls.rs
+++ b/src/librustdoc/passes/bare_urls.rs
@@ -71,16 +71,14 @@ impl<'a, 'tcx> DocVisitor for BareUrlsLinter<'a, 'tcx> {
let report_diag = |cx: &DocContext<'_>, msg: &str, url: &str, range: Range<usize>| {
let sp = super::source_span_for_markdown_range(cx.tcx, &dox, &range, &item.attrs)
.unwrap_or_else(|| item.attr_span(cx.tcx));
- cx.tcx.struct_span_lint_hir(crate::lint::BARE_URLS, hir_id, sp, |lint| {
- lint.build(msg)
- .note("bare URLs are not automatically turned into clickable links")
+ cx.tcx.struct_span_lint_hir(crate::lint::BARE_URLS, hir_id, sp, msg, |lint| {
+ lint.note("bare URLs are not automatically turned into clickable links")
.span_suggestion(
sp,
"use an automatic link instead",
format!("<{}>", url),
Applicability::MachineApplicable,
)
- .emit();
});
};
diff --git a/src/librustdoc/passes/calculate_doc_coverage.rs b/src/librustdoc/passes/calculate_doc_coverage.rs
index 4c6e3eb04..48835abf9 100644
--- a/src/librustdoc/passes/calculate_doc_coverage.rs
+++ b/src/librustdoc/passes/calculate_doc_coverage.rs
@@ -215,7 +215,6 @@ impl<'a, 'b> DocVisitor for CoverageCalculator<'a, 'b> {
None,
);
- let filename = i.span(self.ctx.tcx).filename(self.ctx.sess());
let has_doc_example = tests.found_tests != 0;
// The `expect_def_id()` should be okay because `local_def_id_to_hir_id`
// would presumably panic if a fake `DefIndex` were passed.
@@ -261,13 +260,16 @@ impl<'a, 'b> DocVisitor for CoverageCalculator<'a, 'b> {
let should_have_docs = !should_be_ignored
&& (level != lint::Level::Allow || matches!(source, LintLevelSource::Default));
- debug!("counting {:?} {:?} in {:?}", i.type_(), i.name, filename);
- self.items.entry(filename).or_default().count_item(
- has_docs,
- has_doc_example,
- should_have_doc_example(self.ctx, i),
- should_have_docs,
- );
+ if let Some(span) = i.span(self.ctx.tcx) {
+ let filename = span.filename(self.ctx.sess());
+ debug!("counting {:?} {:?} in {:?}", i.type_(), i.name, filename);
+ self.items.entry(filename).or_default().count_item(
+ has_docs,
+ has_doc_example,
+ should_have_doc_example(self.ctx, i),
+ should_have_docs,
+ );
+ }
}
}
diff --git a/src/librustdoc/passes/check_code_block_syntax.rs b/src/librustdoc/passes/check_code_block_syntax.rs
index 0172ef570..2e651b538 100644
--- a/src/librustdoc/passes/check_code_block_syntax.rs
+++ b/src/librustdoc/passes/check_code_block_syntax.rs
@@ -1,7 +1,9 @@
//! Validates syntax inside Rust code blocks (\`\`\`rust).
use rustc_data_structures::sync::{Lock, Lrc};
use rustc_errors::{
- emitter::Emitter, Applicability, Diagnostic, Handler, LazyFallbackBundle, LintDiagnosticBuilder,
+ emitter::Emitter,
+ translation::{to_fluent_args, Translate},
+ Applicability, Diagnostic, Handler, LazyFallbackBundle,
};
use rustc_parse::parse_stream_from_source_str;
use rustc_session::parse::ParseSess;
@@ -96,48 +98,10 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
None => (item.attr_span(self.cx.tcx), false),
};
- // lambda that will use the lint to start a new diagnostic and add
- // a suggestion to it when needed.
- let diag_builder = |lint: LintDiagnosticBuilder<'_, ()>| {
- let explanation = if is_ignore {
- "`ignore` code blocks require valid Rust code for syntax highlighting; \
- mark blocks that do not contain Rust code as text"
- } else {
- "mark blocks that do not contain Rust code as text"
- };
- let msg = if buffer.has_errors {
- "could not parse code block as Rust code"
- } else {
- "Rust code block is empty"
- };
- let mut diag = lint.build(msg);
-
- if precise_span {
- if is_ignore {
- // giving an accurate suggestion is hard because `ignore` might not have come first in the list.
- // just give a `help` instead.
- diag.span_help(
- sp.from_inner(InnerSpan::new(0, 3)),
- &format!("{}: ```text", explanation),
- );
- } else if empty_block {
- diag.span_suggestion(
- sp.from_inner(InnerSpan::new(0, 3)).shrink_to_hi(),
- explanation,
- "text",
- Applicability::MachineApplicable,
- );
- }
- } else if empty_block || is_ignore {
- diag.help(&format!("{}: ```text", explanation));
- }
-
- // FIXME(#67563): Provide more context for these errors by displaying the spans inline.
- for message in buffer.messages.iter() {
- diag.note(message);
- }
-
- diag.emit();
+ let msg = if buffer.has_errors {
+ "could not parse code block as Rust code"
+ } else {
+ "Rust code block is empty"
};
// Finally build and emit the completed diagnostic.
@@ -147,7 +111,42 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
crate::lint::INVALID_RUST_CODEBLOCKS,
hir_id,
sp,
- diag_builder,
+ msg,
+ |lint| {
+ let explanation = if is_ignore {
+ "`ignore` code blocks require valid Rust code for syntax highlighting; \
+ mark blocks that do not contain Rust code as text"
+ } else {
+ "mark blocks that do not contain Rust code as text"
+ };
+
+ if precise_span {
+ if is_ignore {
+ // giving an accurate suggestion is hard because `ignore` might not have come first in the list.
+ // just give a `help` instead.
+ lint.span_help(
+ sp.from_inner(InnerSpan::new(0, 3)),
+ &format!("{}: ```text", explanation),
+ );
+ } else if empty_block {
+ lint.span_suggestion(
+ sp.from_inner(InnerSpan::new(0, 3)).shrink_to_hi(),
+ explanation,
+ "text",
+ Applicability::MachineApplicable,
+ );
+ }
+ } else if empty_block || is_ignore {
+ lint.help(&format!("{}: ```text", explanation));
+ }
+
+ // FIXME(#67563): Provide more context for these errors by displaying the spans inline.
+ for message in buffer.messages.iter() {
+ lint.note(message);
+ }
+
+ lint
+ },
);
}
}
@@ -181,11 +180,24 @@ struct BufferEmitter {
fallback_bundle: LazyFallbackBundle,
}
+impl Translate for BufferEmitter {
+ fn fluent_bundle(&self) -> Option<&Lrc<rustc_errors::FluentBundle>> {
+ None
+ }
+
+ fn fallback_fluent_bundle(&self) -> &rustc_errors::FluentBundle {
+ &**self.fallback_bundle
+ }
+}
+
impl Emitter for BufferEmitter {
fn emit_diagnostic(&mut self, diag: &Diagnostic) {
let mut buffer = self.buffer.borrow_mut();
- // FIXME(davidtwco): need to support translation here eventually
- buffer.messages.push(format!("error from rustc: {}", diag.message[0].0.expect_str()));
+
+ let fluent_args = to_fluent_args(diag.args());
+ let translated_main_message = self.translate_message(&diag.message[0].0, &fluent_args);
+
+ buffer.messages.push(format!("error from rustc: {}", translated_main_message));
if diag.is_error() {
buffer.has_errors = true;
}
@@ -194,12 +206,4 @@ impl Emitter for BufferEmitter {
fn source_map(&self) -> Option<&Lrc<SourceMap>> {
None
}
-
- fn fluent_bundle(&self) -> Option<&Lrc<rustc_errors::FluentBundle>> {
- None
- }
-
- fn fallback_fluent_bundle(&self) -> &rustc_errors::FluentBundle {
- &**self.fallback_bundle
- }
}
diff --git a/src/librustdoc/passes/check_doc_test_visibility.rs b/src/librustdoc/passes/check_doc_test_visibility.rs
index e86f90833..7740c6d5b 100644
--- a/src/librustdoc/passes/check_doc_test_visibility.rs
+++ b/src/librustdoc/passes/check_doc_test_visibility.rs
@@ -56,7 +56,7 @@ impl crate::doctest::Tester for Tests {
}
pub(crate) fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> bool {
- if !cx.cache.access_levels.is_public(item.item_id.expect_def_id())
+ if !cx.cache.effective_visibilities.is_directly_public(item.item_id.expect_def_id())
|| matches!(
*item.kind,
clean::StructFieldItem(_)
@@ -117,7 +117,7 @@ pub(crate) fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item
find_testable_code(dox, &mut tests, ErrorCodes::No, false, None);
- if tests.found_tests == 0 && cx.tcx.sess.is_nightly_build() {
+ if tests.found_tests == 0 && cx.tcx.features().rustdoc_missing_doc_code_examples {
if should_have_doc_example(cx, item) {
debug!("reporting error for {:?} (hir_id={:?})", item, hir_id);
let sp = item.attr_span(cx.tcx);
@@ -125,21 +125,19 @@ pub(crate) fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item
crate::lint::MISSING_DOC_CODE_EXAMPLES,
hir_id,
sp,
- |lint| {
- lint.build("missing code example in this documentation").emit();
- },
+ "missing code example in this documentation",
+ |lint| lint,
);
}
} else if tests.found_tests > 0
- && !cx.cache.access_levels.is_exported(item.item_id.expect_def_id())
+ && !cx.cache.effective_visibilities.is_exported(item.item_id.expect_def_id())
{
cx.tcx.struct_span_lint_hir(
crate::lint::PRIVATE_DOC_TESTS,
hir_id,
item.attr_span(cx.tcx),
- |lint| {
- lint.build("documentation test in private item").emit();
- },
+ "documentation test in private item",
+ |lint| lint,
);
}
}
diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs
index 7d7a63c53..8aa0abd36 100644
--- a/src/librustdoc/passes/collect_intra_doc_links.rs
+++ b/src/librustdoc/passes/collect_intra_doc_links.rs
@@ -80,10 +80,10 @@ impl Res {
}
}
- fn def_id(self, tcx: TyCtxt<'_>) -> DefId {
+ fn def_id(self, tcx: TyCtxt<'_>) -> Option<DefId> {
match self {
- Res::Def(_, id) => id,
- Res::Primitive(prim) => *PrimitiveType::primitive_locations(tcx).get(&prim).unwrap(),
+ Res::Def(_, id) => Some(id),
+ Res::Primitive(prim) => PrimitiveType::primitive_locations(tcx).get(&prim).copied(),
}
}
@@ -223,6 +223,9 @@ enum MalformedGenerics {
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub(crate) enum UrlFragment {
Item(DefId),
+ /// A part of a page that isn't a rust item.
+ ///
+ /// Eg: `[Vector Examples](std::vec::Vec#examples)`
UserWritten(String),
}
@@ -477,7 +480,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
// If there's no `::`, it's not an associated item.
// So we can be sure that `rustc_resolve` was accurate when it said it wasn't resolved.
.ok_or_else(|| {
- debug!("found no `::`, assumming {} was correctly not in scope", item_name);
+ debug!("found no `::`, assuming {} was correctly not in scope", item_name);
UnresolvedPath {
item_id,
module_id,
@@ -750,7 +753,7 @@ fn resolve_associated_trait_item<'a>(
///
/// This is just a wrapper around [`TyCtxt::impl_item_implementor_ids()`] and
/// [`TyCtxt::associated_item()`] (with some helpful logging added).
-#[instrument(level = "debug", skip(tcx))]
+#[instrument(level = "debug", skip(tcx), ret)]
fn trait_assoc_to_impl_assoc_item<'tcx>(
tcx: TyCtxt<'tcx>,
impl_id: DefId,
@@ -760,9 +763,7 @@ fn trait_assoc_to_impl_assoc_item<'tcx>(
debug!(?trait_to_impl_assoc_map);
let impl_assoc_id = *trait_to_impl_assoc_map.get(&trait_assoc_id)?;
debug!(?impl_assoc_id);
- let impl_assoc = tcx.associated_item(impl_assoc_id);
- debug!(?impl_assoc);
- Some(impl_assoc)
+ Some(tcx.associated_item(impl_assoc_id))
}
/// Given a type, return all trait impls in scope in `module` for that type.
@@ -1126,10 +1127,10 @@ impl LinkCollector<'_, '_> {
}
}
- Some(ItemLink {
+ res.def_id(self.cx.tcx).map(|page_id| ItemLink {
link: ori_link.link.clone(),
link_text: link_text.clone(),
- did: res.def_id(self.cx.tcx),
+ page_id,
fragment,
})
}
@@ -1148,11 +1149,12 @@ impl LinkCollector<'_, '_> {
item,
&diag_info,
)?;
- let id = clean::register_res(self.cx, rustc_hir::def::Res::Def(kind, id));
+
+ let page_id = clean::register_res(self.cx, rustc_hir::def::Res::Def(kind, id));
Some(ItemLink {
link: ori_link.link.clone(),
link_text: link_text.clone(),
- did: id,
+ page_id,
fragment,
})
}
@@ -1200,8 +1202,8 @@ impl LinkCollector<'_, '_> {
item.item_id.expect_def_id().as_local().map(|src_id| (src_id, dst_id))
})
{
- if self.cx.tcx.privacy_access_levels(()).is_exported(src_id)
- && !self.cx.tcx.privacy_access_levels(()).is_exported(dst_id)
+ if self.cx.tcx.effective_visibilities(()).is_exported(src_id)
+ && !self.cx.tcx.effective_visibilities(()).is_exported(dst_id)
{
privacy_error(self.cx, diag_info, path_str);
}
@@ -1256,7 +1258,7 @@ impl LinkCollector<'_, '_> {
&mut self,
key: ResolutionInfo,
diag: DiagnosticInfo<'_>,
- // If errors are cached then they are only reported on first ocurrence
+ // If errors are cached then they are only reported on first occurrence
// which we want in some cases but not in others.
cache_errors: bool,
) -> Option<(Res, Option<UrlFragment>)> {
@@ -1607,9 +1609,7 @@ fn report_diagnostic(
let sp = item.attr_span(tcx);
- tcx.struct_span_lint_hir(lint, hir_id, sp, |lint| {
- let mut diag = lint.build(msg);
-
+ tcx.struct_span_lint_hir(lint, hir_id, sp, msg, |lint| {
let span =
super::source_span_for_markdown_range(tcx, dox, link_range, &item.attrs).map(|sp| {
if dox.as_bytes().get(link_range.start) == Some(&b'`')
@@ -1622,7 +1622,7 @@ fn report_diagnostic(
});
if let Some(sp) = span {
- diag.set_span(sp);
+ lint.set_span(sp);
} else {
// blah blah blah\nblah\nblah [blah] blah blah\nblah blah
// ^ ~~~~
@@ -1632,7 +1632,7 @@ fn report_diagnostic(
let line = dox[last_new_line_offset..].lines().next().unwrap_or("");
// Print the line containing the `link_range` and manually mark it with '^'s.
- diag.note(&format!(
+ lint.note(&format!(
"the link appears in this line:\n\n{line}\n\
{indicator: <before$}{indicator:^<found$}",
line = line,
@@ -1642,9 +1642,9 @@ fn report_diagnostic(
));
}
- decorate(&mut diag, span);
+ decorate(lint, span);
- diag.emit();
+ lint
});
}
@@ -1807,8 +1807,8 @@ fn resolution_failure(
}
return;
}
- Trait | TyAlias | ForeignTy | OpaqueTy | TraitAlias | TyParam
- | Static(_) => "associated item",
+ Trait | TyAlias | ForeignTy | OpaqueTy | ImplTraitPlaceholder
+ | TraitAlias | TyParam | Static(_) => "associated item",
Impl | GlobalAsm => unreachable!("not a path"),
}
} else {
diff --git a/src/librustdoc/passes/collect_intra_doc_links/early.rs b/src/librustdoc/passes/collect_intra_doc_links/early.rs
index 38cfd7a27..1b373cfe5 100644
--- a/src/librustdoc/passes/collect_intra_doc_links/early.rs
+++ b/src/librustdoc/passes/collect_intra_doc_links/early.rs
@@ -37,7 +37,6 @@ pub(crate) fn early_resolve_intra_doc_links(
markdown_links: Default::default(),
doc_link_resolutions: Default::default(),
traits_in_scope: Default::default(),
- all_traits: Default::default(),
all_trait_impls: Default::default(),
all_macro_rules: Default::default(),
document_private_items,
@@ -48,7 +47,6 @@ pub(crate) fn early_resolve_intra_doc_links(
link_resolver.resolve_doc_links_local(&krate.attrs);
link_resolver.process_module_children_or_reexports(CRATE_DEF_ID.to_def_id());
visit::walk_crate(&mut link_resolver, krate);
- link_resolver.process_extern_impls();
// FIXME: somehow rustdoc is still missing crates even though we loaded all
// the known necessary crates. Load them all unconditionally until we find a way to fix this.
@@ -58,11 +56,12 @@ pub(crate) fn early_resolve_intra_doc_links(
link_resolver.resolver.resolve_rustdoc_path(extern_name, TypeNS, parent_scope);
}
+ link_resolver.process_extern_impls();
+
ResolverCaches {
markdown_links: Some(link_resolver.markdown_links),
doc_link_resolutions: link_resolver.doc_link_resolutions,
traits_in_scope: link_resolver.traits_in_scope,
- all_traits: Some(link_resolver.all_traits),
all_trait_impls: Some(link_resolver.all_trait_impls),
all_macro_rules: link_resolver.all_macro_rules,
}
@@ -80,7 +79,6 @@ struct EarlyDocLinkResolver<'r, 'ra> {
markdown_links: FxHashMap<String, Vec<PreprocessedMarkdownLink>>,
doc_link_resolutions: FxHashMap<(Symbol, Namespace, DefId), Option<Res<ast::NodeId>>>,
traits_in_scope: DefIdMap<Vec<TraitCandidate>>,
- all_traits: Vec<DefId>,
all_trait_impls: Vec<DefId>,
all_macro_rules: FxHashMap<Symbol, Res<ast::NodeId>>,
document_private_items: bool,
@@ -121,8 +119,6 @@ impl<'ra> EarlyDocLinkResolver<'_, 'ra> {
loop {
let crates = Vec::from_iter(self.resolver.cstore().crates_untracked());
for &cnum in &crates[start_cnum..] {
- let all_traits =
- Vec::from_iter(self.resolver.cstore().traits_in_crate_untracked(cnum));
let all_trait_impls =
Vec::from_iter(self.resolver.cstore().trait_impls_in_crate_untracked(cnum));
let all_inherent_impls =
@@ -131,20 +127,18 @@ impl<'ra> EarlyDocLinkResolver<'_, 'ra> {
self.resolver.cstore().incoherent_impls_in_crate_untracked(cnum),
);
- // Querying traits in scope is expensive so we try to prune the impl and traits lists
- // using privacy, private traits and impls from other crates are never documented in
+ // Querying traits in scope is expensive so we try to prune the impl lists using
+ // privacy, private traits and impls from other crates are never documented in
// the current crate, and links in their doc comments are not resolved.
- for &def_id in &all_traits {
- if self.resolver.cstore().visibility_untracked(def_id).is_public() {
- self.resolve_doc_links_extern_impl(def_id, false);
- }
- }
for &(trait_def_id, impl_def_id, simplified_self_ty) in &all_trait_impls {
if self.resolver.cstore().visibility_untracked(trait_def_id).is_public()
&& simplified_self_ty.and_then(|ty| ty.def()).map_or(true, |ty_def_id| {
self.resolver.cstore().visibility_untracked(ty_def_id).is_public()
})
{
+ if self.visited_mods.insert(trait_def_id) {
+ self.resolve_doc_links_extern_impl(trait_def_id, false);
+ }
self.resolve_doc_links_extern_impl(impl_def_id, false);
}
}
@@ -157,7 +151,6 @@ impl<'ra> EarlyDocLinkResolver<'_, 'ra> {
self.resolve_doc_links_extern_impl(impl_def_id, true);
}
- self.all_traits.extend(all_traits);
self.all_trait_impls
.extend(all_trait_impls.into_iter().map(|(_, def_id, _)| def_id));
}
@@ -306,15 +299,20 @@ impl<'ra> EarlyDocLinkResolver<'_, 'ra> {
{
if let Some(def_id) = child.res.opt_def_id() && !def_id.is_local() {
let scope_id = match child.res {
- Res::Def(DefKind::Variant, ..) => self.resolver.parent(def_id),
+ Res::Def(
+ DefKind::Variant
+ | DefKind::AssocTy
+ | DefKind::AssocFn
+ | DefKind::AssocConst,
+ ..,
+ ) => self.resolver.parent(def_id),
_ => def_id,
};
self.resolve_doc_links_extern_outer(def_id, scope_id); // Outer attribute scope
if let Res::Def(DefKind::Mod, ..) = child.res {
self.resolve_doc_links_extern_inner(def_id); // Inner attribute scope
}
- // `DefKind::Trait`s are processed in `process_extern_impls`.
- if let Res::Def(DefKind::Mod | DefKind::Enum, ..) = child.res {
+ if let Res::Def(DefKind::Mod | DefKind::Enum | DefKind::Trait, ..) = child.res {
self.process_module_children_or_reexports(def_id);
}
if let Res::Def(DefKind::Struct | DefKind::Union | DefKind::Variant, _) =
@@ -356,10 +354,14 @@ impl Visitor<'_> for EarlyDocLinkResolver<'_, '_> {
self.parent_scope.module = old_module;
} else {
match &item.kind {
- ItemKind::Trait(..) => {
- self.all_traits.push(self.resolver.local_def_id(item.id).to_def_id());
- }
- ItemKind::Impl(box ast::Impl { of_trait: Some(..), .. }) => {
+ ItemKind::Impl(box ast::Impl { of_trait: Some(trait_ref), .. }) => {
+ if let Some(partial_res) = self.resolver.get_partial_res(trait_ref.ref_id)
+ && let Some(res) = partial_res.full_res()
+ && let Some(trait_def_id) = res.opt_def_id()
+ && !trait_def_id.is_local()
+ && self.visited_mods.insert(trait_def_id) {
+ self.resolve_doc_links_extern_impl(trait_def_id, false);
+ }
self.all_trait_impls.push(self.resolver.local_def_id(item.id).to_def_id());
}
ItemKind::MacroDef(macro_def) if macro_def.macro_rules => {
diff --git a/src/librustdoc/passes/html_tags.rs b/src/librustdoc/passes/html_tags.rs
index f3a3c853c..a89ed7c7e 100644
--- a/src/librustdoc/passes/html_tags.rs
+++ b/src/librustdoc/passes/html_tags.rs
@@ -22,10 +22,8 @@ struct InvalidHtmlTagsLinter<'a, 'tcx> {
}
pub(crate) fn check_invalid_html_tags(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
- if cx.tcx.sess.is_nightly_build() {
- let mut coll = InvalidHtmlTagsLinter { cx };
- coll.visit_crate(&krate);
- }
+ let mut coll = InvalidHtmlTagsLinter { cx };
+ coll.visit_crate(&krate);
krate
}
@@ -94,6 +92,34 @@ fn extract_path_backwards(text: &str, end_pos: usize) -> Option<usize> {
if current_pos == end_pos { None } else { Some(current_pos) }
}
+fn extract_path_forward(text: &str, start_pos: usize) -> Option<usize> {
+ use rustc_lexer::{is_id_continue, is_id_start};
+ let mut current_pos = start_pos;
+ loop {
+ if current_pos < text.len() && text[current_pos..].starts_with("::") {
+ current_pos += 2;
+ } else {
+ break;
+ }
+ let mut chars = text[current_pos..].chars();
+ if let Some(c) = chars.next() {
+ if is_id_start(c) {
+ current_pos += c.len_utf8();
+ } else {
+ break;
+ }
+ }
+ while let Some(c) = chars.next() {
+ if is_id_continue(c) {
+ current_pos += c.len_utf8();
+ } else {
+ break;
+ }
+ }
+ }
+ if current_pos == start_pos { None } else { Some(current_pos) }
+}
+
fn is_valid_for_html_tag_name(c: char, is_empty: bool) -> bool {
// https://spec.commonmark.org/0.30/#raw-html
//
@@ -158,7 +184,60 @@ fn extract_html_tag(
}
drop_tag(tags, tag_name, r, f);
} else {
- tags.push((tag_name, r));
+ let mut is_self_closing = false;
+ let mut quote_pos = None;
+ if c != '>' {
+ let mut quote = None;
+ let mut after_eq = false;
+ for (i, c) in text[pos..].char_indices() {
+ if !c.is_whitespace() {
+ if let Some(q) = quote {
+ if c == q {
+ quote = None;
+ quote_pos = None;
+ after_eq = false;
+ }
+ } else if c == '>' {
+ break;
+ } else if c == '/' && !after_eq {
+ is_self_closing = true;
+ } else {
+ if is_self_closing {
+ is_self_closing = false;
+ }
+ if (c == '"' || c == '\'') && after_eq {
+ quote = Some(c);
+ quote_pos = Some(pos + i);
+ } else if c == '=' {
+ after_eq = true;
+ }
+ }
+ } else if quote.is_none() {
+ after_eq = false;
+ }
+ }
+ }
+ if let Some(quote_pos) = quote_pos {
+ let qr = Range { start: quote_pos, end: quote_pos };
+ f(
+ &format!("unclosed quoted HTML attribute on tag `{}`", tag_name),
+ &qr,
+ false,
+ );
+ }
+ if is_self_closing {
+ // https://html.spec.whatwg.org/#parse-error-non-void-html-element-start-tag-with-trailing-solidus
+ let valid = ALLOWED_UNCLOSED.contains(&&tag_name[..])
+ || tags.iter().take(pos + 1).any(|(at, _)| {
+ let at = at.to_lowercase();
+ at == "svg" || at == "math"
+ });
+ if !valid {
+ f(&format!("invalid self-closing HTML tag `{}`", tag_name), &r, false);
+ }
+ } else {
+ tags.push((tag_name, r));
+ }
}
}
break;
@@ -212,27 +291,74 @@ impl<'a, 'tcx> DocVisitor for InvalidHtmlTagsLinter<'a, 'tcx> {
Some(sp) => sp,
None => item.attr_span(tcx),
};
- tcx.struct_span_lint_hir(crate::lint::INVALID_HTML_TAGS, hir_id, sp, |lint| {
+ tcx.struct_span_lint_hir(crate::lint::INVALID_HTML_TAGS, hir_id, sp, msg, |lint| {
use rustc_lint_defs::Applicability;
- let mut diag = lint.build(msg);
// If a tag looks like `<this>`, it might actually be a generic.
// We don't try to detect stuff `<like, this>` because that's not valid HTML,
// and we don't try to detect stuff `<like this>` because that's not valid Rust.
- if let Some(Some(generics_start)) = (is_open_tag
- && dox[..range.end].ends_with('>'))
+ let mut generics_end = range.end;
+ if let Some(Some(mut generics_start)) = (is_open_tag
+ && dox[..generics_end].ends_with('>'))
.then(|| extract_path_backwards(&dox, range.start))
{
+ while generics_start != 0
+ && generics_end < dox.len()
+ && dox.as_bytes()[generics_start - 1] == b'<'
+ && dox.as_bytes()[generics_end] == b'>'
+ {
+ generics_end += 1;
+ generics_start -= 1;
+ if let Some(new_start) = extract_path_backwards(&dox, generics_start) {
+ generics_start = new_start;
+ }
+ if let Some(new_end) = extract_path_forward(&dox, generics_end) {
+ generics_end = new_end;
+ }
+ }
+ if let Some(new_end) = extract_path_forward(&dox, generics_end) {
+ generics_end = new_end;
+ }
let generics_sp = match super::source_span_for_markdown_range(
tcx,
&dox,
- &(generics_start..range.end),
+ &(generics_start..generics_end),
&item.attrs,
) {
Some(sp) => sp,
None => item.attr_span(tcx),
};
+ // Sometimes, we only extract part of a path. For example, consider this:
+ //
+ // <[u32] as IntoIter<u32>>::Item
+ // ^^^^^ unclosed HTML tag `u32`
+ //
+ // We don't have any code for parsing fully-qualified trait paths.
+ // In theory, we could add it, but doing it correctly would require
+ // parsing the entire path grammar, which is problematic because of
+ // overlap between the path grammar and Markdown.
+ //
+ // The example above shows that ambiguity. Is `[u32]` intended to be an
+ // intra-doc link to the u32 primitive, or is it intended to be a slice?
+ //
+ // If the below conditional were removed, we would suggest this, which is
+ // not what the user probably wants.
+ //
+ // <[u32] as `IntoIter<u32>`>::Item
+ //
+ // We know that the user actually wants to wrap the whole thing in a code
+ // block, but the only reason we know that is because `u32` does not, in
+ // fact, implement IntoIter. If the example looks like this:
+ //
+ // <[Vec<i32>] as IntoIter<i32>::Item
+ //
+ // The ideal fix would be significantly different.
+ if (generics_start > 0 && dox.as_bytes()[generics_start - 1] == b'<')
+ || (generics_end < dox.len() && dox.as_bytes()[generics_end] == b'>')
+ {
+ return lint;
+ }
// multipart form is chosen here because ``Vec<i32>`` would be confusing.
- diag.multipart_suggestion(
+ lint.multipart_suggestion(
"try marking as source code",
vec![
(generics_sp.shrink_to_lo(), String::from("`")),
@@ -241,7 +367,8 @@ impl<'a, 'tcx> DocVisitor for InvalidHtmlTagsLinter<'a, 'tcx> {
Applicability::MaybeIncorrect,
);
}
- diag.emit()
+
+ lint
});
};
@@ -278,7 +405,7 @@ impl<'a, 'tcx> DocVisitor for InvalidHtmlTagsLinter<'a, 'tcx> {
for (event, range) in p {
match event {
Event::Start(Tag::CodeBlock(_)) => in_code_block = true,
- Event::Html(text) | Event::Text(text) if !in_code_block => {
+ Event::Html(text) if !in_code_block => {
extract_tags(&mut tags, &text, range, &mut is_in_comment, &report_diag)
}
Event::End(Tag::CodeBlock(_)) => in_code_block = false,
diff --git a/src/librustdoc/passes/propagate_doc_cfg.rs b/src/librustdoc/passes/propagate_doc_cfg.rs
index 0c5d83655..de3a4b339 100644
--- a/src/librustdoc/passes/propagate_doc_cfg.rs
+++ b/src/librustdoc/passes/propagate_doc_cfg.rs
@@ -2,29 +2,74 @@
use std::sync::Arc;
use crate::clean::cfg::Cfg;
-use crate::clean::{Crate, Item};
+use crate::clean::inline::{load_attrs, merge_attrs};
+use crate::clean::{Crate, Item, ItemKind};
use crate::core::DocContext;
use crate::fold::DocFolder;
use crate::passes::Pass;
+use rustc_hir::def_id::LocalDefId;
+
pub(crate) const PROPAGATE_DOC_CFG: Pass = Pass {
name: "propagate-doc-cfg",
run: propagate_doc_cfg,
description: "propagates `#[doc(cfg(...))]` to child items",
};
-pub(crate) fn propagate_doc_cfg(cr: Crate, _: &mut DocContext<'_>) -> Crate {
- CfgPropagator { parent_cfg: None }.fold_crate(cr)
+pub(crate) fn propagate_doc_cfg(cr: Crate, cx: &mut DocContext<'_>) -> Crate {
+ CfgPropagator { parent_cfg: None, parent: None, cx }.fold_crate(cr)
}
-struct CfgPropagator {
+struct CfgPropagator<'a, 'tcx> {
parent_cfg: Option<Arc<Cfg>>,
+ parent: Option<LocalDefId>,
+ cx: &'a mut DocContext<'tcx>,
+}
+
+impl<'a, 'tcx> CfgPropagator<'a, 'tcx> {
+ // Some items need to merge their attributes with their parents' otherwise a few of them
+ // (mostly `cfg` ones) will be missing.
+ fn merge_with_parent_attributes(&mut self, item: &mut Item) {
+ let check_parent = match &*item.kind {
+ // impl blocks can be in different modules with different cfg and we need to get them
+ // as well.
+ ItemKind::ImplItem(_) => false,
+ kind if kind.is_non_assoc() => true,
+ _ => return,
+ };
+
+ let Some(def_id) = item.item_id.as_def_id().and_then(|def_id| def_id.as_local())
+ else { return };
+
+ let hir = self.cx.tcx.hir();
+ let hir_id = hir.local_def_id_to_hir_id(def_id);
+
+ if check_parent {
+ let expected_parent = hir.get_parent_item(hir_id);
+ // If parents are different, it means that `item` is a reexport and we need
+ // to compute the actual `cfg` by iterating through its "real" parents.
+ if self.parent == Some(expected_parent.def_id) {
+ return;
+ }
+ }
+
+ let mut attrs = Vec::new();
+ for (parent_hir_id, _) in hir.parent_iter(hir_id) {
+ if let Some(def_id) = hir.opt_local_def_id(parent_hir_id) {
+ attrs.extend_from_slice(load_attrs(self.cx, def_id.to_def_id()));
+ }
+ }
+ let (_, cfg) = merge_attrs(self.cx, None, item.attrs.other_attrs.as_slice(), Some(&attrs));
+ item.cfg = cfg;
+ }
}
-impl DocFolder for CfgPropagator {
+impl<'a, 'tcx> DocFolder for CfgPropagator<'a, 'tcx> {
fn fold_item(&mut self, mut item: Item) -> Option<Item> {
let old_parent_cfg = self.parent_cfg.clone();
+ self.merge_with_parent_attributes(&mut item);
+
let new_cfg = match (self.parent_cfg.take(), item.cfg.take()) {
(None, None) => None,
(Some(rc), None) | (None, Some(rc)) => Some(rc),
@@ -37,8 +82,15 @@ impl DocFolder for CfgPropagator {
self.parent_cfg = new_cfg.clone();
item.cfg = new_cfg;
+ let old_parent =
+ if let Some(def_id) = item.item_id.as_def_id().and_then(|def_id| def_id.as_local()) {
+ self.parent.replace(def_id)
+ } else {
+ self.parent.take()
+ };
let result = self.fold_item_recur(item);
self.parent_cfg = old_parent_cfg;
+ self.parent = old_parent;
Some(result)
}
diff --git a/src/librustdoc/passes/strip_hidden.rs b/src/librustdoc/passes/strip_hidden.rs
index 533e2ce46..9914edf30 100644
--- a/src/librustdoc/passes/strip_hidden.rs
+++ b/src/librustdoc/passes/strip_hidden.rs
@@ -17,6 +17,7 @@ pub(crate) const STRIP_HIDDEN: Pass = Pass {
/// Strip items marked `#[doc(hidden)]`
pub(crate) fn strip_hidden(krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
let mut retained = ItemIdSet::default();
+ let is_json_output = cx.output_format.is_json() && !cx.show_coverage;
// strip all #[doc(hidden)] items
let krate = {
@@ -25,7 +26,12 @@ pub(crate) fn strip_hidden(krate: clean::Crate, cx: &mut DocContext<'_>) -> clea
};
// strip all impls referencing stripped items
- let mut stripper = ImplStripper { retained: &retained, cache: &cx.cache };
+ let mut stripper = ImplStripper {
+ retained: &retained,
+ cache: &cx.cache,
+ is_json_output,
+ document_private: cx.render_options.document_private,
+ };
stripper.fold_crate(krate)
}
diff --git a/src/librustdoc/passes/strip_private.rs b/src/librustdoc/passes/strip_private.rs
index 9ba841a31..450f69e15 100644
--- a/src/librustdoc/passes/strip_private.rs
+++ b/src/librustdoc/passes/strip_private.rs
@@ -17,19 +17,25 @@ pub(crate) const STRIP_PRIVATE: Pass = Pass {
pub(crate) fn strip_private(mut krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
// This stripper collects all *retained* nodes.
let mut retained = ItemIdSet::default();
+ let is_json_output = cx.output_format.is_json() && !cx.show_coverage;
// strip all private items
{
let mut stripper = Stripper {
retained: &mut retained,
- access_levels: &cx.cache.access_levels,
+ effective_visibilities: &cx.cache.effective_visibilities,
update_retained: true,
- is_json_output: cx.output_format.is_json() && !cx.show_coverage,
+ is_json_output,
};
krate = ImportStripper.fold_crate(stripper.fold_crate(krate));
}
// strip all impls referencing private items
- let mut stripper = ImplStripper { retained: &retained, cache: &cx.cache };
+ let mut stripper = ImplStripper {
+ retained: &retained,
+ cache: &cx.cache,
+ is_json_output,
+ document_private: cx.render_options.document_private,
+ };
stripper.fold_crate(krate)
}
diff --git a/src/librustdoc/passes/stripper.rs b/src/librustdoc/passes/stripper.rs
index 0d419042a..0089ce63d 100644
--- a/src/librustdoc/passes/stripper.rs
+++ b/src/librustdoc/passes/stripper.rs
@@ -1,30 +1,34 @@
//! A collection of utility functions for the `strip_*` passes.
use rustc_hir::def_id::DefId;
-use rustc_middle::middle::privacy::AccessLevels;
+use rustc_middle::middle::privacy::EffectiveVisibilities;
+use rustc_span::symbol::sym;
+
use std::mem;
-use crate::clean::{self, Item, ItemId, ItemIdSet};
+use crate::clean::{self, Item, ItemId, ItemIdSet, NestedAttributesExt};
use crate::fold::{strip_item, DocFolder};
use crate::formats::cache::Cache;
pub(crate) struct Stripper<'a> {
pub(crate) retained: &'a mut ItemIdSet,
- pub(crate) access_levels: &'a AccessLevels<DefId>,
+ pub(crate) effective_visibilities: &'a EffectiveVisibilities<DefId>,
pub(crate) update_retained: bool,
pub(crate) is_json_output: bool,
}
-impl<'a> Stripper<'a> {
- // We need to handle this differently for the JSON output because some non exported items could
- // be used in public API. And so, we need these items as well. `is_exported` only checks if they
- // are in the public API, which is not enough.
- #[inline]
- fn is_item_reachable(&self, item_id: ItemId) -> bool {
- if self.is_json_output {
- self.access_levels.is_reachable(item_id.expect_def_id())
- } else {
- self.access_levels.is_exported(item_id.expect_def_id())
- }
+// We need to handle this differently for the JSON output because some non exported items could
+// be used in public API. And so, we need these items as well. `is_exported` only checks if they
+// are in the public API, which is not enough.
+#[inline]
+fn is_item_reachable(
+ is_json_output: bool,
+ effective_visibilities: &EffectiveVisibilities<DefId>,
+ item_id: ItemId,
+) -> bool {
+ if is_json_output {
+ effective_visibilities.is_reachable(item_id.expect_def_id())
+ } else {
+ effective_visibilities.is_exported(item_id.expect_def_id())
}
}
@@ -61,7 +65,9 @@ impl<'a> DocFolder for Stripper<'a> {
| clean::MacroItem(..)
| clean::ForeignTypeItem => {
let item_id = i.item_id;
- if item_id.is_local() && !self.is_item_reachable(item_id) {
+ if item_id.is_local()
+ && !is_item_reachable(self.is_json_output, self.effective_visibilities, item_id)
+ {
debug!("Stripper: stripping {:?} {:?}", i.type_(), i.name);
return None;
}
@@ -84,7 +90,17 @@ impl<'a> DocFolder for Stripper<'a> {
}
// handled in the `strip-priv-imports` pass
- clean::ExternCrateItem { .. } | clean::ImportItem(..) => {}
+ clean::ExternCrateItem { .. } => {}
+ clean::ImportItem(ref imp) => {
+ // Because json doesn't inline imports from private modules, we need to mark
+ // the imported item as retained so it's impls won't be stripped.
+ //
+ // FIXME: Is it necessary to check for json output here: See
+ // https://github.com/rust-lang/rust/pull/100325#discussion_r941495215
+ if let Some(did) = imp.source.did && self.is_json_output {
+ self.retained.insert(did.into());
+ }
+ }
clean::ImplItem(..) => {}
@@ -133,6 +149,24 @@ impl<'a> DocFolder for Stripper<'a> {
pub(crate) struct ImplStripper<'a> {
pub(crate) retained: &'a ItemIdSet,
pub(crate) cache: &'a Cache,
+ pub(crate) is_json_output: bool,
+ pub(crate) document_private: bool,
+}
+
+impl<'a> ImplStripper<'a> {
+ #[inline]
+ fn should_keep_impl(&self, item: &Item, for_def_id: DefId) -> bool {
+ if !for_def_id.is_local() || self.retained.contains(&for_def_id.into()) {
+ true
+ } else if self.is_json_output {
+ // If the "for" item is exported and the impl block isn't `#[doc(hidden)]`, then we
+ // need to keep it.
+ self.cache.effective_visibilities.is_exported(for_def_id)
+ && !item.attrs.lists(sym::doc).has_word(sym::hidden)
+ } else {
+ false
+ }
+ }
}
impl<'a> DocFolder for ImplStripper<'a> {
@@ -140,18 +174,39 @@ impl<'a> DocFolder for ImplStripper<'a> {
if let clean::ImplItem(ref imp) = *i.kind {
// Impl blocks can be skipped if they are: empty; not a trait impl; and have no
// documentation.
- if imp.trait_.is_none() && imp.items.is_empty() && i.doc_value().is_none() {
- return None;
+ //
+ // There is one special case: if the impl block contains only private items.
+ if imp.trait_.is_none() {
+ // If the only items present are private ones and we're not rendering private items,
+ // we don't document it.
+ if !imp.items.is_empty()
+ && !self.document_private
+ && imp.items.iter().all(|i| {
+ let item_id = i.item_id;
+ item_id.is_local()
+ && !is_item_reachable(
+ self.is_json_output,
+ &self.cache.effective_visibilities,
+ item_id,
+ )
+ })
+ {
+ return None;
+ } else if imp.items.is_empty() && i.doc_value().is_none() {
+ return None;
+ }
}
+ // Because we don't inline in `maybe_inline_local` if the output format is JSON,
+ // we need to make a special check for JSON output: we want to keep it unless it has
+ // a `#[doc(hidden)]` attribute if the `for_` type is exported.
if let Some(did) = imp.for_.def_id(self.cache) {
- if did.is_local() && !imp.for_.is_assoc_ty() && !self.retained.contains(&did.into())
- {
+ if !imp.for_.is_assoc_ty() && !self.should_keep_impl(&i, did) {
debug!("ImplStripper: impl item for stripped type; removing");
return None;
}
}
if let Some(did) = imp.trait_.as_ref().map(|t| t.def_id()) {
- if did.is_local() && !self.retained.contains(&did.into()) {
+ if !self.should_keep_impl(&i, did) {
debug!("ImplStripper: impl item for stripped trait; removing");
return None;
}
@@ -159,7 +214,7 @@ impl<'a> DocFolder for ImplStripper<'a> {
if let Some(generics) = imp.trait_.as_ref().and_then(|t| t.generics()) {
for typaram in generics {
if let Some(did) = typaram.def_id(self.cache) {
- if did.is_local() && !self.retained.contains(&did.into()) {
+ if !self.should_keep_impl(&i, did) {
debug!(
"ImplStripper: stripped item in trait's generics; removing impl"
);
diff --git a/src/librustdoc/scrape_examples.rs b/src/librustdoc/scrape_examples.rs
index 0d9684025..dfa6ba38b 100644
--- a/src/librustdoc/scrape_examples.rs
+++ b/src/librustdoc/scrape_examples.rs
@@ -143,14 +143,14 @@ where
// then we need to exit before calling typeck (which will panic). See
// test/run-make/rustdoc-scrape-examples-invalid-expr for an example.
let hir = tcx.hir();
- if hir.maybe_body_owned_by(ex.hir_id.owner).is_none() {
+ if hir.maybe_body_owned_by(ex.hir_id.owner.def_id).is_none() {
return;
}
// Get type of function if expression is a function call
let (ty, call_span, ident_span) = match ex.kind {
hir::ExprKind::Call(f, _) => {
- let types = tcx.typeck(ex.hir_id.owner);
+ let types = tcx.typeck(ex.hir_id.owner.def_id);
if let Some(ty) = types.node_type_opt(f.hir_id) {
(ty, ex.span, f.span)
@@ -159,8 +159,8 @@ where
return;
}
}
- hir::ExprKind::MethodCall(path, _, call_span) => {
- let types = tcx.typeck(ex.hir_id.owner);
+ hir::ExprKind::MethodCall(path, _, _, call_span) => {
+ let types = tcx.typeck(ex.hir_id.owner.def_id);
let Some(def_id) = types.type_dependent_def_id(ex.hir_id) else {
trace!("type_dependent_def_id({}) = None", ex.hir_id);
return;
@@ -183,9 +183,8 @@ where
// If the enclosing item has a span coming from a proc macro, then we also don't want to include
// the example.
- let enclosing_item_span = tcx
- .hir()
- .span_with_body(tcx.hir().local_def_id_to_hir_id(tcx.hir().get_parent_item(ex.hir_id)));
+ let enclosing_item_span =
+ tcx.hir().span_with_body(tcx.hir().get_parent_item(ex.hir_id).into());
if enclosing_item_span.from_expansion() {
trace!("Rejecting expr ({call_span:?}) from macro item: {enclosing_item_span:?}");
return;
diff --git a/src/librustdoc/theme.rs b/src/librustdoc/theme.rs
index 0118d7dd2..e7a26cb34 100644
--- a/src/librustdoc/theme.rs
+++ b/src/librustdoc/theme.rs
@@ -1,271 +1,252 @@
-use rustc_data_structures::fx::FxHashSet;
+use rustc_data_structures::fx::FxHashMap;
+use std::collections::hash_map::Entry;
use std::fs;
-use std::hash::{Hash, Hasher};
+use std::iter::Peekable;
use std::path::Path;
+use std::str::Chars;
use rustc_errors::Handler;
#[cfg(test)]
mod tests;
-#[derive(Debug, Clone, Eq)]
+#[derive(Debug)]
pub(crate) struct CssPath {
- pub(crate) name: String,
- pub(crate) children: FxHashSet<CssPath>,
-}
-
-// This PartialEq implementation IS NOT COMMUTATIVE!!!
-//
-// The order is very important: the second object must have all first's rules.
-// However, the first is not required to have all of the second's rules.
-impl PartialEq for CssPath {
- fn eq(&self, other: &CssPath) -> bool {
- if self.name != other.name {
- false
- } else {
- for child in &self.children {
- if !other.children.iter().any(|c| child == c) {
- return false;
- }
- }
- true
+ pub(crate) rules: FxHashMap<String, String>,
+ pub(crate) children: FxHashMap<String, CssPath>,
+}
+
+/// When encountering a `"` or a `'`, returns the whole string, including the quote characters.
+fn get_string(iter: &mut Peekable<Chars<'_>>, string_start: char, buffer: &mut String) {
+ buffer.push(string_start);
+ while let Some(c) = iter.next() {
+ buffer.push(c);
+ if c == '\\' {
+ iter.next();
+ } else if c == string_start {
+ break;
}
}
}
-impl Hash for CssPath {
- fn hash<H: Hasher>(&self, state: &mut H) {
- self.name.hash(state);
- for x in &self.children {
- x.hash(state);
+fn get_inside_paren(
+ iter: &mut Peekable<Chars<'_>>,
+ paren_start: char,
+ paren_end: char,
+ buffer: &mut String,
+) {
+ buffer.push(paren_start);
+ while let Some(c) = iter.next() {
+ handle_common_chars(c, buffer, iter);
+ if c == paren_end {
+ break;
}
}
}
-impl CssPath {
- fn new(name: String) -> CssPath {
- CssPath { name, children: FxHashSet::default() }
+/// Skips a `/*` comment.
+fn skip_comment(iter: &mut Peekable<Chars<'_>>) {
+ while let Some(c) = iter.next() {
+ if c == '*' && iter.next() == Some('/') {
+ break;
+ }
}
}
-/// All variants contain the position they occur.
-#[derive(Debug, Clone, Copy)]
-enum Events {
- StartLineComment(usize),
- StartComment(usize),
- EndComment(usize),
- InBlock(usize),
- OutBlock(usize),
-}
-
-impl Events {
- fn get_pos(&self) -> usize {
- match *self {
- Events::StartLineComment(p)
- | Events::StartComment(p)
- | Events::EndComment(p)
- | Events::InBlock(p)
- | Events::OutBlock(p) => p,
+/// Skips a line comment (`//`).
+fn skip_line_comment(iter: &mut Peekable<Chars<'_>>) {
+ while let Some(c) = iter.next() {
+ if c == '\n' {
+ break;
}
}
-
- fn is_comment(&self) -> bool {
- matches!(
- self,
- Events::StartLineComment(_) | Events::StartComment(_) | Events::EndComment(_)
- )
- }
}
-fn previous_is_line_comment(events: &[Events]) -> bool {
- matches!(events.last(), Some(&Events::StartLineComment(_)))
-}
-
-fn is_line_comment(pos: usize, v: &[u8], events: &[Events]) -> bool {
- if let Some(&Events::StartComment(_)) = events.last() {
- return false;
+fn handle_common_chars(c: char, buffer: &mut String, iter: &mut Peekable<Chars<'_>>) {
+ match c {
+ '"' | '\'' => get_string(iter, c, buffer),
+ '/' if iter.peek() == Some(&'*') => skip_comment(iter),
+ '/' if iter.peek() == Some(&'/') => skip_line_comment(iter),
+ '(' => get_inside_paren(iter, c, ')', buffer),
+ '[' => get_inside_paren(iter, c, ']', buffer),
+ _ => buffer.push(c),
}
- v[pos + 1] == b'/'
}
-fn load_css_events(v: &[u8]) -> Vec<Events> {
- let mut pos = 0;
- let mut events = Vec::with_capacity(100);
-
- while pos + 1 < v.len() {
- match v[pos] {
- b'/' if v[pos + 1] == b'*' => {
- events.push(Events::StartComment(pos));
- pos += 1;
- }
- b'/' if is_line_comment(pos, v, &events) => {
- events.push(Events::StartLineComment(pos));
- pos += 1;
- }
- b'\n' if previous_is_line_comment(&events) => {
- events.push(Events::EndComment(pos));
- }
- b'*' if v[pos + 1] == b'/' => {
- events.push(Events::EndComment(pos + 2));
- pos += 1;
- }
- b'{' if !previous_is_line_comment(&events) => {
- if let Some(&Events::StartComment(_)) = events.last() {
- pos += 1;
- continue;
- }
- events.push(Events::InBlock(pos + 1));
- }
- b'}' if !previous_is_line_comment(&events) => {
- if let Some(&Events::StartComment(_)) = events.last() {
- pos += 1;
- continue;
- }
- events.push(Events::OutBlock(pos + 1));
- }
- _ => {}
+/// Returns a CSS property name. Ends when encountering a `:` character.
+///
+/// If the `:` character isn't found, returns `None`.
+///
+/// If a `{` character is encountered, returns an error.
+fn parse_property_name(iter: &mut Peekable<Chars<'_>>) -> Result<Option<String>, String> {
+ let mut content = String::new();
+
+ while let Some(c) = iter.next() {
+ match c {
+ ':' => return Ok(Some(content.trim().to_owned())),
+ '{' => return Err("Unexpected `{` in a `{}` block".to_owned()),
+ '}' => break,
+ _ => handle_common_chars(c, &mut content, iter),
}
- pos += 1;
}
- events
-}
-
-fn get_useful_next(events: &[Events], pos: &mut usize) -> Option<Events> {
- while *pos < events.len() {
- if !events[*pos].is_comment() {
- return Some(events[*pos]);
+ Ok(None)
+}
+
+/// Try to get the value of a CSS property (the `#fff` in `color: #fff`). It'll stop when it
+/// encounters a `{` or a `;` character.
+///
+/// It returns the value string and a boolean set to `true` if the value is ended with a `}` because
+/// it means that the parent block is done and that we should notify the parent caller.
+fn parse_property_value(iter: &mut Peekable<Chars<'_>>) -> (String, bool) {
+ let mut value = String::new();
+ let mut out_block = false;
+
+ while let Some(c) = iter.next() {
+ match c {
+ ';' => break,
+ '}' => {
+ out_block = true;
+ break;
+ }
+ _ => handle_common_chars(c, &mut value, iter),
}
- *pos += 1;
}
- None
+ (value.trim().to_owned(), out_block)
}
-fn get_previous_positions(events: &[Events], mut pos: usize) -> Vec<usize> {
- let mut ret = Vec::with_capacity(3);
+/// This is used to parse inside a CSS `{}` block. If we encounter a new `{` inside it, we consider
+/// it as a new block and therefore recurse into `parse_rules`.
+fn parse_rules(
+ content: &str,
+ selector: String,
+ iter: &mut Peekable<Chars<'_>>,
+ paths: &mut FxHashMap<String, CssPath>,
+) -> Result<(), String> {
+ let mut rules = FxHashMap::default();
+ let mut children = FxHashMap::default();
- ret.push(events[pos].get_pos());
- if pos > 0 {
- pos -= 1;
- }
loop {
- if pos < 1 || !events[pos].is_comment() {
- let x = events[pos].get_pos();
- if *ret.last().unwrap() != x {
- ret.push(x);
- } else {
- ret.push(0);
+ // If the parent isn't a "normal" CSS selector, we only expect sub-selectors and not CSS
+ // properties.
+ if selector.starts_with('@') {
+ parse_selectors(content, iter, &mut children)?;
+ break;
+ }
+ let rule = match parse_property_name(iter)? {
+ Some(r) => {
+ if r.is_empty() {
+ return Err(format!("Found empty rule in selector `{selector}`"));
+ }
+ r
+ }
+ None => break,
+ };
+ let (value, out_block) = parse_property_value(iter);
+ if value.is_empty() {
+ return Err(format!("Found empty value for rule `{rule}` in selector `{selector}`"));
+ }
+ match rules.entry(rule) {
+ Entry::Occupied(mut o) => {
+ *o.get_mut() = value;
+ }
+ Entry::Vacant(v) => {
+ v.insert(value);
}
+ }
+ if out_block {
break;
}
- ret.push(events[pos].get_pos());
- pos -= 1;
- }
- if ret.len() & 1 != 0 && events[pos].is_comment() {
- ret.push(0);
}
- ret.iter().rev().cloned().collect()
-}
-
-fn build_rule(v: &[u8], positions: &[usize]) -> String {
- minifier::css::minify(
- &positions
- .chunks(2)
- .map(|x| ::std::str::from_utf8(&v[x[0]..x[1]]).unwrap_or(""))
- .collect::<String>()
- .trim()
- .chars()
- .filter_map(|c| match c {
- '\n' | '\t' => Some(' '),
- '/' | '{' | '}' => None,
- c => Some(c),
- })
- .collect::<String>()
- .split(' ')
- .filter(|s| !s.is_empty())
- .intersperse(" ")
- .collect::<String>(),
- )
- .map(|css| css.to_string())
- .unwrap_or_else(|_| String::new())
-}
-
-fn inner(v: &[u8], events: &[Events], pos: &mut usize) -> FxHashSet<CssPath> {
- let mut paths = Vec::with_capacity(50);
- while *pos < events.len() {
- if let Some(Events::OutBlock(_)) = get_useful_next(events, pos) {
- *pos += 1;
- break;
+ match paths.entry(selector) {
+ Entry::Occupied(mut o) => {
+ let v = o.get_mut();
+ for (key, value) in rules.into_iter() {
+ v.rules.insert(key, value);
+ }
+ for (sel, child) in children.into_iter() {
+ v.children.insert(sel, child);
+ }
}
- if let Some(Events::InBlock(_)) = get_useful_next(events, pos) {
- paths.push(CssPath::new(build_rule(v, &get_previous_positions(events, *pos))));
- *pos += 1;
+ Entry::Vacant(v) => {
+ v.insert(CssPath { rules, children });
}
- while let Some(Events::InBlock(_)) = get_useful_next(events, pos) {
- if let Some(ref mut path) = paths.last_mut() {
- for entry in inner(v, events, pos).iter() {
- path.children.insert(entry.clone());
- }
+ }
+ Ok(())
+}
+
+pub(crate) fn parse_selectors(
+ content: &str,
+ iter: &mut Peekable<Chars<'_>>,
+ paths: &mut FxHashMap<String, CssPath>,
+) -> Result<(), String> {
+ let mut selector = String::new();
+
+ while let Some(c) = iter.next() {
+ match c {
+ '{' => {
+ let s = minifier::css::minify(selector.trim()).map(|s| s.to_string())?;
+ parse_rules(content, s, iter, paths)?;
+ selector.clear();
}
- }
- if let Some(Events::OutBlock(_)) = get_useful_next(events, pos) {
- *pos += 1;
+ '}' => break,
+ ';' => selector.clear(), // We don't handle inline selectors like `@import`.
+ _ => handle_common_chars(c, &mut selector, iter),
}
}
- paths.iter().cloned().collect()
-}
-
-pub(crate) fn load_css_paths(v: &[u8]) -> CssPath {
- let events = load_css_events(v);
- let mut pos = 0;
-
- let mut parent = CssPath::new("parent".to_owned());
- parent.children = inner(v, &events, &mut pos);
- parent
-}
-
-pub(crate) fn get_differences(against: &CssPath, other: &CssPath, v: &mut Vec<String>) {
- if against.name == other.name {
- for child in &against.children {
- let mut found = false;
- let mut found_working = false;
- let mut tmp = Vec::new();
-
- for other_child in &other.children {
- if child.name == other_child.name {
- if child != other_child {
- get_differences(child, other_child, &mut tmp);
- } else {
- found_working = true;
+ Ok(())
+}
+
+/// The entry point to parse the CSS rules. Every time we encounter a `{`, we then parse the rules
+/// inside it.
+pub(crate) fn load_css_paths(content: &str) -> Result<FxHashMap<String, CssPath>, String> {
+ let mut iter = content.chars().peekable();
+ let mut paths = FxHashMap::default();
+
+ parse_selectors(content, &mut iter, &mut paths)?;
+ Ok(paths)
+}
+
+pub(crate) fn get_differences(
+ origin: &FxHashMap<String, CssPath>,
+ against: &FxHashMap<String, CssPath>,
+ v: &mut Vec<String>,
+) {
+ for (selector, entry) in origin.iter() {
+ match against.get(selector) {
+ Some(a) => {
+ get_differences(&entry.children, &a.children, v);
+ if selector == ":root" {
+ // We need to check that all variables have been set.
+ for rule in entry.rules.keys() {
+ if !a.rules.contains_key(rule) {
+ v.push(format!(" Missing CSS variable `{rule}` in `:root`"));
+ }
}
- found = true;
- break;
}
}
- if !found {
- v.push(format!(" Missing \"{}\" rule", child.name));
- } else if !found_working {
- v.extend(tmp.iter().cloned());
- }
+ None => v.push(format!(" Missing rule `{selector}`")),
}
}
}
pub(crate) fn test_theme_against<P: AsRef<Path>>(
f: &P,
- against: &CssPath,
+ origin: &FxHashMap<String, CssPath>,
diag: &Handler,
) -> (bool, Vec<String>) {
- let data = match fs::read(f) {
+ let against = match fs::read_to_string(f)
+ .map_err(|e| e.to_string())
+ .and_then(|data| load_css_paths(&data))
+ {
Ok(c) => c,
Err(e) => {
- diag.struct_err(&e.to_string()).emit();
+ diag.struct_err(&e).emit();
return (false, vec![]);
}
};
- let paths = load_css_paths(&data);
let mut ret = vec![];
- get_differences(against, &paths, &mut ret);
+ get_differences(origin, &against, &mut ret);
(true, ret)
}
diff --git a/src/librustdoc/theme/tests.rs b/src/librustdoc/theme/tests.rs
index ae8f43c6d..08a174d27 100644
--- a/src/librustdoc/theme/tests.rs
+++ b/src/librustdoc/theme/tests.rs
@@ -44,11 +44,7 @@ rule j end {}
"#;
let mut ret = Vec::new();
- get_differences(
- &load_css_paths(against.as_bytes()),
- &load_css_paths(text.as_bytes()),
- &mut ret,
- );
+ get_differences(&load_css_paths(against).unwrap(), &load_css_paths(text).unwrap(), &mut ret);
assert!(ret.is_empty());
}
@@ -61,46 +57,45 @@ a
c // sdf
d {}
"#;
- let paths = load_css_paths(text.as_bytes());
- assert!(paths.children.contains(&CssPath::new("a b c d".to_owned())));
+ let paths = load_css_paths(text).unwrap();
+ assert!(paths.contains_key(&"a b c d".to_owned()));
}
#[test]
fn test_comparison() {
- let x = r#"
-a {
- b {
- c {}
- }
+ let origin = r#"
+@a {
+ b {}
+ c {}
}
"#;
- let y = r#"
-a {
+ let against = r#"
+@a {
b {}
}
"#;
- let against = load_css_paths(y.as_bytes());
- let other = load_css_paths(x.as_bytes());
+ let origin = load_css_paths(origin).unwrap();
+ let against = load_css_paths(against).unwrap();
let mut ret = Vec::new();
- get_differences(&against, &other, &mut ret);
+ get_differences(&against, &origin, &mut ret);
assert!(ret.is_empty());
- get_differences(&other, &against, &mut ret);
- assert_eq!(ret, vec![" Missing \"c\" rule".to_owned()]);
+ get_differences(&origin, &against, &mut ret);
+ assert_eq!(ret, vec![" Missing rule `c`".to_owned()]);
}
#[test]
fn check_empty_css() {
- let events = load_css_events(&[]);
- assert_eq!(events.len(), 0);
+ let paths = load_css_paths("").unwrap();
+ assert_eq!(paths.len(), 0);
}
#[test]
fn check_invalid_css() {
- let events = load_css_events(b"*");
- assert_eq!(events.len(), 0);
+ let paths = load_css_paths("*").unwrap();
+ assert_eq!(paths.len(), 0);
}
#[test]
@@ -108,10 +103,85 @@ fn test_with_minification() {
let text = include_str!("../html/static/css/themes/dark.css");
let minified = minifier::css::minify(&text).expect("CSS minification failed").to_string();
- let against = load_css_paths(text.as_bytes());
- let other = load_css_paths(minified.as_bytes());
+ let against = load_css_paths(text).unwrap();
+ let other = load_css_paths(&minified).unwrap();
+
+ let mut ret = Vec::new();
+ get_differences(&against, &other, &mut ret);
+ assert!(ret.is_empty());
+}
+
+#[test]
+fn test_media() {
+ let text = r#"
+@media (min-width: 701px) {
+ a:hover {
+ color: #fff;
+ }
+
+ b {
+ x: y;
+ }
+}
+
+@media (max-width: 1001px) {
+ b {
+ x: y;
+ }
+}
+"#;
+
+ let paths = load_css_paths(text).unwrap();
+ let p = paths.get("@media (min-width:701px)");
+ assert!(p.is_some());
+ let p = p.unwrap();
+ assert!(p.children.get("a:hover").is_some());
+ assert!(p.children.get("b").is_some());
+
+ let p = paths.get("@media (max-width:1001px)");
+ assert!(p.is_some());
+ let p = p.unwrap();
+ assert!(p.children.get("b").is_some());
+}
+
+#[test]
+fn test_css_variables() {
+ let x = r#"
+:root {
+ --a: #fff;
+}
+"#;
+
+ let y = r#"
+:root {
+ --a: #fff;
+ --b: #fff;
+}
+"#;
+
+ let against = load_css_paths(x).unwrap();
+ let other = load_css_paths(y).unwrap();
let mut ret = Vec::new();
get_differences(&against, &other, &mut ret);
assert!(ret.is_empty());
+ get_differences(&other, &against, &mut ret);
+ assert_eq!(ret, vec![" Missing CSS variable `--b` in `:root`".to_owned()]);
+}
+
+#[test]
+fn test_weird_rule_value() {
+ let x = r#"
+a[text=("a")] {
+ b: url({;}.png);
+ c: #fff
+}
+"#;
+
+ let paths = load_css_paths(&x).unwrap();
+ let p = paths.get("a[text=(\"a\")]");
+ assert!(p.is_some());
+ let p = p.unwrap();
+ assert_eq!(p.rules.get("b"), Some(&"url({;}.png)".to_owned()));
+ assert_eq!(p.rules.get("c"), Some(&"#fff".to_owned()));
}
diff --git a/src/librustdoc/visit.rs b/src/librustdoc/visit.rs
index 0bb41977c..d29ceead4 100644
--- a/src/librustdoc/visit.rs
+++ b/src/librustdoc/visit.rs
@@ -20,7 +20,7 @@ pub(crate) trait DocVisitor: Sized {
VariantItem(i) => match i {
Variant::Struct(j) => j.fields.iter().for_each(|x| self.visit_item(x)),
Variant::Tuple(fields) => fields.iter().for_each(|x| self.visit_item(x)),
- Variant::CLike => {}
+ Variant::CLike(_) => {}
},
ExternCrateItem { src: _ }
| ImportItem(_)
@@ -65,7 +65,7 @@ pub(crate) trait DocVisitor: Sized {
// FIXME: make this a simple by-ref for loop once external_traits is cleaned up
let external_traits = { std::mem::take(&mut *c.external_traits.borrow_mut()) };
for (k, v) in external_traits {
- v.trait_.items.iter().for_each(|i| self.visit_item(i));
+ v.items.iter().for_each(|i| self.visit_item(i));
c.external_traits.borrow_mut().insert(k, v);
}
}
diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs
index ca7a20bf3..06dffce55 100644
--- a/src/librustdoc/visit_ast.rs
+++ b/src/librustdoc/visit_ast.rs
@@ -7,8 +7,8 @@ use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::Node;
use rustc_hir::CRATE_HIR_ID;
-use rustc_middle::middle::privacy::AccessLevel;
-use rustc_middle::ty::TyCtxt;
+use rustc_middle::middle::privacy::Level;
+use rustc_middle::ty::{TyCtxt, Visibility};
use rustc_span::def_id::{CRATE_DEF_ID, LOCAL_CRATE};
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::Span;
@@ -164,8 +164,20 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
self.inside_public_path &= self.cx.tcx.visibility(def_id).is_public();
for &i in m.item_ids {
let item = self.cx.tcx.hir().item(i);
+ if matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) {
+ continue;
+ }
self.visit_item(item, None, &mut om);
}
+ for &i in m.item_ids {
+ let item = self.cx.tcx.hir().item(i);
+ // To match the way import precedence works, visit glob imports last.
+ // Later passes in rustdoc will de-duplicate by name and kind, so if glob-
+ // imported items appear last, then they'll be the ones that get discarded.
+ if matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) {
+ self.visit_item(item, None, &mut om);
+ }
+ }
self.inside_public_path = orig_inside_public_path;
om
}
@@ -218,7 +230,11 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
} else {
// All items need to be handled here in case someone wishes to link
// to them with intra-doc links
- self.cx.cache.access_levels.map.insert(did, AccessLevel::Public);
+ self.cx.cache.effective_visibilities.set_public_at_level(
+ did,
+ || Visibility::Restricted(CRATE_DEF_ID),
+ Level::Direct,
+ );
}
}
}
@@ -230,7 +246,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
None => return false,
};
- let is_private = !self.cx.cache.access_levels.is_public(res_did);
+ let is_private = !self.cx.cache.effective_visibilities.is_directly_public(res_did);
let is_hidden = inherits_doc_hidden(self.cx.tcx, res_hir_id);
// Only inline if requested or if the item would otherwise be stripped.
@@ -279,11 +295,11 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
debug!("visiting item {:?}", item);
let name = renamed.unwrap_or(item.ident.name);
- let def_id = item.def_id.to_def_id();
+ let def_id = item.owner_id.to_def_id();
let is_pub = self.cx.tcx.visibility(def_id).is_public();
if is_pub {
- self.store_path(item.def_id.to_def_id());
+ self.store_path(item.owner_id.to_def_id());
}
match item.kind {
@@ -344,7 +360,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
// 3. We're inlining, since a reexport where inlining has been requested
// should be inlined even if it is also documented at the top level.
- let def_id = item.def_id.to_def_id();
+ let def_id = item.owner_id.to_def_id();
let is_macro_2_0 = !macro_def.macro_rules;
let nonexported = !self.cx.tcx.has_attr(def_id, sym::macro_export);
@@ -389,7 +405,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
om: &mut Module<'tcx>,
) {
// If inlining we only want to include public functions.
- if !self.inlining || self.cx.tcx.visibility(item.def_id).is_public() {
+ if !self.inlining || self.cx.tcx.visibility(item.owner_id).is_public() {
om.foreigns.push((item, renamed));
}
}
diff --git a/src/librustdoc/visit_lib.rs b/src/librustdoc/visit_lib.rs
index f01ec3866..70214e2ad 100644
--- a/src/librustdoc/visit_lib.rs
+++ b/src/librustdoc/visit_lib.rs
@@ -1,8 +1,8 @@
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::def::{DefKind, Res};
-use rustc_hir::def_id::{CrateNum, DefId};
-use rustc_middle::middle::privacy::{AccessLevel, AccessLevels};
-use rustc_middle::ty::TyCtxt;
+use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_ID};
+use rustc_middle::middle::privacy::{EffectiveVisibilities, Level};
+use rustc_middle::ty::{TyCtxt, Visibility};
// FIXME: this may not be exhaustive, but is sufficient for rustdocs current uses
@@ -10,10 +10,10 @@ use rustc_middle::ty::TyCtxt;
/// specific rustdoc annotations into account (i.e., `doc(hidden)`)
pub(crate) struct LibEmbargoVisitor<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
- // Accessibility levels for reachable nodes
- access_levels: &'a mut AccessLevels<DefId>,
- // Previous accessibility level, None means unreachable
- prev_level: Option<AccessLevel>,
+ // Effective visibilities for reachable nodes
+ effective_visibilities: &'a mut EffectiveVisibilities<DefId>,
+ // Previous level, None means unreachable
+ prev_level: Option<Level>,
// Keeps track of already visited modules, in case a module re-exports its parent
visited_mods: FxHashSet<DefId>,
}
@@ -22,26 +22,30 @@ impl<'a, 'tcx> LibEmbargoVisitor<'a, 'tcx> {
pub(crate) fn new(cx: &'a mut crate::core::DocContext<'tcx>) -> LibEmbargoVisitor<'a, 'tcx> {
LibEmbargoVisitor {
tcx: cx.tcx,
- access_levels: &mut cx.cache.access_levels,
- prev_level: Some(AccessLevel::Public),
+ effective_visibilities: &mut cx.cache.effective_visibilities,
+ prev_level: Some(Level::Direct),
visited_mods: FxHashSet::default(),
}
}
pub(crate) fn visit_lib(&mut self, cnum: CrateNum) {
let did = cnum.as_def_id();
- self.update(did, Some(AccessLevel::Public));
+ self.update(did, Some(Level::Direct));
self.visit_mod(did);
}
// Updates node level and returns the updated level
- fn update(&mut self, did: DefId, level: Option<AccessLevel>) -> Option<AccessLevel> {
+ fn update(&mut self, did: DefId, level: Option<Level>) -> Option<Level> {
let is_hidden = self.tcx.is_doc_hidden(did);
- let old_level = self.access_levels.map.get(&did).cloned();
- // Accessibility levels can only grow
+ let old_level = self.effective_visibilities.public_at_level(did);
+ // Visibility levels can only grow
if level > old_level && !is_hidden {
- self.access_levels.map.insert(did, level.unwrap());
+ self.effective_visibilities.set_public_at_level(
+ did,
+ || Visibility::Restricted(CRATE_DEF_ID),
+ level.unwrap(),
+ );
level
} else {
old_level