@@ -278,50 +278,48 @@ impl<'a> Toolchain<'a> {
278
278
/// Infallible function that describes the version of rustc in an installed distribution
279
279
#[ cfg_attr( feature = "otel" , tracing:: instrument) ]
280
280
pub fn rustc_version ( & self ) -> String {
281
- // TODO: use create_command instead of manual construction!
282
- let rustc_path = self . binary_file ( "rustc" ) ;
283
- if utils:: is_file ( & rustc_path) {
284
- let mut cmd = Command :: new ( & rustc_path) ;
285
- cmd. arg ( "--version" ) ;
286
- cmd. stdin ( Stdio :: null ( ) ) ;
287
- cmd. stdout ( Stdio :: piped ( ) ) ;
288
- cmd. stderr ( Stdio :: piped ( ) ) ;
289
- self . set_ldpath ( & mut cmd) ;
290
-
291
- // some toolchains are faulty with some combinations of platforms and
292
- // may fail to launch but also to timely terminate.
293
- // (known cases include Rust 1.3.0 through 1.10.0 in recent macOS Sierra.)
294
- // we guard against such cases by enforcing a reasonable timeout to read.
295
- let mut line1 = None ;
296
- if let Ok ( mut child) = cmd. spawn ( ) {
297
- let timeout = Duration :: new ( 10 , 0 ) ;
298
- match child. wait_timeout ( timeout) {
299
- Ok ( Some ( status) ) if status. success ( ) => {
300
- let out = child
301
- . stdout
302
- . expect ( "Child::stdout requested but not present" ) ;
303
- let mut line = String :: new ( ) ;
304
- if BufReader :: new ( out) . read_line ( & mut line) . is_ok ( ) {
305
- let lineend = line. trim_end_matches ( & [ '\r' , '\n' ] [ ..] ) . len ( ) ;
306
- line. truncate ( lineend) ;
307
- line1 = Some ( line) ;
281
+ match self . create_command ( "rustc" ) {
282
+ Ok ( mut cmd) => {
283
+ cmd. arg ( "--version" ) ;
284
+ cmd. stdin ( Stdio :: null ( ) ) ;
285
+ cmd. stdout ( Stdio :: piped ( ) ) ;
286
+ cmd. stderr ( Stdio :: piped ( ) ) ;
287
+ self . set_ldpath ( & mut cmd) ;
288
+
289
+ // some toolchains are faulty with some combinations of platforms and
290
+ // may fail to launch but also to timely terminate.
291
+ // (known cases include Rust 1.3.0 through 1.10.0 in recent macOS Sierra.)
292
+ // we guard against such cases by enforcing a reasonable timeout to read.
293
+ let mut line1 = None ;
294
+ if let Ok ( mut child) = cmd. spawn ( ) {
295
+ let timeout = Duration :: new ( 10 , 0 ) ;
296
+ match child. wait_timeout ( timeout) {
297
+ Ok ( Some ( status) ) if status. success ( ) => {
298
+ let out = child
299
+ . stdout
300
+ . expect ( "Child::stdout requested but not present" ) ;
301
+ let mut line = String :: new ( ) ;
302
+ if BufReader :: new ( out) . read_line ( & mut line) . is_ok ( ) {
303
+ let lineend = line. trim_end_matches ( & [ '\r' , '\n' ] [ ..] ) . len ( ) ;
304
+ line. truncate ( lineend) ;
305
+ line1 = Some ( line) ;
306
+ }
308
307
}
308
+ Ok ( None ) => {
309
+ let _ = child. kill ( ) ;
310
+ return String :: from ( "(timeout reading rustc version)" ) ;
311
+ }
312
+ Ok ( Some ( _) ) | Err ( _) => { }
309
313
}
310
- Ok ( None ) => {
311
- let _ = child. kill ( ) ;
312
- return String :: from ( "(timeout reading rustc version)" ) ;
313
- }
314
- Ok ( Some ( _) ) | Err ( _) => { }
315
314
}
316
- }
317
315
318
- if let Some ( line1) = line1 {
319
- line1
320
- } else {
321
- String :: from ( "(error reading rustc version)" )
316
+ if let Some ( line1) = line1 {
317
+ line1
318
+ } else {
319
+ String :: from ( "(error reading rustc version)" )
320
+ }
322
321
}
323
- } else {
324
- String :: from ( "(rustc does not exist)" )
322
+ Err ( _) => String :: from ( "(rustc does not exist)" ) ,
325
323
}
326
324
}
327
325
0 commit comments