Skip to content

Commit 8f7872b

Browse files
committed
Refactor: Subsume different function call cases in a single one.
It makes more sense to have one handler per enum variant.
1 parent 6d5bb00 commit 8f7872b

File tree

2 files changed

+21
-32
lines changed

2 files changed

+21
-32
lines changed

src/translator/function_call.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ pub type FunctionPlaces = (PlaceRef, PlaceRef, Option<PlaceRef>);
99

1010
/// Types of function calls that the translator supports.
1111
pub enum FunctionCall {
12-
/// Call to `std::sync::Arc::<T>::new`
13-
/// Non-recursive call for the translation process.
14-
ArcNew,
15-
/// Call to `std::clone::Clone::clone`
16-
/// Non-recursive call for the translation process.
17-
Clone,
1812
/// Call to `std::sync::Condvar::new`
1913
/// Non-recursive call for the translation process.
2014
CondVarNew,
@@ -24,15 +18,12 @@ pub enum FunctionCall {
2418
/// Call to `std::sync::Condvar::wait`
2519
/// Non-recursive call for the translation process.
2620
CondVarWait,
27-
/// Call to `std::ops::Deref::deref`
28-
/// Non-recursive call for the translation process.
29-
Deref,
30-
/// Call to `std::ops::DerefMut::deref_mut`
31-
/// Non-recursive call for the translation process.
32-
DerefMut,
3321
/// Abridged function call.
3422
/// Non-recursive call for the translation process.
3523
Foreign,
24+
/// Abridged function call that involves a synchronization primitive.
25+
/// Non-recursive call for the translation process.
26+
ForeignWithSyncPrimitive,
3627
/// MIR function call (the "default" case).
3728
/// Recursive call for the translation process.
3829
MirFunction,
@@ -48,9 +39,6 @@ pub enum FunctionCall {
4839
/// Call to `std::thread::spawn`.
4940
/// Non-recursive call for the translation process.
5041
ThreadSpawn,
51-
/// Call to `std::result::Result::<T, E>::unwrap`.
52-
/// Non-recursive call for the translation process.
53-
Unwrap,
5442
}
5543

5644
impl FunctionCall {
@@ -74,11 +62,11 @@ impl FunctionCall {
7462
/// Returns the corresponding variant for the function or `None` otherwise.
7563
fn is_supported_function(function_name: &str) -> Option<Self> {
7664
match function_name {
77-
"std::clone::Clone::clone" => Some(Self::Clone),
78-
"std::ops::Deref::deref" => Some(Self::Deref),
79-
"std::ops::DerefMut::deref_mut" => Some(Self::DerefMut),
80-
"std::result::Result::<T, E>::unwrap" => Some(Self::Unwrap),
81-
"std::sync::Arc::<T>::new" => Some(Self::ArcNew),
65+
"std::clone::Clone::clone"
66+
| "std::ops::Deref::deref"
67+
| "std::ops::DerefMut::deref_mut"
68+
| "std::result::Result::<T, E>::unwrap"
69+
| "std::sync::Arc::<T>::new" => Some(Self::ForeignWithSyncPrimitive),
8270
"std::sync::Condvar::new" => Some(Self::CondVarNew),
8371
"std::sync::Condvar::notify_one" => Some(Self::CondVarNotifyOne),
8472
"std::sync::Condvar::wait" => Some(Self::CondVarWait),

src/translator/function_call_handler.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ impl<'tcx> Translator<'tcx> {
2222
let function_name = self.tcx.def_path_str(function_def_id);
2323

2424
match function_call {
25-
FunctionCall::ArcNew
26-
| FunctionCall::Clone
27-
| FunctionCall::Deref
28-
| FunctionCall::DerefMut
29-
| FunctionCall::Unwrap => {
30-
self.call_sync_primitive(&function_name, args, destination, &function_call_places);
25+
FunctionCall::ForeignWithSyncPrimitive => {
26+
self.call_foreign_function_with_sync_primitive(
27+
&function_name,
28+
args,
29+
destination,
30+
&function_call_places,
31+
);
3132
}
3233
FunctionCall::CondVarNew => {
3334
self.call_condvar_new(&function_name, destination, &function_call_places);
@@ -86,12 +87,12 @@ impl<'tcx> Translator<'tcx> {
8687
)
8788
}
8889

89-
/// Handler for the case `FunctionCall::ArcNew`.
90-
/// Handler for the case `FunctionCall::Clone`.
91-
/// Handler for the case `FunctionCall::Deref`.
92-
/// Handler for the case `FunctionCall::DerefMut`.
93-
/// Handler for the case `FunctionCall::Unwrap`.
94-
fn call_sync_primitive(
90+
/// Handler for the case `FunctionCall::ForeignWithSyncPrimitive`.
91+
/// It is an extension of `call_foreign_function` that performs a check
92+
/// to keep track of synchronization primitives.
93+
/// The goal is to link the first argument of the function to its return value
94+
/// in case the first argument is a mutex, lock guard, join handle or condition variable.
95+
fn call_foreign_function_with_sync_primitive(
9596
&mut self,
9697
function_name: &str,
9798
args: &[rustc_middle::mir::Operand<'tcx>],

0 commit comments

Comments
 (0)