Skip to content

Commit

Permalink
Rollup merge of #104943 - aDotInTheVoid:jsondoclint-use-enum, r=Guill…
Browse files Browse the repository at this point in the history
…aumeGomez

jsondoclint: Handle using enum variants and glob using enums.

More work on jsondoclint for `core.json`

Closes #104942

r? `@GuillaumeGomez`

`@rustbot` modify labels: +A-testsuite
  • Loading branch information
GuillaumeGomez authored Nov 26, 2022
2 parents d99201c + ed0f097 commit 95e6356
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/test/rustdoc-json/enums/use_glob.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Regression test for <https://github.com/rust-lang/rust/issues/104942>

#![feature(no_core)]
#![no_core]

// @set Color = "$.index[*][?(@.name == 'Color')].id"
pub enum Color {
Red,
Green,
Blue,
}

// @set use_Color = "$.index[*][?(@.kind == 'import')].id"
// @is "$.index[*][?(@.kind == 'import')].inner.id" $Color
// @is "$.index[*][?(@.kind == 'import')].inner.glob" true
pub use Color::*;

// @ismany "$.index[*][?(@.name == 'use_glob')].inner.items[*]" $Color $use_Color
15 changes: 15 additions & 0 deletions src/test/rustdoc-json/enums/use_variant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![feature(no_core)]
#![no_core]

// @set AlwaysNone = "$.index[*][?(@.name == 'AlwaysNone')].id"
pub enum AlwaysNone {
// @set None = "$.index[*][?(@.name == 'None')].id"
None,
}
// @is "$.index[*][?(@.name == 'AlwaysNone')].inner.variants[*]" $None

// @set use_None = "$.index[*][?(@.kind == 'import')].id"
// @is "$.index[*][?(@.kind == 'import')].inner.id" $None
pub use AlwaysNone::None;

// @ismany "$.index[*][?(@.name == 'use_variant')].inner.items[*]" $AlwaysNone $use_None
18 changes: 17 additions & 1 deletion src/tools/jsondoclint/src/item_kind.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rustdoc_json_types::{Item, ItemEnum, ItemKind, ItemSummary};

/// A univeral way to represent an [`ItemEnum`] or [`ItemKind`]
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub(crate) enum Kind {
Module,
ExternCrate,
Expand Down Expand Up @@ -68,6 +68,22 @@ impl Kind {
}
}

pub fn can_appear_in_import(self) -> bool {
match self {
Kind::Variant => true,
Kind::Import => false,
other => other.can_appear_in_mod(),
}
}

pub fn can_appear_in_glob_import(self) -> bool {
match self {
Kind::Module => true,
Kind::Enum => true,
_ => false,
}
}

pub fn can_appear_in_trait(self) -> bool {
match self {
Kind::AssocConst => true,
Expand Down
13 changes: 11 additions & 2 deletions src/tools/jsondoclint/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ impl<'a> Validator<'a> {

fn check_import(&mut self, x: &'a Import) {
if x.glob {
self.add_mod_id(x.id.as_ref().unwrap());
self.add_glob_import_item_id(x.id.as_ref().unwrap());
} else if let Some(id) = &x.id {
self.add_mod_item_id(id);
self.add_import_item_id(id);
}
}

Expand Down Expand Up @@ -404,6 +404,15 @@ impl<'a> Validator<'a> {
self.add_id_checked(id, Kind::can_appear_in_trait, "Trait inner item");
}

/// Add an Id that can be `use`d
fn add_import_item_id(&mut self, id: &'a Id) {
self.add_id_checked(id, Kind::can_appear_in_import, "Import inner item");
}

fn add_glob_import_item_id(&mut self, id: &'a Id) {
self.add_id_checked(id, Kind::can_appear_in_glob_import, "Glob import inner item");
}

/// Add an Id that appeared in a mod
fn add_mod_item_id(&mut self, id: &'a Id) {
self.add_id_checked(id, Kind::can_appear_in_mod, "Module inner item")
Expand Down

0 comments on commit 95e6356

Please sign in to comment.