diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 8835fba1adcdc..0ed6dea40cd4c 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -1622,16 +1622,8 @@ impl<'a> Parser<'a> { let first_expr = self.parse_expr()?; if self.eat(exp!(Semi)) { // Repeating array syntax: `[ 0; 512 ]` - let count = if self.eat_keyword(exp!(Const)) { - // While we could just disambiguate `Direct` from `AnonConst` by - // treating all const block exprs as `AnonConst`, that would - // complicate the DefCollector and likely all other visitors. - // So we strip the const blockiness and just store it as a block - // in the AST with the extra disambiguator on the AnonConst - self.parse_mgca_const_block(false)? - } else { - self.parse_expr_anon_const(|this, expr| this.mgca_direct_lit_hack(expr))? - }; + let count = + self.parse_expr_anon_const(|this, expr| this.mgca_direct_lit_hack(expr))?; self.expect(close)?; ExprKind::Repeat(first_expr, count) } else if self.eat(exp!(Comma)) { diff --git a/compiler/rustc_parse/src/parser/ty.rs b/compiler/rustc_parse/src/parser/ty.rs index 0185c51c5c560..1734bb573f90d 100644 --- a/compiler/rustc_parse/src/parser/ty.rs +++ b/compiler/rustc_parse/src/parser/ty.rs @@ -658,16 +658,8 @@ impl<'a> Parser<'a> { }; let ty = if self.eat(exp!(Semi)) { - let mut length = if self.eat_keyword(exp!(Const)) { - // While we could just disambiguate `Direct` from `AnonConst` by - // treating all const block exprs as `AnonConst`, that would - // complicate the DefCollector and likely all other visitors. - // So we strip the const blockiness and just store it as a block - // in the AST with the extra disambiguator on the AnonConst - self.parse_mgca_const_block(false)? - } else { - self.parse_expr_anon_const(|this, expr| this.mgca_direct_lit_hack(expr))? - }; + let mut length = + self.parse_expr_anon_const(|this, expr| this.mgca_direct_lit_hack(expr))?; if let Err(e) = self.expect(exp!(CloseBracket)) { // Try to recover from `X` when `X::` works diff --git a/src/tools/rustfmt/tests/source/issue-6788.rs b/src/tools/rustfmt/tests/source/issue-6788.rs new file mode 100644 index 0000000000000..0e63ab53a1ac9 --- /dev/null +++ b/src/tools/rustfmt/tests/source/issue-6788.rs @@ -0,0 +1,7 @@ +fn foo() { + let a = [(); const { let x = 1; x }]; +} + +fn foo() { + let x = [(); const { 1 }]; +} diff --git a/src/tools/rustfmt/tests/target/issue-6788.rs b/src/tools/rustfmt/tests/target/issue-6788.rs new file mode 100644 index 0000000000000..c559438b2d52b --- /dev/null +++ b/src/tools/rustfmt/tests/target/issue-6788.rs @@ -0,0 +1,10 @@ +fn foo() { + let a = [(); const { + let x = 1; + x + }]; +} + +fn foo() { + let x = [(); const { 1 }]; +} diff --git a/tests/ui/const-generics/mgca/explicit_anon_consts.rs b/tests/ui/const-generics/mgca/explicit_anon_consts.rs deleted file mode 100644 index bf825a44b1c04..0000000000000 --- a/tests/ui/const-generics/mgca/explicit_anon_consts.rs +++ /dev/null @@ -1,73 +0,0 @@ -#![feature(generic_const_items, min_generic_const_args)] -#![expect(incomplete_features)] -// library crates exercise weirder code paths around -// DefIds which were created for const args. -#![crate_type = "lib"] - -struct Foo; - -type Adt1 = Foo; -type Adt2 = Foo<{ N }>; -type Adt3 = Foo; -//~^ ERROR: generic parameters may not be used in const operations -type Adt4 = Foo<{ 1 + 1 }>; -//~^ ERROR: complex const arguments must be placed inside of a `const` block -type Adt5 = Foo; - -type Arr = [(); N]; -type Arr2 = [(); { N }]; -type Arr3 = [(); const { N }]; -//~^ ERROR: generic parameters may not be used in const operations -type Arr4 = [(); 1 + 1]; -//~^ ERROR: complex const arguments must be placed inside of a `const` block -type Arr5 = [(); const { 1 + 1 }]; - -fn repeats() -> [(); N] { - let _1 = [(); N]; - let _2 = [(); { N }]; - let _3 = [(); const { N }]; - //~^ ERROR: generic parameters may not be used in const operations - let _4 = [(); 1 + 1]; - //~^ ERROR: complex const arguments must be placed inside of a `const` block - let _5 = [(); const { 1 + 1 }]; - let _6: [(); const { N }] = todo!(); - //~^ ERROR: generic parameters may not be used in const operations -} - -#[type_const] -const ITEM1: usize = N; -#[type_const] -const ITEM2: usize = { N }; -#[type_const] -const ITEM3: usize = const { N }; -//~^ ERROR: generic parameters may not be used in const operations -#[type_const] -const ITEM4: usize = { 1 + 1 }; -//~^ ERROR: complex const arguments must be placed inside of a `const` block -#[type_const] -const ITEM5: usize = const { 1 + 1}; - -trait Trait { - #[type_const] - const ASSOC: usize; -} - -fn ace_bounds< - const N: usize, - // We skip the T1 case because it doesn't resolve - // T1: Trait, - T2: Trait, - T3: Trait, - //~^ ERROR: generic parameters may not be used in const operations - T4: Trait, - //~^ ERROR: complex const arguments must be placed inside of a `const` block - T5: Trait, ->() {} - -struct Default1; -struct Default2; -struct Default3; -//~^ ERROR: generic parameters may not be used in const operations -struct Default4; -//~^ ERROR: complex const arguments must be placed inside of a `const` block -struct Default5; diff --git a/tests/ui/const-generics/mgca/explicit_anon_consts.stderr b/tests/ui/const-generics/mgca/explicit_anon_consts.stderr deleted file mode 100644 index 551815c4c31a0..0000000000000 --- a/tests/ui/const-generics/mgca/explicit_anon_consts.stderr +++ /dev/null @@ -1,80 +0,0 @@ -error: complex const arguments must be placed inside of a `const` block - --> $DIR/explicit_anon_consts.rs:13:33 - | -LL | type Adt4 = Foo<{ 1 + 1 }>; - | ^^^^^^^^^ - -error: complex const arguments must be placed inside of a `const` block - --> $DIR/explicit_anon_consts.rs:21:34 - | -LL | type Arr4 = [(); 1 + 1]; - | ^^^^^ - -error: complex const arguments must be placed inside of a `const` block - --> $DIR/explicit_anon_consts.rs:30:19 - | -LL | let _4 = [(); 1 + 1]; - | ^^^^^ - -error: complex const arguments must be placed inside of a `const` block - --> $DIR/explicit_anon_consts.rs:45:38 - | -LL | const ITEM4: usize = { 1 + 1 }; - | ^^^^^^^^^ - -error: complex const arguments must be placed inside of a `const` block - --> $DIR/explicit_anon_consts.rs:62:23 - | -LL | T4: Trait, - | ^^^^^^^^^ - -error: complex const arguments must be placed inside of a `const` block - --> $DIR/explicit_anon_consts.rs:71:50 - | -LL | struct Default4; - | ^^^^^^^^^ - -error: generic parameters may not be used in const operations - --> $DIR/explicit_anon_consts.rs:42:46 - | -LL | const ITEM3: usize = const { N }; - | ^ - -error: generic parameters may not be used in const operations - --> $DIR/explicit_anon_consts.rs:60:31 - | -LL | T3: Trait, - | ^ - -error: generic parameters may not be used in const operations - --> $DIR/explicit_anon_consts.rs:69:58 - | -LL | struct Default3; - | ^ - -error: generic parameters may not be used in const operations - --> $DIR/explicit_anon_consts.rs:28:27 - | -LL | let _3 = [(); const { N }]; - | ^ - -error: generic parameters may not be used in const operations - --> $DIR/explicit_anon_consts.rs:33:26 - | -LL | let _6: [(); const { N }] = todo!(); - | ^ - -error: generic parameters may not be used in const operations - --> $DIR/explicit_anon_consts.rs:11:41 - | -LL | type Adt3 = Foo; - | ^ - -error: generic parameters may not be used in const operations - --> $DIR/explicit_anon_consts.rs:19:42 - | -LL | type Arr3 = [(); const { N }]; - | ^ - -error: aborting due to 13 previous errors - diff --git a/tests/ui/const-generics/mgca/selftyalias-containing-param.rs b/tests/ui/const-generics/mgca/selftyalias-containing-param.rs deleted file mode 100644 index 5ab39799078fa..0000000000000 --- a/tests/ui/const-generics/mgca/selftyalias-containing-param.rs +++ /dev/null @@ -1,13 +0,0 @@ -#![feature(min_generic_const_args)] -#![expect(incomplete_features)] - -struct S([(); N]); - -impl S { - fn foo() -> [(); const { let _: Self = loop {}; 1 }] { - //~^ ERROR generic `Self` - todo!() - } -} - -fn main() {} diff --git a/tests/ui/const-generics/mgca/selftyalias-containing-param.stderr b/tests/ui/const-generics/mgca/selftyalias-containing-param.stderr deleted file mode 100644 index fdd3e6efdf653..0000000000000 --- a/tests/ui/const-generics/mgca/selftyalias-containing-param.stderr +++ /dev/null @@ -1,14 +0,0 @@ -error: generic `Self` types are currently not permitted in anonymous constants - --> $DIR/selftyalias-containing-param.rs:7:37 - | -LL | fn foo() -> [(); const { let _: Self = loop {}; 1 }] { - | ^^^^ - | -note: not a concrete type - --> $DIR/selftyalias-containing-param.rs:6:22 - | -LL | impl S { - | ^^^^ - -error: aborting due to 1 previous error - diff --git a/tests/ui/const-generics/mgca/selftyparam.rs b/tests/ui/const-generics/mgca/selftyparam.rs deleted file mode 100644 index 58433405b8ca0..0000000000000 --- a/tests/ui/const-generics/mgca/selftyparam.rs +++ /dev/null @@ -1,9 +0,0 @@ -#![feature(min_generic_const_args)] -#![expect(incomplete_features)] - -trait Tr { - fn foo() -> [(); const { let _: Self; 1 }]; - //~^ ERROR generic parameters -} - -fn main() {} diff --git a/tests/ui/const-generics/mgca/selftyparam.stderr b/tests/ui/const-generics/mgca/selftyparam.stderr deleted file mode 100644 index 376e63da9a759..0000000000000 --- a/tests/ui/const-generics/mgca/selftyparam.stderr +++ /dev/null @@ -1,8 +0,0 @@ -error: generic parameters may not be used in const operations - --> $DIR/selftyparam.rs:5:37 - | -LL | fn foo() -> [(); const { let _: Self; 1 }]; - | ^^^^ - -error: aborting due to 1 previous error - diff --git a/tests/ui/const-generics/mgca/type_const-array-return.rs b/tests/ui/const-generics/mgca/type_const-array-return.rs deleted file mode 100644 index 5375e4fded6d7..0000000000000 --- a/tests/ui/const-generics/mgca/type_const-array-return.rs +++ /dev/null @@ -1,26 +0,0 @@ -//@ check-pass -// This test should compile without an ICE. -#![expect(incomplete_features)] -#![feature(min_generic_const_args)] - -pub struct A; - -pub trait Array { - #[type_const] - const LEN: usize; - fn arr() -> [u8; Self::LEN]; -} - -impl Array for A { - #[type_const] - const LEN: usize = 4; - - #[allow(unused_braces)] - fn arr() -> [u8; const { Self::LEN }] { - return [0u8; const { Self::LEN }]; - } -} - -fn main() { - let _ = A::arr(); -}