- Feature Name: N/A
- Start Date: 2019-09-24
- RFC PR: rust-lang/rfcs#0000
- Rust Issue: N/A
Summary
Cargo should alert developers to upstream dependencies that trigger
future-incompatibility warnings. Cargo should list such dependencies
even when these warnings have been suppressed (e.g. via cap-lints or
#[allow(..)]
attributes.)
Cargo could additionally provide feedback for tactics a maintainer of the downstream crate could use to address the problem (the details of such tactics is not specified nor mandated by this RFC).
Motivation
From rust-lang/rust#34596:
if you author a library that is widely used, but which you are not actively using at the moment, you might not notice that it will break in the future -- moreover, your users won't either, since cargo will cap lints when it builds your library as a dependency.
Today, cargo will cap lints when it builds libraries as dependencies. This behavior includes future-incompatibility lints.
As a running example, assume we have a crate unwary
with an upstream
crate dependency brash
, and brash
has code that triggers a
future-incompatibility lint, such as match x { 3.4 => 5, _ => 6 }
,
(see rust-lang/rust#41620).
If brash
is a non-path dependency of unwary
, then building
unwary
will suppress the warning associated with brash
in its
diagnostic output, because the build of brash
will pass
--cap-lints=allow
to its rustc
invocation. This means that a
future version of Rust is going to fail to compile the unwary
project, with no warning to the developer of unwary
.
Example of today's behavior (where in this case, brash
is non-path dependency of unwary
):
crates % cd unwary
unwary % cargo build # no warning issued about problem in the `brash` dependency.
Compiling brash v0.1.0
Compiling unwary v0.1.0 (/tmp/unwary)
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
unwary % cd ../brash
brash % cargo build # (but a `brash` developer will see it when they build.)
Compiling brash v0.1.0 (/tmp/brash)
warning: floating-point types cannot be used in patterns
--> /tmp/brash/src/lib.rs:1:37
|
1 | pub fn f() { let x = 1.2; match x { 3.4 => 5, _ => 6 }; }
| ^^^
|
= note: #[warn(illegal_floating_point_literal_pattern)] on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #41620 <https://github.com/rust-lang/rust/issues/41620>
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
brash %
Even if brash
is a path-dependency of unwary
(which means building
unwary
will issue the warning associated with brash
when it builds
that crate), but subsequent rebuilds of unwary
will not rebuild
brash
, and thus will not emit the diagnostic for the
future-incompatibility issue (at least not until unwary
updates to a
newer version of brash
or of Rust itself).
Example of today's behavior (where in this case, brash
is a path dependency of unwary
):
crates % cd unwary
unwary % cargo build # We *do* see warning on first build...
Compiling brash v0.1.0 (/tmp/brash)
warning: floating-point types cannot be used in patterns
--> /tmp/brash/src/lib.rs:1:37
|
1 | pub fn f() { let x = 1.2; match x { 3.4 => 5, _ => 6 }; }
| ^^^
|
= note: #[warn(illegal_floating_point_literal_pattern)] on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #41620 <https://github.com/rust-lang/rust/issues/41620>
Compiling unwary v0.1.0 (/tmp/unwary)
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
unwary % touch src/main.rs
unwary % cargo build # ... but no warning issued about still-present problem in path dependency.
Compiling unwary v0.1.0 (/tmp/unwary)
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
unwary %
Therefore, if the developer does not do something to address the
warning the first time they see it, it is likely to go unaddressed
until the developer upgrades to a version of Rust that actually fails
to compile brash
. This is a frustrating developer experience.
Cargo passes --cap-lints=allow
on upstream dependencies for good
reason, as discussed in Rust RFC 1193 and the comment thread from
rust-lang/rust#59658.
For cases like future-incompatibility lints, which
are more severe warnings for the long-term viability of a crate, we
need to provide some feedback to the unwary
maintainer.
But this feedback should not be just the raw diagnostic output for
brash
! The developer of a crate like unwary
typically cannot do
anything in the short term about warnings emitted by upstream crates,
and therefore the diagnostics associated with building upstream
brash
are usually just noise from the viewpoint of unwary
's
maintainer.
Therefore, we want to continue passing --cap-lints=allow
for
upstream dependencies. But we also want rustc
to tell cargo
(via
some channel) about when future-incompatibility lints are triggered,
and we want cargo
to provide a succinct report of the triggers.
Furthermore, we want the feedback to provide guidance as to how the
unwary
maintainer can address the issue. Here are some potential
forms this additional guidance could take.
-
If cargo is not running in "offline mode", it can take the future-incompatibilty signaling as an opportunity to query
crates.io
to find out if a newer version of the upstream crate is available, and if so, suggest to the user they might upgrade to it. If such an upgrade could be done viacargo update
, then the output could obviously suggest that as well. (This is just a heuristic measure, as it would not attempt to check ahead of time if the newer version actually resolves the problem in question.) -
Cargo could suggest to the
unwary
maintainer that they file a bug (or search for previously-filed bug) in the source repository for the upstream crate that is issuing the future-incompatibility warning. (That is, thebrash
author might not be aware of the issue; for example, if they last updated their crate before the lint in question was deployed on the Rust compiler.) -
rustc
itself could embed, for each future-incompatibility lint, how soon the Rust developers will turn the lint to a hard error. This would give theunwary
maintainer an idea of how much time they have before they will be forced to address the issue (by posting a PR upstream, or switching to a fork ofbrash
, et cetera).
This RFC suggests the provided feedback take the form of a summary at
the end of the build of unwary
, as illustrated in the explanation below.
Guide-level explanation
After cargo finishes compiling a crate and its upstream dependencies, it may include a final warning about future incompatibilties.
A future incompatibility is a pattern of code that is scheduled to be removed from the language in some future release. Such code patterns are usually instances of constructs that exhibit undefined behavior (i.e. they are unsound), but are in widespread use and thus need a grace period before they are removed.
If any crate or any of its upstream dependencies has code that triggers a future incompatibility warning, but the overall compilation is otherwise without error, then cargo will report all instances of crates with future incompatibilities at the end of the compilation. When possible, this report includes the future date or release version where we expect Rust to stop compiling the code in question.
Example:
crates % cd unwary
unwary % cargo build
Compiling brash v0.1.0
Compiling bold v0.1.0
Compiling rash v0.1.0
Compiling unwary v0.1.0
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
warning: the crates brash, bold, and rash contain code that will be rejected by a future version of Rust.
note: the crate rash will stop compiling in Rust 1.50 (scheduled for February 2021).
unwary %
Rebuilding unwary
continues to emit the report even if the upstream
dependencies are not rebuilt.
Example:
unwary % touch src/main.rs
unwary % cargo build
Compiling unwary v0.1.0
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
warning: the crates brash, bold, and rash contain code that will be rejected by a future version of Rust.
note: the crate rash will stop compiling in Rust 1.50 (scheduled for February 2021).
unwary %
To keep the user experience consistent, we should probably emit the same warning at the end even when the root crate is the sole trigger of incompatibility lints.
crates % cd brash
brash % cargo build
Compiling brash v0.1.0 (/tmp/brash)
warning: floating-point types cannot be used in patterns
--> /tmp/brash/src/lib.rs:1:37
|
1 | pub fn f() { let x = 1.2; match x { 3.4 => 5, _ => 6 }; }
| ^^^
|
= note: #[warn(illegal_floating_point_literal_pattern)] on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #41620 <https://github.com/rust-lang/rust/issues/41620>
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
warning: the crate brash contains code that will be rejected by a future version of Rust.
brash % cargo build
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
warning: the crate brash contains code that will be rejected by a future version of Rust.
brash %
And as you might expect, if there are no future-incompatibilty warnings issused, then the output of cargo
is unchanged from today.
Example:
crates % cd unwary
unwary % cargo build
Compiling brash v0.2.0
Compiling bold2 v0.1.0
Compiling unwary v0.2.0
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
Here, the unwary (sic) crate has updated its version of brash
,
switched to bold2
(a fork of bold
), and replaced its internal
usage of rash
with some local code, thus completely eliminating all
current future-incompatibility lint triggers.
Reference-level explanation
As noted above, we want to continue to suppress normal lint checks for
upstream dependencies. Therefore, Cargo will continue to pass
--cap-lints=allow
for non-path upstream depedencies.
However, the Rust compiler's behavior will change slightly. Even when
--cap-lints=allow
is turned on, we need Cargo to know when a
future-incompatibilty lint is triggered.
The division of responsbiilties between Cargo and the Rust compiler may be a little subtle:
The responsibilities of the Rust compiler (rustc
):
-
rustc
must differentiate future-incompatibility lints (c.f. PR #59658: "Minimum Lint Levels") from other lints that are expected to remain as mere warnings forever. -
rustc
will need to have some new mode of operation, which this RFC will call the where it will check for instances of future-incompatibility lints, regardless of whether--cap-lints=allow
is also set. This RFC calls this the future-incompatibility checking mode.-
In the future-incompatibility checking mode of invocation,
rustc
will also need to check for such lints regardless of whether the code appears in the scope of an#[allow(..)]
attribute for the lint. -
In the future-incompatibility checking mode of invocation, emission of the diagnostics themselves may still be silenced as specified by
--cap-lints=allow
or#[allow(..)]
attributes. -
That is, those flags and annotations should be interpreted by
rustc
as silencing the diagnostic report, but not as silencing the feedback about there being some instance of the triggering somewhere in the crate's source code. -
The future-incompatibility checking mode is meant as a way to address the bulk of issue rust-lang/rust#34596.
-
The responsibilities of Cargo:
-
Cargo is responsible for invoking
rustc
in a way that enables the future-incompatibility checking mode. This mode of invocation occurs regardless of whether--cap-lints=allow
is also being passed when the crate is compiled. -
Cargo is responsible for capturing any output from the future-incompatibility checking mode and summarizing it at the end of the whole build.
-
Cargo is responsible for storing a record of any future-incompatibility for a crate somewhere in the
build/
directory, so that it can emit the same report without having to rebuild the crate. -
Cargo is responsible for suggesting ways to address the problem to the user. The specific tactics for constructing such suggestions are not mandated by this RFC, but some ideas are presented as Future possibilities.
Implementation strategy: Leverage JSON error-format
The cleanest way to implement the above division of responsibilities
without perturbing non-cargo uses of rustc
is probably to make two changes:
-
Cargo should always use
--error-format=json
for itsrustc
invocations, and -
rustc
should treat--error-format=json
as signal that it should emit a future-incompatibility summary report for the crate.
It is relatively easy to extend the JSON output of rustc
to include
a new record of any future-incompatibility lints that were triggered
when compiling a given crate.
- However, this RFC does not dictate this choice of implementation
strategy. (Other options include using some environment variable to
opt-in to a change to
rustc
's output, or havingrustc
emit future-incompatibility metadata to the filesystem.)
Some (but not all) future-incompatibility lints will have a concrete schedule established for when they are meant to become hard errors. (This RFC does not specify the details about how such schedules are established or what constraints they will have to meet; it just posits that they will be established by some means.) The metadata for every future-incompatibility lint should include the anticipated version of Rust, if known, where it will become a hard error.
- This adds motivation for using JSON formatted diagnostics: JSON records are more readily extensible to support this sort of feedback in a robust fashion.
Since cargo
is expected to continue to emit the report even when the
upstream dependencies are not rebuilt, Cargo will store the
future-incompatibility status for each crate somewhere in the build/
directory on the file-system. (This is analogous to how incremental
compilation caches diagnostic output, so that future runs will still
emit the same report even when we do not recompile the same input.)
Drawbacks
The change as described requires revisions to both rustc
and
cargo
, which can be tricky to coordinate.
This RFC suggests an approach where the changes are somewhat loosely
coupled: Use of --error-format=json
will enable the
future-compatibility checking mode. This avoids the need to add a new
stable command line flag to rustc
; but it also may be a confusing
change in compiler semantics for any non-cargo client of rustc
that
is using --error-format=json
.
Emitting a warning on all following cargo
invocations until the problem is resolved may be overkill. In particular, it may not be reasonable for someone to resolve this problem in the short term. (Perhaps we could restrict it so that it only gets re-emitted once each day or something; but of course then the diagnostic would have to state very clearly that it is doing that strange magic.)
Rationale and alternatives
No change needed?
Some claim that "our current approach" has been working, and therefore no change is needed here. However, my counterargument is that the only reason we haven't had to resolve this before is that compiler and language teams have been relatively conservative in changing existing future-incompatibility lints into hard errors.
Is there something simpler?
The immediate simple approaches to resolving this problem would have serious drawbacks.
Can be do this in Cargo alone?
With regards to implementation, we could avoid attempt making changes
to rustc
itself, and isolate the implementation to cargo
alone.
The main way I can imagine doing this is to stop passing
--cap-lints=allow
, and then having Cargo capture all diagnostic
output from the compiler and post-processing it to determine which
lints are future-incompatible warnings. However, ths has a number of
problems:
-
It is fragile, since it relies on Cargo post-processing the compiler diagnostic output.
-
It is inefficient, since the compiler will now always run all the lints checks for all dependencies of a crate but we only care about a small subset of the lints.
-
It is insufficient, because it only handles instances of
--cap-lints
; it would fail to catch instances where an upstream dependency is using an#[allow(..)]
attribute in the source to sidestep warnings.- If we addressed that insufficiency by unconditionally changing
rustc
to always emit feedback about future-incompatibilities regardless of--cap-lints=allow
or#[allow(..)]
, then that would probably upset people who expect those flags/annotations to keep the diagnostic output quiet. (In other words, that would be an instance of "the compiler is not listening to me!")
- If we addressed that insufficiency by unconditionally changing
Can we do this in Rust alone?
PR rust-lang/rust#59658 "Minimum Lint Levels" implemented a
solution in the compiler alone, by tagging the future-incompatibility
lints as special cases that would not be silenced by --cap-lints
nor
#[allow(..)]
. The discussion on that PR described a number of
problems with this; in essence, people were concerned about getting
spammed by lints that the downstream developer couldn't actually do
anything about.
The discussion on that PR concluded by saying that it could possibly be reworked to reduce the amount of spam by reporting a single instance of a lint for each dependency (rather than having a separate diagnostic for each expression that triggered the lint within that dependency).
- The latter would indeed be an improvement on PR #rust-lang/rust#59658, but it would not be an ideal user-experience. The change suggested by this RFC deliberately treats occurrences of future-incompatibility lints as separate from normal diagnostics: serious events worthy of being treated specially by Cargo, to the extent that it would do an online query to see if a newer version of the given crate exists. We want to make the process of fixing these issues as easy as we can for the developer, and doing that requires help from Cargo.
Prior art
None I know of, but I'm happy to be educated.
(Has Python done anything here with the migration from Python 2 to Python 3? I briefly did some web searches but failed to find much of use.)
Unresolved questions
There are future-incompatibility warnings emitted by cargo itself, such as discussed on rust-lang/cargo#6313 (comment). I imagine it shouldn't require much to incorporate support for them, but it is something I haven't explicitly addressed nor investigated.
Implementation questions
-
Is using
--error-format=json
as the way to switchrustc
into the future-incompatibility checking mode reasonable? -
Is turning on
--error-format=json
by default everywhere incargo
reasonable?
Future possibilities
The main point I envisage for follow-up work to this RFC is the form of feedback that Cargo gives regarding the issues.
Cargo is responsible for suggesting to the users ways to address an instance of future-incompatbility.
Following are some ideas for suggestions.
Query for newer/alternate versions of the crate
When crates trigger future-incompatibility warnings, Cargo could look for newer versions of the dependency on crates.io.
Example:
crates % cd unwary
unwary % cargo build
Compiling brash v0.1.0
Compiling bold v0.1.0
Compiling rash v0.1.0
Compiling unwary v0.1.0
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
warning: the crates brash, bold, and rash contain code that will be rejected by a future version of Rust.
note: the crate rash will stop compiling in Rust 1.50 (scheduled for February 2021).
note: newer versions of bold and rash are available; upgrading to them via `cargo update` may resolve their problems.
unwary %
This suggestion as written only covers upgrading to newer versions. But with more help from crates.io itself, we could go even further here: We could suggest potential forks of the upstream crate that one might switch to using. This could be useful in dealing with abandonware.
Suggest a bug report
If no newer version of the triggering crate is available, Cargo could print a template for a bug report the user could file with the upstream crate.
Example:
crates % cd unwary
unwary % cargo build
Compiling brash v0.1.0
Compiling unwary v0.1.0
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
warning: the crate brash contains code that will be rejected by a future version of Rust.
note: the following gist contains a bug report you might consider filing with the maintainer of brash.
https://gist.github.com/pnkfelix/ae03d3ea95160fb71a797b15e05f8d49
unwary %
For ease of reference, here is the text located at the gist url above (brash: future incompat issue · GitHub):
This crate currently triggers a future incompatibility warning with Rust.
In src/lib.rs:1:37, there is the following code:
pub fn f() { let x = 1.2; match x { 3.4 => 5, _ => 6 }; }
This causes
rustc
to issue the following diagnostic, from https://github.com/rust-lang/rust/issues/41620warning: floating-point types cannot be used in patterns --> /tmp/brash/src/lib.rs:1:37 | 1 | pub fn f() { let x = 1.2; match x { 3.4 => 5, _ => 6 }; } | ^^^ | = note: #[warn(illegal_floating_point_literal_pattern)] on by default = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #41620 <https://github.com/rust-lang/rust/issues/41620>
Since this construct is going to become a hard error in the future, we should eliminate occurrences of it.
Further refinement of this idea: If we did start suggesting bug report templates, then Cargo might also be able to search for occurrences of the template on that crate's repostory, and advise the user to inspect that bug report to see its current status, rather than file a new bug with the upstream crate, which might be annoying for those maintainers.