Skip to content

Commit aa60157

Browse files
committed
rustdoc-json: Better Header Type
- Make ABI an enum, instead of being stringly typed - Replace Qualifier HashSet with 3 bools - Merge ABI field into header, as they always occor together
1 parent b8c56fa commit aa60157

File tree

3 files changed

+58
-37
lines changed

3 files changed

+58
-37
lines changed

compiler/rustc_hir/src/hir.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2777,6 +2777,10 @@ impl FnHeader {
27772777
pub fn is_const(&self) -> bool {
27782778
matches!(&self.constness, Constness::Const)
27792779
}
2780+
2781+
pub fn is_unsafe(&self) -> bool {
2782+
matches!(&self.unsafety, Unsafety::Unsafe)
2783+
}
27802784
}
27812785

27822786
#[derive(Debug, HashStable_Generic)]

src/librustdoc/json/conversions.rs

+25-23
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ use rustc_hir::{def::CtorKind, def_id::DefId};
1212
use rustc_middle::ty::{self, TyCtxt};
1313
use rustc_span::def_id::CRATE_DEF_INDEX;
1414
use rustc_span::Pos;
15+
use rustc_target::spec::abi::Abi as RustcAbi;
1516

1617
use rustdoc_json_types::*;
1718

1819
use crate::clean::utils::print_const_expr;
1920
use crate::clean::{self, ItemId};
2021
use crate::formats::item_type::ItemType;
2122
use crate::json::JsonRenderer;
22-
use std::collections::HashSet;
2323

2424
impl JsonRenderer<'_> {
2525
pub(super) fn convert_item(&self, item: clean::Item) -> Option<Item> {
@@ -271,22 +271,28 @@ crate fn from_ctor_kind(struct_type: CtorKind) -> StructType {
271271
}
272272
}
273273

