Skip to content

Commit a26fa74

Browse files
committed
Make header a vec of modifiers, make FunctionPointer consistent with Function and Method.
1 parent 36ecbc9 commit a26fa74

File tree

7 files changed

+85
-16
lines changed

7 files changed

+85
-16
lines changed

src/etc/check_missing_items.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def check_type(ty):
108108
elif ty["kind"] == "function_pointer":
109109
for param in ty["inner"]["generic_params"]:
110110
check_generic_param(param)
111-
check_decl(ty["inner"]["inner"])
111+
check_decl(ty["inner"]["decl"])
112112
elif ty["kind"] == "qualified_path":
113113
check_type(ty["inner"]["self_type"])
114114
check_type(ty["inner"]["trait"])

src/librustdoc/json/conversions.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,22 @@ crate fn from_ctor_kind(struct_type: CtorKind) -> StructType {
225225
}
226226
}
227227

228-
fn stringify_header(header: &rustc_hir::FnHeader) -> String {
229-
let mut s = String::from(header.unsafety.prefix_str());
230-
if header.asyncness == rustc_hir::IsAsync::Async {
231-
s.push_str("async ")
228+
crate fn from_fn_header(header: &rustc_hir::FnHeader) -> Vec<Modifiers> {
229+
let mut v = Vec::new();
230+
231+
if let rustc_hir::Unsafety::Unsafe = header.unsafety {
232+
v.push(Modifiers::Unsafe);
233+
}
234+
235+
if let rustc_hir::IsAsync::Async = header.asyncness {
236+
v.push(Modifiers::Async);
232237
}
233-
if header.constness == rustc_hir::Constness::Const {
234-
s.push_str("const ")
238+
239+
if let rustc_hir::Constness::Const = header.constness {
240+
v.push(Modifiers::Const);
235241
}
236-
s
242+
243+
v
237244
}
238245

239246
impl From<clean::Function> for Function {
@@ -242,7 +249,7 @@ impl From<clean::Function> for Function {
242249
Function {
243250
decl: decl.into(),
244251
generics: generics.into(),
245-
header: stringify_header(&header),
252+
header: from_fn_header(&header),
246253
abi: header.abi.to_string(),
247254
}
248255
}
@@ -364,7 +371,11 @@ impl From<clean::BareFunctionDecl> for FunctionPointer {
364371
fn from(bare_decl: clean::BareFunctionDecl) -> Self {
365372
let clean::BareFunctionDecl { unsafety, generic_params, decl, abi } = bare_decl;
366373
FunctionPointer {
367-
is_unsafe: unsafety == rustc_hir::Unsafety::Unsafe,
374+
header: if let rustc_hir::Unsafety::Unsafe = unsafety {
375+
vec![Modifiers::Unsafe]
376+
} else {
377+
vec![]
378+
},
368379
generic_params: generic_params.into_iter().map(Into::into).collect(),
369380
decl: decl.into(),
370381
abi: abi.to_string(),
@@ -439,7 +450,7 @@ crate fn from_function_method(function: clean::Function, has_body: bool) -> Meth
439450
Method {
440451
decl: decl.into(),
441452
generics: generics.into(),
442-
header: stringify_header(&header),
453+
header: from_fn_header(&header),
443454
abi: header.abi.to_string(),
444455
has_body,
445456
}

src/librustdoc/json/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
243243
)
244244
})
245245
.collect(),
246-
format_version: 3,
246+
format_version: 4,
247247
};
248248
let mut p = self.out_path.clone();
249249
p.push(output.index.get(&output.root).unwrap().name.clone().unwrap());

src/rustdoc-json-types/lib.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,19 +281,28 @@ pub enum StructType {
281281
Unit,
282282
}
283283

284+
#[non_exhaustive]
285+
#[serde(rename_all = "snake_case")]
286+
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
287+
pub enum Modifiers {
288+
Const,
289+
Unsafe,
290+
Async,
291+
}
292+
284293
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
285294
pub struct Function {
286295
pub decl: FnDecl,
287296
pub generics: Generics,
288-
pub header: String,
297+
pub header: Vec<Modifiers>,
289298
pub abi: String,
290299
}
291300

292301
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
293302
pub struct Method {
294303
pub decl: FnDecl,
295304
pub generics: Generics,
296-
pub header: String,
305+
pub header: Vec<Modifiers>,
297306
pub abi: String,
298307
pub has_body: bool,
299308
}
@@ -404,9 +413,9 @@ pub enum Type {
404413

405414
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
406415
pub struct FunctionPointer {
407-
pub is_unsafe: bool,
408-
pub generic_params: Vec<GenericParamDef>,
409416
pub decl: FnDecl,
417+
pub generic_params: Vec<GenericParamDef>,
418+
pub header: Vec<Modifiers>,
410419
pub abi: String,
411420
}
412421

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @has header.json "$.index[*][?(@.name=='FnPointer')].inner.type.inner.header" "[]"
2+
pub type FnPointer = fn();
3+
4+
// @has - "$.index[*][?(@.name=='UnsafePointer')].inner.type.inner.header" '["unsafe"]'
5+
pub type UnsafePointer = unsafe fn();

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// edition:2018
2+
3+
// @has header.json "$.index[*][?(@.name=='nothing_fn')].inner.header" "[]"
4+
pub fn nothing_fn() {}
5+
6+
// @has - "$.index[*][?(@.name=='const_fn')].inner.header" '["const"]'
7+
pub const fn const_fn() {}
8+
9+
// @has - "$.index[*][?(@.name=='async_fn')].inner.header" '["async"]'
10+
pub async fn async_fn() {}
11+
12+
// @count - "$.index[*][?(@.name=='async_unsafe_fn')].inner.header[*]" 2
13+
// @has - "$.index[*][?(@.name=='async_unsafe_fn')].inner.header[*]" '"async"'
14+
// @has - "$.index[*][?(@.name=='async_unsafe_fn')].inner.header[*]" '"unsafe"'
15+
pub async unsafe fn async_unsafe_fn() {}
16+
17+
// @count - "$.index[*][?(@.name=='const_unsafe_fn')].inner.header[*]" 2
18+
// @has - "$.index[*][?(@.name=='const_unsafe_fn')].inner.header[*]" '"const"'
19+
// @has - "$.index[*][?(@.name=='const_unsafe_fn')].inner.header[*]" '"unsafe"'
20+
pub const unsafe fn const_unsafe_fn() {}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// edition:2018
2+
3+
pub struct Foo;
4+
5+
impl Foo {
6+
// @has header.json "$.index[*][?(@.name=='nothing_meth')].inner.header" "[]"
7+
pub fn nothing_meth() {}
8+
9+
// @has - "$.index[*][?(@.name=='const_meth')].inner.header" '["const"]'
10+
pub const fn const_meth() {}
11+
12+
// @has - "$.index[*][?(@.name=='async_meth')].inner.header" '["async"]'
13+
pub async fn async_meth() {}
14+
15+
// @count - "$.index[*][?(@.name=='async_unsafe_meth')].inner.header[*]" 2
16+
// @has - "$.index[*][?(@.name=='async_unsafe_meth')].inner.header[*]" '"async"'
17+
// @has - "$.index[*][?(@.name=='async_unsafe_meth')].inner.header[*]" '"unsafe"'
18+
pub async unsafe fn async_unsafe_meth() {}
19+
20+
// @count - "$.index[*][?(@.name=='const_unsafe_meth')].inner.header[*]" 2
21+
// @has - "$.index[*][?(@.name=='const_unsafe_meth')].inner.header[*]" '"const"'
22+
// @has - "$.index[*][?(@.name=='const_unsafe_meth')].inner.header[*]" '"unsafe"'
23+
pub const unsafe fn const_unsafe_meth() {}
24+
}

0 commit comments

Comments
 (0)