Skip to content

Commit 5ec2c20

Browse files
committed
fix: Error on illegal [const]s inside blocks within legal positions
1 parent 64ca23b commit 5ec2c20

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,12 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
16231623
_ => self.with_in_trait_impl(None, |this| visit::walk_assoc_item(this, item, ctxt)),
16241624
}
16251625
}
1626+
1627+
fn visit_block(&mut self, block: &'a Block) {
1628+
// Conditional consts should not be used in invalid contexts—such as const bodies—even if
1629+
// they appear within the generic parameters of const traits or impls.
1630+
self.with_tilde_const(Some(TildeConstReason::Item), |this| visit::walk_block(this, block))
1631+
}
16261632
}
16271633

16281634
/// When encountering an equality constraint in a `where` clause, emit an error. If the code seems

tests/ui/traits/const-traits/conditionally-const-in-struct-args.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
//@ compile-flags: -Znext-solver
2-
//@ known-bug: #132067
3-
//@ check-pass
4-
51
#![feature(const_trait_impl)]
62

73
struct S;
@@ -11,7 +7,11 @@ trait Trait<const N: u32> {}
117
const fn f<
128
T: Trait<
139
{
10+
const fn g<U: [const] Trait<0>>() {}
11+
1412
struct I<U: [const] Trait<0>>(U);
13+
//~^ ERROR `[const]` is not allowed here
14+
1515
0
1616
},
1717
>,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: `[const]` is not allowed here
2+
--> $DIR/conditionally-const-in-struct-args.rs:12:25
3+
|
4+
LL | struct I<U: [const] Trait<0>>(U);
5+
| ^^^^^^^
6+
|
7+
= note: this item cannot have `[const]` trait bounds
8+
9+
error: aborting due to 1 previous error
10+

0 commit comments

Comments
 (0)