Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_ast_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_ast_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/traits/const-traits/const-auto-trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![feature(auto_traits, const_trait_impl)]

const auto trait Marker {}
//~^ ERROR: auto traits cannot be const

fn main() {}
10 changes: 10 additions & 0 deletions tests/ui/traits/const-traits/const-auto-trait.stderr
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could provide a structured suggestion but yeah, it's not really worth the effort.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially thought about that, but decided not to, mainly because not every suggestion can be fixed automatically with rustfix, for example the code sample in the original issue that triggers a panic in next-gen solvers, and because we don't really have anything meaningful to add beyond what the current message already communicates.

Copy link
Member

@fmease fmease Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the next-gen solver still crashes despite this change? Right, of course.

I guess we should replace the unreachable!(…) in consider_auto_trait_candidate of HostEffectPredicate with a cx.delay_bug. Sorry, I forgot about that.


error: aborting due to 1 previous error

1 change: 1 addition & 0 deletions tests/ui/traits/const-traits/parse-const-unsafe-trait.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Test that `const unsafe trait` and `const unsafe auto trait` works.

//@ compile-flags: -Zparse-crate-root-only
//@ check-pass

#![feature(const_trait_impl)]
Expand Down
Loading