Skip to content

Commit

Permalink
rustc_const_eval: support all unit enum variants.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Apr 23, 2017
1 parent 0ff828b commit 8054377
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 67 deletions.
9 changes: 6 additions & 3 deletions src/librustc/ich/impls_ty.rs
Expand Up @@ -273,6 +273,12 @@ for ::middle::const_val::ConstVal<'tcx> {
ConstVal::Bool(value) => {
value.hash_stable(hcx, hasher);
}
ConstVal::Char(value) => {
value.hash_stable(hcx, hasher);
}
ConstVal::Variant(def_id) => {
def_id.hash_stable(hcx, hasher);
}
ConstVal::Function(def_id, substs) => {
def_id.hash_stable(hcx, hasher);
substs.hash_stable(hcx, hasher);
Expand All @@ -296,9 +302,6 @@ for ::middle::const_val::ConstVal<'tcx> {
value.hash_stable(hcx, hasher);
times.hash_stable(hcx, hasher);
}
ConstVal::Char(value) => {
value.hash_stable(hcx, hasher);
}
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/librustc/middle/const_val.rs
Expand Up @@ -38,12 +38,13 @@ pub enum ConstVal<'tcx> {
Str(InternedString),
ByteStr(Rc<Vec<u8>>),
Bool(bool),
Char(char),
Variant(DefId),
Function(DefId, &'tcx Substs<'tcx>),
Struct(BTreeMap<ast::Name, ConstVal<'tcx>>),
Tuple(Vec<ConstVal<'tcx>>),
Array(Vec<ConstVal<'tcx>>),
Repeat(Box<ConstVal<'tcx>>, u64),
Char(char),
}

impl<'tcx> ConstVal<'tcx> {
Expand All @@ -54,12 +55,13 @@ impl<'tcx> ConstVal<'tcx> {
Str(_) => "string literal",
ByteStr(_) => "byte string literal",
Bool(_) => "boolean",
Char(..) => "char",
Variant(_) => "enum variant",
Struct(_) => "struct",
Tuple(_) => "tuple",
Function(..) => "function definition",
Array(..) => "array",
Repeat(..) => "repeat",
Char(..) => "char",
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/librustc/mir/mod.rs
Expand Up @@ -1307,10 +1307,11 @@ fn fmt_const_val<W: Write>(fmt: &mut W, const_val: &ConstVal) -> fmt::Result {
write!(fmt, "b\"{}\"", escaped)
}
Bool(b) => write!(fmt, "{:?}", b),
Char(c) => write!(fmt, "{:?}", c),
Variant(def_id) |
Function(def_id, _) => write!(fmt, "{}", item_path_str(def_id)),
Struct(_) | Tuple(_) | Array(_) | Repeat(..) =>
bug!("ConstVal `{:?}` should not be in MIR", const_val),
Char(c) => write!(fmt, "{:?}", c),
}
}

Expand Down
19 changes: 17 additions & 2 deletions src/librustc/ty/mod.rs
Expand Up @@ -1693,6 +1693,7 @@ impl<'a, 'gcx, 'tcx> AdtDef {
}
}

#[inline]
pub fn discriminants(&'a self, tcx: TyCtxt<'a, 'gcx, 'tcx>)
-> impl Iterator<Item=ConstInt> + 'a {
let repr_type = self.repr.discr_type();
Expand All @@ -1706,7 +1707,13 @@ impl<'a, 'gcx, 'tcx> AdtDef {
Ok(ConstVal::Integral(v)) => {
discr = v;
}
_ => {}
err => {
if !expr_did.is_local() {
span_bug!(tcx.def_span(expr_did),
"variant discriminant evaluation succeeded \
in its crate but failed locally: {:?}", err);
}
}
}
}
prev_discr = Some(discr);
Expand Down Expand Up @@ -1740,7 +1747,15 @@ impl<'a, 'gcx, 'tcx> AdtDef {
explicit_value = v;
break;
}
_ => {
err => {
if !expr_did.is_local() {
span_bug!(tcx.def_span(expr_did),
"variant discriminant evaluation succeeded \
in its crate but failed locally: {:?}", err);
}
if explicit_index == 0 {
break;
}
explicit_index -= 1;
}
}
Expand Down
77 changes: 33 additions & 44 deletions src/librustc_const_eval/eval.rs
Expand Up @@ -15,7 +15,7 @@ use rustc::middle::const_val::{ConstVal, ConstEvalErr, EvalResult, ErrKind};
use rustc::hir::map as hir_map;
use rustc::hir::map::blocks::FnLikeNode;
use rustc::traits;
use rustc::hir::def::Def;
use rustc::hir::def::{Def, CtorKind};
use rustc::hir::def_id::DefId;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::maps::Providers;
Expand Down Expand Up @@ -48,28 +48,6 @@ macro_rules! math {
}
}

