Skip to content

Commit 0415560

Browse files
authored
Rollup merge of #102321 - aDotInTheVoid:rdj-prim-impls, r=GuillaumeGomez
Rustdoc-Json: List impls for primitives Closes #101695 Partially addresses #100961 r? ``@GuillaumeGomez``
2 parents dc4fb6b + aac7429 commit 0415560

File tree

9 files changed

+60
-13
lines changed

9 files changed

+60
-13
lines changed

src/librustdoc/json/conversions.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,12 @@ fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
272272
ConstantItem(c) => ItemEnum::Constant(c.into_tcx(tcx)),
273273
MacroItem(m) => ItemEnum::Macro(m.source),
274274
ProcMacroItem(m) => ItemEnum::ProcMacro(m.into_tcx(tcx)),
275-
PrimitiveItem(p) => ItemEnum::PrimitiveType(p.as_sym().to_string()),
275+
PrimitiveItem(p) => {
276+
ItemEnum::Primitive(Primitive {
277+
name: p.as_sym().to_string(),
278+
impls: Vec::new(), // Added in JsonRenderer::item
279+
})
280+
}
276281
TyAssocConstItem(ty) => ItemEnum::AssocConst { type_: ty.into_tcx(tcx), default: None },
277282
AssocConstItem(ty, default) => {
278283
ItemEnum::AssocConst { type_: ty.into_tcx(tcx), default: Some(default.expr(tcx)) }

src/librustdoc/json/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,15 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
219219
u.impls = self.get_impls(item_id.expect_def_id());
220220
false
221221
}
222+
types::ItemEnum::Primitive(ref mut p) => {
223+
p.impls = self.get_impls(item_id.expect_def_id());
224+
false
225+
}
222226

223227
types::ItemEnum::Method(_)
224228
| types::ItemEnum::Module(_)
225229
| types::ItemEnum::AssocConst { .. }
226-
| types::ItemEnum::AssocType { .. }
227-
| types::ItemEnum::PrimitiveType(_) => true,
230+
| types::ItemEnum::AssocType { .. } => true,
228231
types::ItemEnum::ExternCrate { .. }
229232
| types::ItemEnum::Import(_)
230233
| types::ItemEnum::StructField(_)

src/rustdoc-json-types/lib.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::path::PathBuf;
99
use serde::{Deserialize, Serialize};
1010

1111
/// rustdoc format-version.
12-
pub const FORMAT_VERSION: u32 = 21;
12+
pub const FORMAT_VERSION: u32 = 22;
1313