274-
crate fn from_fn_header(header: &rustc_hir::FnHeader) -> HashSet<Qualifiers> {
275-
let mut v = HashSet::new();
276-
277-
if let rustc_hir::Unsafety::Unsafe = header.unsafety {
278-
v.insert(Qualifiers::Unsafe);
279-
}
280-
281-
if let rustc_hir::IsAsync::Async = header.asyncness {
282-
v.insert(Qualifiers::Async);
274+
crate fn from_fn_header(header: &rustc_hir::FnHeader) -> Header {
275+
Header {
276+
async_: header.is_async(),
277+
const_: header.is_const(),
278+
unsafe_: header.is_unsafe(),
279+
abi: convert_abi(header.abi),
283280
}
281+
}
284282

285-
if let rustc_hir::Constness::Const = header.constness {
286-
v.insert(Qualifiers::Const);
283+
fn convert_abi(a: RustcAbi) -> Abi {
284+
match a {
285+
RustcAbi::Rust => Abi::Rust,
286+
RustcAbi::C { unwind } => Abi::C { unwind },
287+
RustcAbi::Cdecl { unwind } => Abi::Cdecl { unwind },
288+
RustcAbi::Stdcall { unwind } => Abi::Stdcall { unwind },
289+
RustcAbi::Fastcall { unwind } => Abi::Fastcall { unwind },
290+
RustcAbi::Aapcs { unwind } => Abi::Aapcs { unwind },
291+
RustcAbi::Win64 { unwind } => Abi::Win64 { unwind },
292+
RustcAbi::SysV64 { unwind } => Abi::SysV64 { unwind },
293+
RustcAbi::System { unwind } => Abi::System { unwind },
294+
_ => Abi::Other(a.to_string()),
287295
}
288-
289-
v
290296
}
291297

292298
impl FromWithTcx<clean::Function> for Function {
@@ -296,7 +302,6 @@ impl FromWithTcx<clean::Function> for Function {
296302
decl: decl.into_tcx(tcx),
297303
generics: generics.into_tcx(tcx),
298304
header: from_fn_header(&header),
299-
abi: header.abi.to_string(),
300305
}
301306
}
302307
}
@@ -465,16 +470,14 @@ impl FromWithTcx<clean::BareFunctionDecl> for FunctionPointer {
465470
fn from_tcx(bare_decl: clean::BareFunctionDecl, tcx: TyCtxt<'_>) -> Self {
466471
let clean::BareFunctionDecl { unsafety, generic_params, decl, abi } = bare_decl;
467472
FunctionPointer {
468-
header: if let rustc_hir::Unsafety::Unsafe = unsafety {
469-
let mut hs = HashSet::new();
470-
hs.insert(Qualifiers::Unsafe);
471-
hs
472-
} else {
473-
HashSet::new()
473+
header: Header {
474+
unsafe_: matches!(unsafety, rustc_hir::Unsafety::Unsafe),
475+
const_: false,
476+
async_: false,
477+
abi: convert_abi(abi),
474478
},
475479
generic_params: generic_params.into_iter().map(|x| x.into_tcx(tcx)).collect(),
476480
decl: decl.into_tcx(tcx),
477-
abi: abi.to_string(),
478481
}
479482
}
480483
}
@@ -554,7 +557,6 @@ crate fn from_function_method(
554557
decl: decl.into_tcx(tcx),
555558
generics: generics.into_tcx(tcx),
556559
header: from_fn_header(&header),
557-
abi: header.abi.to_string(),
558560
has_body,
559561
}
560562
}

src/rustdoc-json-types/lib.rs

+29-14
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
//! These types are the public API exposed through the `--output-format json` flag. The [`Crate`]
44
//! struct is the root of the JSON blob and all other items are contained within.
55
6-
use std::collections::{HashMap, HashSet};
6+
use std::collections::HashMap;
77
use std::path::PathBuf;
88

99
use serde::{Deserialize, Serialize};
1010

1111
/// rustdoc format-version.
12-
pub const FORMAT_VERSION: u32 = 10;
12+
pub const FORMAT_VERSION: u32 = 11;
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
@@ -287,29 +287,45 @@ pub enum StructType {
287287
Unit,
288288
}
289289

290-
#[non_exhaustive]
291290
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
292-
#[serde(rename_all = "snake_case")]
293-
pub enum Qualifiers {
294-
Const,
295-
Unsafe,
296-
Async,
291+
pub struct Header {
292+
#[serde(rename = "const")]
293+
pub const_: bool,
294+
#[serde(rename = "unsafe")]
295+
pub unsafe_: bool,
296+
#[serde(rename = "async")]
297+
pub async_: bool,
298+
pub abi: Abi,
299+
}
300+
301+
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
302+
pub enum Abi {
303+
// We only have a concrete listing here for stable ABI's because their are so many
304+
// See rustc_ast_passes::feature_gate::PostExpansionVisitor::check_abi for the list
305+
Rust,
306+
C { unwind: bool },
307+
Cdecl { unwind: bool },
308+
Stdcall { unwind: bool },
309+
Fastcall { unwind: bool },
310+
Aapcs { unwind: bool },
311+
Win64 { unwind: bool },
312+
SysV64 { unwind: bool },
313+
System { unwind: bool },
314+
Other(String),
297315
}
298316

299317
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
300318
pub struct Function {
301319
pub decl: FnDecl,
302320
pub generics: Generics,
303-
pub header: HashSet<Qualifiers>,
304-
pub abi: String,
321+
pub header: Header,
305322
}
306323

307324
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
308325
pub struct Method {
309326
pub decl: FnDecl,
310327
pub generics: Generics,
311-
pub header: HashSet<Qualifiers>,
312-
pub abi: String,
328+
pub header: Header,
313329
pub has_body: bool,
314330
}
315331

@@ -426,8 +442,7 @@ pub enum Type {
426442
pub struct FunctionPointer {
427443
pub decl: FnDecl,
428444
pub generic_params: Vec<GenericParamDef>,
429-
pub header: HashSet<Qualifiers>,
430-
pub abi: String,
445+
pub header: Header,
431446
}
432447

433448
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]

0 commit comments

Comments
 (0)