Skip to content

Rustdoc-Json: List impls for primitives #102321

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

Merged
merged 1 commit into from
Sep 27, 2022
Merged
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
7 changes: 6 additions & 1 deletion src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
@@ -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)) }
7 changes: 5 additions & 2 deletions src/librustdoc/json/mod.rs
Original file line number Diff line number Diff line change
@@ -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(_)
10 changes: 8 additions & 2 deletions src/rustdoc-json-types/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
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
@@ -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
3 changes: 1 addition & 2 deletions src/tools/jsondoclint/src/item_kind.rs
Original file line number Diff line number Diff line change
@@ -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,
10 changes: 5 additions & 5 deletions src/tools/jsondoclint/src/validator.rs
Original file line number Diff line number Diff line change
@@ -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) {