Skip to content

Commit d23800f

Browse files
oli-obkOliver 'ker' Schneider
authored and
Oliver 'ker' Schneider
committed
allow const function calls in consts that are used in patterns
closes #30117
1 parent d75f861 commit d23800f

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/librustc/middle/const_eval.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,11 @@ pub fn const_expr_to_pat(tcx: &ty::ctxt, expr: &Expr, span: Span) -> P<hir::Pat>
332332
let path = match def.full_def() {
333333
def::DefStruct(def_id) => def_to_path(tcx, def_id),
334334
def::DefVariant(_, variant_did, _) => def_to_path(tcx, variant_did),
335+
def::DefFn(..) => return P(hir::Pat {
336+
id: expr.id,
337+
node: hir::PatLit(P(expr.clone())),
338+
span: span,
339+
}),
335340
_ => unreachable!()
336341
};
337342
let pats = args.iter().map(|expr| const_expr_to_pat(tcx, &**expr, span)).collect();
@@ -1462,6 +1467,6 @@ fn get_fn_def<'a>(tcx: &'a ty::ctxt,
14621467
_ => signal!(e, NonConstPath),
14631468
},
14641469
Some(ast_map::NodeTraitItem(..)) => signal!(e, NonConstPath),
1465-
Some(_) => unimplemented!(),
1470+
Some(_) => signal!(e, UnimplementedConstVal("calling struct, tuple or variant")),
14661471
}
14671472
}

src/test/run-pass/consts-in-patterns.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -8,15 +8,20 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(const_fn)]
1112

1213
const FOO: isize = 10;
1314
const BAR: isize = 3;
1415

16+
const fn foo() -> isize { 4 }
17+
const BOO: isize = foo();
18+
1519
pub fn main() {
1620
let x: isize = 3;
1721
let y = match x {
1822
FOO => 1,
1923
BAR => 2,
24+
BOO => 4,
2025
_ => 3
2126
};
2227
assert_eq!(y, 2);

0 commit comments

Comments
 (0)