commit
9ace0a46de
src
librustc/middle
test
@ -332,6 +332,11 @@ pub fn const_expr_to_pat(tcx: &ty::ctxt, expr: &Expr, span: Span) -> P<hir::Pat>
|
||||
let path = match def.full_def() {
|
||||
def::DefStruct(def_id) => def_to_path(tcx, def_id),
|
||||
def::DefVariant(_, variant_did, _) => def_to_path(tcx, variant_did),
|
||||
def::DefFn(..) => return P(hir::Pat {
|
||||
id: expr.id,
|
||||
node: hir::PatLit(P(expr.clone())),
|
||||
span: span,
|
||||
}),
|
||||
_ => unreachable!()
|
||||
};
|
||||
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,
|
||||
_ => signal!(e, NonConstPath),
|
||||
},
|
||||
Some(ast_map::NodeTraitItem(..)) => signal!(e, NonConstPath),
|
||||
Some(_) => unimplemented!(),
|
||||
Some(_) => signal!(e, UnimplementedConstVal("calling struct, tuple or variant")),
|
||||
}
|
||||
}
|
||||
|
39
src/test/compile-fail/const-pattern-not-const-evaluable.rs
Normal file
39
src/test/compile-fail/const-pattern-not-const-evaluable.rs
Normal file
@ -0,0 +1,39 @@
|
||||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![feature(const_fn)]
|
||||
|
||||
enum Cake {
|
||||
BlackForest,
|
||||
Marmor,
|
||||
}
|
||||
use Cake::*;
|
||||
|
||||
const BOO: (Cake, Cake) = (Marmor, BlackForest);
|
||||
//~^ ERROR: constant evaluation error: non-constant path in constant expression [E0471]
|
||||
const FOO: Cake = BOO.1;
|
||||
|
||||
const fn foo() -> Cake {
|
||||
Marmor //~ ERROR: constant evaluation error: non-constant path in constant expression [E0471]
|
||||
//~^ ERROR: non-constant path in constant expression
|
||||
}
|
||||
|
||||
const WORKS: Cake = Marmor;
|
||||
|
||||
const GOO: Cake = foo();
|
||||
|
||||
fn main() {
|
||||
match BlackForest {
|
||||
FOO => println!("hi"), //~ NOTE: in pattern here
|
||||
GOO => println!("meh"), //~ NOTE: in pattern here
|
||||
WORKS => println!("möp"),
|
||||
_ => println!("bye"),
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
@ -8,15 +8,20 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![feature(const_fn)]
|
||||
|
||||
const FOO: isize = 10;
|
||||
const BAR: isize = 3;
|
||||
|
||||
const fn foo() -> isize { 4 }
|
||||
const BOO: isize = foo();
|
||||
|
||||
pub fn main() {
|
||||
let x: isize = 3;
|
||||
let y = match x {
|
||||
FOO => 1,
|
||||
BAR => 2,
|
||||
BOO => 4,
|
||||
_ => 3
|
||||
};
|
||||
assert_eq!(y, 2);
|
||||
|
20
src/test/run-pass/issue29927-1.rs
Normal file
20
src/test/run-pass/issue29927-1.rs
Normal file
@ -0,0 +1,20 @@
|
||||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![feature(const_fn)]
|
||||
const fn f() -> usize {
|
||||
5
|
||||
}
|
||||
struct A {
|
||||
field: usize,
|
||||
}
|
||||
fn main() {
|
||||
let _ = [0; f()];
|
||||
}
|
20
src/test/run-pass/issue29927.rs
Normal file
20
src/test/run-pass/issue29927.rs
Normal file
@ -0,0 +1,20 @@
|
||||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![feature(const_fn)]
|
||||
struct A {
|
||||
field: usize,
|
||||
}
|
||||
const fn f() -> usize {
|
||||
5
|
||||
}
|
||||
fn main() {
|
||||
let _ = [0; f()];
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user