From 5bde8dfa0e7fd1d6e3c605c067a4bb6c97672a23 Mon Sep 17 00:00:00 2001 From: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> Date: Tue, 9 Sep 2025 05:55:51 +1000 Subject: [PATCH 1/3] Fix errmsg in RustcCodegenFlags::set_rustc_flag Make sure error message contains the right flag, plus refactor --- src/flags.rs | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/flags.rs b/src/flags.rs index eeabb5b5a..bc998ed8c 100644 --- a/src/flags.rs +++ b/src/flags.rs @@ -102,18 +102,27 @@ impl<'this> RustcCodegenFlags<'this> { } else { Cow::Owned(format!("{prefix}{flag}")) }; + let flag = flag.as_ref(); - fn flag_ok_or<'flag>( - flag: Option<&'flag str>, - msg: &'static str, - ) -> Result<&'flag str, Error> { - flag.ok_or(Error::new(ErrorKind::InvalidFlag, msg)) + fn flag_not_empty_inner( + flag: &str, + flag_value: Option, + ) -> Result, Error> { + if let Some(flag_value) = flag_value { + Ok(Some(flag_value)) + } else { + Err(Error::new( + ErrorKind::InvalidFlag, + format!("{flag} must have a value"), + )) + } } + let flag_not_empty = |flag_value| flag_not_empty_inner(flag, flag_value); - match flag.as_ref() { + match flag { // https://doc.rust-lang.org/rustc/codegen-options/index.html#code-model "-Ccode-model" => { - self.code_model = Some(flag_ok_or(value, "-Ccode-model must have a value")?); + self.code_model = flag_not_empty(value)?; } // https://doc.rust-lang.org/rustc/codegen-options/index.html#no-vectorize-loops "-Cno-vectorize-loops" => self.no_vectorize_loops = true, @@ -121,12 +130,11 @@ impl<'this> RustcCodegenFlags<'this> { "-Cno-vectorize-slp" => self.no_vectorize_slp = true, // https://doc.rust-lang.org/rustc/codegen-options/index.html#profile-generate "-Cprofile-generate" => { - self.profile_generate = - Some(flag_ok_or(value, "-Cprofile-generate must have a value")?); + self.profile_generate = flag_not_empty(value)?; } // https://doc.rust-lang.org/rustc/codegen-options/index.html#profile-use "-Cprofile-use" => { - self.profile_use = Some(flag_ok_or(value, "-Cprofile-use must have a value")?); + self.profile_use = flag_not_empty(value)?; } // https://doc.rust-lang.org/rustc/codegen-options/index.html#control-flow-guard "-Ccontrol-flow-guard" => self.control_flow_guard = value.or(Some("true")), @@ -134,8 +142,7 @@ impl<'this> RustcCodegenFlags<'this> { "-Clto" => self.lto = value.or(Some("true")), // https://doc.rust-lang.org/rustc/codegen-options/index.html#relocation-model "-Crelocation-model" => { - self.relocation_model = - Some(flag_ok_or(value, "-Crelocation-model must have a value")?); + self.relocation_model = flag_not_empty(value)?; } // https://doc.rust-lang.org/rustc/codegen-options/index.html#embed-bitcode "-Cembed-bitcode" => self.embed_bitcode = value.map_or(Some(true), arg_to_bool), @@ -151,22 +158,17 @@ impl<'this> RustcCodegenFlags<'this> { // https://doc.rust-lang.org/beta/unstable-book/compiler-flags/branch-protection.html // FIXME: Drop the -Z variant and update the doc link once the option is stabilised "-Zbranch-protection" | "-Cbranch-protection" => { - self.branch_protection = - Some(flag_ok_or(value, "-Zbranch-protection must have a value")?); + self.branch_protection = flag_not_empty(value)?; } // https://doc.rust-lang.org/beta/unstable-book/compiler-flags/dwarf-version.html // FIXME: Drop the -Z variant and update the doc link once the option is stablized "-Zdwarf-version" | "-Cdwarf-version" => { - self.dwarf_version = Some(value.and_then(arg_to_u32).ok_or(Error::new( - ErrorKind::InvalidFlag, - "-Zdwarf-version must have a value", - ))?); + self.dwarf_version = flag_not_empty(value.and_then(arg_to_u32))?; } // https://github.com/rust-lang/rust/issues/114903 // FIXME: Drop the -Z variant and update the doc link once the option is stabilized "-Zstack-protector" | "-Cstack-protector" => { - self.stack_protector = - Some(flag_ok_or(value, "-Zstack-protector must have a value")?); + self.stack_protector = flag_not_empty(value)?; } _ => {} } From 895e01c50649f1ccd73f58be840a1c665b1e610a Mon Sep 17 00:00:00 2001 From: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> Date: Tue, 9 Sep 2025 05:59:06 +1000 Subject: [PATCH 2/3] Fix flags.rs --- src/flags.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/flags.rs b/src/flags.rs index bc998ed8c..fe127de73 100644 --- a/src/flags.rs +++ b/src/flags.rs @@ -104,10 +104,7 @@ impl<'this> RustcCodegenFlags<'this> { }; let flag = flag.as_ref(); - fn flag_not_empty_inner( - flag: &str, - flag_value: Option, - ) -> Result, Error> { + fn flag_not_empty_generic(flag: &str, flag_value: Option) -> Result, Error> { if let Some(flag_value) = flag_value { Ok(Some(flag_value)) } else { @@ -117,7 +114,7 @@ impl<'this> RustcCodegenFlags<'this> { )) } } - let flag_not_empty = |flag_value| flag_not_empty_inner(flag, flag_value); + let flag_not_empty = |flag_value| flag_not_empty_generic(flag, flag_value); match flag { // https://doc.rust-lang.org/rustc/codegen-options/index.html#code-model @@ -163,7 +160,7 @@ impl<'this> RustcCodegenFlags<'this> { // https://doc.rust-lang.org/beta/unstable-book/compiler-flags/dwarf-version.html // FIXME: Drop the -Z variant and update the doc link once the option is stablized "-Zdwarf-version" | "-Cdwarf-version" => { - self.dwarf_version = flag_not_empty(value.and_then(arg_to_u32))?; + self.dwarf_version = flag_not_empty_generic(flag, value.and_then(arg_to_u32))?; } // https://github.com/rust-lang/rust/issues/114903 // FIXME: Drop the -Z variant and update the doc link once the option is stabilized From 32946351b32c4068c10492a93d5822fd94ec301b Mon Sep 17 00:00:00 2001 From: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> Date: Tue, 9 Sep 2025 06:06:20 +1000 Subject: [PATCH 3/3] Fix fmt of flags.rs --- src/flags.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/flags.rs b/src/flags.rs index fe127de73..18ca5488d 100644 --- a/src/flags.rs +++ b/src/flags.rs @@ -104,7 +104,10 @@ impl<'this> RustcCodegenFlags<'this> { }; let flag = flag.as_ref(); - fn flag_not_empty_generic(flag: &str, flag_value: Option) -> Result, Error> { + fn flag_not_empty_generic( + flag: &str, + flag_value: Option, + ) -> Result, Error> { if let Some(flag_value) = flag_value { Ok(Some(flag_value)) } else {