Skip to content

Commit 9ace0a4

Browse files
committed
Auto merge of #30141 - oli-obk:fix/30117, r=arielb1
r? @arielb1
2 parents 9e63cec + c71dcca commit 9ace0a4

File tree

5 files changed

+91
-2
lines changed

5 files changed

+91
-2
lines changed

src/librustc/middle/const_eval.rs

+6-1
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();
@@ -1440,6 +1445,6 @@ fn get_fn_def<'a>(tcx: &'a ty::ctxt,
14401445
_ => signal!(e, NonConstPath),
14411446
},
14421447
Some(ast_map::NodeTraitItem(..)) => signal!(e, NonConstPath),
1443-
Some(_) => unimplemented!(),
1448+
Some(_) => signal!(e, UnimplementedConstVal("calling struct, tuple or variant")),
14441449
}
14451450
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(const_fn)]
12+
13+
enum Cake {
14+
BlackForest,
15+
Marmor,
16+
}
17+
use Cake::*;
18+
19+
const BOO: (Cake, Cake) = (Marmor, BlackForest);
20+
//~^ ERROR: constant evaluation error: non-constant path in constant expression [E0471]
21+
const FOO: Cake = BOO.1;
22+
23+
const fn foo() -> Cake {
24+
Marmor //~ ERROR: constant evaluation error: non-constant path in constant expression [E0471]
25+
//~^ ERROR: non-constant path in constant expression
26+
}
27+
28+
const WORKS: Cake = Marmor;
29+
30+
const GOO: Cake = foo();
31+
32+
fn main() {
33+
match BlackForest {
34+
FOO => println!("hi"), //~ NOTE: in pattern here
35+
GOO => println!("meh"), //~ NOTE: in pattern here
36+
WORKS => println!("möp"),
37+
_ => println!("bye"),
38+
}
39+
}

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

+6-1
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);

src/test/run-pass/issue29927-1.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(const_fn)]
12+
const fn f() -> usize {
13+
5
14+
}
15+
struct A {
16+
field: usize,
17+
}
18+
fn main() {
19+
let _ = [0; f()];
20+
}

src/test/run-pass/issue29927.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(const_fn)]
12+
struct A {
13+
field: usize,
14+
}
15+
const fn f() -> usize {
16+
5
17+
}
18+
fn main() {
19+
let _ = [0; f()];
20+
}

0 commit comments

Comments
 (0)