Skip to content

Commit d04ec91

Browse files
committed
Convert defer! to scopeguard::guard
Captures are being miscompiled, at least on MSVC. Using scopeguard::guard with explicit argument threading seems to work.
1 parent bc0e31e commit d04ec91

File tree

7 files changed

+10
-13
lines changed

7 files changed

+10
-13
lines changed

src/multirust-cli/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ extern crate openssl;
1313
extern crate itertools;
1414
extern crate time;
1515
extern crate rand;
16-
#[macro_use]
1716
extern crate scopeguard;
1817
extern crate tempdir;
1918

src/multirust-cli/self_update.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use std::path::{Path, PathBuf};
4343
use std::process::{self, Command};
4444
use std::fs;
4545
use tempdir::TempDir;
46+
use scopeguard;
4647

4748
// The big installation messages. These are macros because the first
4849
// argument of format! needs to be a literal.
@@ -475,7 +476,7 @@ fn delete_multirust_and_cargo_home() -> Result<()> {
475476
return Err(Error::WindowsUninstallMadness(err));
476477
}
477478

478-
defer!{{ let _ = CloseHandle(gc_handle); }}
479+
let _g = scopeguard::guard(gc_handle, |h| { let _ = CloseHandle(*h); });
479480

480481
try!(Command::new(gc_exe).spawn()
481482
.map_err(|e| Error::WindowsUninstallMadness(e)));
@@ -537,7 +538,7 @@ fn wait_for_parent() -> Result<()> {
537538
return Err(Error::WindowsUninstallMadness(err));
538539
}
539540

540-
defer! {{ let _ = CloseHandle(snapshot); }}
541+
let _g = scopeguard::guard(snapshot, |h| { let _ = CloseHandle(*h); });
541542

542543
let mut entry: PROCESSENTRY32 = mem::zeroed();
543544
entry.dwSize = mem::size_of::<PROCESSENTRY32>() as DWORD;
@@ -570,7 +571,7 @@ fn wait_for_parent() -> Result<()> {
570571
return Ok(());
571572
}
572573

573-
defer! {{ let _ = CloseHandle(parent); }}
574+
let _g = scopeguard::guard(parent, |h| { let _ = CloseHandle(*h); });
574575

575576
// Wait for our parent to exit
576577
let res = WaitForSingleObject(parent, INFINITE);

src/multirust-mock/src/clitools.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use dist::{MockDistServer, MockChannel, MockPackage,
1414
MockTargettedPackage, MockComponent, change_channel_date,
1515
ManifestVersion};
1616
use hyper::Url;
17+
use scopeguard;
1718

1819
/// The configuration used by the tests in this module
1920
pub struct Config {
@@ -232,9 +233,7 @@ pub fn run(config: &Config, name: &str, args: &[&str], env: &[(&str, &str)]) ->
232233
pub fn change_dir(path: &Path, f: &Fn()) {
233234
let cwd = env::current_dir().unwrap();
234235
env::set_current_dir(path).unwrap();
235-
defer! {
236-
env::set_current_dir(&cwd).unwrap()
237-
}
236+
let _g = scopeguard::guard(cwd, |d| env::set_current_dir(d).unwrap());
238237
f();
239238
}
240239

src/multirust-mock/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
extern crate hyper;
44
#[macro_use]
55
extern crate lazy_static;
6-
#[macro_use]
76
extern crate scopeguard;
87
extern crate walkdir;
98
extern crate flate2;

src/multirust-utils/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
extern crate hyper;
55
extern crate openssl;
66
extern crate rand;
7-
#[macro_use]
87
extern crate scopeguard;
98

109
#[cfg(windows)]

src/multirust-utils/src/utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use errors::{Error, Notification, NotifyHandler};
1212
use raw;
1313
#[cfg(windows)]
1414
use winapi::DWORD;
15+
use scopeguard;
1516

1617
pub use raw::{is_directory, is_file, path_exists, if_not_empty, random_string, prefix_arg,
1718
has_cmd, find_cmd};
@@ -333,7 +334,7 @@ pub fn home_dir() -> Option<PathBuf> {
333334
if OpenProcessToken(me, TOKEN_READ, &mut token) == 0 {
334335
return None
335336
}
336-
defer! {{ let _ = CloseHandle(token); }}
337+
let _g = scopeguard::guard(token, |h| { let _ = CloseHandle(*h); });
337338
fill_utf16_buf(|buf, mut sz| {
338339
match GetUserProfileDirectoryW(token, buf, &mut sz) {
339340
0 if GetLastError() != ERROR_INSUFFICIENT_BUFFER => 0,

tests/cli-self-update.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ extern crate multirust_utils;
55
#[macro_use]
66
extern crate lazy_static;
77
extern crate tempdir;
8-
#[macro_use]
98
extern crate scopeguard;
109

1110
#[cfg(windows)]
@@ -38,8 +37,8 @@ pub fn setup(f: &Fn(&Config)) {
3837

3938
// An windows these tests mess with the user's PATH. Save
4039
// and restore them here to keep from trashing things.
41-
let ref saved_path = get_path();
42-
defer! { restore_path(saved_path) }
40+
let saved_path = get_path();
41+
let _g = scopeguard::guard(saved_path, |p| restore_path(p));
4342

4443
f(config);
4544
});

0 commit comments

Comments
 (0)