Skip to content

Commit 14aa12f

Browse files
committed
Replace version_check dependency with own version parsing code
This gives compiler maintainers a better degree of control over how the version gets parsed and is a good way to ensure that there are no changes of behaviour in the future. Also, issue a warning if the version is invalid instead of erroring so that we stay forwards compatible with possible future changes of the versioning scheme. Last, this improves the present test a little.
1 parent 202720b commit 14aa12f

File tree

5 files changed

+138
-36
lines changed

5 files changed

+138
-36
lines changed

Diff for: Cargo.lock

-1
Original file line numberDiff line numberDiff line change
@@ -3568,7 +3568,6 @@ dependencies = [
35683568
"rustc_serialize",
35693569
"rustc_session",
35703570
"rustc_span",
3571-
"version_check",
35723571
]
35733572

35743573
[[package]]

Diff for: compiler/rustc_attr/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@ rustc_lexer = { path = "../rustc_lexer" }
1818
rustc_macros = { path = "../rustc_macros" }
1919
rustc_session = { path = "../rustc_session" }
2020
rustc_ast = { path = "../rustc_ast" }
21-
version_check = "0.9"

Diff for: compiler/rustc_attr/src/builtin.rs

+28-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use rustc_session::Session;
1010
use rustc_span::hygiene::Transparency;
1111
use rustc_span::{symbol::sym, symbol::Symbol, Span};
1212
use std::num::NonZeroU32;
13-
use version_check::Version;
1413

1514
pub fn is_builtin_attr(attr: &Attribute) -> bool {
1615
attr.is_doc_comment() || attr.ident().filter(|ident| is_builtin_attr_name(ident.name)).is_some()
@@ -526,6 +525,26 @@ fn gate_cfg(gated_cfg: &GatedCfg, cfg_span: Span, sess: &ParseSess, features: &F
526525
}
527526
}
528527

