Skip to content

Commit 10446c8

Browse files
committed
cfg_version: pull out dedicated syntax test from feature gate test
The feature gate test was dual-purposing causing feature gate errors to distract from syntax exercises.
1 parent aa57e46 commit 10446c8

File tree

4 files changed

+338
-243
lines changed

4 files changed

+338
-243
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
//! Check `#[cfg(version(..))]` parsing.
2+
3+
#![feature(cfg_version)]
4+
5+
// Overall grammar
6+
// ===============
7+
//
8+
// `#[cfg(version(..))]` accepts only the `version(VERSION_STRING_LITERAL)` predicate form, where
9+
// only a single string literal is permitted.
10+
11+
#[cfg(version(42))]
12+
//~^ ERROR expected a version literal
13+
fn not_a_string_literal_simple() {}
14+
15+
#[cfg(version(1.20))]
16+
//~^ ERROR expected a version literal
17+
fn not_a_string_literal_semver_like() {}
18+
19+
#[cfg(version(false))]
20+
//~^ ERROR expected a version literal
21+
fn not_a_string_literal_other() {}
22+
23+
#[cfg(version("1.43", "1.44", "1.45"))]
24+
//~^ ERROR expected single version literal
25+
fn multiple_version_literals() {}
26+
27+
// The key-value form `cfg(version = "..")` is not considered a valid `cfg(version(..))` usage, but
28+
// it will only trigger the `unexpected_cfgs` lint and not a hard error.
29+
30+
#[cfg(version = "1.43")]
31+
//~^ WARN unexpected `cfg` condition name: `version`
32+
fn key_value_form() {}
33+
34+
// Additional version string literal constraints
35+
// =============================================
36+
//
37+
// The `VERSION_STRING_LITERAL` ("version literal") has additional constraints on its syntactical
38+
// well-formedness.
39+
40+
// 1. A valid version literal can only constitute of numbers and periods (a "simple" semver version
41+
// string). Non-semver strings or "complex" semver strings (such as build metadata) are not
42+
// considered valid version literals, and will emit a non-lint warning "unknown version literal
43+
// format".
44+
45+
#[cfg(version("1.43.0"))]
46+
fn valid_major_minor_patch() {}
47+
48+
#[cfg(version("0.0.0"))]
49+
fn valid_zero_zero_zero_major_minor_patch() {}
50+
51+
#[cfg(version("foo"))]
52+
//~^ WARN unknown version literal format, assuming it refers to a future version
53+
fn not_numbers_or_periods() {}
54+
55+
#[cfg(version("1.20.0-stable"))]
56+
//~^ WARN unknown version literal format, assuming it refers to a future version
57+
fn complex_semver_with_metadata() {}
58+
59+
// 2. "Shortened" version strings are permitted but *only* for the omission of the patch number.
60+
61+
#[cfg(version("1.0"))]
62+
fn valid_major_minor_1() {}
63+
64+
#[cfg(version("1.43"))]
65+
fn valid_major_minor_2() {}
66+
67+
#[cfg(not(version("1.44")))]
68+
fn valid_major_minor_negated_smoke_test() {}
69+
70+
#[cfg(version("0.0"))]
71+
fn valid_zero_zero_major_minor() {}
72+
73+
#[cfg(version("0.7"))]
74+
fn valid_zero_major_minor() {}
75+
76+
#[cfg(version("1"))]
77+
//~^ WARN unknown version literal format, assuming it refers to a future version
78+
fn invalid_major_only() {}
79+
80+
#[cfg(version("0"))]
81+
//~^ WARN unknown version literal format, assuming it refers to a future version
82+
fn invalid_major_only_zero() {}
83+
84+
// Misc parsing overflow/underflow edge cases
85+
// ==========================================
86+
//
87+
// Check that we report "unknown version literal format" user-facing warnings and not ICEs.
88+
89+
#[cfg(version("-1"))]
90+
//~^ WARN unknown version literal format, assuming it refers to a future version
91+
fn invalid_major_only_negative() {}
92+
93+
// Implementation detail: we store rustc version as `{ major: u16, minor: u16, patch: u16 }`.
94+
95+
#[cfg(version("65536"))]
96+
//~^ WARN unknown version literal format, assuming it refers to a future version
97+
fn exceed_u16_major() {}
98+
99+
#[cfg(version("1.65536.0"))]
100+
//~^ WARN unknown version literal format, assuming it refers to a future version
101+
fn exceed_u16_minor() {}
102+
103+
#[cfg(version("1.0.65536"))]
104+
//~^ WARN unknown version literal format, assuming it refers to a future version
105+
fn exceed_u16_patch() {}
106+
107+
#[cfg(version("65536.0.65536"))]
108+
//~^ WARN unknown version literal format, assuming it refers to a future version
109+
fn exceed_u16_mixed() {}
110+
111+
// Usage as `cfg!()`
112+
// =================
113+
114+
fn cfg_usage() {
115+
assert!(cfg!(version("1.0")));
116+
assert!(cfg!(version("1.43")));
117+
assert!(cfg!(version("1.43.0")));
118+
119+
assert!(cfg!(version("foo")));
120+
//~^ WARN unknown version literal format, assuming it refers to a future version
121+
assert!(cfg!(version("1.20.0-stable")));
122+
//~^ WARN unknown version literal format, assuming it refers to a future version
123+
124+
assert!(cfg!(version = "1.43"));
125+
//~^ WARN unexpected `cfg` condition name: `version`
126+
}
127+
128+
fn main() {
129+
cfg_usage();
130+
131+
// `cfg(version = "..")` is not a valid `cfg_version` form, but it only triggers
132+
// `unexpected_cfgs` lint, and `cfg(version = "..")` eval to `false`.
133+
key_value_form(); //~ ERROR cannot find function
134+
135+
// Invalid version literal formats within valid `cfg(version(..))` form should also cause
136+
// `cfg(version(..))` eval to `false`.
137+
not_numbers_or_periods(); //~ ERROR cannot find function
138+
complex_semver_with_metadata(); //~ ERROR cannot find function
139+
invalid_major_only(); //~ ERROR cannot find function
140+
invalid_major_only_zero(); //~ ERROR cannot find function
141+
invalid_major_only_negative(); //~ ERROR cannot find function
142+
exceed_u16_major(); //~ ERROR cannot find function
143+
exceed_u16_minor(); //~ ERROR cannot find function
144+
exceed_u16_patch(); //~ ERROR cannot find function
145+
exceed_u16_mixed(); //~ ERROR cannot find function
146+
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
error: expected a version literal
2+
--> $DIR/syntax.rs:11:15
3+
|
4+
LL | #[cfg(version(42))]
5+
| ^^
6+
7+
error: expected a version literal
8+
--> $DIR/syntax.rs:15:15
9+
|
10+
LL | #[cfg(version(1.20))]
11+
| ^^^^
12+
13+
error: expected a version literal
14+
--> $DIR/syntax.rs:19:15
15+
|
16+
LL | #[cfg(version(false))]
17+
| ^^^^^
18+
19+
error: expected single version literal
20+
--> $DIR/syntax.rs:23:7
21+
|
22+
LL | #[cfg(version("1.43", "1.44", "1.45"))]
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
25+
warning: unknown version literal format, assuming it refers to a future version
26+
--> $DIR/syntax.rs:51:15
27+
|
28+
LL | #[cfg(version("foo"))]
29+
| ^^^^^
30+
31+
warning: unknown version literal format, assuming it refers to a future version
32+
--> $DIR/syntax.rs:55:15
33+
|
34+
LL | #[cfg(version("1.20.0-stable"))]
35+
| ^^^^^^^^^^^^^^^
36+
37+
warning: unknown version literal format, assuming it refers to a future version
38+
--> $DIR/syntax.rs:76:15
39+
|
40+
LL | #[cfg(version("1"))]
41+
| ^^^
42+
43+
warning: unknown version literal format, assuming it refers to a future version
44+
--> $DIR/syntax.rs:80:15
45+
|
46+
LL | #[cfg(version("0"))]
47+
| ^^^
48+
49+
warning: unknown version literal format, assuming it refers to a future version
50+
--> $DIR/syntax.rs:89:15
51+
|
52+
LL | #[cfg(version("-1"))]
53+
| ^^^^
54+
55+
warning: unknown version literal format, assuming it refers to a future version
56+
--> $DIR/syntax.rs:95:15
57+
|
58+
LL | #[cfg(version("65536"))]
59+
| ^^^^^^^
60+
61+
warning: unknown version literal format, assuming it refers to a future version
62+
--> $DIR/syntax.rs:99:15
63+
|
64+
LL | #[cfg(version("1.65536.0"))]
65+
| ^^^^^^^^^^^
66+
67+
warning: unknown version literal format, assuming it refers to a future version
68+
--> $DIR/syntax.rs:103:15
69+
|
70+
LL | #[cfg(version("1.0.65536"))]
71+
| ^^^^^^^^^^^
72+
73+
warning: unknown version literal format, assuming it refers to a future version
74+
--> $DIR/syntax.rs:107:15
75+
|
76+
LL | #[cfg(version("65536.0.65536"))]
77+
| ^^^^^^^^^^^^^^^
78+
79+
warning: unknown version literal format, assuming it refers to a future version
80+
--> $DIR/syntax.rs:119:26
81+
|
82+
LL | assert!(cfg!(version("foo")));
83+
| ^^^^^
84+
85+
warning: unknown version literal format, assuming it refers to a future version
86+
--> $DIR/syntax.rs:121:26
87+
|
88+
LL | assert!(cfg!(version("1.20.0-stable")));
89+
| ^^^^^^^^^^^^^^^
90+
91+
warning: unexpected `cfg` condition name: `version`
92+
--> $DIR/syntax.rs:30:7
93+
|
94+
LL | #[cfg(version = "1.43")]
95+
| ^^^^^^^^^^^^^^^^
96+
|
97+
= help: to expect this configuration use `--check-cfg=cfg(version, values("1.43"))`
98+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
99+
= note: `#[warn(unexpected_cfgs)]` on by default
100+
help: there is a similar config predicate: `version("..")`
101+
|
102+
LL - #[cfg(version = "1.43")]
103+
LL + #[cfg(version("1.43"))]
104+
|
105+
106+
warning: unexpected `cfg` condition name: `version`
107+
--> $DIR/syntax.rs:124:18
108+
|
109+
LL | assert!(cfg!(version = "1.43"));
110+
| ^^^^^^^^^^^^^^^^
111+
|
112+
= help: to expect this configuration use `--check-cfg=cfg(version, values("1.43"))`
113+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
114+
help: there is a similar config predicate: `version("..")`
115+
|
116+
LL - assert!(cfg!(version = "1.43"));
117+
LL + assert!(cfg!(version("1.43")));
118+
|
119+
120+
error[E0425]: cannot find function `key_value_form` in this scope
121+
--> $DIR/syntax.rs:133:5
122+
|
123+
LL | key_value_form();
124+
| ^^^^^^^^^^^^^^ not found in this scope
125+
126+
error[E0425]: cannot find function `not_numbers_or_periods` in this scope
127+
--> $DIR/syntax.rs:137:5
128+
|
129+
LL | not_numbers_or_periods();
130+
| ^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
131+
132+
error[E0425]: cannot find function `complex_semver_with_metadata` in this scope
133+
--> $DIR/syntax.rs:138:5
134+
|
135+
LL | complex_semver_with_metadata();
136+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
137+
138+
error[E0425]: cannot find function `invalid_major_only` in this scope
139+
--> $DIR/syntax.rs:139:5
140+
|
141+
LL | invalid_major_only();
142+
| ^^^^^^^^^^^^^^^^^^ not found in this scope
143+
144+
error[E0425]: cannot find function `invalid_major_only_zero` in this scope
145+
--> $DIR/syntax.rs:140:5
146+
|
147+
LL | invalid_major_only_zero();
148+
| ^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
149+
150+
error[E0425]: cannot find function `invalid_major_only_negative` in this scope
151+
--> $DIR/syntax.rs:141:5
152+
|
153+
LL | invalid_major_only_negative();
154+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
155+
156+
error[E0425]: cannot find function `exceed_u16_major` in this scope
157+
--> $DIR/syntax.rs:142:5
158+
|
159+
LL | exceed_u16_major();
160+
| ^^^^^^^^^^^^^^^^ not found in this scope
161+
162+
error[E0425]: cannot find function `exceed_u16_minor` in this scope
163+
--> $DIR/syntax.rs:143:5
164+
|
165+
LL | exceed_u16_minor();
166+
| ^^^^^^^^^^^^^^^^ not found in this scope
167+
168+
error[E0425]: cannot find function `exceed_u16_patch` in this scope
169+
--> $DIR/syntax.rs:144:5
170+
|
171+
LL | exceed_u16_patch();
172+
| ^^^^^^^^^^^^^^^^ not found in this scope
173+
174+
error[E0425]: cannot find function `exceed_u16_mixed` in this scope
175+
--> $DIR/syntax.rs:145:5
176+
|
177+
LL | exceed_u16_mixed();
178+
| ^^^^^^^^^^^^^^^^ not found in this scope
179+
180+
error: aborting due to 14 previous errors; 13 warnings emitted
181+
182+
For more information about this error, try `rustc --explain E0425`.
Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,12 @@
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 }
7-
#[cfg(version("1.44"))]
8-
//~^ ERROR `cfg(version)` is experimental and subject to change
9-
fn foo() -> bool { true }
10-
#[cfg(not(version("1.44")))]
11-
//~^ ERROR `cfg(version)` is experimental and subject to change
12-
fn foo() -> bool { false }
1+
//! Feature gate test for `cfg_version`.
2+
//!
3+
//! Tracking issue: #64796.
134
14-
#[cfg(version("1.43", "1.44", "1.45"))] //~ ERROR: expected single version literal
15-
//~^ ERROR `cfg(version)` is experimental and subject to change
16-
fn bar() -> bool { false }
17-
#[cfg(version(false))] //~ ERROR: expected a version literal
18-
//~^ ERROR `cfg(version)` is experimental and subject to change
19-
fn bar() -> bool { false }
20-
#[cfg(version("foo"))] //~ WARNING: unknown version literal format
21-
//~^ ERROR `cfg(version)` is experimental and subject to change
22-
fn bar() -> bool { false }
23-
#[cfg(version("999"))] //~ WARNING: unknown version literal format
24-
//~^ ERROR `cfg(version)` is experimental and subject to change
25-
fn bar() -> bool { false }
26-
#[cfg(version("-1"))] //~ WARNING: unknown version literal format
27-
//~^ ERROR `cfg(version)` is experimental and subject to change
28-
fn bar() -> bool { false }
29-
#[cfg(version("65536"))] //~ WARNING: unknown version literal format
30-
//~^ ERROR `cfg(version)` is experimental and subject to change
31-
fn bar() -> bool { false }
32-
#[cfg(version("0"))] //~ WARNING: unknown version literal format
33-
//~^ ERROR `cfg(version)` is experimental and subject to change
34-
fn bar() -> bool { true }
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
5+
#[cfg(version("1.42"))]
426
//~^ ERROR `cfg(version)` is experimental and subject to change
437
fn bar() {}
448

459
fn main() {
46-
assert!(foo());
47-
assert!(bar());
48-
assert!(cfg!(version("1.42"))); //~ ERROR `cfg(version)` is experimental and subject to change
10+
assert!(cfg!(version("1.42")));
11+
//~^ ERROR `cfg(version)` is experimental and subject to change
4912
}

0 commit comments

Comments
 (0)