Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[rustdoc] Switch to Symbol for item.name #80044

Merged
merged 2 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,10 @@ impl Symbol {
self.0.as_u32()
}

pub fn is_empty(self) -> bool {
self == kw::Invalid
}

/// This method is supposed to be used in error messages, so it's expected to be
/// identical to printing the original identifier token written in source code
/// (`token_to_string`, `Ident::to_string`), except that symbols don't keep the rawness flag
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ crate fn try_inline(
let attrs = merge_attrs(cx, Some(parent_module), target_attrs, attrs_clone);

cx.renderinfo.borrow_mut().inlined.insert(did);
let what_rustc_thinks = clean::Item::from_def_id_and_parts(did, Some(name.clean(cx)), kind, cx);
let what_rustc_thinks = clean::Item::from_def_id_and_parts(did, Some(name), kind, cx);
ret.push(clean::Item { attrs, ..what_rustc_thinks });
Some(ret)
}
Expand Down
23 changes: 9 additions & 14 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl Clean<ExternalCrate> for CrateNum {
for attr in attrs.lists(sym::doc) {
if attr.has_name(sym::keyword) {
if let Some(v) = attr.value_str() {
keyword = Some(v.to_string());
keyword = Some(v);
break;
}
}
Expand Down Expand Up @@ -253,12 +253,7 @@ impl Clean<Item> for doctree::Module<'_> {
ModuleItem(Module { is_crate: self.is_crate, items }),
cx,
);
Item {
name: Some(what_rustc_thinks.name.unwrap_or_default()),
attrs,
source: span.clean(cx),
..what_rustc_thinks
}
Item { attrs, source: span.clean(cx), ..what_rustc_thinks }
}
}

Expand Down Expand Up @@ -1096,7 +1091,7 @@ impl Clean<Item> for hir::TraitItem<'_> {
AssocTypeItem(bounds.clean(cx), default.clean(cx))
}
};
Item::from_def_id_and_parts(local_did, Some(self.ident.name.clean(cx)), inner, cx)
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
})
}
}
Expand Down Expand Up @@ -1124,7 +1119,7 @@ impl Clean<Item> for hir::ImplItem<'_> {
TypedefItem(Typedef { type_, generics: Generics::default(), item_type }, true)
}
};
Item::from_def_id_and_parts(local_did, Some(self.ident.name.clean(cx)), inner, cx)
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
})
}
}
Expand Down Expand Up @@ -1281,7 +1276,7 @@ impl Clean<Item> for ty::AssocItem {
}
};

Item::from_def_id_and_parts(self.def_id, Some(self.ident.name.clean(cx)), kind, cx)
Item::from_def_id_and_parts(self.def_id, Some(self.ident.name), kind, cx)
}
}

Expand Down Expand Up @@ -1771,7 +1766,7 @@ impl Clean<Item> for ty::FieldDef {
fn clean(&self, cx: &DocContext<'_>) -> Item {
let what_rustc_thinks = Item::from_def_id_and_parts(
self.did,
Some(self.ident.name.clean(cx)),
Some(self.ident.name),
StructFieldItem(cx.tcx.type_of(self.did).clean(cx)),
cx,
);
Expand Down Expand Up @@ -1847,7 +1842,7 @@ impl Clean<Item> for ty::VariantDef {
.fields
.iter()
.map(|field| {
let name = Some(field.ident.name.clean(cx));
let name = Some(field.ident.name);
let kind = StructFieldItem(cx.tcx.type_of(field.did).clean(cx));
let what_rustc_thinks =
Item::from_def_id_and_parts(field.did, name, kind, cx);
Expand All @@ -1859,7 +1854,7 @@ impl Clean<Item> for ty::VariantDef {
};
let what_rustc_thinks = Item::from_def_id_and_parts(
self.def_id,
Some(self.ident.name.clean(cx)),
Some(self.ident.name),
VariantItem(Variant { kind }),
cx,
);
Expand Down Expand Up @@ -2033,7 +2028,7 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Symbol>) {
_ => unreachable!("not yet converted"),
};

vec![Item::from_def_id_and_parts(def_id, Some(name.clean(cx)), kind, cx)]
vec![Item::from_def_id_and_parts(def_id, Some(name), kind, cx)]
})
}
}
Expand Down
48 changes: 38 additions & 10 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ crate struct ExternalCrate {
crate src: FileName,
crate attrs: Attributes,
crate primitives: Vec<(DefId, PrimitiveType)>,
crate keywords: Vec<(DefId, String)>,
crate keywords: Vec<(DefId, Symbol)>,
}

