1
Fork 0

Add initial enum initialization support.

This commit is contained in:
Scott Olson 2016-03-12 22:27:54 -06:00
parent 1370008576
commit 7cda22f8c5
2 changed files with 39 additions and 49 deletions

View file

@ -269,9 +269,9 @@ impl<'a, 'tcx: 'a> Interpreter<'a, 'tcx> {
self.memory.write_int(dest, n) self.memory.write_int(dest, n)
} }
fn assign_to_product(&mut self, dest: Pointer, dest_repr: Repr, fn assign_to_product(&mut self, dest: Pointer, dest_repr: &Repr,
operands: &[mir::Operand<'tcx>]) -> EvalResult<()> { operands: &[mir::Operand<'tcx>]) -> EvalResult<()> {
match dest_repr { match *dest_repr {
Repr::Product { ref fields, .. } => { Repr::Product { ref fields, .. } => {
for (field, operand) in fields.iter().zip(operands) { for (field, operand) in fields.iter().zip(operands) {
let src = try!(self.operand_to_ptr(operand)); let src = try!(self.operand_to_ptr(operand));
@ -315,32 +315,26 @@ impl<'a, 'tcx: 'a> Interpreter<'a, 'tcx> {
Aggregate(ref kind, ref operands) => { Aggregate(ref kind, ref operands) => {
use rustc::mir::repr::AggregateKind::*; use rustc::mir::repr::AggregateKind::*;
match *kind { match *kind {
Tuple => self.assign_to_product(dest, dest_repr, operands), Tuple => self.assign_to_product(dest, &dest_repr, operands),
Adt(ref adt_def, variant_idx, _) => { Adt(ref adt_def, variant_idx, _) => match adt_def.adt_kind() {
use rustc::middle::ty::AdtKind::*; ty::AdtKind::Struct => self.assign_to_product(dest, &dest_repr, operands),
match adt_def.adt_kind() {
Struct => self.assign_to_product(dest, dest_repr, operands),
Enum => unimplemented!(), ty::AdtKind::Enum => match dest_repr {
} Repr::Sum { discr_size, ref variants, .. } =>
// TODO(tsion): Write the discriminant value.
self.assign_to_product(
dest.offset(discr_size),
&variants[variant_idx],
operands
),
_ => panic!("expected Repr::Sum target"),
} }
},
Vec => unimplemented!(), Vec => unimplemented!(),
Closure(..) => unimplemented!(), Closure(..) => unimplemented!(),
} }
// let max_fields = adt_def.variants
// .iter()
// .map(|v| v.fields.len())
// .max()
// .unwrap_or(0);
// let ptr = self.allocate_aggregate(max_fields);
// for (i, operand) in operands.iter().enumerate() {
// let val = self.operand_to_ptr(operand);
// self.write_pointer(ptr.offset(i), val);
// }
// Value::Adt { variant: variant, data_ptr: ptr }
} }
// Ref(_region, _kind, ref lvalue) => { // Ref(_region, _kind, ref lvalue) => {

View file

@ -1,34 +1,30 @@
#![feature(custom_attribute)] #![feature(custom_attribute)]
#![allow(dead_code, unused_attributes)] #![allow(dead_code, unused_attributes)]
// enum MyOption<T> { #[miri_run]
// Some { data: T }, fn return_none() -> Option<i64> {
// None, None
// } }
// #[miri_run(expected = "Int(13)")]
// fn match_my_opt_some() -> i32 {
// let x = MyOption::Some { data: 13 };
// match x {
// MyOption::Some { data } => data,
// MyOption::None => 42,
// }
// }
// #[miri_run(expected = "Int(42)")]
// fn match_my_opt_none() -> i32 {
// let x = MyOption::None;
// match x {
// MyOption::Some { data } => data,
// MyOption::None => 42,
// }
// }
#[miri_run] #[miri_run]
fn match_opt_some() -> i64 { fn return_some() -> Option<i64> {
let x = Some(13); Some(42)
match x {
Some(data) => data,
None => 42,
}
} }
// #[miri_run]
// fn match_opt_none() -> i64 {
// let x = None,
// match x {
// Some(data) => data,
// None => 42,
// }
// }
// #[miri_run]
// fn match_opt_some() -> i64 {
// let x = Some(13);
// match x {
// Some(data) => data,
// None => 42,
// }
// }