Skip to content

Commit 8bb6051

Browse files
authored
Rollup merge of rust-lang#94137 - aDotInTheVoid:abi-enum, r=CraftSpider
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 r? ``@CraftSpider`` ``@rustbot`` modify labels: +A-rustdoc-json +T-rustdoc
2 parents 0c676a8 + fd5adef commit 8bb6051

File tree

13 files changed

+242
-115
lines changed

13 files changed

+242
-115
lines changed

compiler/rustc_hir/src/hir.rs

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

27812785
#[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)]
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// ignore-tidy-linelength
2+
3+
#![feature(abi_vectorcall)]
4+
#![feature(c_unwind)]
5+
6+
// @is abi.json "$.index[*][?(@.name=='AbiRust')].inner.type.inner.header.abi" \"Rust\"
7+
pub type AbiRust = fn();
8+
9+
// @is - "$.index[*][?(@.name=='AbiC')].inner.type.inner.header.abi" '{"C": {"unwind": false}}'
10+
pub type AbiC = extern "C" fn();
11+
12+
// @is - "$.index[*][?(@.name=='AbiSystem')].inner.type.inner.header.abi" '{"System": {"unwind": false}}'
13+
pub type AbiSystem = extern "system" fn();
14+
15+
// @is - "$.index[*][?(@.name=='AbiCUnwind')].inner.type.inner.header.abi" '{"C": {"unwind": true}}'
16+
pub type AbiCUnwind = extern "C-unwind" fn();
17+
18+
// @is - "$.index[*][?(@.name=='AbiSystemUnwind')].inner.type.inner.header.abi" '{"System": {"unwind": true}}'
19+
pub type AbiSystemUnwind = extern "system-unwind" fn();
20+
21+
// @is - "$.index[*][?(@.name=='AbiVecorcall')].inner.type.inner.header.abi.Other" '"\"vectorcall\""'
22+
pub type AbiVecorcall = extern "vectorcall" fn();
23+
24+
// @is - "$.index[*][?(@.name=='AbiVecorcallUnwind')].inner.type.inner.header.abi.Other" '"\"vectorcall-unwind\""'
25+
pub type AbiVecorcallUnwind = extern "vectorcall-unwind" fn();

src/test/rustdoc-json/fn_pointer/header.rs

-5
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @is qualifiers.json "$.index[*][?(@.name=='FnPointer')].inner.type.inner.header.unsafe" false
2+
// @is - "$.index[*][?(@.name=='FnPointer')].inner.type.inner.header.const" false
3+
// @is - "$.index[*][?(@.name=='FnPointer')].inner.type.inner.header.async" false
4+
pub type FnPointer = fn();
5+
6+
// @is - "$.index[*][?(@.name=='UnsafePointer')].inner.type.inner.header.unsafe" true
7+
// @is - "$.index[*][?(@.name=='UnsafePointer')].inner.type.inner.header.const" false
8+
// @is - "$.index[*][?(@.name=='UnsafePointer')].inner.type.inner.header.async" false
9+
pub type UnsafePointer = unsafe fn();

src/test/rustdoc-json/fns/abi.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// ignore-tidy-linelength
2+
3+
#![feature(abi_vectorcall)]
4+
#![feature(c_unwind)]
5+
6+
// @is abi.json "$.index[*][?(@.name=='abi_rust')].inner.header.abi" \"Rust\"
7+
pub fn abi_rust() {}
8+
9+
// @is - "$.index[*][?(@.name=='abi_c')].inner.header.abi" '{"C": {"unwind": false}}'
10+
pub extern "C" fn abi_c() {}
11+
12+
// @is - "$.index[*][?(@.name=='abi_system')].inner.header.abi" '{"System": {"unwind": false}}'
13+
pub extern "system" fn abi_system() {}
14+
15+
// @is - "$.index[*][?(@.name=='abi_c_unwind')].inner.header.abi" '{"C": {"unwind": true}}'
16+
pub extern "C-unwind" fn abi_c_unwind() {}
17+
18+
// @is - "$.index[*][?(@.name=='abi_system_unwind')].inner.header.abi" '{"System": {"unwind": true}}'
19+
pub extern "system-unwind" fn abi_system_unwind() {}
20+
21+
// @is - "$.index[*][?(@.name=='abi_vectorcall')].inner.header.abi.Other" '"\"vectorcall\""'
22+
pub extern "vectorcall" fn abi_vectorcall() {}
23+
24+
// @is - "$.index[*][?(@.name=='abi_vectorcall_unwind')].inner.header.abi.Other" '"\"vectorcall-unwind\""'
25+
pub extern "vectorcall-unwind" fn abi_vectorcall_unwind() {}

src/test/rustdoc-json/fns/header.rs

