Skip to content
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

jsondoclint: Handle using enum variants and glob using enums. #104943

Merged
merged 1 commit into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
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
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