1
Fork 0

jsondoclint: Handle using enum variants and glob using enums.

Closes #104942
This commit is contained in:
Nixon Enraght-Moony 2022-11-26 15:06:46 +00:00
parent 8681d4cffc
commit ed0f097e4d
4 changed files with 61 additions and 3 deletions

View file

@ -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

View file

@ -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

View file

@ -1,7 +1,7 @@
use rustdoc_json_types::{Item, ItemEnum, ItemKind, ItemSummary}; use rustdoc_json_types::{Item, ItemEnum, ItemKind, ItemSummary};
/// A univeral way to represent an [`ItemEnum`] or [`ItemKind`] /// A univeral way to represent an [`ItemEnum`] or [`ItemKind`]
#[derive(Debug)] #[derive(Debug, Clone, Copy)]
pub(crate) enum Kind { pub(crate) enum Kind {
Module, Module,
ExternCrate, ExternCrate,
@ -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 { pub fn can_appear_in_trait(self) -> bool {
match self { match self {
Kind::AssocConst => true, Kind::AssocConst => true,

View file

@ -103,9 +103,9 @@ impl<'a> Validator<'a> {
fn check_import(&mut self, x: &'a Import) { fn check_import(&mut self, x: &'a Import) {
if x.glob { 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 { } else if let Some(id) = &x.id {
self.add_mod_item_id(id); self.add_import_item_id(id);
} }
} }
@ -404,6 +404,15 @@ impl<'a> Validator<'a> {
self.add_id_checked(id, Kind::can_appear_in_trait, "Trait inner item"); 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 /// Add an Id that appeared in a mod
fn add_mod_item_id(&mut self, id: &'a Id) { fn add_mod_item_id(&mut self, id: &'a Id) {
self.add_id_checked(id, Kind::can_appear_in_mod, "Module inner item") self.add_id_checked(id, Kind::can_appear_in_mod, "Module inner item")