Skip to content

Commit ba86034

Browse files
committed
Don't synthesize host effect params for trait assoc fns marked const
1 parent 88d69b7 commit ba86034

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,11 +1254,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
12541254
coroutine_kind: Option<CoroutineKind>,
12551255
) -> (&'hir hir::Generics<'hir>, hir::FnSig<'hir>) {
12561256
let header = self.lower_fn_header(sig.header);
1257+
// Don't pass along the user-provided constness of trait associated functions; we don't want to
1258+
// synthesize a host effect param for them. We reject `const` on them during AST validation.
1259+
let constness = if kind == FnDeclKind::Inherent { sig.header.constness } else { Const::No };
12571260
let itctx = ImplTraitContext::Universal;
1258-
let (generics, decl) =
1259-
self.lower_generics(generics, sig.header.constness, id, &itctx, |this| {
1260-
this.lower_fn_decl(&sig.decl, id, sig.span, kind, coroutine_kind)
1261-
});
1261+
let (generics, decl) = self.lower_generics(generics, constness, id, &itctx, |this| {
1262+
this.lower_fn_decl(&sig.decl, id, sig.span, kind, coroutine_kind)
1263+
});
12621264
(generics, hir::FnSig { header, decl, span: self.lower_span(sig.span) })
12631265
}
12641266

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Regression test for issue #113378.
2+
#![feature(const_trait_impl, effects)]
3+
4+
#[const_trait]
5+
trait Trait {
6+
const fn fun(); //~ ERROR functions in traits cannot be declared const
7+
}
8+
9+
impl const Trait for () {
10+
const fn fun() {} //~ ERROR functions in traits cannot be declared const
11+
}
12+
13+
fn main() {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0379]: functions in traits cannot be declared const
2+
--> $DIR/trait-fn-const.rs:6:5
3+
|
4+
LL | const fn fun();
5+
| ^^^^^ functions in traits cannot be const
6+
7+
error[E0379]: functions in traits cannot be declared const
8+
--> $DIR/trait-fn-const.rs:10:5
9+
|
10+
LL | const fn fun() {}
11+
| ^^^^^ functions in traits cannot be const
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0379`.

0 commit comments

Comments
 (0)