fn lookup_variant_by_id<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
variant_def: DefId)
-> Option<(&'tcx Expr, &'a ty::TypeckTables<'tcx>)> {
if let Some(variant_node_id) = tcx.hir.as_local_node_id(variant_def) {
let enum_node_id = tcx.hir.get_parent(variant_node_id);
if let Some(hir_map::NodeItem(it)) = tcx.hir.find(enum_node_id) {
if let hir::ItemEnum(ref edef, _) = it.node {
for variant in &edef.variants {
if variant.node.data.id() == variant_node_id {
return variant.node.disr_expr.map(|e| {
let def_id = tcx.hir.body_owner_def_id(e);
(&tcx.hir.body(e).value,
tcx.item_tables(def_id))
});
}
}
}
}
}
None
}

/// * `def_id` is the id of the constant.
/// * `substs` is the monomorphized substitutions for the expression.
///
Expand Down Expand Up @@ -289,9 +267,22 @@ fn eval_const_expr_partial<'a, 'tcx>(cx: &ConstContext<'a, 'tcx>,
}
}
hir::ExprCast(ref base, _) => {
match cast_const(tcx, cx.eval(base)?, ety) {
Ok(val) => val,
Err(kind) => return Err(ConstEvalErr { span: e.span, kind: kind }),
let base_val = cx.eval(base)?;
let base_ty = cx.tables.expr_ty(base);

// Avoid applying substitutions if they're empty, that'd ICE.
let base_ty = if cx.substs.is_empty() {
base_ty
} else {
base_ty.subst(tcx, cx.substs)
};
if ety == base_ty {
base_val
} else {
match cast_const(tcx, base_val, ety) {
Ok(val) => val,
Err(kind) => signal!(e, kind),
}
}
}
hir::ExprPath(ref qpath) => {
Expand All @@ -317,27 +308,20 @@ fn eval_const_expr_partial<'a, 'tcx>(cx: &ConstContext<'a, 'tcx>,
debug!("bad reference: {:?}, {:?}", err.description(), err.span);
signal!(e, ErroneousReferencedConstant(box err))
},
}
},
Def::VariantCtor(variant_def, ..) => {
if let Some((expr, tables)) = lookup_variant_by_id(tcx, variant_def) {
let cx = ConstContext::with_tables(tcx, tables);
match cx.eval(expr) {
Ok(val) => val,
Err(ConstEvalErr { kind: TypeckError, .. }) => {
signal!(e, TypeckError);
}
Err(err) => {
debug!("bad reference: {:?}, {:?}", err.description(), err.span);
signal!(e, ErroneousReferencedConstant(box err))
},
}
} else {
signal!(e, UnimplementedConstVal("enum variants"));
}
Def::VariantCtor(variant_def, CtorKind::Const) => {
Variant(variant_def)
}
Def::VariantCtor(_, CtorKind::Fn) => {
signal!(e, UnimplementedConstVal("enum variants"));
}
Def::StructCtor(..) => {
Def::StructCtor(_, CtorKind::Const) => {
ConstVal::Struct(Default::default())
}
Def::StructCtor(_, CtorKind::Fn) => {
signal!(e, UnimplementedConstVal("tuple struct constructors"))
}
Def::Local(def_id) => {
debug!("Def::Local({:?}): {:?}", def_id, cx.fn_args);
if let Some(val) = cx.fn_args.as_ref().and_then(|args| args.get(&def_id)) {
Expand Down Expand Up @@ -578,7 +562,7 @@ fn cast_const_int<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
U8(u) => Ok(Char(u as char)),
_ => bug!(),
},
_ => bug!(),
_ => Err(CannotCast),
}
}

Expand Down Expand Up @@ -622,6 +606,11 @@ fn cast_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
Bool(b) => cast_const_int(tcx, U8(b as u8), ty),
Float(f) => cast_const_float(tcx, f, ty),
Char(c) => cast_const_int(tcx, U32(c as u32), ty),
Variant(v) => {
let adt = tcx.lookup_adt_def(tcx.parent_def_id(v).unwrap());
let idx = adt.variant_index_with_id(v);
cast_const_int(tcx, adt.discriminant_for_variant(tcx, idx), ty)
}
Function(..) => Err(UnimplementedConstVal("casting fn pointers")),
ByteStr(b) => match ty.sty {
ty::TyRawPtr(_) => {
Expand Down
8 changes: 7 additions & 1 deletion src/librustc_const_eval/pattern.rs
Expand Up @@ -116,6 +116,7 @@ fn print_const_val(value: &ConstVal, f: &mut fmt::Formatter) -> fmt::Result {
ConstVal::ByteStr(ref b) => write!(f, "{:?}", &b[..]),
ConstVal::Bool(b) => write!(f, "{:?}", b),
ConstVal::Char(c) => write!(f, "{:?}", c),
ConstVal::Variant(_) |
ConstVal::Struct(_) |
ConstVal::Tuple(_) |
ConstVal::Function(..) |
Expand Down Expand Up @@ -620,7 +621,12 @@ impl<'a, 'gcx, 'tcx> PatternContext<'a, 'gcx, 'tcx> {
let const_cx = eval::ConstContext::with_tables(self.tcx.global_tcx(), self.tables);
match const_cx.eval(expr) {
Ok(value) => {
PatternKind::Constant { value: value }
if let ConstVal::Variant(def_id) = value {
let ty = self.tables.expr_ty(expr);
self.lower_variant_or_leaf(Def::Variant(def_id), ty, vec![])
} else {
PatternKind::Constant { value: value }
}
}
Err(e) => {
self.errors.push(PatternError::ConstEval(e));
Expand Down
8 changes: 3 additions & 5 deletions src/librustc_trans/mir/constant.rs
Expand Up @@ -100,15 +100,13 @@ impl<'tcx> Const<'tcx> {
ConstVal::Integral(ref i) => return Const::from_constint(ccx, i),
ConstVal::Str(ref v) => C_str_slice(ccx, v.clone()),
ConstVal::ByteStr(ref v) => consts::addr_of(ccx, C_bytes(ccx, v), 1, "byte_str"),
ConstVal::Char(c) => C_integral(Type::char(ccx), c as u64, false),
ConstVal::Function(..) => C_null(type_of::type_of(ccx, ty)),
ConstVal::Variant(_) |
ConstVal::Struct(_) | ConstVal::Tuple(_) |
ConstVal::Array(..) | ConstVal::Repeat(..) => {
bug!("MIR must not use `{:?}` (aggregates are expanded to MIR rvalues)", cv)
}
ConstVal::Function(..) => {
let llty = type_of::type_of(ccx, ty);
return Const::new(C_null(llty), ty);
}
ConstVal::Char(c) => C_integral(Type::char(ccx), c as u64, false),
};

assert!(!ty.has_erasable_regions());
Expand Down
11 changes: 6 additions & 5 deletions src/test/compile-fail/const-pattern-not-const-evaluable.rs
Expand Up @@ -10,21 +10,22 @@

#![feature(const_fn)]

#[derive(PartialEq, Eq)]
enum Cake {
BlackForest,
Marmor,
}
use Cake::*;

const BOO: (Cake, Cake) = (Marmor, BlackForest);
struct Pair<A, B>(A, B);

const BOO: Pair<Cake, Cake> = Pair(Marmor, BlackForest);
//~^ ERROR: constant evaluation error [E0080]
//~| unimplemented constant expression: enum variants
//~| unimplemented constant expression: tuple struct constructors
const FOO: Cake = BOO.1;

const fn foo() -> Cake {
Marmor
//~^ ERROR: constant evaluation error [E0080]
//~| unimplemented constant expression: enum variants
}

const WORKS: Cake = Marmor;
Expand All @@ -34,7 +35,7 @@ const GOO: Cake = foo();
fn main() {
match BlackForest {
FOO => println!("hi"), //~ NOTE: for pattern here
GOO => println!("meh"), //~ NOTE: for pattern here
GOO => println!("meh"),
WORKS => println!("möp"),
_ => println!("bye"),
}
Expand Down
20 changes: 20 additions & 0 deletions src/test/compile-fail/issue-41394.rs
@@ -0,0 +1,20 @@
// Copyright 2017 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.

enum Foo {
A = "" + 1
//~^ ERROR binary operation `+` cannot be applied to type `&'static str`
}

enum Bar {
A = Foo::A as isize
}

fn main() {}
26 changes: 26 additions & 0 deletions src/test/run-pass/auxiliary/issue-41394.rs
@@ -0,0 +1,26 @@
// Copyright 2017 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.

#![crate_type = "lib"]

#[repr(u32)]
pub enum Foo {
Foo = Private::Variant as u32
}

#[repr(u8)]
enum Private {
Variant = 42
}

#[inline(always)]
pub fn foo() -> Foo {
Foo::Foo
}
38 changes: 38 additions & 0 deletions src/test/run-pass/const-pattern-variant.rs
@@ -0,0 +1,38 @@
// 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)]

#[derive(PartialEq, Eq)]
enum Cake {
BlackForest,
Marmor,
}
use Cake::*;

const BOO: (Cake, Cake) = (Marmor, BlackForest);
const FOO: Cake = BOO.1;

const fn foo() -> Cake {
Marmor
}

const WORKS: Cake = Marmor;

const GOO: Cake = foo();

fn main() {
match BlackForest {
FOO => println!("hi"),
GOO => println!("meh"),
WORKS => println!("möp"),
_ => println!("bye"),
}
}

0 comments on commit 8054377

Please sign in to comment.