Skip to content

Commit 31b3232

Browse files
committed
Parse hints permissively to allow for future expansion
Make it only a warning, not an error, to have a hint value of the wrong type.
1 parent b8556e5 commit 31b3232

File tree

4 files changed

+80
-8
lines changed

4 files changed

+80
-8
lines changed

crates/cargo-util-schemas/manifest.schema.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,9 +1012,13 @@
10121012
"type": "object",
10131013
"properties": {
10141014
"mostly-unused": {
1015-
"type": [
1016-
"boolean",
1017-
"null"
1015+
"anyOf": [
1016+
{
1017+
"$ref": "#/$defs/TomlValue"
1018+
},
1019+
{
1020+
"type": "null"
1021+
}
10181022
]
10191023
}
10201024
}

crates/cargo-util-schemas/src/manifest/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1647,7 +1647,11 @@ pub enum TomlLintLevel {
16471647
#[serde(rename_all = "kebab-case")]
16481648
#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
16491649
pub struct Hints {
1650-
pub mostly_unused: Option<bool>,
1650+
#[cfg_attr(
1651+
feature = "unstable-schema",
1652+
schemars(with = "Option<TomlValueWrapper>")
1653+
)]
1654+
pub mostly_unused: Option<toml::Value>,
16511655
}
16521656

16531657
#[derive(Copy, Clone, Debug)]

src/cargo/core/compiler/mod.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ fn build_base_args(
11301130
strip,
11311131
rustflags: profile_rustflags,
11321132
trim_paths,
1133-
hint_mostly_unused,
1133+
hint_mostly_unused: profile_hint_mostly_unused,
11341134
..
11351135
} = unit.profile.clone();
11361136
let hints = unit.pkg.hints().cloned().unwrap_or_default();
@@ -1322,15 +1322,29 @@ fn build_base_args(
13221322
opt(cmd, "-C", "incremental=", Some(dir));
13231323
}
13241324

1325-
if hint_mostly_unused.or(hints.mostly_unused).unwrap_or(false) {
1325+
let pkg_hint_mostly_unused = match hints.mostly_unused {
1326+
None => None,
1327+
Some(toml::Value::Boolean(b)) => Some(b),
1328+
Some(v) => {
1329+
bcx.gctx.shell().warn(format!(
1330+
"ignoring unknown value type ({}) for 'hints.mostly-unused'",
1331+
v.type_str()
1332+
))?;
1333+
None
1334+
}
1335+
};
1336+
if profile_hint_mostly_unused
1337+
.or(pkg_hint_mostly_unused)
1338+
.unwrap_or(false)
1339+
{
13261340
if bcx.gctx.cli_unstable().profile_hint_mostly_unused {
13271341
cmd.arg("-Zhint-mostly-unused");
13281342
} else {
1329-
if hint_mostly_unused.is_some() {
1343+
if profile_hint_mostly_unused.is_some() {
13301344
bcx.gctx
13311345
.shell()
13321346
.warn("ignoring 'hint-mostly-unused' profile option, pass `-Zprofile-hint-mostly-unused` to enable it")?;
1333-
} else if hints.mostly_unused.is_some() {
1347+
} else if pkg_hint_mostly_unused.is_some() {
13341348
bcx.gctx
13351349
.shell()
13361350
.warn("ignoring 'hints.mostly-unused', pass `-Zprofile-hint-mostly-unused` to enable it")?;

tests/testsuite/hints.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,56 @@ fn unknown_hints_warn() {
5858
.run();
5959
}
6060

61+
#[cargo_test]
62+
fn hint_unknown_type_warn() {
63+
Package::new("bar", "1.0.0")
64+
.file(
65+
"Cargo.toml",
66+
r#"
67+
[package]
68+
name = "bar"
69+
version = "1.0.0"
70+
edition = "2015"
71+
72+
[hints]
73+
mostly-unused = 1
74+
"#,
75+
)
76+
.file("src/lib.rs", "")
77+
.publish();
78+
let p = project()
79+
.file(
80+
"Cargo.toml",
81+
r#"
82+
[package]
83+
name = "foo"
84+
version = "0.0.1"
85+
edition = "2015"
86+
87+
[dependencies]
88+
bar = "1.0"
89+
"#,
90+
)
91+
.file("src/main.rs", "fn main() {}")
92+
.build();
93+
p.cargo("check -v")
94+
.with_stderr_data(str![[r#"
95+
[UPDATING] `dummy-registry` index
96+
[LOCKING] 1 package to latest compatible version
97+
[DOWNLOADING] crates ...
98+
[DOWNLOADED] bar v1.0.0 (registry `dummy-registry`)
99+
[WARNING] ignoring unknown value type (integer) for 'hints.mostly-unused'
100+
[CHECKING] bar v1.0.0
101+
[RUNNING] `rustc --crate-name bar [..]`
102+
[CHECKING] foo v0.0.1 ([ROOT]/foo)
103+
[RUNNING] `rustc --crate-name foo [..]`
104+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
105+
106+
"#]])
107+
.with_stderr_does_not_contain("-Zhint-mostly-unused")
108+
.run();
109+
}
110+
61111
#[cargo_test]
62112
fn hints_mostly_unused_warn_without_gate() {
63113
Package::new("bar", "1.0.0")

0 commit comments

Comments
 (0)