Skip to content

Commit 2e6c5c7

Browse files
refactor: Transform Callable into an enum
1 parent b9a764b commit 2e6c5c7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+621
-577
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ fn must_be_cloneable(
143143
"I can't generate code that will pass the borrow checker *and* match the \
144144
instructions in your blueprint.\n\
145145
`{}` consumes `{type_}` by value, but `{type_}` is a singleton and can't be moved out of `ApplicationState`.",
146-
consumer_callable.path,
146+
consumer_callable,
147147
);
148148
let help = is_clone
149149
.then(|| format!("Allow Pavex to clone `{type_}` by marking it `{clone_if_necessary}`.",));
150150
let second_help = format!(
151151
"Can `{}` take a reference to `{type_}`, rather than consuming it by value?",
152-
consumer_callable.path,
152+
consumer_callable,
153153
);
154154
let diagnostic = CompilerDiagnostic::builder(e)
155155
.optional_source(source)

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

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use super::{
2020
use crate::{
2121
compiler::app::GENERATED_APP_PACKAGE_ID,
2222
language::{
23-
Callable, CallableInput, GenericArgument, InvocationStyle, ParameterName, PathTypeExt,
24-
Type,
23+
Callable, CallableInput, CallableMetadata, GenericArgument, ParameterName, PathTypeExt,
24+
StructLiteralInit, Type,
2525
},
2626
rustdoc::CrateCollection,
2727
};
@@ -188,30 +188,24 @@ impl ApplicationState {
188188
// that builds the dependency graph.
189189
let ty_ = self.type_();
190190

191-
Callable {
192-
is_async: false,
193-
takes_self_as_ref: false,
191+
Callable::StructLiteralInit(StructLiteralInit {
194192
path: ty_.callable_struct_literal_path(),
195-
output: Some(ty_.into()),
196-
inputs: {
197-
// Ensure that the inputs are sorted by name.
198-
let b = self.bindings.iter().collect::<BTreeMap<_, _>>();
199-
b.into_iter()
200-
.map(|(name, type_)| CallableInput {
201-
name: ParameterName::new(name.to_string()),
202-
type_: type_.clone(),
203-
})
204-
.collect()
193+
metadata: CallableMetadata {
194+
output: Some(ty_.into()),
195+
inputs: {
196+
// Ensure that the inputs are sorted by name.
197+
let b = self.bindings.iter().collect::<BTreeMap<_, _>>();
198+
b.into_iter()
199+
.map(|(name, type_)| CallableInput {
200+
name: ParameterName::new(name.to_string()),
201+
type_: type_.clone(),
202+
})
203+
.collect()
204+
},
205+
source_coordinates: None,
205206
},
206-
invocation_style: InvocationStyle::StructLiteral {
207-
extra_field2default_value: Default::default(),
208-
},
209-
source_coordinates: None,
210-
abi: rustdoc_types::Abi::Rust,
211-
is_unsafe: false,
212-
is_c_variadic: false,
213-
symbol_name: None,
214-
}
207+
extra_field2default_value: Default::default(),
208+
})
215209
}
216210

217211
/// Return a bi-directional map between field names and their types.

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

Lines changed: 44 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use crate::compiler::analyses::framework_items::FrameworkItemDb;
2323
use crate::compiler::app::GENERATED_APP_PACKAGE_ID;
2424
use crate::compiler::computation::{Computation, MatchResultVariant};
2525
use crate::language::{
26-
Callable, CallableInput, CallablePath, EnumVariantConstructorPath, GenericArgument,
27-
InvocationStyle, ParameterName, PathType, Type,
26+
Callable, CallableInput, CallableMetadata, EnumVariantConstructorPath, EnumVariantInit,
27+
GenericArgument, ParameterName, PathType, Type,
2828
};
2929
use crate::rustdoc::{CORE_PACKAGE_ID_REPR, CrateCollection};
3030

@@ -148,48 +148,38 @@ pub(crate) fn application_state_call_graph(
148148
};
149149
// We need to add an `Ok` wrap around `ApplicationState`, since we are returning a `Result`.
150150
let ok_wrapper = {
151-
Callable {
152-
is_async: false,
153-
takes_self_as_ref: false,
154-
output: Some(application_state_result.clone().into()),
155-
path: CallablePath::EnumVariantConstructor(EnumVariantConstructorPath {
151+
Callable::EnumVariantInit(EnumVariantInit {
152+
path: EnumVariantConstructorPath {
156153
package_id: PackageId::new(CORE_PACKAGE_ID_REPR),
157154
crate_name: "core".into(),
158155
module_path: vec!["result".into()],
159156
enum_name: "Result".into(),
160157
enum_generics: vec![],
161158
variant_name: "Ok".into(),
162-
}),
163-
inputs: vec![CallableInput { name: ParameterName::new("_0".into()), type_: application_state.type_().into() }],
164-
invocation_style: InvocationStyle::FunctionCall,
165-
source_coordinates: None,
166-
abi: rustdoc_types::Abi::Rust,
167-
is_unsafe: false,
168-
is_c_variadic: false,
169-
symbol_name: None,
170-
}
159+
},
160+
metadata: CallableMetadata {
161+
output: Some(application_state_result.clone().into()),
162+
inputs: vec![CallableInput { name: ParameterName::new("_0".into()), type_: application_state.type_().into() }],
163+
source_coordinates: None,
164+
},
165+
})
171166
};
172167
let err_wrapper = {
173-
Callable {
174-
is_async: false,
175-
takes_self_as_ref: false,
176-
output: Some(application_state_result.into()),
177-
path: CallablePath::EnumVariantConstructor(EnumVariantConstructorPath {
168+
Callable::EnumVariantInit(EnumVariantInit {
169+
path: EnumVariantConstructorPath {
178170
package_id: PackageId::new(CORE_PACKAGE_ID_REPR),
179171
crate_name: "core".into(),
180172
module_path: vec!["result".into()],
181173
enum_name: "Result".into(),
182174
enum_generics: vec![],
183175
variant_name: "Err".into(),
184-
}),
185-
inputs: vec![CallableInput { name: ParameterName::new("_0".into()), type_: error_enum.clone().into() }],
186-
invocation_style: InvocationStyle::FunctionCall,
187-
source_coordinates: None,
188-
abi: rustdoc_types::Abi::Rust,
189-
is_unsafe: false,
190-
is_c_variadic: false,
191-
symbol_name: None,
192-
}
176+
},
177+
metadata: CallableMetadata {
178+
output: Some(application_state_result.into()),
179+
inputs: vec![CallableInput { name: ParameterName::new("_0".into()), type_: error_enum.clone().into() }],
180+
source_coordinates: None,
181+
},
182+
})
193183
};
194184
component_db.get_or_intern_transformer(
195185
computation_db.get_or_intern(ok_wrapper),
@@ -207,7 +197,7 @@ pub(crate) fn application_state_call_graph(
207197
for err_match_id in err_match_ids {
208198
let fallible_id = component_db.fallible_id(*err_match_id);
209199
let fallible = component_db.hydrated_component(fallible_id, computation_db);
210-
let fallible_callable = match &fallible {
200+
let fallible_callable: &Callable = match &fallible {
211201
HydratedComponent::Constructor(c) => {
212202
let Computation::Callable(c) = &c.0 else {
213203
unreachable!()
@@ -225,32 +215,32 @@ pub(crate) fn application_state_call_graph(
225215
unreachable!()
226216
}
227217
};
228-
let error_type_name = match &fallible_callable.path {
229-
CallablePath::InherentMethod(p) => {
218+
let error_type_name = match fallible_callable {
219+
Callable::InherentMethod(m) => {
230220
format!(
231221
"{}{}",
232-
p.type_name,
233-
p.method_name.to_case(Case::Pascal)
222+
m.path.type_name,
223+
m.path.method_name.to_case(Case::Pascal)
234224
)
235225
}
236-
CallablePath::TraitMethod(p) => {
226+
Callable::TraitMethod(m) => {
237227
format!(
238228
"{}{}",
239-
p.trait_name,
240-
p.method_name.to_case(Case::Pascal)
229+
m.path.trait_name,
230+
m.path.method_name.to_case(Case::Pascal)
241231
)
242232
}
243-
CallablePath::FreeFunction(p) => {
244-
p.function_name.to_case(Case::Pascal)
233+
Callable::FreeFunction(f) => {
234+
f.path.function_name.to_case(Case::Pascal)
245235
}
246-
CallablePath::StructLiteral(p) => {
247-
p.type_name.to_case(Case::Pascal)
236+
Callable::StructLiteralInit(s) => {
237+
s.path.type_name.to_case(Case::Pascal)
248238
}
249-
CallablePath::EnumVariantConstructor(p) => {
239+
Callable::EnumVariantInit(e) => {
250240
format!(
251241
"{}{}",
252-
p.enum_name,
253-
p.variant_name.to_case(Case::Pascal)
242+
e.path.enum_name,
243+
e.path.variant_name.to_case(Case::Pascal)
254244
)
255245
}
256246
};
@@ -262,26 +252,21 @@ pub(crate) fn application_state_call_graph(
262252
};
263253
error_variants.insert(error_type_name.clone(), error_type.clone());
264254
*n_duplicates += 1;
265-
let error_variant_constructor = Callable {
266-
is_async: false,
267-
takes_self_as_ref: false,
268-
path: CallablePath::EnumVariantConstructor(EnumVariantConstructorPath {
255+
let error_variant_constructor = Callable::EnumVariantInit(EnumVariantInit {
256+
path: EnumVariantConstructorPath {
269257
package_id: PackageId::new(GENERATED_APP_PACKAGE_ID),
270258
crate_name: "crate".into(),
271259
module_path: vec![],
272260
enum_name: "ApplicationStateError".into(),
273261
enum_generics: vec![],
274262
variant_name: error_type_name.to_owned(),
275-
}),
276-
output: Some(error_enum.clone().into()),
277-
inputs: vec![CallableInput { name: ParameterName::new("_0".into()), type_: error_type.to_owned() }],
278-
invocation_style: InvocationStyle::FunctionCall,
279-
source_coordinates: None,
280-
abi: rustdoc_types::Abi::Rust,
281-
is_unsafe: false,
282-
is_c_variadic: false,
283-
symbol_name: None,
284-
};
263+
},
264+
metadata: CallableMetadata {
265+
output: Some(error_enum.clone().into()),
266+
inputs: vec![CallableInput { name: ParameterName::new("_0".into()), type_: error_type.to_owned() }],
267+
source_coordinates: None,
268+
},
269+
});
285270
let transformer_id = component_db.get_or_intern_transformer(
286271
computation_db.get_or_intern(error_variant_constructor.clone()),
287272
*err_match_id,

compiler/pavexc/src/compiler/analyses/call_graph/borrow_checker/clone.rs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use crate::compiler::analyses::user_components::ScopeId;
1313
use crate::compiler::computation::Computation;
1414
use crate::compiler::utils::resolve_type_path;
1515
use crate::language::{
16-
Callable, CallableInput, CallablePath, InvocationStyle, Lifetime, ParameterName, PathType,
17-
TraitMethodPath, Type, TypeReference,
16+
Callable, CallableInput, CallableMetadata, FnHeader, Lifetime, ParameterName, PathType,
17+
TraitMethod, TraitMethodPath, Type, TypeReference,
1818
};
1919
use crate::rustdoc::CrateCollection;
2020

@@ -60,11 +60,8 @@ pub(super) fn get_clone_component_id(
6060
vec![]
6161
};
6262

63-
let clone_callable = Callable {
64-
is_async: false,
65-
takes_self_as_ref: true,
66-
output: Some(output.clone()),
67-
path: CallablePath::TraitMethod(TraitMethodPath {
63+
let clone_callable = Callable::TraitMethod(TraitMethod {
64+
path: TraitMethodPath {
6865
package_id: clone.package_id.clone(),
6966
crate_name,
7067
module_path,
@@ -73,22 +70,28 @@ pub(super) fn get_clone_component_id(
7370
self_type: output.clone().into(),
7471
method_name: "clone".into(),
7572
method_generics: vec![],
76-
}),
77-
inputs: vec![CallableInput {
78-
name: ParameterName::new("_0".into()),
79-
type_: Type::Reference(TypeReference {
80-
is_mutable: false,
81-
lifetime: Lifetime::Elided,
82-
inner: Box::new(output),
83-
}),
84-
}],
85-
invocation_style: InvocationStyle::FunctionCall,
86-
source_coordinates: None,
87-
abi: rustdoc_types::Abi::Rust,
88-
is_unsafe: false,
89-
is_c_variadic: false,
90-
symbol_name: None,
91-
};
73+
},
74+
metadata: CallableMetadata {
75+
output: Some(output.clone()),
76+
inputs: vec![CallableInput {
77+
name: ParameterName::new("_0".into()),
78+
type_: Type::Reference(TypeReference {
79+
is_mutable: false,
80+
lifetime: Lifetime::Elided,
81+
inner: Box::new(output),
82+
}),
83+
}],
84+
source_coordinates: None,
85+
},
86+
header: FnHeader {
87+
is_async: false,
88+
abi: rustdoc_types::Abi::Rust,
89+
is_unsafe: false,
90+
is_c_variadic: false,
91+
symbol_name: None,
92+
},
93+
takes_self_as_ref: true,
94+
});
9295

9396
let clone_computation_id =
9497
computation_db.get_or_intern(Computation::Callable(Cow::Owned(clone_callable)));

compiler/pavexc/src/compiler/analyses/call_graph/borrow_checker/complex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ fn emit_borrow_checking_error(
309309
let help_msg = format!(
310310
"Considering changing the signature of `{}`.\n\
311311
It takes `{type_:?}` by value. Would a shared reference, `&{type_:?}`, be enough?",
312-
callable.path
312+
callable
313313
);
314314
let help = HelpWithSnippet::new(help_msg, AnnotatedSource::empty());
315315
diagnostic = diagnostic.help_with_snippet(help);

compiler/pavexc/src/compiler/analyses/call_graph/borrow_checker/move_while_borrowed.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub(super) fn move_while_borrowed(
7575
.inputs_that_output_borrows_immutably_from()
7676
.iter()
7777
.map(|&i| {
78-
let t = &callable.inputs[i].type_;
78+
let t = &callable.inputs()[i].type_;
7979
if let Type::Reference(r) = t {
8080
r.inner.deref().to_owned()
8181
} else {
@@ -87,7 +87,7 @@ pub(super) fn move_while_borrowed(
8787
.inputs_with_lifetime_tied_with_output()
8888
.iter()
8989
.map(|&i| {
90-
let t = &callable.inputs[i].type_;
90+
let t = &callable.inputs()[i].type_;
9191
if let Type::Reference(r) = t {
9292
r.inner.deref().to_owned()
9393
} else {
@@ -388,8 +388,8 @@ fn emit_ancestor_descendant_borrow_error(
388388
let (contended_component_id, contended_type) =
389389
get_component_id_and_type(call_graph, contended_node_id, computation_db, db);
390390

391-
let borrower_path = &borrower_callable.path;
392-
let consumer_path = &consumer_callable.path;
391+
let borrower_path = borrower_callable.to_string();
392+
let consumer_path = consumer_callable.to_string();
393393
let mut error_msg =
394394
"I can't generate code that will pass the borrow checker *and* match the instructions \
395395
in your blueprint:\n"
@@ -513,8 +513,8 @@ fn emit_tried_to_borrow_mut_while_borrowed_immutably(
513513
let (_, contended_type) =
514514
get_component_id_and_type(call_graph, contended_node_id, computation_db, component_db);
515515

516-
let borrower_path = &borrower_callable.path;
517-
let mut_borrower_path = &mut_borrower_callable.path;
516+
let borrower_path = borrower_callable.to_string();
517+
let mut_borrower_path = mut_borrower_callable.to_string();
518518
let mut error_msg =
519519
"I can't generate code that will pass the borrow checker *and* match the instructions \
520520
in your blueprint:\n"

compiler/pavexc/src/compiler/analyses/call_graph/borrow_checker/multiple_consumers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ fn emit_multiple_consumers_error(
239239
};
240240
let user_id = db.user_component_id(component_id);
241241

242-
write!(&mut error_msg, "- `{}`", callable.path).unwrap();
242+
write!(&mut error_msg, "- `{}`", callable).unwrap();
243243
match user_id {
244244
None => writeln!(&mut error_msg),
245245
Some(user_component_id) => {

0 commit comments

Comments
 (0)