Skip to content

Commit

Permalink
Rollup merge of rust-lang#102321 - aDotInTheVoid:rdj-prim-impls, r=Gu…
Browse files Browse the repository at this point in the history
…illaumeGomez

 Rustdoc-Json: List impls for primitives

 Closes rust-lang#101695

 Partially addresses rust-lang#100961

r? `@GuillaumeGomez`
  • Loading branch information
notriddle authored Sep 26, 2022
2 parents 76e894a + aac7429 commit a97f73c
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 13 deletions.
7 changes: 6 additions & 1 deletion src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) }
Expand Down
7 changes: 5 additions & 2 deletions src/librustdoc/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_)
Expand Down
10 changes: 8 additions & 2 deletions src/rustdoc-json-types/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -254,7 +254,7 @@ pub enum ItemEnum {
Macro(String),
ProcMacro(ProcMacro),

PrimitiveType(String),
Primitive(Primitive),

AssocConst {
#[serde(rename = "type")]
Expand Down Expand Up @@ -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;
34 changes: 34 additions & 0 deletions src/test/rustdoc-json/primitives/primitive_impls.rs
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/tools/jsondoclint/src/item_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions src/tools/jsondoclint/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 { .. } => {}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a97f73c

Please sign in to comment.