Skip to content

Commit 749eeee

Browse files
committed
hir ty lower
1 parent f8adfcb commit 749eeee

24 files changed

+171
-192
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2486,16 +2486,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
24862486
let expr = &tcx.hir_body(anon.body).value;
24872487
debug!(?expr);
24882488

2489-
// If the rhs is an anon const naming generics it shouldn't have
2490-
// access to then we lower to `ConstKind::Error`. This prevents
2491-
// `try_lower_anon_const_lit` from ICEing on anon consts such as
2492-
// `const { N }` which aren't supposed to be legal.
2493-
if let ty::AnonConstKind::MCG = tcx.anon_const_kind(anon.def_id)
2494-
&& let Err(e) = tcx.check_anon_const_invalid_param_uses(anon.def_id)
2495-
{
2496-
return ty::Const::new_error(tcx, e);
2497-
}
2498-
24992489
// FIXME(generic_const_parameter_types): We should use the proper generic args
25002490
// here. It's only used as a hint for literals so doesn't matter too much to use the right
25012491
// generic arguments, just weaker type inference.
@@ -2521,24 +2511,27 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
25212511
) -> Option<Const<'tcx>> {
25222512
let tcx = self.tcx();
25232513

2524-
// Unwrap a block, so that e.g. `{ P }` is recognised as a parameter. Const arguments
2525-
// currently have to be wrapped in curly brackets, so it's necessary to special-case.
2514+
// Unwrap a block, so that e.g. `{ 1 }` is recognised as a literal. This makes the
2515+
// performance optimisation of directly lowering anon consts occur more often.
25262516
let expr = match &expr.kind {
25272517
hir::ExprKind::Block(block, _) if block.stmts.is_empty() && block.expr.is_some() => {
25282518
block.expr.as_ref().unwrap()
25292519
}
25302520
_ => expr,
25312521
};
25322522

2523+
// FIXME(mgca): remove this delayed bug once we start checking this
2524+
// when lowering `Ty/ConstKind::Param`s more generally.
25332525
if let hir::ExprKind::Path(hir::QPath::Resolved(
25342526
_,
25352527
&hir::Path { res: Res::Def(DefKind::ConstParam, _), .. },
25362528
)) = expr.kind
25372529
{
2538-
span_bug!(
2530+
let e = tcx.dcx().span_delayed_bug(
25392531
expr.span,
2540-
"try_lower_anon_const_lit: received const param which shouldn't be possible"
2532+
"try_lower_anon_const_lit: received const param which shouldn't be possible",
25412533
);
2534+
return Some(ty::Const::new_error(tcx, e));
25422535
};
25432536

25442537
let lit_input = match expr.kind {

tests/crashes/127972.rs

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/crashes/137888.rs

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/crashes/140275.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/ui/const-generics/mgca/adt_expr_arg_simple.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ fn bar<T: Trait, const N: u32>() {
3333
// in anon const const args
3434
foo::<{ Some::<u32> { 0: const { N + 1 } } }>();
3535
//~^ ERROR: generic parameters may not be used in const operations
36-
//~| ERROR: generic parameters may not be used in const operations
3736
}
3837

3938
fn main() {}

tests/ui/const-generics/mgca/adt_expr_arg_simple.stderr

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,5 @@ error: generic parameters may not be used in const operations
1010
LL | foo::<{ Some::<u32> { 0: const { N + 1 } } }>();
1111
| ^
1212

13-
error: generic parameters may not be used in const operations
14-
--> $DIR/adt_expr_arg_simple.rs:34:38
15-
|
16-
LL | foo::<{ Some::<u32> { 0: const { N + 1 } } }>();
17-
| ^
18-
|
19-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
20-
21-
error: aborting due to 3 previous errors
13+
error: aborting due to 2 previous errors
2214

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(associated_const_equality, generic_const_items, min_generic_const_args)]
2+
#![expect(incomplete_features)]
3+
// library crates exercise weirder code paths around
4+
// DefIds which were created for const args.
5+
#![crate_type = "lib"]
6+
7+
struct Foo<const N: usize>;
8+
9+
type Alias<const N: usize> = Foo<const { N }>;
10+
//~^ ERROR: generic parameters may not be used in const operations
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: generic parameters may not be used in const operations
2+
--> $DIR/explicit_anon_consts-2.rs:9:42
3+
|
4+
LL | type Alias<const N: usize> = Foo<const { N }>;
5+
| ^
6+
7+
error: aborting due to 1 previous error
8+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(associated_const_equality, generic_const_items, min_generic_const_args)]
2+
#![expect(incomplete_features)]
3+
// library crates exercise weirder code paths around
4+
// DefIds which were created for const args.
5+
#![crate_type = "lib"]
6+
7+
struct Foo<const N: usize>;
8+
9+
type Alias<const N: usize> = [(); const { N }];
10+
//~^ ERROR: generic parameters may not be used in const operations
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: generic parameters may not be used in const operations
2+
--> $DIR/explicit_anon_consts-3.rs:9:43
3+
|
4+
LL | type Alias<const N: usize> = [(); const { N }];
5+
| ^
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)