Skip to content

Commit

Permalink
Adapt to rustc 1.26.0 features (rust-lang/rust#49394)
Browse files Browse the repository at this point in the history
  • Loading branch information
koba-e964 committed Jun 10, 2018
1 parent 8751014 commit 1a25224
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 191 deletions.
76 changes: 38 additions & 38 deletions src/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ pub struct Prog(pub Box<[Fundef]>, pub Closure);
impl Closure {
fn fmt2(&self, f: &mut fmt::Formatter, level: usize) -> fmt::Result {
use self::Closure::*;
match *self {
match self {
Unit => write!(f, "()"),
Int(v) => write!(f, "{}", v),
Float(fv) => write!(f, "{}", fv),
Neg(ref x) => write!(f, "-{}", x),
IntBin(op, ref x, ref y) => {
Neg(x) => write!(f, "-{}", x),
IntBin(op, x, y) => {
let op_str = match op {
self::IntBin::Add => "+",
self::IntBin::Sub => "-",
};
write!(f, "{} {} {}", x, op_str, y)
},
FNeg(ref x) => write!(f, "-.{}", x),
FloatBin(op, ref x, ref y) => {
FNeg(x) => write!(f, "-.{}", x),
FloatBin(op, x, y) => {
let op_str = match op {
self::FloatBin::FAdd => "+.",
self::FloatBin::FSub => "-.",
Expand All @@ -70,7 +70,7 @@ impl Closure {
};
write!(f, "{} {} {}", x, op_str, y)
},
IfComp(op, ref x, ref y, ref e1, ref e2) => {
IfComp(op, x, y, e1, e2) => {
let op_str = match op {
self::CompBin::Eq => "=",
self::CompBin::LE => "<=",
Expand All @@ -90,7 +90,7 @@ impl Closure {
}
e2.fmt2(f, level + 2)
},
Let((ref x, ref t), ref e1, ref e2) => {
Let((x, t), e1, e2) => {
if let Type::Unit = *t {
if x.len() >= 6 && &x[0..6] == "_dummy" {
// this let expression is actually "e1; e2"
Expand All @@ -110,31 +110,31 @@ impl Closure {
}
e2.fmt2(f, level)
},
Var(ref x) => write!(f, "{}", x),
MakeCls(ref x, ref t, ref cls, ref e) => {
Var(x) => write!(f, "{}", x),
MakeCls(x, t, cls, e) => {
write!(f, "MakeCls {}: {} (", x, t)?;
let Cls { entry: id::L(ref l), actual_fv: ref fv } = *cls;
let Cls { entry: id::L(l), actual_fv: fv } = cls;
write!(f, "{} {:?}) in\n", l, fv)?;
for _ in 0 .. level {
write!(f, " ")?;
}
e.fmt2(f, level)
},
AppDir(id::L(ref func), ref args) => {
AppDir(id::L(func), args) => {
write!(f, "{}", func)?;
for v in args.iter() {
write!(f, " {}", v)?;
}
Ok(())
},
AppCls(ref func, ref args) => {
AppCls(func, args) => {
write!(f, "[{}]", func)?;
for v in args.iter() {
write!(f, " {}", v)?;
}
Ok(())
},
Tuple(ref elems) => {
Tuple(elems) => {
write!(f, "(")?;
for i in 0 .. elems.len() {
write!(f, "{}", elems[i])?;
Expand All @@ -144,7 +144,7 @@ impl Closure {
}
write!(f, ")")
},
LetTuple(ref xts, ref y, ref e) => {
LetTuple(xts, y, e) => {
write!(f, "let (")?;
for i in 0 .. xts.len() {
write!(f, "{}: {}", xts[i].0, xts[i].1)?;
Expand All @@ -158,11 +158,11 @@ impl Closure {
}
e.fmt2(f, level)
},
Get(ref x, ref y) =>
Get(x, y) =>
write!(f, "{}.({})", x, y),
Put(ref x, ref y, ref z) =>
Put(x, y, z) =>
write!(f, "{}.({}) <- {}", x, y, z),
ExtArray(id::L(ref a)) => write!(f, "(extarr:{})", a),
ExtArray(id::L(a)) => write!(f, "(extarr:{})", a),
}
}
}
Expand All @@ -175,15 +175,15 @@ impl fmt::Display for Closure {

impl fmt::Display for Fundef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let Fundef { name: (id::L(ref x), ref t), args: ref yts, body: ref e1,
formal_fv: ref fv } = *self;
let Fundef { name: (id::L(x), t), args: yts, body: e1,
formal_fv: fv } = self;
write!(f, "define ({}: {})", x, t)?;
for &(ref y, ref t) in yts.iter() {
for (y, t) in yts.iter() {
write!(f, " ({}: {})", y, t)?;
}
if !fv.is_empty() {
write!(f, " freevar:")?;
for &(ref x, ref t) in fv.iter() {
for (x, t) in fv.iter() {
write!(f, " ({}: {})", x, t)?;
}
}
Expand All @@ -196,7 +196,7 @@ impl fmt::Display for Fundef {

impl fmt::Display for Prog {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let Prog(ref fundefs, ref e) = *self;
let Prog(fundefs, e) = self;
for fundef in fundefs.iter() {
write!(f, "{}\n", fundef)?;
}
Expand All @@ -210,35 +210,35 @@ pub fn fv(e: &Closure) -> HashSet<String> {
macro_rules! invoke {
($e:expr) => (fv($e));
}
match *e {
match e {
Unit | Int(_) | Float(_) | ExtArray(_) => HashSet::new(),
Neg(ref x) | FNeg(ref x) => build_set!(x),
IntBin(_, ref x, ref y) | FloatBin(_, ref x, ref y) |
Get(ref x, ref y) =>
Neg(x) | FNeg(x) => build_set!(x),
IntBin(_, x, y) | FloatBin(_, x, y) |
Get(x, y) =>
build_set!(x, y),
IfComp(_, ref x, ref y, ref e1, ref e2) => {
IfComp(_, x, y, e1, e2) => {
let h = build_set!(x, y);
let s1 = invoke!(e1);
let s2 = invoke!(e2);
&(&h | &s1) | &s2
},
Let((ref x, _), ref e1, ref e2) => {
Let((x, _), e1, e2) => {
let s1 = invoke!(e1);
let s2 = &invoke!(e2) - &build_set!(x);
&s1 | &s2
}
Var(ref x) => build_set!(x),
MakeCls(ref x, _, Cls { entry: _, actual_fv: ref ys }, ref e) =>
Var(x) => build_set!(x),
MakeCls(x, _, Cls { entry: _, actual_fv: ys }, e) =>
&(&ys.iter().cloned().collect() | &invoke!(e)) - &build_set!(x),
AppCls(ref x, ref ys) =>
AppCls(x, ys) =>
&build_set!(x) | &ys.iter().cloned().collect::<HashSet<_>>(),
AppDir(_, ref xs) | Tuple(ref xs) => xs.iter().cloned().collect(),
LetTuple(ref xs, ref y, ref e) => {
AppDir(_, xs) | Tuple(xs) => xs.iter().cloned().collect(),
LetTuple(xs, y, e) => {
let tmp: HashSet<String> = xs.iter().map(|x| x.0.clone())
.collect(); // S.of_list (List.map fst xs)
&build_set!(y) | &(&invoke!(e) - &tmp)
},
Put(ref x, ref y, ref z) => build_set!(x, y, z),
Put(x, y, z) => build_set!(x, y, z),
}
}

Expand Down Expand Up @@ -272,13 +272,13 @@ fn g(env: &HashMap<String, Type>, known: &HashSet<String>,
let mut known_p = known.clone();
known_p.insert(x.clone());
let mut env_p2 = env_p.clone();
for &(ref y, ref t) in yts.iter() {
for (y, t) in yts.iter() {
env_p2.insert(y.clone(), t.clone());
}
let e1p = g(&env_p2, &known_p, (*e1).clone(), &mut toplevel_cp);
/* Check if e1p contains free variables */
let zs =
&fv(&e1p) - &yts.iter().map(|&(ref y, _)| y.clone()).collect();
&fv(&e1p) - &yts.iter().map(|(y, _)| y.clone()).collect();
let (known_p, e1p) = if zs.is_empty() {
*toplevel = toplevel_cp;
(&known_p, e1p)
Expand All @@ -290,7 +290,7 @@ fn g(env: &HashMap<String, Type>, known: &HashSet<String>,
(known, e1p)
};
let zs: Vec<String> = (&zs - &build_set!(x)).into_iter().collect();
let zts: Vec<(String, Type)> = zs.iter().map(|&ref z| (z.clone(), env.get(z).unwrap().clone())).collect();
let zts: Vec<(String, Type)> = zs.iter().map(|z| (z.clone(), env.get(z).unwrap().clone())).collect();
toplevel.push(Fundef { name: (id::L(x.clone()), t.clone()),
args: yts,
formal_fv: zts.into_boxed_slice(),
Expand All @@ -315,7 +315,7 @@ fn g(env: &HashMap<String, Type>, known: &HashSet<String>,
KNormal::Tuple(xs) => Tuple(xs),
KNormal::LetTuple(xts, y, e) => {
let mut cp_env = env.clone();
for &(ref x, ref t) in xts.iter() {
for (x, t) in xts.iter() {
cp_env.insert(x.clone(), t.clone());
}
LetTuple(xts, y, Box::new(g(&cp_env, known, *e, toplevel)))
Expand Down
21 changes: 9 additions & 12 deletions src/const_fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,15 @@ type ConstEnv = HashMap<String, Const>;
fn add_env(env: &mut ConstEnv, x: String, e: &KNormal) {
use self::KNormal::*;
use self::Const::*;
match *e {
Int(v) => { env.insert(x, IntConst(v)); },
Float(f) => { env.insert(x, FloatConst(f.into())); },
match e {
Int(v) => { env.insert(x, IntConst(*v)); },
Float(f) => { env.insert(x, FloatConst((*f).into())); },
// Tuple const folding is done only if all components are constant.
Tuple(ref xs) => {
Tuple(xs) => {
let result: Option<Vec<Const>> =
xs.iter().map(|x| env.get(x).cloned()).collect();
match result {
None => (),
Some(result) => {
env.insert(x, TupleConst(result.into_boxed_slice()));
},
if let Some(result) = result {
env.insert(x, TupleConst(result.into_boxed_slice()));
}
},
_ => (),
Expand All @@ -40,21 +37,21 @@ fn add_env(env: &mut ConstEnv, x: String, e: &KNormal) {
fn findi(x: &str, env: &ConstEnv) -> Option<i64> {
use self::Const::*;
match env.get(x) {
Some(&IntConst(v)) => Some(v),
Some(IntConst(v)) => Some(*v),
_ => None,
}
}
fn findf(x: &str, env: &ConstEnv) -> Option<f64> {
use self::Const::*;
match env.get(x) {
Some(&FloatConst(v)) => Some(v.into()),
Some(FloatConst(v)) => Some((*v).into()),
_ => None,
}
}
fn findt(x: &str, env: &ConstEnv) -> Option<Box<[Const]>> {
use self::Const::*;
match env.get(x) {
Some(&TupleConst(ref vals)) => Some(vals.clone()),
Some(TupleConst(vals)) => Some(vals.clone()),
_ => None,
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/elim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use k_normal::fv;

fn has_effect(e: &KNormal) -> bool {
use self::KNormal::*;
match *e {
Let(_, ref e1, ref e2) | IfComp(_, _, _, ref e1, ref e2) =>
match e {
Let(_, e1, e2) | IfComp(_, _, _, e1, e2) =>
has_effect(e1) || has_effect(e2),
LetRec(_, ref e) | LetTuple(_, _, ref e) => has_effect(e),
LetRec(_, e) | LetTuple(_, _, e) => has_effect(e),
App(_, _) | Put(_, _, _) | ExtFunApp(_, _) => true,
_ => false,
}
Expand Down Expand Up @@ -46,7 +46,7 @@ pub fn f(e: KNormal) -> KNormal {
LetTuple(xts, y, e) => {
let ep = invoke!(e);
let live = fv(&ep);
let any_used = xts.iter().any(|&(ref x, _)| live.contains(x));
let any_used = xts.iter().any(|(x, _)| live.contains(x));
if any_used {
LetTuple(xts, y, ep)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl IdGen {
Type::Var(a)
}
fn id_of_typ(t: &Type) -> String {
match *t {
match t {
Type::Unit => "u",
Type::Bool => "b",
Type::Int => "i",
Expand Down
14 changes: 7 additions & 7 deletions src/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ use k_normal::{KNormal, KFundef, fv};
use syntax::Type;
use id::IdGen;

fn is_recursive(&KFundef { name: (ref x, ref _t), args: ref _yts, body: ref e1 }: &KFundef)
fn is_recursive(KFundef { name: (x, _t), args: _yts, body: e1 }: &KFundef)
-> bool {
fv(&e1).contains(x)
fv(e1).contains(x)
}

pub fn size(e: &KNormal) -> usize {
use self::KNormal::*;
match *e {
IfComp(_, _, _, ref e1, ref e2) |
Let(_, ref e1, ref e2) |
LetRec(KFundef { name: _, args: _, body: ref e1 }, ref e2) =>
match e {
IfComp(_, _, _, e1, e2) |
Let(_, e1, e2) |
LetRec(KFundef { name: _, args: _, body: e1 }, e2) =>
1 + size(e1) + size(e2),
LetTuple(_, _, ref e) => 1 + size(e),
LetTuple(_, _, e) => 1 + size(e),
_ => 1,
}
}
Expand Down
Loading

0 comments on commit 1a25224

Please sign in to comment.