Skip to content

Commit af6ace6

Browse files
committed
convert custom try macro to ?
resolves rust-lang#60580
1 parent c7fcbfb commit af6ace6

File tree

2 files changed

+31
-45
lines changed

2 files changed

+31
-45
lines changed

src/libstd/sys/redox/process.rs

+21-28
Original file line numberDiff line numberDiff line change
@@ -254,44 +254,37 @@ impl Command {
254254
// have the drop glue anyway because this code never returns (the
255255
// child will either exec() or invoke syscall::exit)
256256
unsafe fn do_exec(&mut self, stdio: ChildPipes) -> io::Error {
257-
macro_rules! t {
258-
($e:expr) => (match $e {
259-
Ok(e) => e,
260-
Err(e) => return e,
261-
})
262-
}
263-
264257
if let Some(fd) = stdio.stderr.fd() {
265-
t!(cvt(syscall::dup2(fd, 2, &[])));
266-
let mut flags = t!(cvt(syscall::fcntl(2, syscall::F_GETFD, 0)));
258+
cvt(syscall::dup2(fd, 2, &[]))?;
259+
let mut flags = cvt(syscall::fcntl(2, syscall::F_GETFD, 0))?;
267260
flags &= ! syscall::O_CLOEXEC;
268-
t!(cvt(syscall::fcntl(2, syscall::F_SETFD, flags)));
261+
cvt(syscall::fcntl(2, syscall::F_SETFD, flags))?;
269262
}
270263
if let Some(fd) = stdio.stdout.fd() {
271-
t!(cvt(syscall::dup2(fd, 1, &[])));
272-
let mut flags = t!(cvt(syscall::fcntl(1, syscall::F_GETFD, 0)));
264+
cvt(syscall::dup2(fd, 1, &[]))?;
265+
let mut flags = cvt(syscall::fcntl(1, syscall::F_GETFD, 0))?;
273266
flags &= ! syscall::O_CLOEXEC;
274-
t!(cvt(syscall::fcntl(1, syscall::F_SETFD, flags)));
267+
cvt(syscall::fcntl(1, syscall::F_SETFD, flags))?;
275268
}
276269
if let Some(fd) = stdio.stdin.fd() {
277-
t!(cvt(syscall::dup2(fd, 0, &[])));
278-
let mut flags = t!(cvt(syscall::fcntl(0, syscall::F_GETFD, 0)));
270+
cvt(syscall::dup2(fd, 0, &[]))?;
271+
let mut flags = cvt(syscall::fcntl(0, syscall::F_GETFD, 0))?;
279272
flags &= ! syscall::O_CLOEXEC;
280-
t!(cvt(syscall::fcntl(0, syscall::F_SETFD, flags)));
273+
cvt(syscall::fcntl(0, syscall::F_SETFD, flags))?;
281274
}
282275

283276
if let Some(g) = self.gid {
284-
t!(cvt(syscall::setregid(g as usize, g as usize)));
277+
cvt(syscall::setregid(g as usize, g as usize))?;
285278
}
286279
if let Some(u) = self.uid {
287-
t!(cvt(syscall::setreuid(u as usize, u as usize)));
280+
cvt(syscall::setreuid(u as usize, u as usize))?;
288281
}
289282
if let Some(ref cwd) = self.cwd {
290-
t!(cvt(syscall::chdir(cwd)));
283+
cvt(syscall::chdir(cwd))?;
291284
}
292285

293286
for callback in self.closures.iter_mut() {
294-
t!(callback());
287+
callback()?;
295288
}
296289

297290
self.env.apply();
@@ -313,7 +306,7 @@ impl Command {
313306
};
314307

315308
let mut file = if let Some(program) = program {
316-
t!(File::open(program.as_os_str()))
309+
File::open(program.as_os_str())?
317310
} else {
318311
return io::Error::from_raw_os_error(syscall::ENOENT);
319312
};
@@ -327,7 +320,7 @@ impl Command {
327320
let mut shebang = [0; 2];
328321
let mut read = 0;
329322
loop {
330-
match t!(reader.read(&mut shebang[read..])) {
323+
match reader.read(&mut shebang[read..])? {
331324
0 => break,
332325
n => read += n,
333326
}
@@ -338,9 +331,9 @@ impl Command {
338331
// First of all, since we'll be passing another file to
339332
// fexec(), we need to manually check that we have permission
340333
// to execute this file:
341-
let uid = t!(cvt(syscall::getuid()));
342-
let gid = t!(cvt(syscall::getgid()));
343-
let meta = t!(file.metadata());
334+
let uid = cvt(syscall::getuid())?;
335+
let gid = cvt(syscall::getgid())?;
336+
let meta = file.metadata()?;
344337

345338
let mode = if uid == meta.uid() as usize {
346339
meta.mode() >> 3*2 & 0o7
@@ -355,7 +348,7 @@ impl Command {
355348

356349
// Second of all, we need to actually read which interpreter it wants
357350
let mut interpreter = Vec::new();
358-
t!(reader.read_until(b'\n', &mut interpreter));
351+
reader.read_until(b'\n', &mut interpreter)?;
359352
// Pop one trailing newline, if any
360353
if interpreter.ends_with(&[b'\n']) {
361354
interpreter.pop().unwrap();
@@ -373,11 +366,11 @@ impl Command {
373366
};
374367
if let Some(ref interpreter) = interpreter {
375368
let path: &OsStr = OsStr::from_bytes(&interpreter);
376-
file = t!(File::open(path));
369+
file = File::open(path)?;
377370

378371
args.push([interpreter.as_ptr() as usize, interpreter.len()]);
379372
} else {
380-
t!(file.seek(SeekFrom::Start(0)));
373+
file.seek(SeekFrom::Start(0))?;
381374
}
382375

383376
args.push([self.program.as_ptr() as usize, self.program.len()]);

src/libstd/sys/unix/process/process_unix.rs

+10-17
Original file line numberDiff line numberDiff line change
@@ -167,26 +167,19 @@ impl Command {
167167
) -> io::Error {
168168
use crate::sys::{self, cvt_r};
169169

170-
macro_rules! t {
171-
($e:expr) => (match $e {
172-
Ok(e) => e,
173-
Err(e) => return e,
174-
})
175-
}
176-
177170
if let Some(fd) = stdio.stdin.fd() {
178-
t!(cvt_r(|| libc::dup2(fd, libc::STDIN_FILENO)));
171+
cvt_r(|| libc::dup2(fd, libc::STDIN_FILENO))?;
179172
}
180173
if let Some(fd) = stdio.stdout.fd() {
181-
t!(cvt_r(|| libc::dup2(fd, libc::STDOUT_FILENO)));
174+
cvt_r(|| libc::dup2(fd, libc::STDOUT_FILENO))?;
182175
}
183176
if let Some(fd) = stdio.stderr.fd() {
184-
t!(cvt_r(|| libc::dup2(fd, libc::STDERR_FILENO)));
177+
cvt_r(|| libc::dup2(fd, libc::STDERR_FILENO))?;
185178
}
186179

187180
if cfg!(not(any(target_os = "l4re"))) {
188181
if let Some(u) = self.get_gid() {
189-
t!(cvt(libc::setgid(u as gid_t)));
182+
cvt(libc::setgid(u as gid_t))?;
190183
}
191184
if let Some(u) = self.get_uid() {
192185
// When dropping privileges from root, the `setgroups` call
@@ -198,11 +191,11 @@ impl Command {
198191
// privilege dropping function.
199192
let _ = libc::setgroups(0, ptr::null());
200193

201-
t!(cvt(libc::setuid(u as uid_t)));
194+
cvt(libc::setuid(u as uid_t))?;
202195
}
203196
}
204197
if let Some(ref cwd) = *self.get_cwd() {
205-
t!(cvt(libc::chdir(cwd.as_ptr())));
198+
cvt(libc::chdir(cwd.as_ptr()))?;
206199
}
207200

208201
// emscripten has no signal support.
@@ -225,18 +218,18 @@ impl Command {
225218
0,
226219
mem::size_of::<libc::sigset_t>());
227220
} else {
228-
t!(cvt(libc::sigemptyset(&mut set)));
221+
cvt(libc::sigemptyset(&mut set))?;
229222
}
230-
t!(cvt(libc::pthread_sigmask(libc::SIG_SETMASK, &set,
231-
ptr::null_mut())));
223+
cvt(libc::pthread_sigmask(libc::SIG_SETMASK, &set,
224+
ptr::null_mut()))?;
232225
let ret = sys::signal(libc::SIGPIPE, libc::SIG_DFL);
233226
if ret == libc::SIG_ERR {
234227
return io::Error::last_os_error()
235228
}
236229
}
237230

238231
for callback in self.get_closures().iter_mut() {
239-
t!(callback());
232+
callback()?;
240233
}
241234

242235
// Although we're performing an exec here we may also return with an

0 commit comments

Comments
 (0)