Skip to content

Commit f7ce9e6

Browse files
feat: Track parameter names for function pointer types, if available
1 parent ba39290 commit f7ce9e6

7 files changed

Lines changed: 75 additions & 28 deletions

File tree

compiler/pavexc/src/compiler/analyses/application_state/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ fn _field_name_candidate(ty_: &Type, strategy: NamingStrategy, candidate: &mut S
311311
if i > 0 {
312312
candidate.push('_');
313313
}
314-
_field_name_candidate(input, strategy, candidate);
314+
_field_name_candidate(&input.type_, strategy, candidate);
315315
}
316316
if let Some(output) = &fp.output {
317317
candidate.push_str("_ret_");

compiler/pavexc/src/compiler/codegen/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ fn collect_type_package_ids(package_ids: &mut IndexSet<PackageId>, t: &Type) {
485485
}
486486
Type::FunctionPointer(fp) => {
487487
for input in &fp.inputs {
488-
collect_type_package_ids(package_ids, input);
488+
collect_type_package_ids(package_ids, &input.type_);
489489
}
490490
if let Some(output) = &fp.output {
491491
collect_type_package_ids(package_ids, output);

rustdoc/rustdoc_ir/src/function_pointer.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,23 @@ use rustdoc_types::Abi;
44

55
use crate::Type;
66

7+
/// A single input parameter for a function pointer type.
8+
#[derive(serde::Serialize, serde::Deserialize, Eq, PartialEq, Hash, Clone)]
9+
pub struct FunctionPointerInput {
10+
/// The name of the parameter, if any.
11+
///
12+
/// `None` for unnamed parameters (e.g. `fn(usize)`),
13+
/// `Some` for named parameters (e.g. `fn(a: usize)`).
14+
pub name: Option<String>,
15+
/// The type of the parameter.
16+
pub type_: Type,
17+
}
18+
719
/// A Rust function pointer type—e.g. `fn(u32) -> u8` or `unsafe extern "C" fn()`.
820
#[derive(serde::Serialize, serde::Deserialize, Eq, PartialEq, Hash, Clone)]
921
pub struct FunctionPointer {
10-
/// The input parameter types.
11-
pub inputs: Vec<Type>,
22+
/// The input parameters, including optional names and types.
23+
pub inputs: Vec<FunctionPointerInput>,
1224
/// The return type. `None` means the unit type `()`.
1325
pub output: Option<Box<Type>>,
1426
/// The ABI of the function pointer (e.g. `Abi::Rust`, `Abi::C { unwind: false }`).
@@ -65,7 +77,10 @@ impl Debug for FunctionPointer {
6577
if i > 0 {
6678
write!(f, ", ")?;
6779
}
68-
write!(f, "{:?}", input)?;
80+
if let Some(name) = &input.name {
81+
write!(f, "{name}: ")?;
82+
}
83+
write!(f, "{:?}", input.type_)?;
6984
}
7085
write!(f, ")")?;
7186
if let Some(output) = &self.output {

rustdoc/rustdoc_ir/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub use callable_path::{
2525
EnumVariantConstructorPath, FreeFunctionPath, InherentMethodPath, StructLiteralPath,
2626
TraitMethodPath,
2727
};
28-
pub use function_pointer::FunctionPointer;
28+
pub use function_pointer::{FunctionPointer, FunctionPointerInput};
2929
pub use generic::Generic;
3030
pub use generic_argument::{GenericArgument, GenericLifetimeParameter};
3131
pub use lifetime::Lifetime;

rustdoc/rustdoc_ir/src/render.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,10 @@ impl Type {
226226
write!(buffer, "fn(").unwrap();
227227
let mut inputs = fp.inputs.iter().peekable();
228228
while let Some(input) = inputs.next() {
229-
input.render_into(config, buffer);
229+
if let Some(name) = &input.name {
230+
write!(buffer, "{name}: ").unwrap();
231+
}
232+
input.type_.render_into(config, buffer);
230233
if inputs.peek().is_some() {
231234
write!(buffer, ", ").unwrap();
232235
}

rustdoc/rustdoc_ir/src/type_.rs

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ use indexmap::{IndexMap, IndexSet};
55

66
use crate::generics_equivalence::UnassignedIdGenerator;
77
use crate::{
8-
Array, FunctionPointer, Generic, GenericArgument, GenericLifetimeParameter, Lifetime,
9-
NamedLifetime, PathType, RawPointer, Slice, Tuple, Type, TypeReference,
8+
Array, FunctionPointer, FunctionPointerInput, Generic, GenericArgument,
9+
GenericLifetimeParameter, Lifetime, NamedLifetime, PathType, RawPointer, Slice, Tuple, Type,
10+
TypeReference,
1011
};
1112

1213
/// A `Type` with canonicalized names for lifetimes and unassigned generic type parameters.
@@ -115,7 +116,10 @@ impl Type {
115116
inputs: fp
116117
.inputs
117118
.iter()
118-
.map(|t| t.bind_generic_type_parameters(bindings))
119+
.map(|input| FunctionPointerInput {
120+
name: input.name.clone(),
121+
type_: input.type_.bind_generic_type_parameters(bindings),
122+
})
119123
.collect(),
120124
output: fp
121125
.output
@@ -154,7 +158,7 @@ impl Type {
154158
Type::Array(a) => a.element_type.is_a_template(),
155159
Type::RawPointer(r) => r.inner.is_a_template(),
156160
Type::FunctionPointer(fp) => {
157-
fp.inputs.iter().any(|t| t.is_a_template())
161+
fp.inputs.iter().any(|input| input.type_.is_a_template())
158162
|| fp.output.as_ref().is_some_and(|t| t.is_a_template())
159163
}
160164
Type::Generic(_) => true,
@@ -194,7 +198,7 @@ impl Type {
194198
Type::RawPointer(r) => r.inner._unassigned_generic_type_parameters(set),
195199
Type::FunctionPointer(fp) => {
196200
for input in &fp.inputs {
197-
input._unassigned_generic_type_parameters(set);
201+
input.type_._unassigned_generic_type_parameters(set);
198202
}
199203
if let Some(output) = &fp.output {
200204
output._unassigned_generic_type_parameters(set);
@@ -283,7 +287,11 @@ impl Type {
283287
.inputs
284288
.iter()
285289
.zip(templated_fp.inputs.iter())
286-
.all(|(concrete, templated)| templated._is_a_template_for(concrete, bindings));
290+
.all(|(concrete, templated)| {
291+
templated
292+
.type_
293+
._is_a_template_for(&concrete.type_, bindings)
294+
});
287295
if !inputs_match {
288296
return false;
289297
}
@@ -385,11 +393,15 @@ impl Type {
385393
if self_fp.inputs.len() != other_fp.inputs.len() {
386394
return false;
387395
}
388-
let inputs_match = self_fp
389-
.inputs
390-
.iter()
391-
.zip(other_fp.inputs.iter())
392-
.all(|(s, o)| s._is_equivalent_to(o, self_id_gen, other_id_gen));
396+
let inputs_match =
397+
self_fp
398+
.inputs
399+
.iter()
400+
.zip(other_fp.inputs.iter())
401+
.all(|(s, o)| {
402+
s.type_
403+
._is_equivalent_to(&o.type_, self_id_gen, other_id_gen)
404+
});
393405
if !inputs_match {
394406
return false;
395407
}
@@ -445,7 +457,7 @@ impl Type {
445457
Type::FunctionPointer(fp) => {
446458
fp.inputs
447459
.iter()
448-
.any(|t| t.has_implicit_lifetime_parameters())
460+
.any(|input| input.type_.has_implicit_lifetime_parameters())
449461
|| fp
450462
.output
451463
.as_ref()
@@ -495,7 +507,9 @@ impl Type {
495507
Type::RawPointer(r) => r.inner.set_implicit_lifetimes(inferred_lifetime),
496508
Type::FunctionPointer(fp) => {
497509
for input in fp.inputs.iter_mut() {
498-
input.set_implicit_lifetimes(inferred_lifetime.clone());
510+
input
511+
.type_
512+
.set_implicit_lifetimes(inferred_lifetime.clone());
499513
}
500514
if let Some(output) = fp.output.as_mut() {
501515
output.set_implicit_lifetimes(inferred_lifetime);
@@ -553,7 +567,7 @@ impl Type {
553567
}
554568
Type::FunctionPointer(fp) => {
555569
for input in fp.inputs.iter_mut() {
556-
input.rename_lifetime_parameters(original2renamed);
570+
input.type_.rename_lifetime_parameters(original2renamed);
557571
}
558572
if let Some(output) = fp.output.as_mut() {
559573
output.rename_lifetime_parameters(original2renamed);
@@ -598,7 +612,7 @@ impl Type {
598612
Type::RawPointer(r) => r.inner._lifetime_parameters(set),
599613
Type::FunctionPointer(fp) => {
600614
for input in &fp.inputs {
601-
input._lifetime_parameters(set);
615+
input.type_._lifetime_parameters(set);
602616
}
603617
if let Some(output) = &fp.output {
604618
output._lifetime_parameters(set);
@@ -651,7 +665,7 @@ impl Type {
651665
Type::RawPointer(r) => r.inner._named_lifetime_parameters(set),
652666
Type::FunctionPointer(fp) => {
653667
for input in &fp.inputs {
654-
input._named_lifetime_parameters(set);
668+
input.type_._named_lifetime_parameters(set);
655669
}
656670
if let Some(output) = &fp.output {
657671
output._named_lifetime_parameters(set);
@@ -810,7 +824,14 @@ impl Type {
810824
inputs: fp
811825
.inputs
812826
.iter()
813-
.map(|t| t._canonicalize(lifetime_counter, generic_counter, generic_name_map))
827+
.map(|input| FunctionPointerInput {
828+
name: None,
829+
type_: input.type_._canonicalize(
830+
lifetime_counter,
831+
generic_counter,
832+
generic_name_map,
833+
),
834+
})
814835
.collect(),
815836
output: fp.output.as_ref().map(|t| {
816837
Box::new(t._canonicalize(lifetime_counter, generic_counter, generic_name_map))

rustdoc/rustdoc_resolver/src/resolve_type.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use once_cell::sync::OnceCell;
77
use rustdoc_types::{GenericArg, GenericArgs, GenericParamDefKind, ItemEnum, Type as RustdocType};
88

99
use rustdoc_ir::{
10-
Array, FunctionPointer, Generic, GenericArgument, GenericLifetimeParameter, PathType,
11-
RawPointer, Slice, Tuple, Type, TypeReference,
10+
Array, FunctionPointer, FunctionPointerInput, Generic, GenericArgument,
11+
GenericLifetimeParameter, PathType, RawPointer, Slice, Tuple, Type, TypeReference,
1212
};
1313
use rustdoc_processor::CrateCollection;
1414
use rustdoc_processor::indexing::CrateIndexer;
@@ -516,8 +516,8 @@ fn _resolve_type<I: CrateIndexer>(
516516
.inputs
517517
.iter()
518518
.enumerate()
519-
.map(|(i, (_, ty))| {
520-
resolve_type(
519+
.map(|(i, (name, ty))| {
520+
let resolved_ty = resolve_type(
521521
ty,
522522
used_by_package_id,
523523
krate_collection,
@@ -531,6 +531,14 @@ fn _resolve_type<I: CrateIndexer>(
531531
source,
532532
},
533533
))
534+
})?;
535+
let name = match name.as_str() {
536+
"" | "_" => None,
537+
s => Some(s.to_owned()),
538+
};
539+
Ok(FunctionPointerInput {
540+
name,
541+
type_: resolved_ty,
534542
})
535543
})
536544
.collect::<Result<Vec<_>, _>>()?;

0 commit comments

Comments
 (0)