Skip to content

Commit 4989f6a

Browse files
Rollup merge of rust-lang#100335 - aDotInTheVoid:rdj-resolved-path, r=GuillaumeGomez
Rustdoc-Json: Add `Path` type for traits. Avoids using `Type` for trait fields, as a trait must always be a path, and not any other kind of type. ``@rustbot`` modify labels: +A-rustdoc-json +T-rustdoc Closes rust-lang#100106
2 parents d496c4e + 86bdb3e commit 4989f6a

File tree

9 files changed

+96
-94
lines changed

9 files changed

+96
-94
lines changed

src/etc/check_missing_items.py

+29-29
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def check_generic_bound(bound):
5757
if "trait_bound" in bound:
5858
for param in bound["trait_bound"]["generic_params"]:
5959
check_generic_param(param)
60-
check_type(bound["trait_bound"]["trait"])
60+
check_path(bound["trait_bound"]["trait"])
6161

6262

6363
def check_decl(decl):
@@ -66,35 +66,35 @@ def check_decl(decl):
6666
if decl["output"]:
6767
check_type(decl["output"])
6868

69+
def check_path(path):
70+
args = path["args"]
71+
if args:
72+
if "angle_bracketed" in args:
73+
for arg in args["angle_bracketed"]["args"]:
74+
if "type" in arg:
75+
check_type(arg["type"])
76+
elif "const" in arg:
77+
check_type(arg["const"]["type"])
78+
for binding in args["angle_bracketed"]["bindings"]:
79+
if "equality" in binding["binding"]:
80+
term = binding["binding"]["equality"]
81+
if "type" in term: check_type(term["type"])
82+
elif "const" in term: check_type(term["const"])
83+
elif "constraint" in binding["binding"]:
84+
for bound in binding["binding"]["constraint"]:
85+
check_generic_bound(bound)
86+
elif "parenthesized" in args:
87+
for input_ty in args["parenthesized"]["inputs"]:
88+
check_type(input_ty)
89+
if args["parenthesized"]["output"]:
90+
check_type(args["parenthesized"]["output"])
91+
if not valid_id(path["id"]):
92+
print("Type contained an invalid ID:", path["id"])
93+
sys.exit(1)
6994

7095
def check_type(ty):
7196
if ty["kind"] == "resolved_path":
72-
for bound in ty["inner"]["param_names"]:
73-
check_generic_bound(bound)
74-
args = ty["inner"]["args"]
75-
if args:
76-
if "angle_bracketed" in args:
77-
for arg in args["angle_bracketed"]["args"]:
78-
if "type" in arg:
79-
check_type(arg["type"])
80-
elif "const" in arg:
81-
check_type(arg["const"]["type"])
82-
for binding in args["angle_bracketed"]["bindings"]:
83-
if "equality" in binding["binding"]:
84-
term = binding["binding"]["equality"]
85-
if "type" in term: check_type(term["type"])
86-
elif "const" in term: check_type(term["const"])
87-
elif "constraint" in binding["binding"]:
88-
for bound in binding["binding"]["constraint"]:
89-
check_generic_bound(bound)
90-
elif "parenthesized" in args:
91-
for input_ty in args["parenthesized"]["inputs"]:
92-
check_type(input_ty)
93-
if args["parenthesized"]["output"]:
94-
check_type(args["parenthesized"]["output"])
95-
if not valid_id(ty["inner"]["id"]):
96-
print("Type contained an invalid ID:", ty["inner"]["id"])
97-
sys.exit(1)
97+
check_path(ty["inner"])
9898
elif ty["kind"] == "tuple":
9999
for ty in ty["inner"]:
100100
check_type(ty)
@@ -111,7 +111,7 @@ def check_type(ty):
111111
check_decl(ty["inner"]["decl"])
112112
elif ty["kind"] == "qualified_path":
113113
check_type(ty["inner"]["self_type"])
114-
check_type(ty["inner"]["trait"])
114+
check_path(ty["inner"]["trait"])
115115