/// Anything with a source location and set of attributes and, optionally, a
Expand All @@ -81,7 +81,7 @@ crate struct Item {
/// Stringified span
crate source: Span,
/// Not everything has a name. E.g., impls
crate name: Option<String>,
crate name: Option<Symbol>,
crate attrs: Attributes,
crate visibility: Visibility,
crate kind: ItemKind,
Expand Down Expand Up @@ -123,17 +123,12 @@ impl Item {
kind: ItemKind,
cx: &DocContext<'_>,
) -> Item {
Item::from_def_id_and_parts(
cx.tcx.hir().local_def_id(hir_id).to_def_id(),
name.clean(cx),
kind,
cx,
)
Item::from_def_id_and_parts(cx.tcx.hir().local_def_id(hir_id).to_def_id(), name, kind, cx)
}

pub fn from_def_id_and_parts(
def_id: DefId,
name: Option<String>,
name: Option<Symbol>,
kind: ItemKind,
cx: &DocContext<'_>,
) -> Item {
Expand Down Expand Up @@ -334,7 +329,7 @@ crate enum ItemKind {
AssocTypeItem(Vec<GenericBound>, Option<Type>),
/// An item that has been stripped by a rustdoc pass
StrippedItem(Box<ItemKind>),
KeywordItem(String),
KeywordItem(Symbol),
}

impl ItemKind {
Expand Down Expand Up @@ -1163,6 +1158,8 @@ crate enum Type {
}

#[derive(Clone, PartialEq, Eq, Hash, Copy, Debug)]
/// N.B. this has to be different from `hir::PrimTy` because it also includes types that aren't
/// paths, like `Unit`.
crate enum PrimitiveType {
Isize,
I8,
Expand Down Expand Up @@ -1502,6 +1499,37 @@ impl PrimitiveType {
crate fn to_url_str(&self) -> &'static str {
self.as_str()
}

crate fn as_sym(&self) -> Symbol {
use PrimitiveType::*;
match self {
Isize => sym::isize,
I8 => sym::i8,
I16 => sym::i16,
I32 => sym::i32,
I64 => sym::i64,
I128 => sym::i128,
Usize => sym::usize,
U8 => sym::u8,
U16 => sym::u16,
U32 => sym::u32,
U64 => sym::u64,
U128 => sym::u128,
F32 => sym::f32,
F64 => sym::f64,
Str => sym::str,
Bool => sym::bool,
Char => sym::char,
Array => sym::array,
Slice => sym::slice,
Tuple => sym::tuple,
Unit => sym::unit,
RawPointer => sym::pointer,
Reference => sym::reference,
Fn => kw::Fn,
Never => sym::never,
}
}
}

impl From<ast::IntTy> for PrimitiveType {
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ crate fn krate(mut cx: &mut DocContext<'_>) -> Crate {
m.items.extend(primitives.iter().map(|&(def_id, prim)| {
Item::from_def_id_and_parts(
def_id,
Some(prim.to_url_str().to_owned()),
Some(prim.as_sym()),
ItemKind::PrimitiveItem(prim),
cx,
)
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/formats/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::Arc;
use rustc_data_structures::sync::Lrc;
use rustc_session::Session;
use rustc_span::edition::Edition;
use rustc_span::Symbol;

use crate::clean;
use crate::config::{RenderInfo, RenderOptions};
Expand Down Expand Up @@ -75,7 +76,7 @@ crate fn run_format<T: FormatRenderer>(
None => return Ok(()),
};

item.name = Some(krate.name.clone());
item.name = Some(Symbol::intern(&krate.name));

// Render the crate documentation
let mut work = vec![(format_renderer.clone(), item)];
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ crate fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
if let Some(&(ref fqp, _)) = paths.get(&did) {
search_index.push(IndexItem {
ty: item.type_(),
name: item.name.clone().unwrap(),
name: item.name.unwrap().to_string(),
path: fqp[..fqp.len() - 1].join("::"),
desc: item.doc_value().map_or_else(|| String::new(), short_markdown_summary),
parent: Some(did),
Expand Down
27 changes: 12 additions & 15 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ use rustc_session::Session;
use rustc_span::edition::Edition;
use rustc_span::hygiene::MacroKind;
use rustc_span::source_map::FileName;
use rustc_span::symbol::{sym, Symbol};
use rustc_span::symbol::{kw, sym, Symbol};
use serde::ser::SerializeSeq;
use serde::{Serialize, Serializer};

Expand Down Expand Up @@ -665,7 +665,7 @@ impl FormatRenderer for Context {
if !buf.is_empty() {
let name = item.name.as_ref().unwrap();
let item_type = item.type_();
let file_name = &item_path(item_type, name);
let file_name = &item_path(item_type, &name.as_str());
self.shared.ensure_dir(&self.dst)?;
let joint_dst = self.dst.join(file_name);
self.shared.fs.write(&joint_dst, buf.as_bytes())?;
Expand Down Expand Up @@ -1543,7 +1543,7 @@ impl Context {
if !title.is_empty() {
title.push_str("::");
}
title.push_str(it.name.as_ref().unwrap());
title.push_str(&it.name.unwrap().as_str());
}
title.push_str(" - Rust");
let tyname = it.type_();
Expand Down Expand Up @@ -1815,7 +1815,7 @@ fn item_path(ty: ItemType, name: &str) -> String {
fn full_path(cx: &Context, item: &clean::Item) -> String {
let mut s = cx.current.join("::");
s.push_str("::");
s.push_str(item.name.as_ref().unwrap());
s.push_str(&item.name.unwrap().as_str());
s
}

Expand Down Expand Up @@ -2065,9 +2065,9 @@ fn item_module(w: &mut Buffer, cx: &Context, item: &clean::Item, items: &[clean:
(true, false) => return Ordering::Greater,
}
}
let lhs = i1.name.as_ref().map_or("", |s| &**s);
let rhs = i2.name.as_ref().map_or("", |s| &**s);
compare_names(lhs, rhs)
let lhs = i1.name.unwrap_or(kw::Invalid).as_str();
let rhs = i2.name.unwrap_or(kw::Invalid).as_str();
compare_names(&lhs, &rhs)
}

if cx.shared.sort_modules_alphabetically {
Expand Down Expand Up @@ -2191,7 +2191,7 @@ fn item_module(w: &mut Buffer, cx: &Context, item: &clean::Item, items: &[clean:
add = add,
stab = stab.unwrap_or_else(String::new),
unsafety_flag = unsafety_flag,
href = item_path(myitem.type_(), myitem.name.as_ref().unwrap()),
href = item_path(myitem.type_(), &myitem.name.unwrap().as_str()),
title = [full_path(cx, myitem), myitem.type_().to_string()]
.iter()
.filter_map(|s| if !s.is_empty() { Some(s.as_str()) } else { None })
Expand Down Expand Up @@ -2623,7 +2623,7 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait,

fn trait_item(w: &mut Buffer, cx: &Context, m: &clean::Item, t: &clean::Item, cache: &Cache) {
let name = m.name.as_ref().unwrap();
info!("Documenting {} on {}", name, t.name.as_deref().unwrap_or_default());
info!("Documenting {} on {:?}", name, t.name);
let item_type = m.type_();
let id = cx.derive_id(format!("{}.{}", item_type, name));
write!(w, "<h3 id=\"{id}\" class=\"method\"><code>", id = id,);
Expand Down Expand Up @@ -2951,7 +2951,7 @@ fn render_assoc_item(
AssocItemLink::GotoSource(did, provided_methods) => {
// We're creating a link from an impl-item to the corresponding
// trait-item and need to map the anchored type accordingly.
let ty = if provided_methods.contains(name) {
let ty = if provided_methods.contains(&*name.as_str()) {
ItemType::Method
} else {
ItemType::TyMethod
Expand Down Expand Up @@ -3434,10 +3434,7 @@ fn render_assoc_items(
what: AssocItemRender<'_>,
cache: &Cache,
) {
info!(
"Documenting associated items of {}",
containing_item.name.as_deref().unwrap_or_default()
);
info!("Documenting associated items of {:?}", containing_item.name);
let v = match cache.impls.get(&it) {
Some(v) => v,
None => return,
Expand Down Expand Up @@ -4139,7 +4136,7 @@ fn print_sidebar(cx: &Context, it: &clean::Item, buffer: &mut Buffer, cache: &Ca
ty: \"{ty}\", \
relpath: \"{path}\"\
}};</script>",
name = it.name.as_ref().map(|x| &x[..]).unwrap_or(""),
name = it.name.unwrap_or(kw::Invalid),
ty = it.type_(),
path = relpath
);
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl JsonRenderer {
_ => Some(Item {
id: def_id.into(),
crate_id: def_id.krate.as_u32(),
name,
name: name.map(|sym| sym.to_string()),
source: self.convert_span(source),
visibility: visibility.into(),
docs: attrs.collapsed_doc_value().unwrap_or_default(),
Expand Down
11 changes: 9 additions & 2 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ fn resolve_associated_trait_item(
// Give precedence to methods that were overridden
if !impl_.provided_trait_methods.contains(&*item_name.as_str()) {
let mut items = impl_.items.into_iter().filter_map(|assoc| {
if assoc.name.as_deref() != Some(&*item_name.as_str()) {
if assoc.name != Some(item_name) {
return None;
}
let kind = assoc
Expand Down Expand Up @@ -1942,7 +1942,14 @@ fn privacy_error(
dox: &str,
link_range: Option<Range<usize>>,
) {
let item_name = item.name.as_deref().unwrap_or("<unknown>");
let sym;
let item_name = match item.name {
Some(name) => {
sym = name.as_str();
&*sym
}
None => "<unknown>",
};
let msg =
format!("public documentation for `{}` links to private item `{}`", item_name, path_str);

Expand Down