1414
/// A `Crate` is the root of the emitted JSON blob. It contains all type/documentation information
1515
/// about the language items in the local crate, as well as info about external items to allow
@@ -254,7 +254,7 @@ pub enum ItemEnum {
254254
Macro(String),
255255
ProcMacro(ProcMacro),
256256

257-
PrimitiveType(String),
257+
Primitive(Primitive),
258258

259259
AssocConst {
260260
#[serde(rename = "type")]
@@ -709,5 +709,11 @@ pub struct Static {
709709
pub expr: String,
710710
}
711711

712+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
713+
pub struct Primitive {
714+
pub name: String,
715+
pub impls: Vec<Id>,
716+
}
717+
712718
#[cfg(test)]
713719
mod tests;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#![feature(no_core)]
2+
#![feature(rustc_attrs)]
3+
#![feature(rustdoc_internals)]
4+
#![no_core]
5+
#![rustc_coherence_is_core]
6+
7+
// @set impl_i32 = "$.index[*][?(@.docs=='Only core can do this')].id"
8+
9+
/// Only core can do this
10+
impl i32 {
11+
// @set identity = "$.index[*][?(@.docs=='Do Nothing')].id"
12+
13+
/// Do Nothing
14+
pub fn identity(self) -> Self {
15+
self
16+
}
17+
18+
// @is "$.index[*][?(@.docs=='Only core can do this')].inner.items[*]" $identity
19+
}
20+
21+
// @set Trait = "$.index[*][?(@.name=='Trait')].id"
22+
pub trait Trait {}
23+
// @set impl_trait_for_i32 = "$.index[*][?(@.docs=='impl Trait for i32')].id"
24+
/// impl Trait for i32
25+
impl Trait for i32 {}
26+
27+
/// i32
28+
#[doc(primitive = "i32")]
29+
mod prim_i32 {}
30+
31+
// @set i32 = "$.index[*][?(@.docs=='i32')].id"
32+
// @is "$.index[*][?(@.docs=='i32')].name" '"i32"'
33+
// @is "$.index[*][?(@.docs=='i32')].inner.name" '"i32"'
34+
// @ismany "$.index[*][?(@.docs=='i32')].inner.impls[*]" $impl_i32 $impl_trait_for_i32

src/test/rustdoc-json/primitive.rs renamed to src/test/rustdoc-json/primitives/use_primitive.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#[doc(primitive = "usize")]
66
mod usize {}
77

8-
// @set local_crate_id = "$.index[*][?(@.name=='primitive')].crate_id"
8+
// @set local_crate_id = "$.index[*][?(@.name=='use_primitive')].crate_id"
99

1010
// @has "$.index[*][?(@.name=='ilog10')]"
1111
// @!is "$.index[*][?(@.name=='ilog10')].crate_id" $local_crate_id

src/tools/jsondoclint/src/item_kind.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ impl Kind {
142142
ItemEnum::Static(_) => Static,
143143
ItemEnum::Macro(_) => Macro,
144144
ItemEnum::ProcMacro(_) => ProcMacro,
145-
// https://github.com/rust-lang/rust/issues/100961
146-
ItemEnum::PrimitiveType(_) => Primitive,
145+
ItemEnum::Primitive(_) => Primitive,
147146
ItemEnum::ForeignType => ForeignType,
148147
ItemEnum::ExternCrate { .. } => ExternCrate,
149148
ItemEnum::AssocConst { .. } => AssocConst,

src/tools/jsondoclint/src/validator.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::hash::Hash;
44
use rustdoc_json_types::{
55
Constant, Crate, DynTrait, Enum, FnDecl, Function, FunctionPointer, GenericArg, GenericArgs,
66
GenericBound, GenericParamDef, Generics, Id, Impl, Import, ItemEnum, Method, Module, OpaqueTy,
7-
Path, ProcMacro, Static, Struct, StructKind, Term, Trait, TraitAlias, Type, TypeBinding,
8-
TypeBindingKind, Typedef, Union, Variant, WherePredicate,
7+
Path, Primitive, ProcMacro, Static, Struct, StructKind, Term, Trait, TraitAlias, Type,
8+
TypeBinding, TypeBindingKind, Typedef, Union, Variant, WherePredicate,
99
};
1010

1111
use crate::{item_kind::Kind, Error, ErrorKind};
@@ -76,7 +76,7 @@ impl<'a> Validator<'a> {
7676
ItemEnum::ForeignType => {} // nop
7777
ItemEnum::Macro(x) => self.check_macro(x),
7878
ItemEnum::ProcMacro(x) => self.check_proc_macro(x),
79-
ItemEnum::PrimitiveType(x) => self.check_primitive_type(x),
79+
ItemEnum::Primitive(x) => self.check_primitive_type(x),
8080
ItemEnum::Module(x) => self.check_module(x),
8181
// FIXME: Why don't these have their own structs?
8282
ItemEnum::ExternCrate { .. } => {}
@@ -219,8 +219,8 @@ impl<'a> Validator<'a> {
219219
// nop
220220
}
221221

222-
fn check_primitive_type(&mut self, _: &'a str) {
223-
// nop
222+
fn check_primitive_type(&mut self, x: &'a Primitive) {
223+
x.impls.iter().for_each(|i| self.add_impl_id(i));
224224
}
225225

226226
fn check_generics(&mut self, x: &'a Generics) {

0 commit comments

Comments
 (0)