Skip to content

Commit e5e68c4

Browse files
committed
Auto merge of #12148 - epage:lints, r=weihanglo
feat: `lints` feature ### What does this PR try to resolve? Implement rust-lang/rfcs#3389 which shifts a subset of `.cargo/config.toml` functionality to `Cargo.toml` by adding a `[lints]` table. This **should** cover all of the user-facing aspects of the RFC - This doesn't reduce what flags we fingerprint - This will fail if any lint name as `::` in it. What to do in this case was in the RFC discussion but I couldn't find the thread to see what all was involved in that discussion - This does not fail if a `[lints]` table is present or malformed unless nightly with the `lints` feature enabled - The idea is this will act like a `[lints]` table is present in an existing version of rust, ignore it - The intent is to not force an MSRV bump to use it. - When disabled, it will be a warning - When disabled, it will be stripped so we don't publish it Tracking issue for this is #12115. ### How should we test and review this PR? 1. Look at this commit by commit to see it gradually build up 2. Look through the final set of test cases to make sure everything in the RFC is covered I tried to write this in a way that will make it easy to strip out the special handling of this unstable feature, both in code and commit history ### Additional information I'd love to bypass the need for `cargo-features = ["lints"]` so users today can test it on their existing projects but hesitated for now. We can re-evaluate that later. I broke out the `warn_for_feature` as an experiment towards us systemitizing this stabilization approach which we also used with #9732. This works well when we can ignore the new information which isn't too often but sometimes happens. This does involve a subtle change to `profile.rustflags` precedence but its nightly and most likely people won't notice it? The benefit is its in a location more like the rest of the rustflags.
2 parents a27af88 + b435eda commit e5e68c4

File tree

8 files changed

+1008
-4
lines changed

8 files changed

+1008
-4
lines changed

src/cargo/core/compiler/fingerprint/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
//! [`Lto`] flags | ✓ | ✓
7676
//! config settings[^5] | ✓ |
7777
//! is_std | | ✓
78+
//! `[lints]` table[^6] | ✓ |
7879
//!
7980
//! [^1]: Build script and bin dependencies are not included.
8081
//!
@@ -86,6 +87,8 @@
8687
//! [^5]: Config settings that are not otherwise captured anywhere else.
8788
//! Currently, this is only `doc.extern-map`.
8889
//!
90+
//! [^6]: Via [`Manifest::lint_rustflags`][crate::core::Manifest::lint_rustflags]
91+
//!
8992
//! When deciding what should go in the Metadata vs the Fingerprint, consider
9093
//! that some files (like dylibs) do not have a hash in their filename. Thus,
9194
//! if a value changes, only the fingerprint will detect the change (consider,
@@ -1414,6 +1417,7 @@ fn calculate_normal(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Finger
14141417
unit.mode,
14151418
cx.bcx.extra_args_for(unit),
14161419
cx.lto[unit],
1420+
unit.pkg.manifest().lint_rustflags(),
14171421
));
14181422
// Include metadata since it is exposed as environment variables.
14191423
let m = unit.pkg.manifest().metadata();

src/cargo/core/compiler/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,7 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
762762
add_error_format_and_color(cx, &mut rustdoc);
763763
add_allow_features(cx, &mut rustdoc);
764764

765+
rustdoc.args(unit.pkg.manifest().lint_rustflags());
765766
if let Some(args) = cx.bcx.extra_args_for(unit) {
766767
rustdoc.args(args);
767768
}
@@ -1040,10 +1041,6 @@ fn build_base_args(
10401041
cmd.arg("-C").arg(&format!("opt-level={}", opt_level));
10411042
}
10421043

1043-
if !rustflags.is_empty() {
1044-
cmd.args(&rustflags);
1045-
}
1046-
10471044
if *panic != PanicStrategy::Unwind {
10481045
cmd.arg("-C").arg(format!("panic={}", panic));
10491046
}
@@ -1078,6 +1075,10 @@ fn build_base_args(
10781075
cmd.arg("-C").arg(format!("debuginfo={}", debuginfo));
10791076
}
10801077

1078+
cmd.args(unit.pkg.manifest().lint_rustflags());
1079+
if !rustflags.is_empty() {
1080+
cmd.args(&rustflags);
1081+
}
10811082
if let Some(args) = cx.bcx.extra_args_for(unit) {
10821083
cmd.args(args);
10831084
}

src/cargo/core/features.rs

+3
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,9 @@ features! {
483483

484484
// Allow specifying rustflags directly in a profile
485485
(stable, workspace_inheritance, "1.64", "reference/unstable.html#workspace-inheritance"),
486+
487+
// Allow specifying rustflags directly in a profile
488+
(unstable, lints, "", "reference/unstable.html#lints"),
486489
}
487490

488491
pub struct Feature {

src/cargo/core/manifest.rs

+8
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub struct Manifest {
6363
default_run: Option<String>,
6464
metabuild: Option<Vec<String>>,
6565
resolve_behavior: Option<ResolveBehavior>,
66+
lint_rustflags: Vec<String>,
6667
}
6768

6869
/// When parsing `Cargo.toml`, some warnings should silenced
@@ -405,6 +406,7 @@ impl Manifest {
405406
original: Rc<TomlManifest>,
406407
metabuild: Option<Vec<String>>,
407408
resolve_behavior: Option<ResolveBehavior>,
409+
lint_rustflags: Vec<String>,
408410
) -> Manifest {
409411
Manifest {
410412
summary,
@@ -430,6 +432,7 @@ impl Manifest {
430432
default_run,
431433
metabuild,
432434
resolve_behavior,
435+
lint_rustflags,
433436
}
434437
}
435438

@@ -514,6 +517,11 @@ impl Manifest {
514517
self.resolve_behavior
515518
}
516519

520+
/// `RUSTFLAGS` from the `[lints]` table
521+
pub fn lint_rustflags(&self) -> &[String] {
522+
self.lint_rustflags.as_slice()
523+
}
524+
517525
pub fn map_source(self, to_replace: SourceId, replace_with: SourceId) -> Manifest {
518526
Manifest {
519527
summary: self.summary.map_source(to_replace, replace_with),

0 commit comments

Comments
 (0)