Skip to content

Commit

Permalink
Auto merge of #88088 - nbdd0121:const2, r=nagisa
Browse files Browse the repository at this point in the history
Forbid inline const block referencing params from being used in patterns

Fix #82518
  • Loading branch information
bors committed Aug 29, 2021
2 parents 677b517 + dc52040 commit 3a21a5b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion compiler/rustc_mir_build/src/thir/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
16 changes: 16 additions & 0 deletions src/test/ui/inline-const/const-match-pat-generic.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions src/test/ui/inline-const/const-match-pat-generic.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit 3a21a5b

Please sign in to comment.