Skip to content

Commit c02624b

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

File tree

7 files changed

+21
-19
lines changed

7 files changed

+21
-19
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+

tests/ui/unpretty/exhaustive.expanded.stdout

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,6 @@ mod types {
602602
let _: impl Send + 'static;
603603
let _: impl 'static + Send;
604604
let _: impl ?Sized;
605-
let _: impl [const] Clone;
606605
let _: impl for<'a> Send;
607606
}
608607
/// TyKind::Paren

tests/ui/unpretty/exhaustive.hir.stderr

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,6 @@ LL | let _: impl ?Sized;
147147
error[E0562]: `impl Trait` is not allowed in the type of variable bindings
148148
--> $DIR/exhaustive.rs:810:16
149149
|
150-
LL | let _: impl [const] Clone;
151-
| ^^^^^^^^^^^^^^^^^^
152-
|
153-
= note: `impl Trait` is only allowed in arguments and return types of functions and methods
154-
= note: see issue #63065 <https://github.com/rust-lang/rust/issues/63065> for more information
155-
= help: add `#![feature(impl_trait_in_bindings)]` to the crate attributes to enable
156-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
157-
158-
error[E0562]: `impl Trait` is not allowed in the type of variable bindings
159-
--> $DIR/exhaustive.rs:811:16
160-
|
161150
LL | let _: impl for<'a> Send;
162151
| ^^^^^^^^^^^^^^^^^
163152
|
@@ -166,7 +155,7 @@ LL | let _: impl for<'a> Send;
166155
= help: add `#![feature(impl_trait_in_bindings)]` to the crate attributes to enable
167156
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
168157

169-
error: aborting due to 20 previous errors
158+
error: aborting due to 19 previous errors
170159

171160
Some errors have detailed explanations: E0214, E0562, E0697, E0703, E0728.
172161
For more information about an error, try `rustc --explain E0214`.

tests/ui/unpretty/exhaustive.hir.stdout

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,6 @@ mod types {
668668
let _: (/*ERROR*/);
669669
let _: (/*ERROR*/);
670670
let _: (/*ERROR*/);
671-
let _: (/*ERROR*/);
672671
}
673672
/// TyKind::Paren
674673
fn ty_paren() { let _: T; }

tests/ui/unpretty/exhaustive.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,6 @@ mod types {
807807
let _: impl Send + 'static; //[hir]~ ERROR `impl Trait` is not allowed
808808
let _: impl 'static + Send; //[hir]~ ERROR `impl Trait` is not allowed
809809
let _: impl ?Sized; //[hir]~ ERROR `impl Trait` is not allowed
810-
let _: impl [const] Clone; //[hir]~ ERROR `impl Trait` is not allowed
811810
let _: impl for<'a> Send; //[hir]~ ERROR `impl Trait` is not allowed
812811
}
813812

0 commit comments

Comments
 (0)