Skip to content

Commit 3a21a5b

Browse files
committed
Auto merge of #88088 - nbdd0121:const2, r=nagisa
Forbid inline const block referencing params from being used in patterns Fix #82518
2 parents 677b517 + dc52040 commit 3a21a5b

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_middle::mir::UserTypeProjection;
2121
use rustc_middle::mir::{BorrowKind, Field, Mutability};
2222
use rustc_middle::thir::{Ascription, BindingMode, FieldPat, Pat, PatKind, PatRange, PatTyProj};
2323
use rustc_middle::ty::subst::{GenericArg, SubstsRef};
24-
use rustc_middle::ty::{self, AdtDef, DefIdTree, Region, Ty, TyCtxt, UserType};
24+
use rustc_middle::ty::{self, AdtDef, ConstKind, DefIdTree, Region, Ty, TyCtxt, UserType};
2525
use rustc_span::{Span, Symbol};
2626

2727
use std::cmp::Ordering;
@@ -545,6 +545,11 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
545545
hir::ExprKind::ConstBlock(ref anon_const) => {
546546
let anon_const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id);
547547
let value = ty::Const::from_anon_const(self.tcx, anon_const_def_id);
548+
if matches!(value.val, ConstKind::Param(_)) {
549+
let span = self.tcx.hir().span(anon_const.hir_id);
550+
self.errors.push(PatternError::ConstParamInPattern(span));
551+
return PatKind::Wild;
552+
}
548553
return *self.const_to_pat(value, expr.hir_id, expr.span, false).kind;
549554
}
550555
hir::ExprKind::Lit(ref lit) => (lit, false),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![allow(incomplete_features)]
2+
#![feature(inline_const)]
3+
4+
// rust-lang/rust#82518: ICE with inline-const in match referencing const-generic parameter
5+
6+
fn foo<const V: usize>() {
7+
match 0 {
8+
const { V } => {},
9+
//~^ ERROR const parameters cannot be referenced in patterns [E0158]
10+
_ => {},
11+
}
12+
}
13+
14+
fn main() {
15+
foo::<1>();
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0158]: const parameters cannot be referenced in patterns
2+
--> $DIR/const-match-pat-generic.rs:8:11
3+
|
4+
LL | const { V } => {},
5+
| ^^^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0158`.

0 commit comments

Comments
 (0)