Skip to content

Commit f95183e

Browse files
committed
Reject defaultness on free consts
1 parent 9ab0749 commit f95183e

5 files changed

+114
-12
lines changed

Diff for: compiler/rustc_ast_passes/src/ast_validation.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1008,12 +1008,14 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10081008
_ => {}
10091009
}
10101010
}
1011-
ItemKind::Const(box ConstItem { defaultness, expr: None, .. }) => {
1011+
ItemKind::Const(box ConstItem { defaultness, expr, .. }) => {
10121012
self.check_defaultness(item.span, *defaultness);
1013-
self.session.emit_err(errors::ConstWithoutBody {
1014-
span: item.span,
1015-
replace_span: self.ending_semi_or_hi(item.span),
1016-
});
1013+
if expr.is_none() {
1014+
self.session.emit_err(errors::ConstWithoutBody {
1015+
span: item.span,
1016+
replace_span: self.ending_semi_or_hi(item.span),
1017+
});
1018+
}
10171019
}
10181020
ItemKind::Static(box StaticItem { expr: None, .. }) => {
10191021
self.session.emit_err(errors::StaticWithoutBody {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(specialization)] //~ WARN the feature `specialization` is incomplete
2+
3+
fn main() {}
4+
5+
trait X {
6+
default const A: u8; //~ ERROR `default` is only allowed on items in trait impls
7+
default const B: u8 = 0; //~ ERROR `default` is only allowed on items in trait impls
8+
default type D; //~ ERROR `default` is only allowed on items in trait impls
9+
default type C: Ord; //~ ERROR `default` is only allowed on items in trait impls
10+
default fn f1(); //~ ERROR `default` is only allowed on items in trait impls
11+
default fn f2() {} //~ ERROR `default` is only allowed on items in trait impls
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
error: `default` is only allowed on items in trait impls
2+
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:6:5
3+
|
4+
LL | default const A: u8;
5+
| -------^^^^^^^^^^^^^
6+
| |
7+
| `default` because of this
8+
9+
error: `default` is only allowed on items in trait impls
10+
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:7:5
11+
|
12+
LL | default const B: u8 = 0;
13+
| -------^^^^^^^^^^^^^^^^^
14+
| |
15+
| `default` because of this
16+
17+
error: `default` is only allowed on items in trait impls
18+
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:8:5
19+
|
20+
LL | default type D;
21+
| -------^^^^^^^^
22+
| |
23+
| `default` because of this
24+
25+
error: `default` is only allowed on items in trait impls
26+
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:9:5
27+
|
28+
LL | default type C: Ord;
29+
| -------^^^^^^^^^^^^^
30+
| |
31+
| `default` because of this
32+
33+
error: `default` is only allowed on items in trait impls
34+
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:10:5
35+
|
36+
LL | default fn f1();
37+
| -------^^^^^^^^^
38+
| |
39+
| `default` because of this
40+
41+
error: `default` is only allowed on items in trait impls
42+
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:11:5
43+
|
44+
LL | default fn f2() {}
45+
| -------^^^^^^^^
46+
| |
47+
| `default` because of this
48+
49+
warning: the feature `specialization` is incomplete and may not be safe to use and/or cause compiler crashes
50+
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:1:12
51+
|
52+
LL | #![feature(specialization)]
53+
| ^^^^^^^^^^^^^^
54+
|
55+
= note: see issue #31844 <https://github.com/rust-lang/rust/issues/31844> for more information
56+
= help: consider using `min_specialization` instead, which is more stable and complete
57+
= note: `#[warn(incomplete_features)]` on by default
58+
59+
error: aborting due to 6 previous errors; 1 warning emitted
60+

Diff for: tests/ui/parser/trait-item-with-defaultness-fail-semantic.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ trait X {
77
default const B: u8 = 0; //~ ERROR `default` is only allowed on items in trait impls
88
default type D; //~ ERROR `default` is only allowed on items in trait impls
99
default type C: Ord; //~ ERROR `default` is only allowed on items in trait impls
10-
default fn f1(); //~ ERROR `default` is only allowed on items in trait impls
11-
default fn f2() {} //~ ERROR `default` is only allowed on items in trait impls
10+
default fn f(); //~ ERROR `default` is only allowed on items in trait impls
11+
default fn g() {} //~ ERROR `default` is only allowed on items in trait impls
1212
}
13+
14+
default const E: u8 = 0; //~ ERROR `default` is only allowed on items in trait impls
15+
default type F = (); //~ ERROR `default` is only allowed on items in trait impls
16+
default fn h() {} //~ ERROR `default` is only allowed on items in trait impls

Diff for: tests/ui/parser/trait-item-with-defaultness-fail-semantic.stderr

+29-5
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,43 @@ LL | default type C: Ord;
3333
error: `default` is only allowed on items in trait impls
3434
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:10:5
3535
|
36-
LL | default fn f1();
37-
| -------^^^^^^^^^
36+
LL | default fn f();
37+
| -------^^^^^^^^
3838
| |
3939
| `default` because of this
4040

4141
error: `default` is only allowed on items in trait impls
4242
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:11:5
4343
|
44-
LL | default fn f2() {}
45-
| -------^^^^^^^^
44+
LL | default fn g() {}
45+
| -------^^^^^^^
4646
| |
4747
| `default` because of this
4848

49+
error: `default` is only allowed on items in trait impls
50+
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:14:1
51+
|
52+
LL | default const E: u8 = 0;
53+
| -------^^^^^^^^^^^^^^^^^
54+
| |
55+
| `default` because of this
56+
57+
error: `default` is only allowed on items in trait impls
58+
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:15:1
59+
|
60+
LL | default type F = ();
61+
| -------^^^^^^^^^^^^^
62+
| |
63+
| `default` because of this
64+
65+
error: `default` is only allowed on items in trait impls
66+
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:16:1
67+
|
68+
LL | default fn h() {}
69+
| -------^^^^^^^
70+
| |
71+
| `default` because of this
72+
4973
warning: the feature `specialization` is incomplete and may not be safe to use and/or cause compiler crashes
5074
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:1:12
5175
|
@@ -56,5 +80,5 @@ LL | #![feature(specialization)]
5680
= help: consider using `min_specialization` instead, which is more stable and complete
5781
= note: `#[warn(incomplete_features)]` on by default
5882

59-
error: aborting due to 6 previous errors; 1 warning emitted
83+
error: aborting due to 9 previous errors; 1 warning emitted
6084

0 commit comments

Comments
 (0)