Deny const auto traits

This commit is contained in:
lapla 2025-11-25 17:53:48 +09:00
parent d3e1ccdf40
commit 231a3a241b
No known key found for this signature in database
GPG key ID: B39C71D9F127FF9F
5 changed files with 35 additions and 0 deletions

View file

@ -89,6 +89,9 @@ ast_passes_const_and_coroutine = functions cannot be both `const` and `{$corouti
.coroutine = `{$coroutine_kind}` because of this
.label = {""}
ast_passes_const_auto_trait = auto traits cannot be const
.help = remove the `const` keyword
ast_passes_const_bound_trait_object = const trait bounds are not allowed in trait object types
ast_passes_const_without_body =

View file

@ -820,6 +820,12 @@ impl<'a> AstValidator<'a> {
self.dcx().emit_err(errors::ModuleNonAscii { span: ident.span, name: ident.name });
}
fn deny_const_auto_traits(&self, constness: Const) {
if let Const::Yes(span) = constness {
self.dcx().emit_err(errors::ConstAutoTrait { span });
}
}
fn deny_generic_params(&self, generics: &Generics, ident_span: Span) {
if !generics.params.is_empty() {
self.dcx()
@ -1257,6 +1263,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}) => {
self.visit_attrs_vis_ident(&item.attrs, &item.vis, ident);
if *is_auto == IsAuto::Yes {
// For why we reject `const auto trait`, see rust-lang/rust#149285.
self.deny_const_auto_traits(*constness);
// Auto traits cannot have generics, super traits nor contain items.
self.deny_generic_params(generics, ident.span);
self.deny_super_traits(bounds, ident.span);

View file

@ -429,6 +429,14 @@ pub(crate) struct AutoTraitItems {
pub ident: Span,
}
#[derive(Diagnostic)]
#[diag(ast_passes_const_auto_trait)]
#[help]
pub(crate) struct ConstAutoTrait {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag(ast_passes_generic_before_constraints)]
pub(crate) struct ArgsBeforeConstraint {

View file

@ -0,0 +1,6 @@
#![feature(auto_traits, const_trait_impl)]
const auto trait Marker {}
//~^ ERROR: auto traits cannot be const
fn main() {}

View file

@ -0,0 +1,10 @@
error: auto traits cannot be const
--> $DIR/const-auto-trait.rs:3:1
|
LL | const auto trait Marker {}
| ^^^^^
|
= help: remove the `const` keyword
error: aborting due to 1 previous error