Skip to content

Commit 9ae2c01

Browse files
committed
Handle all the cases of extract-n-th-argument in the new way, removing the old function.
This makes error messages clearer.
1 parent 18ca935 commit 9ae2c01

File tree

4 files changed

+16
-34
lines changed

4 files changed

+16
-34
lines changed

src/translator/sync/condvar_manager.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::data_structures::petri_net_interface::{PetriNet, TransitionRef};
1212
use crate::naming::condvar::wait_transition_labels;
1313
use crate::translator::function_call::FunctionPlaces;
1414
use crate::translator::mir_function::Memory;
15-
use crate::utils::extract_nth_argument;
15+
use crate::utils::extract_nth_argument_as_place;
1616
use log::debug;
1717

1818
#[derive(Default)]
@@ -76,13 +76,15 @@ impl CondvarManager {
7676
memory: &mut Memory<'tcx>,
7777
) {
7878
// Retrieve the lock guard from the local variable passed to the function as an argument.
79-
let lock_guard = extract_nth_argument(args, 1);
79+
let lock_guard = extract_nth_argument_as_place(args, 1)
80+
.expect("BUG: `std::sync::Condvar::wait` should receive the first argument as a place");
8081
let mutex_ref = memory.get_linked_lock_guard(&lock_guard);
8182
// Unlock the mutex when waiting, lock it when the waiting ends.
8283
mutex_manager.add_unlock_guard(*mutex_ref, &wait_transitions.0, net);
8384
mutex_manager.add_lock_guard(*mutex_ref, &wait_transitions.1, net);
8485
// Retrieve the condvar from the local variable passed to the function as an argument.
85-
let self_ref = extract_nth_argument(args, 0);
86+
let self_ref = extract_nth_argument_as_place(args, 0)
87+
.expect("BUG: `std::sync::Condvar::wait` should receive the self reference as a place");
8688
let condvar_ref = memory.get_linked_condvar(&self_ref);
8789
self.link_to_wait_call(*condvar_ref, wait_transitions, net);
8890
// The return value contains the lock guard passed to the function. Link the local variable to it.
@@ -101,7 +103,9 @@ impl CondvarManager {
101103
memory: &mut Memory<'tcx>,
102104
) {
103105
// Retrieve the condvar from the local variable passed to the function as an argument.
104-
let self_ref = extract_nth_argument(args, 0);
106+
let self_ref = extract_nth_argument_as_place(args, 0).expect(
107+
"BUG: `std::sync::Condvar::notify_one` should receive the self reference as a place",
108+
);
105109
let condvar_ref = memory.get_linked_condvar(&self_ref);
106110
self.link_to_notify_one_call(*condvar_ref, notify_one_transition, net);
107111
}

src/translator/sync/mutex_manager.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use super::mutex::Mutex;
77
use crate::data_structures::petri_net_interface::{PetriNet, TransitionRef};
88
use crate::translator::mir_function::Memory;
9-
use crate::utils::extract_nth_argument;
9+
use crate::utils::extract_nth_argument_as_place;
1010
use log::debug;
1111

1212
#[derive(Default)]
@@ -53,7 +53,9 @@ impl MutexManager {
5353
memory: &mut Memory<'tcx>,
5454
) {
5555
// Retrieve the mutex from the local variable passed to the function as an argument.
56-
let self_ref = extract_nth_argument(args, 0);
56+
let self_ref = extract_nth_argument_as_place(args, 0).expect(
57+
"BUG: `std::sync::Mutex::<T>::lock` should receive the self reference as a place",
58+
);
5759
let mutex_ref = memory.get_linked_mutex(&self_ref);
5860
self.add_lock_guard(*mutex_ref, transition_function_call, net);
5961
// The return value contains a new lock guard. Link the local variable to it.

src/translator/sync/thread_manager.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::translator::mir_function::{
1212
CondvarEntries, JoinHandleEntries, LockGuardEntries, Memory, MutexEntries,
1313
};
1414
use crate::utils::{
15-
extract_closure, extract_def_id_of_called_function_from_operand, extract_nth_argument,
15+
extract_closure, extract_def_id_of_called_function_from_operand, extract_nth_argument_as_place,
1616
};
1717
use log::{debug, info};
1818
use std::collections::VecDeque;
@@ -78,7 +78,9 @@ impl ThreadManager {
7878
memory: &Memory<'tcx>,
7979
) {
8080
// Retrieve the join handle from the local variable passed to the function as an argument.
81-
let self_ref = extract_nth_argument(args, 0);
81+
let self_ref = extract_nth_argument_as_place(args, 0).expect(
82+
"BUG: `std::thread::JoinHandle::<T>::join` should receive the self reference as a place",
83+
);
8284
let thread_ref = memory.get_linked_join_handle(&self_ref);
8385
self.set_join_transition(*thread_ref, transition_function_call);
8486
}

src/utils.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,32 +43,6 @@ pub fn extract_def_id_of_called_function_from_operand<'tcx>(
4343
}
4444
}
4545

46-
/// Extracts the n-th argument from the arguments for the function call.
47-
/// Returns the place corresponding to that argument.
48-
///
49-
/// This is also useful for obtaining the self reference for method calls.
50-
/// For example: The call `mutex.lock()` desugars to `std::sync::Mutex::lock(&mutex)`
51-
/// where `&self` is the first argument.
52-
///
53-
/// # Panics
54-
///
55-
/// If the argument passed to the function is a constant, then the function panics.
56-
pub fn extract_nth_argument<'tcx>(
57-
args: &[rustc_middle::mir::Operand<'tcx>],
58-
index: usize,
59-
) -> rustc_middle::mir::Place<'tcx> {
60-
let operand = args
61-
.get(index)
62-
.expect("BUG: Function should receive at least `index` arguments");
63-
64-
match operand {
65-
rustc_middle::mir::Operand::Move(place) | rustc_middle::mir::Operand::Copy(place) => *place,
66-
rustc_middle::mir::Operand::Constant(_) => {
67-
panic!("BUG: Function should not receive arguments passed as constants");
68-
}
69-
}
70-
}
71-
7246
/// Extracts the n-th argument from the arguments for the function call.
7347
/// Returns the place corresponding to that argument.
7448
///

0 commit comments

Comments
 (0)