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

rustdoc-json: Inconsistant Enum Representation #93667

Closed
aDotInTheVoid opened this issue Feb 5, 2022 · 2 comments
Closed

rustdoc-json: Inconsistant Enum Representation #93667

aDotInTheVoid opened this issue Feb 5, 2022 · 2 comments
Labels
A-rustdoc-json Area: Rustdoc JSON backend C-discussion Category: Discussion or questions that doesn't represent real issues. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@aDotInTheVoid
Copy link
Member

Currently we use 3 different ways to serialse enums to json:

  1. For some of the enums, we use Adjacently tagged

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
#[serde(tag = "kind", content = "inner")]
pub enum Type {
/// Structs, enums, and traits
ResolvedPath {
name: String,
id: Id,
args: Option<Box<GenericArgs>>,
param_names: Vec<GenericBound>,
},
/// Parameterized types
Generic(String),
/// Fixed-size numeric types (plus int/usize/float), char, arrays, slices, and tuples
Primitive(String),
/// `extern "ABI" fn`
FunctionPointer(Box<FunctionPointer>),
/// `(String, u32, Box<usize>)`
Tuple(Vec<Type>),
/// `[u32]`
Slice(Box<Type>),
/// [u32; 15]
Array {
#[serde(rename = "type")]
type_: Box<Type>,
len: String,
},
/// `impl TraitA + TraitB + ...`
ImplTrait(Vec<GenericBound>),
/// `_`
Infer,
/// `*mut u32`, `*u8`, etc.
RawPointer {
mutable: bool,
#[serde(rename = "type")]
type_: Box<Type>,
},
/// `&'a mut String`, `&str`, etc.
BorrowedRef {
lifetime: Option<String>,
mutable: bool,
#[serde(rename = "type")]
type_: Box<Type>,
},
/// `<Type as Trait>::Name` or associated types like `T::Item` where `T: Iterator`
QualifiedPath {
name: String,
self_type: Box<Type>,
#[serde(rename = "trait")]
trait_: Box<Type>,
},
}

Which ends up looking like

              "output": {
                "inner": {
                  "lifetime": "'a",
                  "mutable": false,
                  "type": {"inner": "i32", "kind": "primitive"}
                },
                "kind": "borrowed_ref"
              }
  1. For others we use serdes default which is Externally tagged

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct GenericParamDef {
pub name: String,
pub kind: GenericParamDefKind,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum GenericParamDefKind {
Lifetime { outlives: Vec<String> },
Type { bounds: Vec<GenericBound>, default: Option<Type> },
Const { ty: Type, default: Option<String> },
}

Which gives output that looks like

          "params": [
            {
              "kind": {"lifetime": {"outlives": []}},
              "name": "'a"
            },
            {
              "kind": {"type": {"bounds": [], "default": null}},
              "name": "T"
            }
  1. Finaly we have the Item / ItemEnum, which are their own special weird, because of Remove Item::kind, use tagged enum. Rename variants to match #82613.
    Its basicly Ajacenctly tagged, but the tag/content are inlined to the Item

I'm not sure how much it matters which one we pick as almost all users will be going through the rust types, but it would be good to be consistant. I have no prefernce between 1 and 2, but 3 seems weird, so I dont think we should do that.

cc @CraftSpider

@rustbot modify labels: +C-discussion +A-rustdoc-json +T-rustdoc
but I think 3 is bad

@rustbot rustbot added A-rustdoc-json Area: Rustdoc JSON backend C-discussion Category: Discussion or questions that doesn't represent real issues. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Feb 5, 2022
@aDotInTheVoid
Copy link
Member Author

This shouldn't be done untill after #94140, because rewriting all the tests is a pain

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue May 23, 2023
…eVoid

[rustdoc][JSON] Use exclusively externally tagged enums in the JSON representation

See [this Zulip thread](https://rust-lang.zulipchat.com/#narrow/stream/266220-rustdoc/topic/.28De.29serialization.20speed.20of.20JSON.20docs) and [this issue](rust-lang#93667) for the relevant context.
@aDotInTheVoid
Copy link
Member Author

Done in #111427

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-rustdoc-json Area: Rustdoc JSON backend C-discussion Category: Discussion or questions that doesn't represent real issues. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants