@@ -60,7 +60,7 @@ struct CrateRunEnv {
60
60
61
61
impl CrateRunEnv {
62
62
/// Gather all the information we need.
63
- fn collect ( args : env :: Args , capture_stdin : bool ) -> Self {
63
+ fn collect ( args : impl Iterator < Item = String > , capture_stdin : bool ) -> Self {
64
64
let args = args. collect ( ) ;
65
65
let env = env:: vars_os ( ) . collect ( ) ;
66
66
let current_dir = env:: current_dir ( ) . unwrap ( ) . into_os_string ( ) ;
@@ -757,7 +757,7 @@ enum RustcPhase {
757
757
Rustdoc ,
758
758
}
759
759
760
- fn phase_rustc ( mut args : env :: Args , phase : RustcPhase ) {
760
+ fn phase_rustc ( mut args : impl Iterator < Item = String > , phase : RustcPhase ) {
761
761
/// Determines if we are being invoked (as rustc) to build a crate for
762
762
/// the "target" architecture, in contrast to the "host" architecture.
763
763
/// Host crates are for build scripts and proc macros and still need to
@@ -978,10 +978,11 @@ enum RunnerPhase {
978
978
Rustdoc ,
979
979
}
980
980
981
- fn phase_runner ( binary : & Path , binary_args : impl Iterator < Item = String > , phase : RunnerPhase ) {
981
+ fn phase_runner ( mut binary_args : impl Iterator < Item = String > , phase : RunnerPhase ) {
982
982
let verbose = std:: env:: var ( "MIRI_VERBOSE" )
983
983
. map_or ( 0 , |verbose| verbose. parse ( ) . expect ( "verbosity flag must be an integer" ) ) ;
984
984
985
+ let binary = binary_args. next ( ) . unwrap ( ) ;
985
986
let file = File :: open ( & binary)
986
987
. unwrap_or_else ( |_| show_error ( format ! ( "file {:?} not found or `cargo-miri` invoked incorrectly; please only invoke this binary through `cargo miri`" , binary) ) ) ;
987
988
let file = BufReader :: new ( file) ;
@@ -1071,25 +1072,16 @@ fn phase_runner(binary: &Path, binary_args: impl Iterator<Item = String>, phase:
1071
1072
}
1072
1073
}
1073
1074
1074
- fn phase_rustdoc ( fst_arg : & str , mut args : env :: Args ) {
1075
+ fn phase_rustdoc ( mut args : impl Iterator < Item = String > ) {
1075
1076
let verbose = std:: env:: var ( "MIRI_VERBOSE" )
1076
1077
. map_or ( 0 , |verbose| verbose. parse ( ) . expect ( "verbosity flag must be an integer" ) ) ;
1077
1078
1078
1079
// phase_cargo_miri sets the RUSTDOC env var to ourselves, so we can't use that here;
1079
1080
// just default to a straight-forward invocation for now:
1080
1081
let mut cmd = Command :: new ( "rustdoc" ) ;
1081
1082
1082
- // Because of the way the main function is structured, we have to take the first argument spearately
1083
- // from the rest; to simplify the following argument patching loop, we'll just skip that one.
1084
- // This is fine for now, because cargo will never pass --extern arguments in the first position,
1085
- // but we should defensively assert that this will work.
1086
1083
let extern_flag = "--extern" ;
1087
- assert ! ( fst_arg != extern_flag) ;
1088
- cmd. arg ( fst_arg) ;
1089
-
1090
1084
let runtool_flag = "--runtool" ;
1091
- // `crossmode` records if *any* argument matches `runtool_flag`; here we check the first one.
1092
- let mut crossmode = fst_arg == runtool_flag;
1093
1085
while let Some ( arg) = args. next ( ) {
1094
1086
if arg == extern_flag {
1095
1087
// Patch --extern arguments to use *.rmeta files, since phase_cargo_rustc only creates stub *.rlib files.
@@ -1098,17 +1090,12 @@ fn phase_rustdoc(fst_arg: &str, mut args: env::Args) {
1098
1090
// An existing --runtool flag indicates cargo is running in cross-target mode, which we don't support.
1099
1091
// Note that this is only passed when cargo is run with the unstable -Zdoctest-xcompile flag;
1100
1092
// otherwise, we won't be called as rustdoc at all.
1101
- crossmode = true ;
1102
- break ;
1093
+ show_error ( format ! ( "cross-interpreting doctests is not currently supported by Miri." ) ) ;
1103
1094
} else {
1104
1095
cmd. arg ( arg) ;
1105
1096
}
1106
1097
}
1107
1098
1108
- if crossmode {
1109
- show_error ( format ! ( "cross-interpreting doctests is not currently supported by Miri." ) ) ;
1110
- }
1111
-
1112
1099
// Doctests of `proc-macro` crates (and their dependencies) are always built for the host,
1113
1100
// so we are not able to run them in Miri.
1114
1101
if ArgFlagValueIter :: new ( "--crate-type" ) . any ( |crate_type| crate_type == "proc-macro" ) {
@@ -1178,52 +1165,46 @@ fn main() {
1178
1165
// since we don't specify any runtool-args, and rustdoc supplies multiple arguments to
1179
1166
// the test-builder unconditionally, we can just check the number of remaining arguments:
1180
1167
if args. len ( ) == 1 {
1181
- let arg = args. next ( ) . unwrap ( ) ;
1182
- let binary = Path :: new ( & arg) ;
1183
- if binary. exists ( ) {
1184
- phase_runner ( binary, args, RunnerPhase :: Rustdoc ) ;
1185
- } else {
1186
- show_error ( format ! (
1187
- "`cargo-miri` called with non-existing path argument `{}` in rustdoc mode; please invoke this binary through `cargo miri`" ,
1188
- arg
1189
- ) ) ;
1190
- }
1168
+ phase_runner ( args, RunnerPhase :: Rustdoc ) ;
1191
1169
} else {
1192
1170
phase_rustc ( args, RustcPhase :: Rustdoc ) ;
1193
1171
}
1194
1172
1195
1173
return ;
1196
1174
}
1197
1175
1198
- match args. next ( ) . as_deref ( ) {
1199
- Some ( "miri" ) => phase_cargo_miri ( args) ,
1200
- Some ( arg) => {
1201
- // If the first arg is equal to the RUSTC variable (which should be set at this point),
1202
- // then we need to behave as rustc. This is the somewhat counter-intuitive behavior of
1203
- // having both RUSTC and RUSTC_WRAPPER set (see
1204
- // https://github.com/rust-lang/cargo/issues/10886).
1205
- if arg == env:: var_os ( "RUSTC" ) . unwrap ( ) {
1206
- return phase_rustc ( args, RustcPhase :: Build ) ;
1207
- }
1208
- // We have to distinguish the "runner" and "rustdoc" cases.
1209
- // As runner, the first argument is the binary (a file that should exist, with an absolute path);
1210
- // as rustdoc, the first argument is a flag (`--something`).
1211
- let binary = Path :: new ( arg) ;
1212
- if binary. exists ( ) {
1213
- assert ! ( !arg. starts_with( "--" ) ) ; // not a flag
1214
- phase_runner ( binary, args, RunnerPhase :: Cargo ) ;
1215
- } else if arg. starts_with ( "--" ) {
1216
- phase_rustdoc ( arg, args) ;
1217
- } else {
1218
- show_error ( format ! (
1219
- "`cargo-miri` called with unexpected first argument `{}`; please only invoke this binary through `cargo miri`" ,
1220
- arg
1221
- ) ) ;
1222
- }
1176
+ let mut args = args. peekable ( ) ;
1177
+ if args. next_if ( |a| a == "miri" ) . is_some ( ) {
1178
+ phase_cargo_miri ( args) ;
1179
+ } else if let Some ( arg) = args. peek ( ) . cloned ( ) {
1180
+ // Cargo calls us for everything it does. We could be invoked as rustc, rustdoc, or the runner.
1181
+
1182
+ // If the first arg is equal to the RUSTC variable (which should be set at this point),
1183
+ // then we need to behave as rustc. This is the somewhat counter-intuitive behavior of
1184
+ // having both RUSTC and RUSTC_WRAPPER set (see
1185
+ // https://github.com/rust-lang/cargo/issues/10886).
1186
+ if arg == env:: var ( "RUSTC" ) . unwrap ( ) {
1187
+ args. next ( ) . unwrap ( ) ; // consume wrapped RUSTC command.
1188
+ return phase_rustc ( args, RustcPhase :: Build ) ;
1223
1189
}
1224
- _ =>
1190
+ // We have to distinguish the "runner" and "rustdoc" cases.
1191
+ // As runner, the first argument is the binary (a file that should exist, with an absolute path);
1192
+ // as rustdoc, the first argument is a flag (`--something`).
1193
+ let binary = Path :: new ( & arg) ;
1194
+ if binary. exists ( ) {
1195
+ assert ! ( !arg. starts_with( "--" ) ) ; // not a flag
1196
+ phase_runner ( args, RunnerPhase :: Cargo ) ;
1197
+ } else if arg. starts_with ( "--" ) {
1198
+ phase_rustdoc ( args) ;
1199
+ } else {
1225
1200
show_error ( format ! (
1226
- "`cargo-miri` called without first argument; please only invoke this binary through `cargo miri`"
1227
- ) ) ,
1201
+ "`cargo-miri` called with unexpected first argument `{}`; please only invoke this binary through `cargo miri`" ,
1202
+ arg
1203
+ ) ) ;
1204
+ }
1205
+ } else {
1206
+ show_error ( format ! (
1207
+ "`cargo-miri` called without first argument; please only invoke this binary through `cargo miri`"
1208
+ ) ) ;
1228
1209
}
1229
1210
}
0 commit comments