116116

117117
work_list = set([crate["root"]])
@@ -174,7 +174,7 @@ def check_type(ty):
174174
elif item["kind"] == "impl":
175175
check_generics(item["inner"]["generics"])
176176
if item["inner"]["trait"]:
177-
check_type(item["inner"]["trait"])
177+
check_path(item["inner"]["trait"])
178178
if item["inner"]["blanket_impl"]:
179179
check_type(item["inner"]["blanket_impl"])
180180
check_type(item["inner"]["for"])

src/librustdoc/json/conversions.rs

+20-26
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,8 @@ impl FromWithTcx<clean::GenericBound> for GenericBound {
428428
use clean::GenericBound::*;
429429
match bound {
430430
TraitBound(clean::PolyTrait { trait_, generic_params }, modifier) => {
431-
// FIXME: should `trait_` be a clean::Path equivalent in JSON?
432-
let trait_ = clean::Type::Path { path: trait_ }.into_tcx(tcx);
433431
GenericBound::TraitBound {
434-
trait_,
432+
trait_: trait_.into_tcx(tcx),
435433
generic_params: generic_params.into_tcx(tcx),
436434
modifier: from_trait_bound_modifier(modifier),
437435
}
@@ -460,12 +458,7 @@ impl FromWithTcx<clean::Type> for Type {
460458
};
461459

462460
match ty {
463-
clean::Type::Path { path } => Type::ResolvedPath {
464-
name: path.whole_name(),
465-
id: from_item_id(path.def_id().into(), tcx),
466-
args: path.segments.last().map(|args| Box::new(args.clone().args.into_tcx(tcx))),
467-
param_names: Vec::new(),
468-
},
461+
clean::Type::Path { path } => Type::ResolvedPath(path.into_tcx(tcx)),
469462
clean::Type::DynTrait(bounds, lt) => Type::DynTrait(DynTrait {
470463
lifetime: lt.map(convert_lifetime),
471464
traits: bounds.into_tcx(tcx),
@@ -487,16 +480,22 @@ impl FromWithTcx<clean::Type> for Type {
487480
mutable: mutability == ast::Mutability::Mut,
488481
type_: Box::new((*type_).into_tcx(tcx)),
489482
},
490-
QPath { assoc, self_type, trait_, .. } => {
491-
// FIXME: should `trait_` be a clean::Path equivalent in JSON?
492-
let trait_ = clean::Type::Path { path: trait_ }.into_tcx(tcx);
493-
Type::QualifiedPath {
494-
name: assoc.name.to_string(),
495-
args: Box::new(assoc.args.clone().into_tcx(tcx)),
496-
self_type: Box::new((*self_type).into_tcx(tcx)),
497-
trait_: Box::new(trait_),
498-
}
499-
}
483+
QPath { assoc, self_type, trait_, .. } => Type::QualifiedPath {
484+
name: assoc.name.to_string(),
485+
args: Box::new(assoc.args.clone().into_tcx(tcx)),
486+
self_type: Box::new((*self_type).into_tcx(tcx)),
487+
trait_: trait_.into_tcx(tcx),
488+
},
489+
}
490+
}
491+
}
492+
493+
impl FromWithTcx<clean::Path> for Path {
494+
fn from_tcx(path: clean::Path, tcx: TyCtxt<'_>) -> Path {
495+
Path {
496+
name: path.whole_name(),
497+
id: from_item_id(path.def_id().into(), tcx),
498+
args: path.segments.last().map(|args| Box::new(args.clone().args.into_tcx(tcx))),
500499
}
501500
}
502501
}
@@ -565,19 +564,14 @@ impl FromWithTcx<clean::PolyTrait> for PolyTrait {
565564
clean::PolyTrait { trait_, generic_params }: clean::PolyTrait,
566565
tcx: TyCtxt<'_>,
567566
) -> Self {
568-
PolyTrait {
569-
trait_: clean::Type::Path { path: trait_ }.into_tcx(tcx),
570-
generic_params: generic_params.into_tcx(tcx),
571-
}
567+
PolyTrait { trait_: trait_.into_tcx(tcx), generic_params: generic_params.into_tcx(tcx) }
572568
}
573569
}
574570

575571
impl FromWithTcx<clean::Impl> for Impl {
576572
fn from_tcx(impl_: clean::Impl, tcx: TyCtxt<'_>) -> Self {
577573
let provided_trait_methods = impl_.provided_trait_methods(tcx);
578574
let clean::Impl { unsafety, generics, trait_, for_, items, polarity, kind } = impl_;
579-
// FIXME: should `trait_` be a clean::Path equivalent in JSON?
580-
let trait_ = trait_.map(|path| clean::Type::Path { path }.into_tcx(tcx));
581575
// FIXME: use something like ImplKind in JSON?
582576
let (synthetic, blanket_impl) = match kind {
583577
clean::ImplKind::Normal | clean::ImplKind::FakeVaradic => (false, None),
@@ -595,7 +589,7 @@ impl FromWithTcx<clean::Impl> for Impl {
595589
.into_iter()
596590
.map(|x| x.to_string())
597591
.collect(),
598-
trait_,
592+
trait_: trait_.map(|path| path.into_tcx(tcx)),
599593
for_: for_.into_tcx(tcx),
600594
items: ids(items, tcx),
601595
negative: negative_polarity,

src/rustdoc-json-types/lib.rs

+20-11
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 = 17;
12+
pub const FORMAT_VERSION: u32 = 18;
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
@@ -133,7 +133,7 @@ pub struct DynTrait {
133133
/// A trait and potential HRTBs
134134
pub struct PolyTrait {
135135
#[serde(rename = "trait")]
136-
pub trait_: Type,
136+
pub trait_: Path,
137137
/// Used for Higher-Rank Trait Bounds (HRTBs)
138138
/// ```text
139139
/// dyn for<'a> Fn() -> &'a i32"
@@ -447,7 +447,7 @@ pub enum WherePredicate {
447447
pub enum GenericBound {
448448
TraitBound {
449449
#[serde(rename = "trait")]
450-
trait_: Type,
450+
trait_: Path,
451451
/// Used for Higher-Rank Trait Bounds (HRTBs)
452452
/// ```text
453453
/// where F: for<'a, 'b> Fn(&'a u8, &'b u8)
@@ -481,12 +481,7 @@ pub enum Term {
481481
#[serde(tag = "kind", content = "inner")]
482482
pub enum Type {
483483
/// Structs, enums, and traits
484-
ResolvedPath {
485-
name: String,
486-
id: Id,
487-
args: Option<Box<GenericArgs>>,
488-
param_names: Vec<GenericBound>,
489-
},
484+
ResolvedPath(Path),
490485
DynTrait(DynTrait),
491486
/// Parameterized types
492487
Generic(String),
@@ -527,10 +522,24 @@ pub enum Type {
527522
args: Box<GenericArgs>,
528523
self_type: Box<Type>,
529524
#[serde(rename = "trait")]
530-
trait_: Box<Type>,
525+
trait_: Path,
531526
},
532527
}
533528

529+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
530+
pub struct Path {
531+
pub name: String,
532+
pub id: Id,
533+
/// Generic arguments to the type
534+
/// ```test
535+
/// std::borrow::Cow<'static, str>
536+
/// ^^^^^^^^^^^^^^
537+
/// |
538+
/// this part
539+
/// ```
540+
pub args: Option<Box<GenericArgs>>,
541+
}
542+
534543
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
535544
pub struct FunctionPointer {
536545
pub decl: FnDecl,
@@ -574,7 +583,7 @@ pub struct Impl {
574583
pub generics: Generics,
575584
pub provided_trait_methods: Vec<String>,
576585
#[serde(rename = "trait")]
577-
pub trait_: Option<Type>,
586+
pub trait_: Option<Path>,
578587
#[serde(rename = "for")]
579588
pub for_: Type,
580589
pub items: Vec<Id>,

src/test/rustdoc-json/fns/generic_args.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub trait GenericFoo<'a> {}
1414
// @is - "$.index[*][?(@.name=='generics')].inner.generics.params[0].name" '"F"'
1515
// @is - "$.index[*][?(@.name=='generics')].inner.generics.params[0].kind.type.default" 'null'
1616
// @count - "$.index[*][?(@.name=='generics')].inner.generics.params[0].kind.type.bounds[*]" 1
17-
// @is - "$.index[*][?(@.name=='generics')].inner.generics.params[0].kind.type.bounds[0].trait_bound.trait.inner.id" '$foo'
17+
// @is - "$.index[*][?(@.name=='generics')].inner.generics.params[0].kind.type.bounds[0].trait_bound.trait.id" '$foo'
1818
// @count - "$.index[*][?(@.name=='generics')].inner.decl.inputs[*]" 1
1919
// @is - "$.index[*][?(@.name=='generics')].inner.decl.inputs[0][0]" '"f"'
2020
// @is - "$.index[*][?(@.name=='generics')].inner.decl.inputs[0][1].kind" '"generic"'
@@ -24,12 +24,12 @@ pub fn generics<F: Foo>(f: F) {}
2424
// @is - "$.index[*][?(@.name=='impl_trait')].inner.generics.where_predicates" "[]"
2525
// @count - "$.index[*][?(@.name=='impl_trait')].inner.generics.params[*]" 1
2626
// @is - "$.index[*][?(@.name=='impl_trait')].inner.generics.params[0].name" '"impl Foo"'
27-
// @is - "$.index[*][?(@.name=='impl_trait')].inner.generics.params[0].kind.type.bounds[0].trait_bound.trait.inner.id" $foo
27+
// @is - "$.index[*][?(@.name=='impl_trait')].inner.generics.params[0].kind.type.bounds[0].trait_bound.trait.id" $foo
2828
// @count - "$.index[*][?(@.name=='impl_trait')].inner.decl.inputs[*]" 1
2929
// @is - "$.index[*][?(@.name=='impl_trait')].inner.decl.inputs[0][0]" '"f"'
3030
// @is - "$.index[*][?(@.name=='impl_trait')].inner.decl.inputs[0][1].kind" '"impl_trait"'
3131
// @count - "$.index[*][?(@.name=='impl_trait')].inner.decl.inputs[0][1].inner[*]" 1
32-
// @is - "$.index[*][?(@.name=='impl_trait')].inner.decl.inputs[0][1].inner[0].trait_bound.trait.inner.id" $foo
32+
// @is - "$.index[*][?(@.name=='impl_trait')].inner.decl.inputs[0][1].inner[0].trait_bound.trait.id" $foo
3333
pub fn impl_trait(f: impl Foo) {}
3434

3535
// @count - "$.index[*][?(@.name=='where_clase')].inner.generics.params[*]" 3
@@ -43,11 +43,11 @@ pub fn impl_trait(f: impl Foo) {}
4343

4444
// @is - "$.index[*][?(@.name=='where_clase')].inner.generics.where_predicates[0].bound_predicate.type" '{"inner": "F", "kind": "generic"}'
4545
// @count - "$.index[*][?(@.name=='where_clase')].inner.generics.where_predicates[0].bound_predicate.bounds[*]" 1
46-
// @is - "$.index[*][?(@.name=='where_clase')].inner.generics.where_predicates[0].bound_predicate.bounds[0].trait_bound.trait.inner.id" $foo
46+
// @is - "$.index[*][?(@.name=='where_clase')].inner.generics.where_predicates[0].bound_predicate.bounds[0].trait_bound.trait.id" $foo
4747

4848
// @is - "$.index[*][?(@.name=='where_clase')].inner.generics.where_predicates[1].bound_predicate.type" '{"inner": "G", "kind": "generic"}'
4949
// @count - "$.index[*][?(@.name=='where_clase')].inner.generics.where_predicates[1].bound_predicate.bounds[*]" 1
50-
// @is - "$.index[*][?(@.name=='where_clase')].inner.generics.where_predicates[1].bound_predicate.bounds[0].trait_bound.trait.inner.id" $generic_foo
50+
// @is - "$.index[*][?(@.name=='where_clase')].inner.generics.where_predicates[1].bound_predicate.bounds[0].trait_bound.trait.id" $generic_foo
5151
// @count - "$.index[*][?(@.name=='where_clase')].inner.generics.where_predicates[1].bound_predicate.bounds[0].trait_bound.generic_params[*]" 1
5252
// @is - "$.index[*][?(@.name=='where_clase')].inner.generics.where_predicates[1].bound_predicate.bounds[0].trait_bound.generic_params[0].name" \"\'a\"
5353
// @is - "$.index[*][?(@.name=='where_clase')].inner.generics.where_predicates[1].bound_predicate.bounds[0].trait_bound.generic_params[0].kind" '{ "lifetime": { "outlives": [] } }'
@@ -57,7 +57,7 @@ pub fn impl_trait(f: impl Foo) {}
5757
// @is - "$.index[*][?(@.name=='where_clase')].inner.generics.where_predicates[2].bound_predicate.type.inner.lifetime" \"\'b\"
5858
// @is - "$.index[*][?(@.name=='where_clase')].inner.generics.where_predicates[2].bound_predicate.type.inner.type" '{"inner": "H", "kind": "generic"}'
5959
// @count - "$.index[*][?(@.name=='where_clase')].inner.generics.where_predicates[2].bound_predicate.bounds[*]" 1
60-
// @is - "$.index[*][?(@.name=='where_clase')].inner.generics.where_predicates[2].bound_predicate.bounds[0].trait_bound.trait.inner.id" $foo
60+
// @is - "$.index[*][?(@.name=='where_clase')].inner.generics.where_predicates[2].bound_predicate.bounds[0].trait_bound.trait.id" $foo
6161
// @is - "$.index[*][?(@.name=='where_clase')].inner.generics.where_predicates[2].bound_predicate.bounds[0].trait_bound.generic_params" "[]"
6262
// @count - "$.index[*][?(@.name=='where_clase')].inner.generics.where_predicates[2].bound_predicate.generic_params[*]" 1
6363
// @is - "$.index[*][?(@.name=='where_clase')].inner.generics.where_predicates[2].bound_predicate.generic_params[0].name" \"\'b\"

src/test/rustdoc-json/fns/generic_returns.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub trait Foo {}
1111
// @is - "$.index[*][?(@.name=='get_foo')].inner.decl.inputs" []
1212
// @is - "$.index[*][?(@.name=='get_foo')].inner.decl.output.kind" '"impl_trait"'
1313
// @count - "$.index[*][?(@.name=='get_foo')].inner.decl.output.inner[*]" 1
14-
// @is - "$.index[*][?(@.name=='get_foo')].inner.decl.output.inner[0].trait_bound.trait.inner.id" $foo
14+
// @is - "$.index[*][?(@.name=='get_foo')].inner.decl.output.inner[0].trait_bound.trait.id" $foo
1515
pub fn get_foo() -> impl Foo {
1616
Fooer {}
1717
}

src/test/rustdoc-json/fns/generics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ pub trait Wham {}
1010
// @count - "$.index[*][?(@.name=='one_generic_param_fn')].inner.generics.params[*]" 1
1111
// @is - "$.index[*][?(@.name=='one_generic_param_fn')].inner.generics.params[0].name" '"T"'
1212
// @has - "$.index[*][?(@.name=='one_generic_param_fn')].inner.generics.params[0].kind.type.synthetic" false
13-
// @has - "$.index[*][?(@.name=='one_generic_param_fn')].inner.generics.params[0].kind.type.bounds[0].trait_bound.trait.inner.id" $wham_id
13+
// @has - "$.index[*][?(@.name=='one_generic_param_fn')].inner.generics.params[0].kind.type.bounds[0].trait_bound.trait.id" $wham_id
1414
// @is - "$.index[*][?(@.name=='one_generic_param_fn')].inner.decl.inputs" '[["w", {"inner": "T", "kind": "generic"}]]'
1515
pub fn one_generic_param_fn<T: Wham>(w: T) {}
1616

1717
// @is - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.generics.where_predicates" []
1818
// @count - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.generics.params[*]" 1
1919
// @is - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.generics.params[0].name" '"impl Wham"'
2020
// @has - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.generics.params[0].kind.type.synthetic" true
21-
// @has - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.generics.params[0].kind.type.bounds[0].trait_bound.trait.inner.id" $wham_id
21+
// @has - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.generics.params[0].kind.type.bounds[0].trait_bound.trait.id" $wham_id
2222
// @count - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.decl.inputs[*]" 1
2323
// @is - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.decl.inputs[0][0]" '"w"'
2424
// @is - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.decl.inputs[0][1].kind" '"impl_trait"'
25-
// @is - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.decl.inputs[0][1].inner[0].trait_bound.trait.inner.id" $wham_id
25+
// @is - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.decl.inputs[0][1].inner[0].trait_bound.trait.id" $wham_id
2626
pub fn one_synthetic_generic_param_fn(w: impl Wham) {}

src/test/rustdoc-json/traits/supertrait.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ pub trait Loud {}
99

1010
// @set very_loud_id = - "$.index[*][?(@.name=='VeryLoud')].id"
1111
// @count - "$.index[*][?(@.name=='VeryLoud')].inner.bounds[*]" 1
12-
// @is - "$.index[*][?(@.name=='VeryLoud')].inner.bounds[0].trait_bound.trait.inner.id" $loud_id
12+
// @is - "$.index[*][?(@.name=='VeryLoud')].inner.bounds[0].trait_bound.trait.id" $loud_id
1313
pub trait VeryLoud: Loud {}
1414

1515
// @set sounds_good_id = - "$.index[*][?(@.name=='SoundsGood')].id"
1616
pub trait SoundsGood {}
1717

1818
// @count - "$.index[*][?(@.name=='MetalBand')].inner.bounds[*]" 2
19-
// @is - "$.index[*][?(@.name=='MetalBand')].inner.bounds[0].trait_bound.trait.inner.id" $very_loud_id
20-
// @is - "$.index[*][?(@.name=='MetalBand')].inner.bounds[1].trait_bound.trait.inner.id" $sounds_good_id
19+
// @is - "$.index[*][?(@.name=='MetalBand')].inner.bounds[0].trait_bound.trait.id" $very_loud_id
20+
// @is - "$.index[*][?(@.name=='MetalBand')].inner.bounds[1].trait_bound.trait.id" $sounds_good_id
2121
pub trait MetalBand: VeryLoud + SoundsGood {}
2222

2323
// @count - "$.index[*][?(@.name=='DnabLatem')].inner.bounds[*]" 2
24-
// @is - "$.index[*][?(@.name=='DnabLatem')].inner.bounds[1].trait_bound.trait.inner.id" $very_loud_id
25-
// @is - "$.index[*][?(@.name=='DnabLatem')].inner.bounds[0].trait_bound.trait.inner.id" $sounds_good_id
24+
// @is - "$.index[*][?(@.name=='DnabLatem')].inner.bounds[1].trait_bound.trait.id" $very_loud_id
25+
// @is - "$.index[*][?(@.name=='DnabLatem')].inner.bounds[0].trait_bound.trait.id" $sounds_good_id
2626
pub trait DnabLatem: SoundsGood + VeryLoud {}

0 commit comments

Comments
 (0)