528+
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
529+
struct Version {
530+
major: u16,
531+
minor: u16,
532+
patch: u16,
533+
}
534+
535+
fn parse_version(s: &str, allow_appendix: bool) -> Option<Version> {
536+
let mut components = s.split('-');
537+
let d = components.next()?;
538+
if !allow_appendix && components.next().is_some() {
539+
return None;
540+
}
541+
let mut digits = d.splitn(3, '.');
542+
let major = digits.next()?.parse().ok()?;
543+
let minor = digits.next()?.parse().ok()?;
544+
let patch = digits.next().unwrap_or("0").parse().ok()?;
545+
Some(Version { major, minor, patch })
546+
}
547+
529548
/// Evaluate a cfg-like condition (with `any` and `all`), using `eval` to
530549
/// evaluate individual items.
531550
pub fn eval_condition(
@@ -555,16 +574,21 @@ pub fn eval_condition(
555574
return false;
556575
}
557576
};
558-
let min_version = match Version::parse(&min_version.as_str()) {
577+
let min_version = match parse_version(&min_version.as_str(), false) {
559578
Some(ver) => ver,
560579
None => {
561-
sess.span_diagnostic.struct_span_err(*span, "invalid version literal").emit();
580+
sess.span_diagnostic
581+
.struct_span_warn(
582+
*span,
583+
"unknown version literal format, assuming it refers to a future version",
584+
)
585+
.emit();
562586
return false;
563587
}
564588
};
565589
let channel = env!("CFG_RELEASE_CHANNEL");
566590
let nightly = channel == "nightly" || channel == "dev";
567-
let rustc_version = Version::parse(env!("CFG_RELEASE")).unwrap();
591+
let rustc_version = parse_version(env!("CFG_RELEASE"), true).unwrap();
568592

569593
// See https://github.com/rust-lang/rust/issues/64796#issuecomment-625474439 for details
570594
if nightly { rustc_version > min_version } else { rustc_version >= min_version }

Diff for: src/test/ui/feature-gates/feature-gate-cfg-version.rs

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#[cfg(version(42))] //~ ERROR: expected a version literal
2+
//~^ ERROR `cfg(version)` is experimental and subject to change
3+
fn foo() {}
4+
#[cfg(version(1.20))] //~ ERROR: expected a version literal
5+
//~^ ERROR `cfg(version)` is experimental and subject to change
6+
fn foo() -> bool { true }
17
#[cfg(version("1.44"))]
28
//~^ ERROR `cfg(version)` is experimental and subject to change
39
fn foo() -> bool { true }
@@ -11,30 +17,32 @@ fn bar() -> bool { false }
1117
#[cfg(version(false))] //~ ERROR: expected a version literal
1218
//~^ ERROR `cfg(version)` is experimental and subject to change
1319
fn bar() -> bool { false }
14-
#[cfg(version("foo"))] //~ ERROR: invalid version literal
20+
#[cfg(version("foo"))] //~ WARNING: unknown version literal format
1521
//~^ ERROR `cfg(version)` is experimental and subject to change
1622
fn bar() -> bool { false }
17-
#[cfg(version("999"))]
23+
#[cfg(version("999"))] //~ WARNING: unknown version literal format
1824
//~^ ERROR `cfg(version)` is experimental and subject to change
1925
fn bar() -> bool { false }
20-
#[cfg(version("-1"))] //~ ERROR: invalid version literal
26+
#[cfg(version("-1"))] //~ WARNING: unknown version literal format
2127
//~^ ERROR `cfg(version)` is experimental and subject to change
2228
fn bar() -> bool { false }
23-
#[cfg(version("65536"))] //~ ERROR: invalid version literal
29+
#[cfg(version("65536"))] //~ WARNING: unknown version literal format
2430
//~^ ERROR `cfg(version)` is experimental and subject to change
2531
fn bar() -> bool { false }
26-
#[cfg(version("0"))]
32+
#[cfg(version("0"))] //~ WARNING: unknown version literal format
2733
//~^ ERROR `cfg(version)` is experimental and subject to change
2834
fn bar() -> bool { true }
29-
30-
#[cfg(version("1.65536.2"))]
35+
#[cfg(version("1.0"))]
36+
//~^ ERROR `cfg(version)` is experimental and subject to change
37+
fn bar() -> bool { true }
38+
#[cfg(version("1.65536.2"))] //~ WARNING: unknown version literal format
39+
//~^ ERROR `cfg(version)` is experimental and subject to change
40+
fn bar() -> bool { false }
41+
#[cfg(version("1.20.0-stable"))] //~ WARNING: unknown version literal format
3142
//~^ ERROR `cfg(version)` is experimental and subject to change
32-
fn version_check_bug() {}
43+
fn bar() {}
3344

3445
fn main() {
35-
// This should fail but due to a bug in version_check `1.65536.2` is interpreted as `1.2`.
36-
// See https://github.com/SergioBenitez/version_check/issues/11
37-
version_check_bug();
3846
assert!(foo());
3947
assert!(bar());
4048
assert!(cfg!(version("1.42"))); //~ ERROR `cfg(version)` is experimental and subject to change
+91-19
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,44 @@
11
error[E0658]: `cfg(version)` is experimental and subject to change
22
--> $DIR/feature-gate-cfg-version.rs:1:7
33
|
4+
LL | #[cfg(version(42))]
5+
| ^^^^^^^^^^^
6+
|
7+
= note: see issue #64796 <https://github.com/rust-lang/rust/issues/64796> for more information
8+
= help: add `#![feature(cfg_version)]` to the crate attributes to enable
9+
10+
error: expected a version literal
11+
--> $DIR/feature-gate-cfg-version.rs:1:15
12+
|
13+
LL | #[cfg(version(42))]
14+
| ^^
15+
16+
error[E0658]: `cfg(version)` is experimental and subject to change
17+
--> $DIR/feature-gate-cfg-version.rs:4:7
18+
|
19+
LL | #[cfg(version(1.20))]
20+
| ^^^^^^^^^^^^^
21+
|
22+
= note: see issue #64796 <https://github.com/rust-lang/rust/issues/64796> for more information
23+
= help: add `#![feature(cfg_version)]` to the crate attributes to enable
24+
25+
error: expected a version literal
26+
--> $DIR/feature-gate-cfg-version.rs:4:15
27+
|
28+
LL | #[cfg(version(1.20))]
29+
| ^^^^
30+
31+
error[E0658]: `cfg(version)` is experimental and subject to change
32+
--> $DIR/feature-gate-cfg-version.rs:7:7
33+
|
434
LL | #[cfg(version("1.44"))]
535
| ^^^^^^^^^^^^^^^
636
|
737
= note: see issue #64796 <https://github.com/rust-lang/rust/issues/64796> for more information
838
= help: add `#![feature(cfg_version)]` to the crate attributes to enable
939

1040
error[E0658]: `cfg(version)` is experimental and subject to change
11-
--> $DIR/feature-gate-cfg-version.rs:4:11
41+
--> $DIR/feature-gate-cfg-version.rs:10:11
1242
|
1343
LL | #[cfg(not(version("1.44")))]
1444
| ^^^^^^^^^^^^^^^
@@ -17,7 +47,7 @@ LL | #[cfg(not(version("1.44")))]
1747
= help: add `#![feature(cfg_version)]` to the crate attributes to enable
1848

1949
error[E0658]: `cfg(version)` is experimental and subject to change
20-
--> $DIR/feature-gate-cfg-version.rs:8:7
50+
--> $DIR/feature-gate-cfg-version.rs:14:7
2151
|
2252
LL | #[cfg(version("1.43", "1.44", "1.45"))]
2353
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -26,13 +56,13 @@ LL | #[cfg(version("1.43", "1.44", "1.45"))]
2656
= help: add `#![feature(cfg_version)]` to the crate attributes to enable
2757

2858
error: expected single version literal
29-
--> $DIR/feature-gate-cfg-version.rs:8:7
59+
--> $DIR/feature-gate-cfg-version.rs:14:7
3060
|
3161
LL | #[cfg(version("1.43", "1.44", "1.45"))]
3262
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3363

3464
error[E0658]: `cfg(version)` is experimental and subject to change
35-
--> $DIR/feature-gate-cfg-version.rs:11:7
65+
--> $DIR/feature-gate-cfg-version.rs:17:7
3666
|
3767
LL | #[cfg(version(false))]
3868
| ^^^^^^^^^^^^^^
@@ -41,92 +71,134 @@ LL | #[cfg(version(false))]
4171
= help: add `#![feature(cfg_version)]` to the crate attributes to enable
4272

4373
error: expected a version literal
44-
--> $DIR/feature-gate-cfg-version.rs:11:15
74+
--> $DIR/feature-gate-cfg-version.rs:17:15
4575
|
4676
LL | #[cfg(version(false))]
4777
| ^^^^^
4878

4979
error[E0658]: `cfg(version)` is experimental and subject to change
50-
--> $DIR/feature-gate-cfg-version.rs:14:7
80+
--> $DIR/feature-gate-cfg-version.rs:20:7
5181
|
5282
LL | #[cfg(version("foo"))]
5383
| ^^^^^^^^^^^^^^
5484
|
5585
= note: see issue #64796 <https://github.com/rust-lang/rust/issues/64796> for more information
5686
= help: add `#![feature(cfg_version)]` to the crate attributes to enable
5787

58-
error: invalid version literal
59-
--> $DIR/feature-gate-cfg-version.rs:14:15
88+
warning: unknown version literal format, assuming it refers to a future version
89+
--> $DIR/feature-gate-cfg-version.rs:20:15
6090
|
6191
LL | #[cfg(version("foo"))]
6292
| ^^^^^
6393

6494
error[E0658]: `cfg(version)` is experimental and subject to change
65-
--> $DIR/feature-gate-cfg-version.rs:17:7
95+
--> $DIR/feature-gate-cfg-version.rs:23:7
6696
|
6797
LL | #[cfg(version("999"))]
6898
| ^^^^^^^^^^^^^^
6999
|
70100
= note: see issue #64796 <https://github.com/rust-lang/rust/issues/64796> for more information
71101
= help: add `#![feature(cfg_version)]` to the crate attributes to enable
72102

103+
warning: unknown version literal format, assuming it refers to a future version
104+
--> $DIR/feature-gate-cfg-version.rs:23:15
105+
|
106+
LL | #[cfg(version("999"))]
107+
| ^^^^^
108+
73109
error[E0658]: `cfg(version)` is experimental and subject to change
74-
--> $DIR/feature-gate-cfg-version.rs:20:7
110+
--> $DIR/feature-gate-cfg-version.rs:26:7
75111
|
76112
LL | #[cfg(version("-1"))]
77113
| ^^^^^^^^^^^^^
78114
|
79115
= note: see issue #64796 <https://github.com/rust-lang/rust/issues/64796> for more information
80116
= help: add `#![feature(cfg_version)]` to the crate attributes to enable
81117

82-
error: invalid version literal
83-
--> $DIR/feature-gate-cfg-version.rs:20:15
118+
warning: unknown version literal format, assuming it refers to a future version
119+
--> $DIR/feature-gate-cfg-version.rs:26:15
84120
|
85121
LL | #[cfg(version("-1"))]
86122
| ^^^^
87123

88124
error[E0658]: `cfg(version)` is experimental and subject to change
89-
--> $DIR/feature-gate-cfg-version.rs:23:7
125+
--> $DIR/feature-gate-cfg-version.rs:29:7
90126
|
91127
LL | #[cfg(version("65536"))]
92128
| ^^^^^^^^^^^^^^^^
93129
|
94130
= note: see issue #64796 <https://github.com/rust-lang/rust/issues/64796> for more information
95131
= help: add `#![feature(cfg_version)]` to the crate attributes to enable
96132

97-
error: invalid version literal
98-
--> $DIR/feature-gate-cfg-version.rs:23:15
133+
warning: unknown version literal format, assuming it refers to a future version
134+
--> $DIR/feature-gate-cfg-version.rs:29:15
99135
|
100136
LL | #[cfg(version("65536"))]
101137
| ^^^^^^^
102138

103139
error[E0658]: `cfg(version)` is experimental and subject to change
104-
--> $DIR/feature-gate-cfg-version.rs:26:7
140+
--> $DIR/feature-gate-cfg-version.rs:32:7
105141
|
106142
LL | #[cfg(version("0"))]
107143
| ^^^^^^^^^^^^
108144
|
109145
= note: see issue #64796 <https://github.com/rust-lang/rust/issues/64796> for more information
110146
= help: add `#![feature(cfg_version)]` to the crate attributes to enable
111147

148+
warning: unknown version literal format, assuming it refers to a future version
149+
--> $DIR/feature-gate-cfg-version.rs:32:15
150+
|
151+
LL | #[cfg(version("0"))]
152+
| ^^^
153+
154+
error[E0658]: `cfg(version)` is experimental and subject to change
155+
--> $DIR/feature-gate-cfg-version.rs:35:7
156+
|
157+
LL | #[cfg(version("1.0"))]
158+
| ^^^^^^^^^^^^^^
159+
|
160+
= note: see issue #64796 <https://github.com/rust-lang/rust/issues/64796> for more information
161+
= help: add `#![feature(cfg_version)]` to the crate attributes to enable
162+
112163
error[E0658]: `cfg(version)` is experimental and subject to change
113-
--> $DIR/feature-gate-cfg-version.rs:30:7
164+
--> $DIR/feature-gate-cfg-version.rs:38:7
114165
|
115166
LL | #[cfg(version("1.65536.2"))]
116167
| ^^^^^^^^^^^^^^^^^^^^
117168
|
118169
= note: see issue #64796 <https://github.com/rust-lang/rust/issues/64796> for more information
119170
= help: add `#![feature(cfg_version)]` to the crate attributes to enable
120171

172+
warning: unknown version literal format, assuming it refers to a future version
173+
--> $DIR/feature-gate-cfg-version.rs:38:15
174+
|
175+
LL | #[cfg(version("1.65536.2"))]
176+
| ^^^^^^^^^^^
177+
178+
error[E0658]: `cfg(version)` is experimental and subject to change
179+
--> $DIR/feature-gate-cfg-version.rs:41:7
180+
|
181+
LL | #[cfg(version("1.20.0-stable"))]
182+
| ^^^^^^^^^^^^^^^^^^^^^^^^
183+
|
184+
= note: see issue #64796 <https://github.com/rust-lang/rust/issues/64796> for more information
185+
= help: add `#![feature(cfg_version)]` to the crate attributes to enable
186+
187+
warning: unknown version literal format, assuming it refers to a future version
188+
--> $DIR/feature-gate-cfg-version.rs:41:15
189+
|
190+
LL | #[cfg(version("1.20.0-stable"))]
191+
| ^^^^^^^^^^^^^^^
192+
121193
error[E0658]: `cfg(version)` is experimental and subject to change
122-
--> $DIR/feature-gate-cfg-version.rs:40:18
194+
--> $DIR/feature-gate-cfg-version.rs:48:18
123195
|
124196
LL | assert!(cfg!(version("1.42")));
125197
| ^^^^^^^^^^^^^^^
126198
|
127199
= note: see issue #64796 <https://github.com/rust-lang/rust/issues/64796> for more information
128200
= help: add `#![feature(cfg_version)]` to the crate attributes to enable
129201

130-
error: aborting due to 16 previous errors
202+
error: aborting due to 19 previous errors; 7 warnings emitted
131203

132204
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)