Skip to content

Commit a9efb06

Browse files
committed
Auto merge of #10676 - djmcgill:origin/master, r=weihanglo
add validation for string "true"/"false" in lto profile ### What does this PR try to resolve? Adds a special-cased error message for when `lto` is set to the _string_ `"true"`/`"false"` which is surprisingly (I was surprised anyway) not allowed and the error message is ambiguous. The new error message makes it clear what values are accepted. Fixes #10572 ### How should we test and review this PR? <!-- Demonstrate how you test this change and guide reviewers through your PR. With a smooth review process, a pull request usually gets reviewed quicker. If you don't know how to write and run your tests, please read the guide: https://doc.crates.io/contrib/tests --> Uh I've not actually tested yet that's the WIP part. But put ``` [profile.dev] lto="false" ``` in your TOML and run `cargo build`, check that you get the new error message and that it makes sense and is helpful. ### Additional information It's worth noting that as per rust-lang/rust#97051 this doesn't fix the _real_ problem here IMO which is that [rust's `opt_parse_bool` cli parsing](https://github.com/rust-lang/rust/blob/491f619f564a4ff9ae4cc837e27bb919d04c31be/compiler/rustc_session/src/options.rs#L456) doesn't accept true/false which certainly seems an ad-hoc historical choice to me on first glance but also it's a much bigger change to change those semantics than this error message.
2 parents cce487e + 0daf70d commit a9efb06

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,16 @@ impl TomlProfile {
655655
}
656656
}
657657

658+
if let Some(StringOrBool::String(arg)) = &self.lto {
659+
if arg == "true" || arg == "false" {
660+
bail!(
661+
"`lto` setting of string `\"{arg}\"` for `{name}` profile is not \
662+
a valid setting, must be a boolean (`true`/`false`) or a string \
663+
(`\"thin\"`/`\"fat\"`/`\"off\"`) or omitted.",
664+
);
665+
}
666+
}
667+
658668
Ok(())
659669
}
660670

tests/testsuite/profiles.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,37 @@ fn profile_in_virtual_manifest_works() {
323323
.run();
324324
}
325325

326+
#[cargo_test]
327+
fn profile_lto_string_bool_dev() {
328+
let p = project()
329+
.file(
330+
"Cargo.toml",
331+
r#"
332+
[package]
333+
name = "foo"
334+
version = "0.0.1"
335+
336+
[profile.dev]
337+
lto = "true"
338+
"#,
339+
)
340+
.file("src/lib.rs", "")
341+
.build();
342+
343+
p.cargo("build")
344+
.with_status(101)
345+
.with_stderr(
346+
"\
347+
error: failed to parse manifest at `[ROOT]/foo/Cargo.toml`
348+
349+
Caused by:
350+
`lto` setting of string `\"true\"` for `dev` profile is not a valid setting, \
351+
must be a boolean (`true`/`false`) or a string (`\"thin\"`/`\"fat\"`/`\"off\"`) or omitted.
352+
",
353+
)
354+
.run();
355+
}
356+
326357
#[cargo_test]
327358
fn profile_panic_test_bench() {
328359
let p = project()

0 commit comments

Comments
 (0)