Auto merge of #88088 - nbdd0121:const2, r=nagisa
Forbid inline const block referencing params from being used in patterns Fix #82518
This commit is contained in:
commit
3a21a5b324
3 changed files with 31 additions and 1 deletions
|
@ -21,7 +21,7 @@ use rustc_middle::mir::UserTypeProjection;
|
|||
use rustc_middle::mir::{BorrowKind, Field, Mutability};
|
||||
use rustc_middle::thir::{Ascription, BindingMode, FieldPat, Pat, PatKind, PatRange, PatTyProj};
|
||||
use rustc_middle::ty::subst::{GenericArg, SubstsRef};
|
||||
use rustc_middle::ty::{self, AdtDef, DefIdTree, Region, Ty, TyCtxt, UserType};
|
||||
use rustc_middle::ty::{self, AdtDef, ConstKind, DefIdTree, Region, Ty, TyCtxt, UserType};
|
||||
use rustc_span::{Span, Symbol};
|
||||
|
||||
use std::cmp::Ordering;
|
||||
|
@ -545,6 +545,11 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
|
|||
hir::ExprKind::ConstBlock(ref anon_const) => {
|
||||
let anon_const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id);
|
||||
let value = ty::Const::from_anon_const(self.tcx, anon_const_def_id);
|
||||
if matches!(value.val, ConstKind::Param(_)) {
|
||||
let span = self.tcx.hir().span(anon_const.hir_id);
|
||||
self.errors.push(PatternError::ConstParamInPattern(span));
|
||||
return PatKind::Wild;
|
||||
}
|
||||
return *self.const_to_pat(value, expr.hir_id, expr.span, false).kind;
|
||||
}
|
||||
hir::ExprKind::Lit(ref lit) => (lit, false),
|
||||
|
|
16
src/test/ui/inline-const/const-match-pat-generic.rs
Normal file
16
src/test/ui/inline-const/const-match-pat-generic.rs
Normal file
|
@ -0,0 +1,16 @@
|
|||
#![allow(incomplete_features)]
|
||||
#![feature(inline_const)]
|
||||
|
||||
// rust-lang/rust#82518: ICE with inline-const in match referencing const-generic parameter
|
||||
|
||||
fn foo<const V: usize>() {
|
||||
match 0 {
|
||||
const { V } => {},
|
||||
//~^ ERROR const parameters cannot be referenced in patterns [E0158]
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
foo::<1>();
|
||||
}
|
9
src/test/ui/inline-const/const-match-pat-generic.stderr
Normal file
9
src/test/ui/inline-const/const-match-pat-generic.stderr
Normal file
|
@ -0,0 +1,9 @@
|
|||
error[E0158]: const parameters cannot be referenced in patterns
|
||||
--> $DIR/const-match-pat-generic.rs:8:11
|
||||
|
|
||||
LL | const { V } => {},
|
||||
| ^^^^^
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0158`.
|
Loading…
Add table
Reference in a new issue