Skip to content

Commit b7854c6

Browse files
Rollup merge of rust-lang#141552 - jieyouxu:cfg-version-tests, r=est31
Pull out dedicated `cfg_version` syntax test from feature gate test Tracking issue: rust-lang#64796. Closes rust-lang#141452, as a follow-up to rust-lang#141413 (comment) (point 3 of that is probably too pedantic). The feature gate test was dual-purposing causing feature gate errors to distract from syntax exercises. ``@rustbot`` label +F-cfg_version r? ``@est31``
2 parents a0d77f3 + 0ea12c3 commit b7854c6

File tree

4 files changed

+350
-243
lines changed

4 files changed

+350
-243
lines changed

tests/ui/cfg/cfg-version/syntax.rs

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
// 3. Major-only, or other non-Semver-like strings are not permitted.
77+
78+
#[cfg(version("1"))]
79+
//~^ WARN unknown version literal format, assuming it refers to a future version
80+
fn invalid_major_only() {}
81+
82+
#[cfg(version("0"))]
83+
//~^ WARN unknown version literal format, assuming it refers to a future version
84+
fn invalid_major_only_zero() {}
85+
86+
#[cfg(version(".7"))]
87+
//~^ WARN unknown version literal format, assuming it refers to a future version
88+
fn invalid_decimal_like() {}
89+
90+
// Misc parsing overflow/underflow edge cases
91+
// ==========================================
92+
//
93+
// Check that we report "unknown version literal format" user-facing warnings and not ICEs.
94+
95+
#[cfg(version("-1"))]
96+
//~^ WARN unknown version literal format, assuming it refers to a future version
97+
fn invalid_major_only_negative() {}
98+
99+
// Implementation detail: we store rustc version as `{ major: u16, minor: u16, patch: u16 }`.
100+
101+
#[cfg(version("65536"))]
102+
//~^ WARN unknown version literal format, assuming it refers to a future version
103+
fn exceed_u16_major() {}
104+
105+
#[cfg(version("1.65536.0"))]
106+
//~^ WARN unknown version literal format, assuming it refers to a future version
107+
fn exceed_u16_minor() {}
108+
109+
#[cfg(version("1.0.65536"))]
110+
//~^ WARN unknown version literal format, assuming it refers to a future version
111+
fn exceed_u16_patch() {}
112+
113+
#[cfg(version("65536.0.65536"))]
114+
//~^ WARN unknown version literal format, assuming it refers to a future version
115+
fn exceed_u16_mixed() {}
116+
117+
// Usage as `cfg!()`
118+
// =================
119+
120+
fn cfg_usage() {
121+
assert!(cfg!(version("1.0")));
122+
assert!(cfg!(version("1.43")));
123+
assert!(cfg!(version("1.43.0")));
124+
125+
assert!(cfg!(version("foo")));
126+
//~^ WARN unknown version literal format, assuming it refers to a future version
127+
assert!(cfg!(version("1.20.0-stable")));
128+
//~^ WARN unknown version literal format, assuming it refers to a future version
129+
130+
assert!(cfg!(version = "1.43"));
131+
//~^ WARN unexpected `cfg` condition name: `version`
132+
}
133+
134+
fn main() {
135+
cfg_usage();
136+
137+
// `cfg(version = "..")` is not a valid `cfg_version` form, but it only triggers
138+
// `unexpected_cfgs` lint, and `cfg(version = "..")` eval to `false`.
139+
key_value_form(); //~ ERROR cannot find function
140+
141+
// Invalid version literal formats within valid `cfg(version(..))` form should also cause
142+
// `cfg(version(..))` eval to `false`.
143+
not_numbers_or_periods(); //~ ERROR cannot find function
144+
complex_semver_with_metadata(); //~ ERROR cannot find function
145+
invalid_major_only(); //~ ERROR cannot find function
146+
invalid_major_only_zero(); //~ ERROR cannot find function
147+
invalid_major_only_negative(); //~ ERROR cannot find function
148+
exceed_u16_major(); //~ ERROR cannot find function
149+
exceed_u16_minor(); //~ ERROR cannot find function
150+
exceed_u16_patch(); //~ ERROR cannot find function
151+
exceed_u16_mixed(); //~ ERROR cannot find function
152+
}
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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:78: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:82: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:86:15
51+
|
52+
LL | #[cfg(version(".7"))]
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("-1"))]
59+
| ^^^^
60+
61+
warning: unknown version literal format, assuming it refers to a future version
62+
--> $DIR/syntax.rs:101:15
63+
|
64+
LL | #[cfg(version("65536"))]
65+
| ^^^^^^^
66+
67+
warning: unknown version literal format, assuming it refers to a future version
68+
--> $DIR/syntax.rs:105:15
69+
|
70+
LL | #[cfg(version("1.65536.0"))]
71+
| ^^^^^^^^^^^
72+
73+
warning: unknown version literal format, assuming it refers to a future version
74+
--> $DIR/syntax.rs:109:15
75+
|
76+
LL | #[cfg(version("1.0.65536"))]
77+
| ^^^^^^^^^^^
78+
79+
warning: unknown version literal format, assuming it refers to a future version
80+
--> $DIR/syntax.rs:113:15
81+
|
82+
LL | #[cfg(version("65536.0.65536"))]
83+
| ^^^^^^^^^^^^^^^
84+
85+
warning: unknown version literal format, assuming it refers to a future version
86+
--> $DIR/syntax.rs:125:26
87+
|
88+
LL | assert!(cfg!(version("foo")));
89+
| ^^^^^
90+
91+
warning: unknown version literal format, assuming it refers to a future version
92+
--> $DIR/syntax.rs:127:26
93+
|
94+
LL | assert!(cfg!(version("1.20.0-stable")));
95+
| ^^^^^^^^^^^^^^^
96+
97+
warning: unexpected `cfg` condition name: `version`
98+
--> $DIR/syntax.rs:30:7
99+
|
100+
LL | #[cfg(version = "1.43")]
101+
| ^^^^^^^^^^^^^^^^
102+
|
103+
= help: to expect this configuration use `--check-cfg=cfg(version, values("1.43"))`
104+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
105+
= note: `#[warn(unexpected_cfgs)]` on by default
106+
help: there is a similar config predicate: `version("..")`
107+
|
108+
LL - #[cfg(version = "1.43")]
109+
LL + #[cfg(version("1.43"))]
110+
|
111+
112+
warning: unexpected `cfg` condition name: `version`
113+
--> $DIR/syntax.rs:130:18
114+
|
115+
LL | assert!(cfg!(version = "1.43"));
116+
| ^^^^^^^^^^^^^^^^
117+
|
118+
= help: to expect this configuration use `--check-cfg=cfg(version, values("1.43"))`
119+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
120+
help: there is a similar config predicate: `version("..")`
121+
|
122+
LL - assert!(cfg!(version = "1.43"));
123+
LL + assert!(cfg!(version("1.43")));
124+
|
125+
126+
error[E0425]: cannot find function `key_value_form` in this scope
127+
--> $DIR/syntax.rs:139:5
128+
|
129+
LL | key_value_form();
130+
| ^^^^^^^^^^^^^^ not found in this scope
131+
132+
error[E0425]: cannot find function `not_numbers_or_periods` in this scope
133+
--> $DIR/syntax.rs:143:5
134+
|
135+
LL | not_numbers_or_periods();
136+
| ^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
137+
138+
error[E0425]: cannot find function `complex_semver_with_metadata` in this scope
139+
--> $DIR/syntax.rs:144:5
140+
|
141+
LL | complex_semver_with_metadata();
142+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
143+
144+
error[E0425]: cannot find function `invalid_major_only` in this scope
145+
--> $DIR/syntax.rs:145:5
146+
|
147+
LL | invalid_major_only();
148+
| ^^^^^^^^^^^^^^^^^^ not found in this scope
149+
150+
error[E0425]: cannot find function `invalid_major_only_zero` in this scope
151+
--> $DIR/syntax.rs:146:5
152+
|
153+
LL | invalid_major_only_zero();
154+
| ^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
155+
156+
error[E0425]: cannot find function `invalid_major_only_negative` in this scope
157+
--> $DIR/syntax.rs:147:5
158+
|
159+
LL | invalid_major_only_negative();
160+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
161+
162+
error[E0425]: cannot find function `exceed_u16_major` in this scope
163+
--> $DIR/syntax.rs:148:5
164+
|
165+
LL | exceed_u16_major();
166+
| ^^^^^^^^^^^^^^^^ not found in this scope
167+
168+
error[E0425]: cannot find function `exceed_u16_minor` in this scope
169+
--> $DIR/syntax.rs:149:5
170+
|
171+
LL | exceed_u16_minor();
172+
| ^^^^^^^^^^^^^^^^ not found in this scope
173+
174+
error[E0425]: cannot find function `exceed_u16_patch` in this scope
175+
--> $DIR/syntax.rs:150:5
176+
|
177+
LL | exceed_u16_patch();
178+
| ^^^^^^^^^^^^^^^^ not found in this scope
179+
180+
error[E0425]: cannot find function `exceed_u16_mixed` in this scope
181+
--> $DIR/syntax.rs:151:5
182+
|
183+
LL | exceed_u16_mixed();
184+
| ^^^^^^^^^^^^^^^^ not found in this scope
185+
186+
error: aborting due to 14 previous errors; 14 warnings emitted
187+
188+
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)