Skip to content

Commit 231a3a2

Browse files
committed
Deny const auto traits
1 parent d3e1ccd commit 231a3a2

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

compiler/rustc_ast_passes/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ ast_passes_const_and_coroutine = functions cannot be both `const` and `{$corouti
8989
.coroutine = `{$coroutine_kind}` because of this
9090
.label = {""}
9191
92+
ast_passes_const_auto_trait = auto traits cannot be const
93+
.help = remove the `const` keyword
94+
9295
ast_passes_const_bound_trait_object = const trait bounds are not allowed in trait object types
9396
9497
ast_passes_const_without_body =

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,12 @@ impl<'a> AstValidator<'a> {
820820
self.dcx().emit_err(errors::ModuleNonAscii { span: ident.span, name: ident.name });
821821
}
822822

823+
fn deny_const_auto_traits(&self, constness: Const) {
824+
if let Const::Yes(span) = constness {
825+
self.dcx().emit_err(errors::ConstAutoTrait { span });
826+
}
827+
}
828+
823829
fn deny_generic_params(&self, generics: &Generics, ident_span: Span) {
824830
if !generics.params.is_empty() {
825831
self.dcx()
@@ -1257,6 +1263,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12571263
}) => {
12581264
self.visit_attrs_vis_ident(&item.attrs, &item.vis, ident);
12591265
if *is_auto == IsAuto::Yes {
1266+
// For why we reject `const auto trait`, see rust-lang/rust#149285.
1267+
self.deny_const_auto_traits(*constness);
12601268
// Auto traits cannot have generics, super traits nor contain items.
12611269
self.deny_generic_params(generics, ident.span);
12621270
self.deny_super_traits(bounds, ident.span);

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,14 @@ pub(crate) struct AutoTraitItems {
429429
pub ident: Span,
430430
}
431431

432+
#[derive(Diagnostic)]
433+
#[diag(ast_passes_const_auto_trait)]
434+
#[help]
435+
pub(crate) struct ConstAutoTrait {
436+
#[primary_span]
437+
pub span: Span,
438+
}
439+
432440
#[derive(Diagnostic)]
433441
#[diag(ast_passes_generic_before_constraints)]
434442
pub(crate) struct ArgsBeforeConstraint {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![feature(auto_traits, const_trait_impl)]
2+
3+
const auto trait Marker {}
4+
//~^ ERROR: auto traits cannot be const
5+
6+
fn main() {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: auto traits cannot be const
2+
--> $DIR/const-auto-trait.rs:3:1
3+
|
4+
LL | const auto trait Marker {}
5+
| ^^^^^
6+
|
7+
= help: remove the `const` keyword
8+
9+
error: aborting due to 1 previous error
10+

0 commit comments

Comments
 (0)