-
Notifications
You must be signed in to change notification settings - Fork 1.6k
RFC: #[cfg(version_since(rust, "1.95"))]
for Rust-version conditional compilation
#3857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
4dbd2d2
ecb3459
236f2a5
2ee3718
20233b0
3d827bc
a24a6a4
5a7c0e9
d392d50
e51caf2
cb8acba
a1a8164
c5e9d5f
465cecc
0fed553
3a7609a
55a7632
545ef52
4352a95
c0a7dc4
ce77858
7ba5578
0d7f46c
b2e8cdb
50e6949
2348d45
6f656d5
21b8dc0
ee1c8cf
ec2e54c
96ffa15
a07e1c6
c386700
741f854
5ee6b1e
95c2998
1b8355a
0ab2847
f1dedc5
7139923
e71bdc1
c12e4a4
28b52e1
b445f73
5eef426
9da21e1
d27c10d
0a839bd
005c608
5d01721
e036b23
940b979
b37fe10
2c3cbe4
75c486b
602eec6
e352338
995eeed
c7ebc40
679116a
5accada
19dca3e
12f6745
303bb64
4347c60
53e0d39
8a57b6b
97d86b6
6241a5d
955a11e
3e89ae8
e544016
3e632bf
5b9fefe
1024808
4031627
2bbed78
56305c3
7561a72
fed559b
459c87b
dbbcab4
bc71f84
94d2438
b3b7c76
be317a1
851d2ed
f461c5b
102adb1
83a393c
fb896af
18a9594
f3b4a69
ae5822c
6b7ec78
1589434
cab1e93
674ec04
be94add
79f50aa
3f765c1
56ddfc3
3519b83
b3bff0f
b4338cb
6703ecd
ff3f075
ce9390b
d089ddd
4551bbd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
This reverts commit 7139923.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,8 @@ | |
[summary]: #summary | ||
|
||
Allow Rust-version conditional compilation by adding | ||
- `#[cfg(since(cfg_name, "<version>"))]`, a minimum-version `cfg` predicate | ||
- a built-in `--cfg rust=<version>`, for the Rust language version | ||
- a built-in `--cfg has_rust`, for detecting the presence of the above | ||
- `#[cfg(since(cfg_name, "<version>"))]`, a minimum-version `cfg` predicate | ||
|
||
Say this was added before 1.70, you could do: | ||
```toml | ||
|
@@ -164,10 +163,10 @@ fn main() { | |
Now, | ||
let's say your MSRV is 1.10, | ||
the above code would error when compiling with your MSRV because the `since` predicate does not exist with that version. | ||
However, the presence of `--cfg has_rust` implies that we are on 1.27, | ||
However, the presence of `--cfg rust` implies that we are on 1.27, | ||
so you can "detect" support for `since` by changing your code to: | ||
```rust | ||
#[cfg_attr(has_rust, cfg_attr(since(rust, "1.27"), must_use))] | ||
#[cfg_attr(rust, cfg_attr(since(rust, "1.27"), must_use))] | ||
fn double(x: i32) -> i32 { | ||
2 * x | ||
} | ||
|
@@ -178,10 +177,10 @@ fn main() { | |
// ^--- This warning only happens if we are on Rust >= 1.27. | ||
} | ||
``` | ||
However, this would produce an `unexpected_cfgs` lint on the MSRV compiler and you can avoid that by adding the following to `Cargo.toml`: | ||
However, this would produce an `unexpected_cfgs` lint and you would need to add the following to `Cargo.toml`: | ||
```toml | ||
[lints.rust] | ||
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(has_rust,values(none()))'] } | ||
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(rust,values(none()))'] } | ||
``` | ||
|
||
Say you were wanting to test out `#[must_use]` after it got stabilized on nightly to provide feedback and to be ready for when it hits stable, | ||
|
@@ -275,13 +274,6 @@ Cargo will expose `rust` in: | |
- build scripts as `CARGO_CFG_RUST` | ||
- `[target."cfg()".dependencies]` | ||
|
||
## `has_rust` cfg | ||
|
||
A new built-in cfg `--cfg has_rust` will be added by the compiler | ||
to allow checking of `rust` and `since` can be used. | ||
|
||
`has_rust` will be specified as `--check-cfg 'cfg(rust, values(none()))'` | ||
|
||
## clippy | ||
|
||
Clippy has a [`clippy::incompatible_msrv`](https://rust-lang.github.io/rust-clippy/master/index.html#incompatible_msrv) lint | ||
|
@@ -393,28 +385,31 @@ allow operators inside of the `cfg` `since` predicate, e.g. `#[cfg(since(rust, " | |
However, with the rename of the predicate from `version` to `since`, operators don't fit in as easily. | ||
If someone wanted to support equality checks, there wouldn't be a way to support a continuous range of `values()` but would instead have to manually specify each likely potential version. | ||
|
||
`--check-cfg` will cause the following to warn on older compilers: | ||
`--check-cfg` will cause the following to warn: | ||
```rust | ||
fn is_stderr_terminal() -> bool { | ||
#[cfg(has_rust)] | ||
#[cfg(rust)] | ||
#[cfg(since(rust, "1.70"))] | ||
use std::io::IsTerminal as _; | ||
#[cfg(has_rust)] | ||
#[cfg(rust)] | ||
#[cfg(not(since(rust, "1.70")))] | ||
use is_terminal::IsTerminal as _; | ||
#[cfg(not(has_rust))] | ||
#[cfg(not(rust))] | ||
use is_terminal::IsTerminal as _; | ||
|
||
std::io::stderr().is_terminal() | ||
} | ||
``` | ||
|
||
To allow checking for the presence of `has_rust` without warnings on older compiler versions, | ||
add the following to your `Cargo.toml`: | ||
To allow checking for the presence of `rust`, add the following to your `Cargo.toml`: | ||
```toml | ||
[lints.rust] | ||
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(has_rust,values(none()))'] } | ||
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(rust,values(none()))'] } | ||
``` | ||
Alternatively, we could have the built-in `--check-cfg` for `rust` include `values(none())` but: | ||
- When building on an old version, users will see the warning and will likely want to add it anyways. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not including it seems odd to me: We provide a cfg for you to match on, but warn if you try to use it. It's a bit like punishing you for using the happy path, and you might not be a crate maintainer who cares about warnings on older crate versions. I do see one benefit though: we can provide a dedicated message explaining what to add to your Cargo.toml. This option would not be available if you only saw the warning in older compilers. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
- We lose out on `--check-cfg` identifying misused. | ||
Instead, we may wish to add a dedicated predicate intended for "is set". | ||
|
||
## `rust` cfg rationale | ||
|
||
|
@@ -425,20 +420,6 @@ Rust does appear in some parts of the language, | |
but is capitalized like with [`repr(Rust)`](https://doc.rust-lang.org/reference/type-layout.html?#the-rust-representation). | ||
However, the convention for `--cfg`s is generally lower case. | ||
|
||
## `has_rust` cfg rationale | ||
|
||
We'd prefer to allow adoption of `since(rust, "<version>")` | ||
without requiring an MSRV bump so core packages with low MSRVs can take advantage of it immediately. | ||
However, unknown predicates are a compiler error and there isn't a way to check if a cfg with a value is set, | ||
so we added this cfg which is either set or unset and can be checked with `#[cfg(has_rust)]`. | ||
|
||
Without the context of `#[cfg(since(rust, "<version>"))]`, | ||
`has_rust` could be confusing (this compiling with a Rust compiler, why is it a question?). | ||
|
||
Other potential names: | ||
- `rust_is_set` | ||
- `has_since` | ||
|
||
### Pre-release | ||
|
||
When translating `rustc --version` to a language version, we have several choices when it comes to pre-releases, including: | ||
|
@@ -514,7 +495,7 @@ between the version it was first supported up to the current version, | |
making the `=` operate as a "contains" operator, | ||
rather than an equality operator, | ||
like with `#[cfg(feature = "std")]`. | ||
This was proposed to allow `#[cfg_attr(rust = "1.95")]` to more naturally allow adoption before the feature is stabilized. | ||
This was proposed to allow `#[cfg_attr(rust, cfg(rust = "1.95"))]` to more naturally allow adoption before the feature is stabilized. | ||
|
||
This could be implemented statically, by hard coding the versions. | ||
This would work with `--print-cfg` and so would automatically work with Cargo. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can omit
values(none())
here, i.e.Based on my reading of the rustc book.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gave it a try in 102adb1.
After making that change, I feel like its clearer to see
values(none())
especially in contrast to othervalues()
but open to other thoughts. This is more about what we document than anything else and isn't critical at this stage.