Skip to content

Commit 12db222

Browse files
committed
Dogfood 'str_split_once() with compiler/
1 parent 0f6f2d6 commit 12db222

File tree

7 files changed

+83
-89
lines changed

7 files changed

+83
-89
lines changed

Diff for: compiler/rustc_mir/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Rust MIR: a lowered representation of Rust.
2828
#![feature(or_patterns)]
2929
#![feature(once_cell)]
3030
#![feature(control_flow_enum)]
31+
#![feature(str_split_once)]
3132
#![recursion_limit = "256"]
3233

3334
#[macro_use]

Diff for: compiler/rustc_mir/src/transform/coverage/debug.rs

+25-23
Original file line numberDiff line numberDiff line change
@@ -148,23 +148,19 @@ impl DebugOptions {
148148

149149
if let Ok(env_debug_options) = std::env::var(RUSTC_COVERAGE_DEBUG_OPTIONS) {
150150
for setting_str in env_debug_options.replace(" ", "").replace("-", "_").split(',') {
151-
let mut setting = setting_str.splitn(2, '=');
152-
match setting.next() {
153-
Some(option) if option == "allow_unused_expressions" => {
154-
allow_unused_expressions = bool_option_val(option, setting.next());
155-
debug!(
156-
"{} env option `allow_unused_expressions` is set to {}",
157-
RUSTC_COVERAGE_DEBUG_OPTIONS, allow_unused_expressions
158-
);
159-
}
160-
Some(option) if option == "counter_format" => {
161-
if let Some(strval) = setting.next() {
162-
counter_format = counter_format_option_val(strval);
163-
debug!(
164-
"{} env option `counter_format` is set to {:?}",
165-
RUSTC_COVERAGE_DEBUG_OPTIONS, counter_format
166-
);
167-
} else {
151+
let (option, value) = match setting_str.split_once('=') {
152+
None => (setting_str, None),
153+
Some((k, v)) => (k, Some(v)),
154+
};
155+
if option == "allow_unused_expressions" {
156+
allow_unused_expressions = bool_option_val(option, value);
157+
debug!(
158+
"{} env option `allow_unused_expressions` is set to {}",
159+
RUSTC_COVERAGE_DEBUG_OPTIONS, allow_unused_expressions
160+
);
161+
} else if option == "counter_format" {
162+
match value {
163+
None => {
168164
bug!(
169165
"`{}` option in environment variable {} requires one or more \
170166
plus-separated choices (a non-empty subset of \
@@ -173,14 +169,20 @@ impl DebugOptions {
173169
RUSTC_COVERAGE_DEBUG_OPTIONS
174170
);
175171
}
176-
}
177-
Some("") => {}
178-
Some(invalid) => bug!(
172+
Some(val) => {
173+
counter_format = counter_format_option_val(val);
174+
debug!(
175+
"{} env option `counter_format` is set to {:?}",
176+
RUSTC_COVERAGE_DEBUG_OPTIONS, counter_format
177+
);
178+
}
179+
};
180+
} else {
181+
bug!(
179182
"Unsupported setting `{}` in environment variable {}",
180-
invalid,
183+
option,
181184
RUSTC_COVERAGE_DEBUG_OPTIONS
182-
),
183-
None => {}
185+
)
184186
}
185187
}
186188
}

Diff for: compiler/rustc_session/src/config.rs

+50-59
Original file line numberDiff line numberDiff line change
@@ -1296,8 +1296,10 @@ fn parse_output_types(
12961296
if !debugging_opts.parse_only {
12971297
for list in matches.opt_strs("emit") {
12981298
for output_type in list.split(',') {
1299-
let mut parts = output_type.splitn(2, '=');
1300-
let shorthand = parts.next().unwrap();
1299+
let (shorthand, path) = match output_type.split_once('=') {
1300+
None => (output_type, None),
1301+
Some((shorthand, path)) => (shorthand, Some(PathBuf::from(path))),
1302+
};
13011303
let output_type = OutputType::from_shorthand(shorthand).unwrap_or_else(|| {
13021304
early_error(
13031305
error_format,
@@ -1308,7 +1310,6 @@ fn parse_output_types(
13081310
),
13091311
)
13101312
});
1311-
let path = parts.next().map(PathBuf::from);
13121313
output_types.insert(output_type, path);
13131314
}
13141315
}
@@ -1452,11 +1453,10 @@ fn parse_opt_level(
14521453
let max_c = matches
14531454
.opt_strs_pos("C")
14541455
.into_iter()
1455-
.flat_map(
1456-
|(i, s)| {
1457-
if let Some("opt-level") = s.splitn(2, '=').next() { Some(i) } else { None }
1458-
},
1459-
)
1456+
.flat_map(|(i, s)| {
1457+
// NB: This can match a string without `=`.
1458+
if let Some("opt-level") = s.splitn(2, '=').next() { Some(i) } else { None }
1459+
})
14601460
.max();
14611461
if max_o > max_c {
14621462
OptLevel::Default
@@ -1491,11 +1491,10 @@ fn select_debuginfo(
14911491
let max_c = matches
14921492
.opt_strs_pos("C")
14931493
.into_iter()
1494-
.flat_map(
1495-
|(i, s)| {
1496-
if let Some("debuginfo") = s.splitn(2, '=').next() { Some(i) } else { None }
1497-
},
1498-
)
1494+
.flat_map(|(i, s)| {
1495+
// NB: This can match a string without `=`.
1496+
if let Some("debuginfo") = s.splitn(2, '=').next() { Some(i) } else { None }
1497+
})
14991498
.max();
15001499
if max_g > max_c {
15011500
DebugInfo::Full
@@ -1528,23 +1527,26 @@ fn parse_libs(
15281527
.map(|s| {
15291528
// Parse string of the form "[KIND=]lib[:new_name]",
15301529
// where KIND is one of "dylib", "framework", "static".
1531-
let mut parts = s.splitn(2, '=');
1532-
let kind = parts.next().unwrap();
1533-
let (name, kind) = match (parts.next(), kind) {
1534-
(None, name) => (name, NativeLibKind::Unspecified),
1535-
(Some(name), "dylib") => (name, NativeLibKind::Dylib),
1536-
(Some(name), "framework") => (name, NativeLibKind::Framework),
1537-
(Some(name), "static") => (name, NativeLibKind::StaticBundle),
1538-
(Some(name), "static-nobundle") => (name, NativeLibKind::StaticNoBundle),
1539-
(_, s) => {
1540-
early_error(
1541-
error_format,
1542-
&format!(
1543-
"unknown library kind `{}`, expected \
1544-
one of dylib, framework, or static",
1545-
s
1546-
),
1547-
);
1530+
let (name, kind) = match s.split_once('=') {
1531+
None => (s, NativeLibKind::Unspecified),
1532+
Some((kind, name)) => {
1533+
let kind = match kind {
1534+
"dylib" => NativeLibKind::Dylib,
1535+
"framework" => NativeLibKind::Framework,
1536+
"static" => NativeLibKind::StaticBundle,
1537+
"static-nobundle" => NativeLibKind::StaticNoBundle,
1538+
s => {
1539+
early_error(
1540+
error_format,
1541+
&format!(
1542+
"unknown library kind `{}`, expected \
1543+
one of dylib, framework, or static",
1544+
s
1545+
),
1546+
);
1547+
}
1548+
};
1549+
(name.to_string(), kind)
15481550
}
15491551
};
15501552
if kind == NativeLibKind::StaticNoBundle
@@ -1556,10 +1558,11 @@ fn parse_libs(
15561558
accepted on the nightly compiler",
15571559
);
15581560
}
1559-
let mut name_parts = name.splitn(2, ':');
1560-
let name = name_parts.next().unwrap();
1561-
let new_name = name_parts.next();
1562-
(name.to_owned(), new_name.map(|n| n.to_owned()), kind)
1561+
let (name, new_name) = match name.split_once(':') {
1562+
None => (name, None),
1563+
Some((name, new_name)) => (name.to_string(), Some(new_name.to_owned())),
1564+
};
1565+
(name, new_name, kind)
15631566
})
15641567
.collect()
15651568
}
@@ -1580,20 +1583,13 @@ pub fn parse_externs(
15801583
let is_unstable_enabled = debugging_opts.unstable_options;
15811584
let mut externs: BTreeMap<String, ExternEntry> = BTreeMap::new();
15821585
for arg in matches.opt_strs("extern") {
1583-
let mut parts = arg.splitn(2, '=');
1584-
let name = parts
1585-
.next()
1586-
.unwrap_or_else(|| early_error(error_format, "--extern value must not be empty"));
1587-
let path = parts.next().map(|s| s.to_string());
1588-
1589-
let mut name_parts = name.splitn(2, ':');
1590-
let first_part = name_parts.next();
1591-
let second_part = name_parts.next();
1592-
let (options, name) = match (first_part, second_part) {
1593-
(Some(opts), Some(name)) => (Some(opts), name),
1594-
(Some(name), None) => (None, name),
1595-
(None, None) => early_error(error_format, "--extern name must not be empty"),
1596-
_ => unreachable!(),
1586+
let (name, path) = match arg.split_once('=') {
1587+
None => (arg, None),
1588+
Some((name, path)) => (name.to_string(), Some(path.to_string())),
1589+
};
1590+
let (options, name) = match name.split_once(':') {
1591+
None => (None, name),
1592+
Some((opts, name)) => (Some(opts), name.to_string()),
15971593
};
15981594

15991595
let entry = externs.entry(name.to_owned());
@@ -1682,17 +1678,12 @@ fn parse_remap_path_prefix(
16821678
matches
16831679
.opt_strs("remap-path-prefix")
16841680
.into_iter()
1685-
.map(|remap| {
1686-
let mut parts = remap.rsplitn(2, '='); // reverse iterator
1687-
let to = parts.next();
1688-
let from = parts.next();
1689-
match (from, to) {
1690-
(Some(from), Some(to)) => (PathBuf::from(from), PathBuf::from(to)),
1691-
_ => early_error(
1692-
error_format,
1693-
"--remap-path-prefix must contain '=' between FROM and TO",
1694-
),
1695-
}
1681+
.map(|remap| match remap.rsplit_once('=') {
1682+
None => early_error(
1683+
error_format,
1684+
"--remap-path-prefix must contain '=' between FROM and TO",
1685+
),
1686+
Some((from, to)) => (PathBuf::from(from), PathBuf::from(to)),
16961687
})
16971688
.collect()
16981689
}

Diff for: compiler/rustc_session/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![feature(crate_visibility_modifier)]
22
#![feature(once_cell)]
33
#![feature(or_patterns)]
4+
#![feature(str_split_once)]
45

56
#[macro_use]
67
extern crate bitflags;

Diff for: compiler/rustc_session/src/options.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,10 @@ macro_rules! options {
179179
{
180180
let mut op = $defaultfn();
181181
for option in matches.opt_strs($prefix) {
182-
let mut iter = option.splitn(2, '=');
183-
let key = iter.next().unwrap();
184-
let value = iter.next();
182+
let (key, value) = match option.split_once('=') {
183+
None => (option, None),
184+
Some((k, v)) => (k.to_string(), Some(v)),
185+
};
185186
let option_to_lookup = key.replace("-", "_");
186187
let mut found = false;
187188
for &(candidate, setter, type_desc, _) in $stat {

Diff for: compiler/rustc_target/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#![feature(never_type)]
1616
#![feature(associated_type_bounds)]
1717
#![feature(exhaustive_patterns)]
18+
#![feature(str_split_once)]
1819

1920
#[macro_use]
2021
extern crate rustc_macros;

Diff for: compiler/rustc_target/src/spec/apple_base.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ fn macos_deployment_target() -> (u32, u32) {
5454
let deployment_target = env::var("MACOSX_DEPLOYMENT_TARGET").ok();
5555
let version = deployment_target
5656
.as_ref()
57-
.and_then(|s| {
58-
let mut i = s.splitn(2, '.');
59-
i.next().and_then(|a| i.next().map(|b| (a, b)))
60-
})
57+
.and_then(|s| s.split_once('.'))
6158
.and_then(|(a, b)| a.parse::<u32>().and_then(|a| b.parse::<u32>().map(|b| (a, b))).ok());
6259

6360
version.unwrap_or((10, 7))

0 commit comments

Comments
 (0)