Don't synthesize host effect params for trait assoc fns marked const

This commit is contained in:
León Orell Valerian Liehr 2024-01-01 23:23:11 +01:00
parent 88d69b72b4
commit ba860344e1
No known key found for this signature in database
GPG Key ID: D17A07215F68E713
3 changed files with 34 additions and 4 deletions

View File

@ -1254,9 +1254,11 @@ fn lower_method_sig(
coroutine_kind: Option<CoroutineKind>, coroutine_kind: Option<CoroutineKind>,
) -> (&'hir hir::Generics<'hir>, hir::FnSig<'hir>) { ) -> (&'hir hir::Generics<'hir>, hir::FnSig<'hir>) {
let header = self.lower_fn_header(sig.header); let header = self.lower_fn_header(sig.header);
// Don't pass along the user-provided constness of trait associated functions; we don't want to
// synthesize a host effect param for them. We reject `const` on them during AST validation.
let constness = if kind == FnDeclKind::Inherent { sig.header.constness } else { Const::No };
let itctx = ImplTraitContext::Universal; let itctx = ImplTraitContext::Universal;
let (generics, decl) = let (generics, decl) = self.lower_generics(generics, constness, id, &itctx, |this| {
self.lower_generics(generics, sig.header.constness, id, &itctx, |this| {
this.lower_fn_decl(&sig.decl, id, sig.span, kind, coroutine_kind) this.lower_fn_decl(&sig.decl, id, sig.span, kind, coroutine_kind)
}); });
(generics, hir::FnSig { header, decl, span: self.lower_span(sig.span) }) (generics, hir::FnSig { header, decl, span: self.lower_span(sig.span) })

View File

@ -0,0 +1,13 @@
// Regression test for issue #113378.
#![feature(const_trait_impl, effects)]
#[const_trait]
trait Trait {
const fn fun(); //~ ERROR functions in traits cannot be declared const
}
impl const Trait for () {
const fn fun() {} //~ ERROR functions in traits cannot be declared const
}
fn main() {}

View File

@ -0,0 +1,15 @@
error[E0379]: functions in traits cannot be declared const
--> $DIR/trait-fn-const.rs:6:5
|
LL | const fn fun();
| ^^^^^ functions in traits cannot be const
error[E0379]: functions in traits cannot be declared const
--> $DIR/trait-fn-const.rs:10:5
|
LL | const fn fun() {}
| ^^^^^ functions in traits cannot be const
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0379`.