Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ fn _field_name_candidate(ty_: &Type, strategy: NamingStrategy, candidate: &mut S
if i > 0 {
candidate.push('_');
}
_field_name_candidate(input, strategy, candidate);
_field_name_candidate(&input.type_, strategy, candidate);
}
if let Some(output) = &fp.output {
candidate.push_str("_ret_");
Expand Down
2 changes: 1 addition & 1 deletion compiler/pavexc/src/compiler/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ fn collect_type_package_ids(package_ids: &mut IndexSet<PackageId>, t: &Type) {
}
Type::FunctionPointer(fp) => {
for input in &fp.inputs {
collect_type_package_ids(package_ids, input);
collect_type_package_ids(package_ids, &input.type_);
}
if let Some(output) = &fp.output {
collect_type_package_ids(package_ids, output);
Expand Down
21 changes: 18 additions & 3 deletions rustdoc/rustdoc_ir/src/function_pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@ use rustdoc_types::Abi;

use crate::Type;

/// A single input parameter for a function pointer type.
#[derive(serde::Serialize, serde::Deserialize, Eq, PartialEq, Hash, Clone)]
pub struct FunctionPointerInput {
/// The name of the parameter, if any.
///
/// `None` for unnamed parameters (e.g. `fn(usize)`),
/// `Some` for named parameters (e.g. `fn(a: usize)`).
pub name: Option<String>,
/// The type of the parameter.
pub type_: Type,
}

/// A Rust function pointer type—e.g. `fn(u32) -> u8` or `unsafe extern "C" fn()`.
#[derive(serde::Serialize, serde::Deserialize, Eq, PartialEq, Hash, Clone)]
pub struct FunctionPointer {
/// The input parameter types.
pub inputs: Vec<Type>,
/// The input parameters, including optional names and types.
pub inputs: Vec<FunctionPointerInput>,
/// The return type. `None` means the unit type `()`.
pub output: Option<Box<Type>>,
/// The ABI of the function pointer (e.g. `Abi::Rust`, `Abi::C { unwind: false }`).
Expand Down Expand Up @@ -65,7 +77,10 @@ impl Debug for FunctionPointer {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{:?}", input)?;
if let Some(name) = &input.name {
write!(f, "{name}: ")?;
}
write!(f, "{:?}", input.type_)?;
}
write!(f, ")")?;
if let Some(output) = &self.output {
Expand Down
2 changes: 1 addition & 1 deletion rustdoc/rustdoc_ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub use callable_path::{
EnumVariantConstructorPath, FreeFunctionPath, InherentMethodPath, StructLiteralPath,
TraitMethodPath,
};
pub use function_pointer::FunctionPointer;
pub use function_pointer::{FunctionPointer, FunctionPointerInput};
pub use generic::Generic;
pub use generic_argument::{GenericArgument, GenericLifetimeParameter};
pub use lifetime::Lifetime;
Expand Down
5 changes: 4 additions & 1 deletion rustdoc/rustdoc_ir/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,10 @@ impl Type {
write!(buffer, "fn(").unwrap();
let mut inputs = fp.inputs.iter().peekable();
while let Some(input) = inputs.next() {
input.render_into(config, buffer);
if let Some(name) = &input.name {
write!(buffer, "{name}: ").unwrap();
}
input.type_.render_into(config, buffer);
if inputs.peek().is_some() {
write!(buffer, ", ").unwrap();
}
Expand Down
55 changes: 38 additions & 17 deletions rustdoc/rustdoc_ir/src/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use indexmap::{IndexMap, IndexSet};

use crate::generics_equivalence::UnassignedIdGenerator;
use crate::{
Array, FunctionPointer, Generic, GenericArgument, GenericLifetimeParameter, Lifetime,
NamedLifetime, PathType, RawPointer, Slice, Tuple, Type, TypeReference,
Array, FunctionPointer, FunctionPointerInput, Generic, GenericArgument,
GenericLifetimeParameter, Lifetime, NamedLifetime, PathType, RawPointer, Slice, Tuple, Type,
TypeReference,
};

/// A `Type` with canonicalized names for lifetimes and unassigned generic type parameters.
Expand Down Expand Up @@ -115,7 +116,10 @@ impl Type {
inputs: fp
.inputs
.iter()
.map(|t| t.bind_generic_type_parameters(bindings))
.map(|input| FunctionPointerInput {
name: input.name.clone(),
type_: input.type_.bind_generic_type_parameters(bindings),
})
.collect(),
output: fp
.output
Expand Down Expand Up @@ -154,7 +158,7 @@ impl Type {
Type::Array(a) => a.element_type.is_a_template(),
Type::RawPointer(r) => r.inner.is_a_template(),
Type::FunctionPointer(fp) => {
fp.inputs.iter().any(|t| t.is_a_template())
fp.inputs.iter().any(|input| input.type_.is_a_template())
|| fp.output.as_ref().is_some_and(|t| t.is_a_template())
}
Type::Generic(_) => true,
Expand Down Expand Up @@ -194,7 +198,7 @@ impl Type {
Type::RawPointer(r) => r.inner._unassigned_generic_type_parameters(set),
Type::FunctionPointer(fp) => {
for input in &fp.inputs {
input._unassigned_generic_type_parameters(set);
input.type_._unassigned_generic_type_parameters(set);
}
if let Some(output) = &fp.output {
output._unassigned_generic_type_parameters(set);
Expand Down Expand Up @@ -283,7 +287,11 @@ impl Type {
.inputs
.iter()
.zip(templated_fp.inputs.iter())
.all(|(concrete, templated)| templated._is_a_template_for(concrete, bindings));
.all(|(concrete, templated)| {
templated
.type_
._is_a_template_for(&concrete.type_, bindings)
});
if !inputs_match {
return false;
}
Expand Down Expand Up @@ -385,11 +393,15 @@ impl Type {
if self_fp.inputs.len() != other_fp.inputs.len() {
return false;
}
let inputs_match = self_fp
.inputs
.iter()
.zip(other_fp.inputs.iter())
.all(|(s, o)| s._is_equivalent_to(o, self_id_gen, other_id_gen));
let inputs_match =
self_fp
.inputs
.iter()
.zip(other_fp.inputs.iter())
.all(|(s, o)| {
s.type_
._is_equivalent_to(&o.type_, self_id_gen, other_id_gen)
});
if !inputs_match {
return false;
}
Expand Down Expand Up @@ -445,7 +457,7 @@ impl Type {
Type::FunctionPointer(fp) => {
fp.inputs
.iter()
.any(|t| t.has_implicit_lifetime_parameters())
.any(|input| input.type_.has_implicit_lifetime_parameters())
|| fp
.output
.as_ref()
Expand Down Expand Up @@ -495,7 +507,9 @@ impl Type {
Type::RawPointer(r) => r.inner.set_implicit_lifetimes(inferred_lifetime),
Type::FunctionPointer(fp) => {
for input in fp.inputs.iter_mut() {
input.set_implicit_lifetimes(inferred_lifetime.clone());
input
.type_
.set_implicit_lifetimes(inferred_lifetime.clone());
}
if let Some(output) = fp.output.as_mut() {
output.set_implicit_lifetimes(inferred_lifetime);
Expand Down Expand Up @@ -553,7 +567,7 @@ impl Type {
}
Type::FunctionPointer(fp) => {
for input in fp.inputs.iter_mut() {
input.rename_lifetime_parameters(original2renamed);
input.type_.rename_lifetime_parameters(original2renamed);
}
if let Some(output) = fp.output.as_mut() {
output.rename_lifetime_parameters(original2renamed);
Expand Down Expand Up @@ -598,7 +612,7 @@ impl Type {
Type::RawPointer(r) => r.inner._lifetime_parameters(set),
Type::FunctionPointer(fp) => {
for input in &fp.inputs {
input._lifetime_parameters(set);
input.type_._lifetime_parameters(set);
}
if let Some(output) = &fp.output {
output._lifetime_parameters(set);
Expand Down Expand Up @@ -651,7 +665,7 @@ impl Type {
Type::RawPointer(r) => r.inner._named_lifetime_parameters(set),
Type::FunctionPointer(fp) => {
for input in &fp.inputs {
input._named_lifetime_parameters(set);
input.type_._named_lifetime_parameters(set);
}
if let Some(output) = &fp.output {
output._named_lifetime_parameters(set);
Expand Down Expand Up @@ -810,7 +824,14 @@ impl Type {
inputs: fp
.inputs
.iter()
.map(|t| t._canonicalize(lifetime_counter, generic_counter, generic_name_map))
.map(|input| FunctionPointerInput {
name: None,
type_: input.type_._canonicalize(
lifetime_counter,
generic_counter,
generic_name_map,
),
})
.collect(),
output: fp.output.as_ref().map(|t| {
Box::new(t._canonicalize(lifetime_counter, generic_counter, generic_name_map))
Expand Down
16 changes: 12 additions & 4 deletions rustdoc/rustdoc_resolver/src/resolve_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use once_cell::sync::OnceCell;
use rustdoc_types::{GenericArg, GenericArgs, GenericParamDefKind, ItemEnum, Type as RustdocType};

use rustdoc_ir::{
Array, FunctionPointer, Generic, GenericArgument, GenericLifetimeParameter, PathType,
RawPointer, Slice, Tuple, Type, TypeReference,
Array, FunctionPointer, FunctionPointerInput, Generic, GenericArgument,
GenericLifetimeParameter, PathType, RawPointer, Slice, Tuple, Type, TypeReference,
};
use rustdoc_processor::CrateCollection;
use rustdoc_processor::indexing::CrateIndexer;
Expand Down Expand Up @@ -516,8 +516,8 @@ fn _resolve_type<I: CrateIndexer>(
.inputs
.iter()
.enumerate()
.map(|(i, (_, ty))| {
resolve_type(
.map(|(i, (name, ty))| {
let resolved_ty = resolve_type(
ty,
used_by_package_id,
krate_collection,
Expand All @@ -531,6 +531,14 @@ fn _resolve_type<I: CrateIndexer>(
source,
},
))
})?;
let name = match name.as_str() {
"" | "_" => None,
s => Some(s.to_owned()),
};
Ok(FunctionPointerInput {
name,
type_: resolved_ty,
})
})
.collect::<Result<Vec<_>, _>>()?;
Expand Down
Loading