Skip to content

Commit 1f6ffb1

Browse files
authored
Fix errmsg in RustcCodegenFlags::set_rustc_flag (#1551)
* Fix errmsg in RustcCodegenFlags::set_rustc_flag Make sure error message contains the right flag, plus refactor * Fix flags.rs * Fix fmt of flags.rs
1 parent 79beddf commit 1f6ffb1

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/flags.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -102,40 +102,47 @@ impl<'this> RustcCodegenFlags<'this> {
102102
} else {
103103
Cow::Owned(format!("{prefix}{flag}"))
104104
};
105+
let flag = flag.as_ref();
105106

106-
fn flag_ok_or<'flag>(
107-
flag: Option<&'flag str>,
108-
msg: &'static str,
109-
) -> Result<&'flag str, Error> {
110-
flag.ok_or(Error::new(ErrorKind::InvalidFlag, msg))
107+
fn flag_not_empty_generic<T>(
108+
flag: &str,
109+
flag_value: Option<T>,
110+
) -> Result<Option<T>, Error> {
111+
if let Some(flag_value) = flag_value {
112+
Ok(Some(flag_value))
113+
} else {
114+
Err(Error::new(
115+
ErrorKind::InvalidFlag,
116+
format!("{flag} must have a value"),
117+
))
118+
}
111119
}
120+
let flag_not_empty = |flag_value| flag_not_empty_generic(flag, flag_value);
112121

113-
match flag.as_ref() {
122+
match flag {
114123
// https://doc.rust-lang.org/rustc/codegen-options/index.html#code-model
115124
"-Ccode-model" => {
116-
self.code_model = Some(flag_ok_or(value, "-Ccode-model must have a value")?);
125+
self.code_model = flag_not_empty(value)?;
117126
}
118127
// https://doc.rust-lang.org/rustc/codegen-options/index.html#no-vectorize-loops
119128
"-Cno-vectorize-loops" => self.no_vectorize_loops = true,
120129
// https://doc.rust-lang.org/rustc/codegen-options/index.html#no-vectorize-slp
121130
"-Cno-vectorize-slp" => self.no_vectorize_slp = true,
122131
// https://doc.rust-lang.org/rustc/codegen-options/index.html#profile-generate
123132
"-Cprofile-generate" => {
124-
self.profile_generate =
125-
Some(flag_ok_or(value, "-Cprofile-generate must have a value")?);
133+
self.profile_generate = flag_not_empty(value)?;
126134
}
127135
// https://doc.rust-lang.org/rustc/codegen-options/index.html#profile-use
128136
"-Cprofile-use" => {
129-
self.profile_use = Some(flag_ok_or(value, "-Cprofile-use must have a value")?);
137+
self.profile_use = flag_not_empty(value)?;
130138
}
131139
// https://doc.rust-lang.org/rustc/codegen-options/index.html#control-flow-guard
132140
"-Ccontrol-flow-guard" => self.control_flow_guard = value.or(Some("true")),
133141
// https://doc.rust-lang.org/rustc/codegen-options/index.html#lto
134142
"-Clto" => self.lto = value.or(Some("true")),
135143
// https://doc.rust-lang.org/rustc/codegen-options/index.html#relocation-model
136144
"-Crelocation-model" => {
137-
self.relocation_model =
138-
Some(flag_ok_or(value, "-Crelocation-model must have a value")?);
145+
self.relocation_model = flag_not_empty(value)?;
139146
}
140147
// https://doc.rust-lang.org/rustc/codegen-options/index.html#embed-bitcode
141148
"-Cembed-bitcode" => self.embed_bitcode = value.map_or(Some(true), arg_to_bool),
@@ -151,22 +158,17 @@ impl<'this> RustcCodegenFlags<'this> {
151158
// https://doc.rust-lang.org/beta/unstable-book/compiler-flags/branch-protection.html
152159
// FIXME: Drop the -Z variant and update the doc link once the option is stabilised
153160
"-Zbranch-protection" | "-Cbranch-protection" => {
154-
self.branch_protection =
155-
Some(flag_ok_or(value, "-Zbranch-protection must have a value")?);
161+
self.branch_protection = flag_not_empty(value)?;
156162
}
157163
// https://doc.rust-lang.org/beta/unstable-book/compiler-flags/dwarf-version.html
158164
// FIXME: Drop the -Z variant and update the doc link once the option is stablized
159165
"-Zdwarf-version" | "-Cdwarf-version" => {
160-
self.dwarf_version = Some(value.and_then(arg_to_u32).ok_or(Error::new(
161-
ErrorKind::InvalidFlag,
162-
"-Zdwarf-version must have a value",
163-
))?);
166+
self.dwarf_version = flag_not_empty_generic(flag, value.and_then(arg_to_u32))?;
164167
}
165168
// https://github.com/rust-lang/rust/issues/114903
166169
// FIXME: Drop the -Z variant and update the doc link once the option is stabilized
167170
"-Zstack-protector" | "-Cstack-protector" => {
168-
self.stack_protector =
169-
Some(flag_ok_or(value, "-Zstack-protector must have a value")?);
171+
self.stack_protector = flag_not_empty(value)?;
170172
}
171173
_ => {}
172174
}

0 commit comments

Comments
 (0)