-22
This file was deleted.
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// edition:2018
2+
3+
// @is qualifiers.json "$.index[*][?(@.name=='nothing_fn')].inner.header.async" false
4+
// @is - "$.index[*][?(@.name=='nothing_fn')].inner.header.const" false
5+
// @is - "$.index[*][?(@.name=='nothing_fn')].inner.header.unsafe" false
6+
pub fn nothing_fn() {}
7+
8+
// @is - "$.index[*][?(@.name=='unsafe_fn')].inner.header.async" false
9+
// @is - "$.index[*][?(@.name=='unsafe_fn')].inner.header.const" false
10+
// @is - "$.index[*][?(@.name=='unsafe_fn')].inner.header.unsafe" true
11+
pub unsafe fn unsafe_fn() {}
12+
13+
// @is - "$.index[*][?(@.name=='const_fn')].inner.header.async" false
14+
// @is - "$.index[*][?(@.name=='const_fn')].inner.header.const" true
15+
// @is - "$.index[*][?(@.name=='const_fn')].inner.header.unsafe" false
16+
pub const fn const_fn() {}
17+
18+
// @is - "$.index[*][?(@.name=='async_fn')].inner.header.async" true
19+
// @is - "$.index[*][?(@.name=='async_fn')].inner.header.const" false
20+
// @is - "$.index[*][?(@.name=='async_fn')].inner.header.unsafe" false
21+
pub async fn async_fn() {}
22+
23+
// @is - "$.index[*][?(@.name=='async_unsafe_fn')].inner.header.async" true
24+
// @is - "$.index[*][?(@.name=='async_unsafe_fn')].inner.header.const" false
25+
// @is - "$.index[*][?(@.name=='async_unsafe_fn')].inner.header.unsafe" true
26+
pub async unsafe fn async_unsafe_fn() {}
27+
28+
// @is - "$.index[*][?(@.name=='const_unsafe_fn')].inner.header.async" false
29+
// @is - "$.index[*][?(@.name=='const_unsafe_fn')].inner.header.const" true
30+
// @is - "$.index[*][?(@.name=='const_unsafe_fn')].inner.header.unsafe" true
31+
pub const unsafe fn const_unsafe_fn() {}
32+
33+
// It's impossible for a function to be both const and async, so no test for that

src/test/rustdoc-json/method_abi.rs

-25
This file was deleted.

src/test/rustdoc-json/methods/abi.rs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// ignore-tidy-linelength
2+
3+
#![feature(abi_vectorcall)]
4+
#![feature(c_unwind)]
5+
#![feature(no_core)]
6+
#![no_core]
7+
8+
// @has abi.json "$.index[*][?(@.name=='Foo')]"
9+
pub struct Foo;
10+
11+
impl Foo {
12+
// @is - "$.index[*][?(@.name=='abi_rust')].inner.header.abi" \"Rust\"
13+
pub fn abi_rust() {}
14+
15+
// @is - "$.index[*][?(@.name=='abi_c')].inner.header.abi" '{"C": {"unwind": false}}'
16+
pub extern "C" fn abi_c() {}
17+
18+
// @is - "$.index[*][?(@.name=='abi_system')].inner.header.abi" '{"System": {"unwind": false}}'
19+
pub extern "system" fn abi_system() {}
20+
21+
// @is - "$.index[*][?(@.name=='abi_c_unwind')].inner.header.abi" '{"C": {"unwind": true}}'
22+
pub extern "C-unwind" fn abi_c_unwind() {}
23+
24+
// @is - "$.index[*][?(@.name=='abi_system_unwind')].inner.header.abi" '{"System": {"unwind": true}}'
25+
pub extern "system-unwind" fn abi_system_unwind() {}
26+
27+
// @is - "$.index[*][?(@.name=='abi_vectorcall')].inner.header.abi.Other" '"\"vectorcall\""'
28+
pub extern "vectorcall" fn abi_vectorcall() {}
29+
30+
// @is - "$.index[*][?(@.name=='abi_vectorcall_unwind')].inner.header.abi.Other" '"\"vectorcall-unwind\""'
31+
pub extern "vectorcall-unwind" fn abi_vectorcall_unwind() {}
32+
}
33+
34+
pub trait Bar {
35+
// @is - "$.index[*][?(@.name=='trait_abi_rust')].inner.header.abi" \"Rust\"
36+
fn trait_abi_rust() {}
37+
38+
// @is - "$.index[*][?(@.name=='trait_abi_c')].inner.header.abi" '{"C": {"unwind": false}}'
39+
extern "C" fn trait_abi_c() {}
40+
41+
// @is - "$.index[*][?(@.name=='trait_abi_system')].inner.header.abi" '{"System": {"unwind": false}}'
42+
extern "system" fn trait_abi_system() {}
43+
44+
// @is - "$.index[*][?(@.name=='trait_abi_c_unwind')].inner.header.abi" '{"C": {"unwind": true}}'
45+
extern "C-unwind" fn trait_abi_c_unwind() {}
46+
47+
// @is - "$.index[*][?(@.name=='trait_abi_system_unwind')].inner.header.abi" '{"System": {"unwind": true}}'
48+
extern "system-unwind" fn trait_abi_system_unwind() {}
49+
50+
// @is - "$.index[*][?(@.name=='trait_abi_vectorcall')].inner.header.abi.Other" '"\"vectorcall\""'
51+
extern "vectorcall" fn trait_abi_vectorcall() {}
52+
53+
// @is - "$.index[*][?(@.name=='trait_abi_vectorcall_unwind')].inner.header.abi.Other" '"\"vectorcall-unwind\""'
54+
extern "vectorcall-unwind" fn trait_abi_vectorcall_unwind() {}
55+
}

0 commit comments

Comments
 (0)