Skip to content

Commit

Permalink
Reject defaultness on free consts
Browse files Browse the repository at this point in the history
  • Loading branch information
fmease committed Nov 11, 2023
1 parent 9ab0749 commit f95183e
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 12 deletions.
12 changes: 7 additions & 5 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1008,12 +1008,14 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
_ => {}
}
}
ItemKind::Const(box ConstItem { defaultness, expr: None, .. }) => {
ItemKind::Const(box ConstItem { defaultness, expr, .. }) => {
self.check_defaultness(item.span, *defaultness);
self.session.emit_err(errors::ConstWithoutBody {
span: item.span,
replace_span: self.ending_semi_or_hi(item.span),
});
if expr.is_none() {
self.session.emit_err(errors::ConstWithoutBody {
span: item.span,
replace_span: self.ending_semi_or_hi(item.span),
});
}
}
ItemKind::Static(box StaticItem { expr: None, .. }) => {
self.session.emit_err(errors::StaticWithoutBody {
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/parser/defaultness-invalid-places-fail-semantic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![feature(specialization)] //~ WARN the feature `specialization` is incomplete

fn main() {}

trait X {
default const A: u8; //~ ERROR `default` is only allowed on items in trait impls
default const B: u8 = 0; //~ ERROR `default` is only allowed on items in trait impls
default type D; //~ ERROR `default` is only allowed on items in trait impls
default type C: Ord; //~ ERROR `default` is only allowed on items in trait impls
default fn f1(); //~ ERROR `default` is only allowed on items in trait impls
default fn f2() {} //~ ERROR `default` is only allowed on items in trait impls
}
60 changes: 60 additions & 0 deletions tests/ui/parser/defaultness-invalid-places-fail-semantic.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
error: `default` is only allowed on items in trait impls
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:6:5
|
LL | default const A: u8;
| -------^^^^^^^^^^^^^
| |
| `default` because of this

error: `default` is only allowed on items in trait impls
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:7:5
|
LL | default const B: u8 = 0;
| -------^^^^^^^^^^^^^^^^^
| |
| `default` because of this

error: `default` is only allowed on items in trait impls
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:8:5
|
LL | default type D;
| -------^^^^^^^^
| |
| `default` because of this

error: `default` is only allowed on items in trait impls
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:9:5
|
LL | default type C: Ord;
| -------^^^^^^^^^^^^^
| |
| `default` because of this

error: `default` is only allowed on items in trait impls
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:10:5
|
LL | default fn f1();
| -------^^^^^^^^^
| |
| `default` because of this

error: `default` is only allowed on items in trait impls
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:11:5
|
LL | default fn f2() {}
| -------^^^^^^^^
| |
| `default` because of this

warning: the feature `specialization` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:1:12
|
LL | #![feature(specialization)]
| ^^^^^^^^^^^^^^
|
= note: see issue #31844 <https://github.com/rust-lang/rust/issues/31844> for more information
= help: consider using `min_specialization` instead, which is more stable and complete
= note: `#[warn(incomplete_features)]` on by default

error: aborting due to 6 previous errors; 1 warning emitted

8 changes: 6 additions & 2 deletions tests/ui/parser/trait-item-with-defaultness-fail-semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ trait X {
default const B: u8 = 0; //~ ERROR `default` is only allowed on items in trait impls
default type D; //~ ERROR `default` is only allowed on items in trait impls
default type C: Ord; //~ ERROR `default` is only allowed on items in trait impls
default fn f1(); //~ ERROR `default` is only allowed on items in trait impls
default fn f2() {} //~ ERROR `default` is only allowed on items in trait impls
default fn f(); //~ ERROR `default` is only allowed on items in trait impls
default fn g() {} //~ ERROR `default` is only allowed on items in trait impls
}

default const E: u8 = 0; //~ ERROR `default` is only allowed on items in trait impls
default type F = (); //~ ERROR `default` is only allowed on items in trait impls
default fn h() {} //~ ERROR `default` is only allowed on items in trait impls
34 changes: 29 additions & 5 deletions tests/ui/parser/trait-item-with-defaultness-fail-semantic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,43 @@ LL | default type C: Ord;
error: `default` is only allowed on items in trait impls
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:10:5
|
LL | default fn f1();
| -------^^^^^^^^^
LL | default fn f();
| -------^^^^^^^^
| |
| `default` because of this

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

error: `default` is only allowed on items in trait impls
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:14:1
|
LL | default const E: u8 = 0;
| -------^^^^^^^^^^^^^^^^^
| |
| `default` because of this

error: `default` is only allowed on items in trait impls
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:15:1
|
LL | default type F = ();
| -------^^^^^^^^^^^^^
| |
| `default` because of this

error: `default` is only allowed on items in trait impls
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:16:1
|
LL | default fn h() {}
| -------^^^^^^^
| |
| `default` because of this

warning: the feature `specialization` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/trait-item-with-defaultness-fail-semantic.rs:1:12
|
Expand All @@ -56,5 +80,5 @@ LL | #![feature(specialization)]
= help: consider using `min_specialization` instead, which is more stable and complete
= note: `#[warn(incomplete_features)]` on by default

error: aborting due to 6 previous errors; 1 warning emitted
error: aborting due to 9 previous errors; 1 warning emitted

0 comments on commit f95183e

Please sign in to comment.