Rollup merge of #102321 - aDotInTheVoid:rdj-prim-impls, r=GuillaumeGomez

Rustdoc-Json: List impls for primitives

 Closes #101695

 Partially addresses #100961

r? ``@GuillaumeGomez``
This commit is contained in:
Michael Howell 2022-09-26 15:40:54 -07:00 committed by GitHub
commit 0415560382
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 60 additions and 13 deletions

View File

@ -272,7 +272,12 @@ 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)) }

View File

@ -219,12 +219,15 @@ 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::AssocType { .. } => true,
types::ItemEnum::ExternCrate { .. }
| types::ItemEnum::Import(_)
| types::ItemEnum::StructField(_)

View File

@ -9,7 +9,7 @@ use std::path::PathBuf;
use serde::{Deserialize, Serialize};
/// rustdoc format-version.
pub const FORMAT_VERSION: u32 = 21;
pub const FORMAT_VERSION: u32 = 22;
/// A `Crate` is the root of the emitted JSON blob. It contains all type/documentation information
/// about the language items in the local crate, as well as info about external items to allow
@ -254,7 +254,7 @@ pub enum ItemEnum {
Macro(String),
ProcMacro(ProcMacro),
PrimitiveType(String),
Primitive(Primitive),
AssocConst {
#[serde(rename = "type")]
@ -709,5 +709,11 @@ pub struct Static {
pub expr: String,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Primitive {
pub name: String,
pub impls: Vec<Id>,
}
#[cfg(test)]
mod tests;

View File

@ -0,0 +1,34 @@
#![feature(no_core)]
#![feature(rustc_attrs)]
#![feature(rustdoc_internals)]
#![no_core]
#![rustc_coherence_is_core]
// @set impl_i32 = "$.index[*][?(@.docs=='Only core can do this')].id"
/// Only core can do this
impl i32 {
// @set identity = "$.index[*][?(@.docs=='Do Nothing')].id"
/// Do Nothing
pub fn identity(self) -> Self {
self
}
// @is "$.index[*][?(@.docs=='Only core can do this')].inner.items[*]" $identity
}
// @set Trait = "$.index[*][?(@.name=='Trait')].id"
pub trait Trait {}
// @set impl_trait_for_i32 = "$.index[*][?(@.docs=='impl Trait for i32')].id"
/// impl Trait for i32
impl Trait for i32 {}
/// i32
#[doc(primitive = "i32")]
mod prim_i32 {}
// @set i32 = "$.index[*][?(@.docs=='i32')].id"
// @is "$.index[*][?(@.docs=='i32')].name" '"i32"'
// @is "$.index[*][?(@.docs=='i32')].inner.name" '"i32"'
// @ismany "$.index[*][?(@.docs=='i32')].inner.impls[*]" $impl_i32 $impl_trait_for_i32

View File

@ -5,7 +5,7 @@
#[doc(primitive = "usize")]
mod usize {}
// @set local_crate_id = "$.index[*][?(@.name=='primitive')].crate_id"
// @set local_crate_id = "$.index[*][?(@.name=='use_primitive')].crate_id"
// @has "$.index[*][?(@.name=='ilog10')]"
// @!is "$.index[*][?(@.name=='ilog10')].crate_id" $local_crate_id

View File

@ -142,8 +142,7 @@ impl Kind {
ItemEnum::Static(_) => Static,
ItemEnum::Macro(_) => Macro,
ItemEnum::ProcMacro(_) => ProcMacro,
// https://github.com/rust-lang/rust/issues/100961
ItemEnum::PrimitiveType(_) => Primitive,
ItemEnum::Primitive(_) => Primitive,
ItemEnum::ForeignType => ForeignType,
ItemEnum::ExternCrate { .. } => ExternCrate,
ItemEnum::AssocConst { .. } => AssocConst,

View File

@ -4,8 +4,8 @@ use std::hash::Hash;
use rustdoc_json_types::{
Constant, Crate, DynTrait, Enum, FnDecl, Function, FunctionPointer, GenericArg, GenericArgs,
GenericBound, GenericParamDef, Generics, Id, Impl, Import, ItemEnum, Method, Module, OpaqueTy,
Path, ProcMacro, Static, Struct, StructKind, Term, Trait, TraitAlias, Type, TypeBinding,
TypeBindingKind, Typedef, Union, Variant, WherePredicate,
Path, Primitive, ProcMacro, Static, Struct, StructKind, Term, Trait, TraitAlias, Type,
TypeBinding, TypeBindingKind, Typedef, Union, Variant, WherePredicate,
};
use crate::{item_kind::Kind, Error, ErrorKind};
@ -76,7 +76,7 @@ impl<'a> Validator<'a> {
ItemEnum::ForeignType => {} // nop
ItemEnum::Macro(x) => self.check_macro(x),
ItemEnum::ProcMacro(x) => self.check_proc_macro(x),
ItemEnum::PrimitiveType(x) => self.check_primitive_type(x),
ItemEnum::Primitive(x) => self.check_primitive_type(x),
ItemEnum::Module(x) => self.check_module(x),
// FIXME: Why don't these have their own structs?
ItemEnum::ExternCrate { .. } => {}
@ -219,8 +219,8 @@ impl<'a> Validator<'a> {
// nop
}
fn check_primitive_type(&mut self, _: &'a str) {
// nop
fn check_primitive_type(&mut self, x: &'a Primitive) {
x.impls.iter().for_each(|i| self.add_impl_id(i));
}
fn check_generics(&mut self, x: &'a Generics) {