mirror of
https://github.com/rust-lang/cargo.git
synced 2025-10-01 11:30:39 +00:00

The `[hints]` table in a `Cargo.toml` manifest provides optional information that Cargo can use for building the package, and will use even when using the package as a dependency. All hints can be safely ignored, and Cargo only warns about unknown hints, but does not error. This allows packages to use hints without depending on new Cargo. Add a `mostly-unused` hint, which allows a package to hint that most users of the package will not use most of its items. This is useful for improving the build performance of crates with large dependencies. Crates can override this hint using `hint-mostly-unused = false` in their profile for a dependency.
298 lines
7.6 KiB
Rust
298 lines
7.6 KiB
Rust
//! Tests for hints.
|
|
|
|
use crate::prelude::*;
|
|
use cargo_test_support::registry::Package;
|
|
use cargo_test_support::{project, str};
|
|
|
|
#[cargo_test]
|
|
fn empty_hints_no_warn() {
|
|
let p = project()
|
|
.file(
|
|
"Cargo.toml",
|
|
r#"
|
|
[package]
|
|
name = "foo"
|
|
version = "0.0.1"
|
|
edition = "2015"
|
|
|
|
[hints]
|
|
"#,
|
|
)
|
|
.file("src/main.rs", "fn main() {}")
|
|
.build();
|
|
p.cargo("check -v")
|
|
.with_stderr_data(str![[r#"
|
|
[CHECKING] foo v0.0.1 ([ROOT]/foo)
|
|
[RUNNING] `rustc --crate-name foo [..]`
|
|
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
|
|
|
|
"#]])
|
|
.run();
|
|
}
|
|
|
|
#[cargo_test]
|
|
fn unknown_hints_warn() {
|
|
let p = project()
|
|
.file(
|
|
"Cargo.toml",
|
|
r#"
|
|
[package]
|
|
name = "foo"
|
|
version = "0.0.1"
|
|
edition = "2015"
|
|
|
|
[hints]
|
|
this-is-an-unknown-hint = true
|
|
"#,
|
|
)
|
|
.file("src/main.rs", "fn main() {}")
|
|
.build();
|
|
p.cargo("check -v")
|
|
.with_stderr_data(str![[r#"
|
|
[WARNING] unused manifest key: hints.this-is-an-unknown-hint
|
|
[CHECKING] foo v0.0.1 ([ROOT]/foo)
|
|
[RUNNING] `rustc --crate-name foo [..]`
|
|
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
|
|
|
|
"#]])
|
|
.run();
|
|
}
|
|
|
|
#[cargo_test]
|
|
fn hint_unknown_type_warn() {
|
|
Package::new("bar", "1.0.0")
|
|
.file(
|
|
"Cargo.toml",
|
|
r#"
|
|
[package]
|
|
name = "bar"
|
|
version = "1.0.0"
|
|
edition = "2015"
|
|
|
|
[hints]
|
|
mostly-unused = 1
|
|
"#,
|
|
)
|
|
.file("src/lib.rs", "")
|
|
.publish();
|
|
let p = project()
|
|
.file(
|
|
"Cargo.toml",
|
|
r#"
|
|
[package]
|
|
name = "foo"
|
|
version = "0.0.1"
|
|
edition = "2015"
|
|
|
|
[dependencies]
|
|
bar = "1.0"
|
|
"#,
|
|
)
|
|
.file("src/main.rs", "fn main() {}")
|
|
.build();
|
|
p.cargo("check -v")
|
|
.with_status(101)
|
|
.with_stderr_data(str![[r#"
|
|
[UPDATING] `dummy-registry` index
|
|
[LOCKING] 1 package to latest compatible version
|
|
[DOWNLOADING] crates ...
|
|
[DOWNLOADED] bar v1.0.0 (registry `dummy-registry`)
|
|
[ERROR] invalid type: integer `1`, expected a boolean
|
|
--> ../home/.cargo/registry/src/-[HASH]/bar-1.0.0/Cargo.toml:8:29
|
|
|
|
|
8 | mostly-unused = 1
|
|
| ^
|
|
|
|
|
[ERROR] failed to download replaced source registry `crates-io`
|
|
|
|
"#]])
|
|
.with_stderr_does_not_contain("-Zhint-mostly-unused")
|
|
.run();
|
|
}
|
|
|
|
#[cargo_test]
|
|
fn hints_mostly_unused_warn_without_gate() {
|
|
Package::new("bar", "1.0.0")
|
|
.file(
|
|
"Cargo.toml",
|
|
r#"
|
|
[package]
|
|
name = "bar"
|
|
version = "1.0.0"
|
|
edition = "2015"
|
|
|
|
[hints]
|
|
mostly-unused = true
|
|
"#,
|
|
)
|
|
.file("src/lib.rs", "")
|
|
.publish();
|
|
let p = project()
|
|
.file(
|
|
"Cargo.toml",
|
|
r#"
|
|
[package]
|
|
name = "foo"
|
|
version = "0.0.1"
|
|
edition = "2015"
|
|
|
|
[dependencies]
|
|
bar = "1.0"
|
|
"#,
|
|
)
|
|
.file("src/main.rs", "fn main() {}")
|
|
.build();
|
|
p.cargo("check -v")
|
|
.with_stderr_data(str![[r#"
|
|
[UPDATING] `dummy-registry` index
|
|
[LOCKING] 1 package to latest compatible version
|
|
[DOWNLOADING] crates ...
|
|
[DOWNLOADED] bar v1.0.0 (registry `dummy-registry`)
|
|
[WARNING] ignoring 'hints.mostly-unused', pass `-Zprofile-hint-mostly-unused` to enable it
|
|
[CHECKING] bar v1.0.0
|
|
[RUNNING] `rustc --crate-name bar [..]`
|
|
[CHECKING] foo v0.0.1 ([ROOT]/foo)
|
|
[RUNNING] `rustc --crate-name foo [..]`
|
|
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
|
|
|
|
"#]])
|
|
.with_stderr_does_not_contain("-Zhint-mostly-unused")
|
|
.run();
|
|
}
|
|
|
|
#[cargo_test(nightly, reason = "-Zhint-mostly-unused is unstable")]
|
|
fn hints_mostly_unused_nightly() {
|
|
Package::new("bar", "1.0.0")
|
|
.file(
|
|
"Cargo.toml",
|
|
r#"
|
|
[package]
|
|
name = "bar"
|
|
version = "1.0.0"
|
|
edition = "2015"
|
|
|
|
[hints]
|
|
mostly-unused = true
|
|
"#,
|
|
)
|
|
.file("src/lib.rs", "")
|
|
.publish();
|
|
let p = project()
|
|
.file(
|
|
"Cargo.toml",
|
|
r#"
|
|
[package]
|
|
name = "foo"
|
|
version = "0.0.1"
|
|
edition = "2015"
|
|
|
|
[dependencies]
|
|
bar = "1.0"
|
|
"#,
|
|
)
|
|
.file("src/main.rs", "fn main() {}")
|
|
.build();
|
|
p.cargo("check -Zprofile-hint-mostly-unused -v")
|
|
.masquerade_as_nightly_cargo(&["profile-hint-mostly-unused"])
|
|
.with_stderr_data(str![[r#"
|
|
[UPDATING] `dummy-registry` index
|
|
[LOCKING] 1 package to latest compatible version
|
|
[DOWNLOADING] crates ...
|
|
[DOWNLOADED] bar v1.0.0 (registry `dummy-registry`)
|
|
[CHECKING] bar v1.0.0
|
|
[RUNNING] `rustc --crate-name bar [..] -Zhint-mostly-unused [..]`
|
|
[CHECKING] foo v0.0.1 ([ROOT]/foo)
|
|
[RUNNING] `rustc --crate-name foo [..]`
|
|
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
|
|
|
|
"#]])
|
|
.with_stderr_does_not_contain(
|
|
"[RUNNING] `rustc --crate-name foo [..] -Zhint-mostly-unused [..]",
|
|
)
|
|
.run();
|
|
}
|
|
|
|
#[cargo_test(nightly, reason = "-Zhint-mostly-unused is unstable")]
|
|
fn mostly_unused_profile_overrides_hints_nightly() {
|
|
Package::new("bar", "1.0.0")
|
|
.file(
|
|
"Cargo.toml",
|
|
r#"
|
|
[package]
|
|
name = "bar"
|
|
version = "1.0.0"
|
|
edition = "2015"
|
|
|
|
[hints]
|
|
mostly-unused = true
|
|
"#,
|
|
)
|
|
.file("src/lib.rs", "")
|
|
.publish();
|
|
let p = project()
|
|
.file(
|
|
"Cargo.toml",
|
|
r#"
|
|
[package]
|
|
name = "foo"
|
|
version = "0.0.1"
|
|
edition = "2015"
|
|
|
|
[dependencies]
|
|
bar = "1.0"
|
|
|
|
[profile.dev.package.bar]
|
|
hint-mostly-unused = false
|
|
"#,
|
|
)
|
|
.file("src/main.rs", "fn main() {}")
|
|
.build();
|
|
p.cargo("check -Zprofile-hint-mostly-unused -v")
|
|
.masquerade_as_nightly_cargo(&["profile-hint-mostly-unused"])
|
|
.with_stderr_data(str![[r#"
|
|
[UPDATING] `dummy-registry` index
|
|
[LOCKING] 1 package to latest compatible version
|
|
[DOWNLOADING] crates ...
|
|
[DOWNLOADED] bar v1.0.0 (registry `dummy-registry`)
|
|
[CHECKING] bar v1.0.0
|
|
[RUNNING] `rustc --crate-name bar [..]`
|
|
[CHECKING] foo v0.0.1 ([ROOT]/foo)
|
|
[RUNNING] `rustc --crate-name foo [..]`
|
|
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
|
|
|
|
"#]])
|
|
.with_stderr_does_not_contain("-Zhint-mostly-unused")
|
|
.run();
|
|
}
|
|
|
|
#[cargo_test(nightly, reason = "-Zhint-mostly-unused is unstable")]
|
|
fn mostly_unused_profile_overrides_hints_on_self_nightly() {
|
|
let p = project()
|
|
.file(
|
|
"Cargo.toml",
|
|
r#"
|
|
[package]
|
|
name = "foo"
|
|
version = "0.0.1"
|
|
edition = "2015"
|
|
|
|
[hints]
|
|
mostly-unused = true
|
|
|
|
[profile.dev]
|
|
hint-mostly-unused = false
|
|
"#,
|
|
)
|
|
.file("src/main.rs", "fn main() {}")
|
|
.build();
|
|
p.cargo("check -v")
|
|
.with_stderr_data(str![[r#"
|
|
[CHECKING] foo v0.0.1 ([ROOT]/foo)
|
|
[RUNNING] `rustc --crate-name foo [..]`
|
|
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
|
|
|
|
"#]])
|
|
.with_stderr_does_not_contain("-Zhint-mostly-unused")
|
|
.run();
|
|
}
|