@@ -102,40 +102,47 @@ impl<'this> RustcCodegenFlags<'this> {
102
102
} else {
103
103
Cow :: Owned ( format ! ( "{prefix}{flag}" ) )
104
104
} ;
105
+ let flag = flag. as_ref ( ) ;
105
106
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
+ }
111
119
}
120
+ let flag_not_empty = |flag_value| flag_not_empty_generic ( flag, flag_value) ;
112
121
113
- match flag. as_ref ( ) {
122
+ match flag {
114
123
// https://doc.rust-lang.org/rustc/codegen-options/index.html#code-model
115
124
"-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) ? ;
117
126
}
118
127
// https://doc.rust-lang.org/rustc/codegen-options/index.html#no-vectorize-loops
119
128
"-Cno-vectorize-loops" => self . no_vectorize_loops = true ,
120
129
// https://doc.rust-lang.org/rustc/codegen-options/index.html#no-vectorize-slp
121
130
"-Cno-vectorize-slp" => self . no_vectorize_slp = true ,
122
131
// https://doc.rust-lang.org/rustc/codegen-options/index.html#profile-generate
123
132
"-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) ?;
126
134
}
127
135
// https://doc.rust-lang.org/rustc/codegen-options/index.html#profile-use
128
136
"-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) ? ;
130
138
}
131
139
// https://doc.rust-lang.org/rustc/codegen-options/index.html#control-flow-guard
132
140
"-Ccontrol-flow-guard" => self . control_flow_guard = value. or ( Some ( "true" ) ) ,
133
141
// https://doc.rust-lang.org/rustc/codegen-options/index.html#lto
134
142
"-Clto" => self . lto = value. or ( Some ( "true" ) ) ,
135
143
// https://doc.rust-lang.org/rustc/codegen-options/index.html#relocation-model
136
144
"-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) ?;
139
146
}
140
147
// https://doc.rust-lang.org/rustc/codegen-options/index.html#embed-bitcode
141
148
"-Cembed-bitcode" => self . embed_bitcode = value. map_or ( Some ( true ) , arg_to_bool) ,
@@ -151,22 +158,17 @@ impl<'this> RustcCodegenFlags<'this> {
151
158
// https://doc.rust-lang.org/beta/unstable-book/compiler-flags/branch-protection.html
152
159
// FIXME: Drop the -Z variant and update the doc link once the option is stabilised
153
160
"-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) ?;
156
162
}
157
163
// https://doc.rust-lang.org/beta/unstable-book/compiler-flags/dwarf-version.html
158
164
// FIXME: Drop the -Z variant and update the doc link once the option is stablized
159
165
"-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) ) ?;
164
167
}
165
168
// https://github.com/rust-lang/rust/issues/114903
166
169
// FIXME: Drop the -Z variant and update the doc link once the option is stabilized
167
170
"-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) ?;
170
172
}
171
173
_ => { }
172
174
}
0 commit comments