Skip to content
This repository was archived by the owner on Apr 14, 2020. It is now read-only.

Commit 17e85e3

Browse files
committed
Added pub for scoped_map.rs methods, added derive Copy, fixed borrowing issues
1 parent ef316a5 commit 17e85e3

File tree

13 files changed

+64
-54
lines changed

13 files changed

+64
-54
lines changed

compiler.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use builtins::builtins;
1515

1616
use self::Instruction::*;
1717

18-
#[derive(PartialEq, Clone, Show)]
18+
#[derive(PartialEq, Clone, Copy, Show)]
1919
pub enum Instruction {
2020
Add,
2121
Sub,
@@ -662,7 +662,8 @@ impl <'a> Compiler<'a> {
662662
self.scope(&mut |this| {
663663
let pattern_start = instructions.len() as int;
664664
let mut branches = Vec::new();
665-
let stack_increase = this.compile_pattern(&alt.pattern, &mut branches, instructions, this.stackSize - 1);
665+
let i = this.stackSize - 1;
666+
let stack_increase = this.compile_pattern(&alt.pattern, &mut branches, instructions, i);
666667
let pattern_end = instructions.len() as int;
667668
this.compile(&alt.expression, instructions, strict);
668669
instructions.push(Slide(stack_increase));
@@ -1001,7 +1002,8 @@ impl <'a> Compiler<'a> {
10011002
instructions.push(Split(patterns.len()));
10021003
self.stackSize += patterns.len();
10031004
for (i, p) in patterns.iter().enumerate() {
1004-
self.new_var_at(p.name.clone(), self.stackSize - patterns.len() + i);
1005+
let index = self.stackSize - patterns.len() + i;
1006+
self.new_var_at(p.name.clone(), index);
10051007
}
10061008
patterns.len()
10071009
}

core.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -920,12 +920,13 @@ impl <'a> Translator<'a> {
920920
let &Equation(ps, _) = &equations[last_index];
921921
if ps.len() > 0 {
922922
match ps[0].1 {
923-
Pattern::Constructor(..) | Pattern::Number(..)
924-
if visited.iter().find(|x| matching(**x, &ps[0])).is_none() => {
925-
pattern_test = Some(&ps[0]);
926-
visited.push(&ps[0]);
927-
last_index += 1;
928-
break;
923+
Pattern::Constructor(..) | Pattern::Number(..) => {
924+
if visited.iter().find(|x| matching(**x, &ps[0])).is_none() {
925+
pattern_test = Some(&ps[0]);
926+
visited.push(&ps[0]);
927+
last_index += 1;
928+
break;
929+
}
929930
}
930931
_ => ()
931932
}

deriving.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ impl DerivingGen {
135135
ArgIterator { typ: &constructor.typ.value }
136136
.map(|arg| Id::new(self.name_supply.anonymous(), arg.clone(), constructor.typ.constraints.clone()))
137137
.collect();
138-
let iter = ArgIterator { typ: &constructor.typ.value };
139-
let args_r: Vec<Id<Name>> = iter
138+
let mut iter = ArgIterator { typ: &constructor.typ.value };
139+
let args_r: Vec<Id<Name>> = iter.by_ref()
140140
.map(|arg| Id::new(self.name_supply.anonymous(), arg.clone(), constructor.typ.constraints.clone()))
141141
.collect();
142142
let ctor_id = Id::new(constructor.name, iter.typ.clone(), constructor.typ.constraints.clone());

graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
use std::iter::repeat;
55
use std::cmp::min;
66

7-
#[derive(PartialEq, Show)]
7+
#[derive(PartialEq, Copy, Show)]
88
pub struct VertexIndex(uint);
9-
#[derive(PartialEq)]
9+
#[derive(PartialEq, Copy, Show)]
1010
pub struct EdgeIndex(uint);
1111

1212
impl VertexIndex {

infix.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ impl PrecedenceVisitor {
5656
let mut expr_stack = Vec::new();
5757
let mut op_stack = Vec::new();
5858
loop {
59-
let TypedExpr { typ: typ, location:location, expr: expr } = *input;
59+
let box temp = input;
60+
let TypedExpr { typ: typ, location:location, expr: expr } = temp;
6061
match expr {
6162
Expr::OpApply(l, op, r) => {
6263
expr_stack.push(l);

lambda_lift.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn free_variables(&mut self, variables: &mut HashMap<Name, int>, free_vars: &mut
4141
if variables.get(&i.name).map(|x| *x > 0).unwrap_or(false) {
4242
match variables.entry(i.name.clone()) {
4343
Entry::Vacant(entry) => { entry.insert(1); }
44-
Entry::Occupied(entry) => *entry.get() += 1
44+
Entry::Occupied(mut entry) => *entry.get_mut() += 1
4545
}
4646
}
4747
}
@@ -52,7 +52,7 @@ fn free_variables(&mut self, variables: &mut HashMap<Name, int>, free_vars: &mut
5252
Lambda(ref mut arg, ref mut body) => {
5353
match variables.entry(arg.name.clone()) {
5454
Entry::Vacant(entry) => { entry.insert(1); }
55-
Entry::Occupied(entry) => *entry.get() += 1
55+
Entry::Occupied(mut entry) => *entry.get_mut() += 1
5656
}
5757
self.free_variables(variables, free_vars, &mut **body);
5858
*variables.get_mut(&arg.name).unwrap() -= 1;
@@ -62,7 +62,7 @@ fn free_variables(&mut self, variables: &mut HashMap<Name, int>, free_vars: &mut
6262
for bind in bindings.iter() {
6363
match variables.entry(bind.name.name.clone()) {
6464
Entry::Vacant(entry) => { entry.insert(1); }
65-
Entry::Occupied(entry) => *entry.get() += 1
65+
Entry::Occupied(mut entry) => *entry.get_mut() += 1
6666
}
6767
}
6868
let mut free_vars2 = HashMap::new();
@@ -87,7 +87,7 @@ fn free_variables(&mut self, variables: &mut HashMap<Name, int>, free_vars: &mut
8787
each_pattern_variables(&alt.pattern, &mut |name| {
8888
match variables.entry(name.clone()) {
8989
Entry::Vacant(entry) => { entry.insert(1); }
90-
Entry::Occupied(entry) => *entry.get() += 1
90+
Entry::Occupied(mut entry) => *entry.get_mut() += 1
9191
}
9292
});
9393
self.free_variables(variables, free_vars, &mut alt.expression);

lexer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use interner::*;
77

88
use self::TokenEnum::*;
99

10-
#[derive(Clone, PartialEq, Show)]
10+
#[derive(Clone, Copy, PartialEq, Show)]
1111
pub enum TokenEnum {
1212
EOF,
1313
NAME,
@@ -54,7 +54,7 @@ pub enum TokenEnum {
5454
ELSE
5555
}
5656

57-
#[derive(Clone, PartialEq)]
57+
#[derive(Clone, Copy, PartialEq)]
5858
pub struct Location {
5959
pub column : int,
6060
pub row : int,

module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub struct Newtype<Ident = InternedStr> {
7878
pub deriving: Vec<Ident>
7979
}
8080

81-
#[derive(PartialEq, Clone, Show)]
81+
#[derive(PartialEq, Clone, Copy, Show)]
8282
pub enum Assoc {
8383
Left,
8484
Right,

parser.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,12 @@ fn sub_expression(&mut self) -> Option<TypedExpr> {
292292
Some(TypedExpr::with_location(Identifier(intern("()")), location))
293293
}
294294
else {
295-
let expressions = self.sep_by_1(|this| this.expression_(), COMMA);
295+
let mut expressions = self.sep_by_1(|this| this.expression_(), COMMA);
296296
self.require_next(RPARENS);
297297
if expressions.len() == 1 {
298-
let loc = expressions[0].location;
299-
Some(TypedExpr::with_location(Paren(box expressions[0]), loc))
298+
let expr = expressions.pop().unwrap();
299+
let loc = expr.location;
300+
Some(TypedExpr::with_location(Paren(box expr), loc))
300301
}
301302
else {
302303
Some(new_tuple(expressions))
@@ -690,10 +691,10 @@ fn pattern(&mut self) -> Pattern {
690691
Pattern::Constructor(intern("()"), vec![])
691692
}
692693
else {
693-
let tupleArgs = self.sep_by_1(|this| this.pattern(), COMMA);
694+
let mut tupleArgs = self.sep_by_1(|this| this.pattern(), COMMA);
694695
self.require_next(RPARENS);
695696
if tupleArgs.len() == 1 {
696-
tupleArgs[0]
697+
tupleArgs.pop().unwrap()
697698
}
698699
else {
699700
Pattern::Constructor(intern(tuple_name(tupleArgs.len()).as_slice()), tupleArgs)
@@ -977,7 +978,7 @@ fn sep_by_1<T, F>(&mut self, f : F, sep : TokenEnum) -> Vec<T>
977978
self.sep_by_1_func(f, |tok| tok.token == sep)
978979
}
979980

980-
fn sep_by_1_func<T, F, P>(&mut self, f : F, sep: P) -> Vec<T>
981+
fn sep_by_1_func<T, F, P>(&mut self, mut f : F, mut sep: P) -> Vec<T>
981982
where F: FnMut(&mut Parser<Iter>) -> T, P : FnMut(&Token) -> bool {
982983
let mut result = Vec::new();
983984
loop {
@@ -1026,9 +1027,9 @@ fn new_tuple(arguments : Vec<TypedExpr>) -> TypedExpr {
10261027
make_application(name, arguments.into_iter())
10271028
}
10281029

1029-
fn make_tuple_type(types : Vec<Type>) -> Type {
1030+
fn make_tuple_type(mut types : Vec<Type>) -> Type {
10301031
if types.len() == 1 {
1031-
types[0]
1032+
types.pop().unwrap()
10321033
}
10331034
else {
10341035
Type::new_op(intern(tuple_name(types.len()).as_slice()), types)

scoped_map.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,25 @@ impl <K, V> ScopedMap<K, V>
7171
}
7272

7373
///Returns a reference to the last inserted value corresponding to the key
74-
fn find<'a>(&'a self, k: &K) -> Option<&'a V> {
74+
pub fn find<'a>(&'a self, k: &K) -> Option<&'a V> {
7575
self.map.get(k).and_then(|x| x.last())
7676
}
7777

7878
///Returns the number of elements in the container.
7979
///Shadowed elements are not counted
80-
fn len(&self) -> uint { self.map.len() }
80+
pub fn len(&self) -> uint { self.map.len() }
8181

8282
///Removes all elements
83-
fn clear(&mut self) {
83+
pub fn clear(&mut self) {
8484
self.map.clear();
8585
self.scopes.clear();
8686
}
8787

8888
///Swaps the value stored at key, or inserts it if it is not present
89-
fn swap(&mut self, k: K, v: V) -> Option<V> {
89+
pub fn swap(&mut self, k: K, v: V) -> Option<V> {
9090
let vec = match self.map.entry(k.clone()) {
9191
Entry::Vacant(entry) => entry.insert(Vec::new()),
92-
Entry::Occupied(entry) => entry.get_mut()
92+
Entry::Occupied(entry) => entry.into_mut()
9393
};
9494
if vec.len() != 0 {
9595
let r = vec.pop();
@@ -102,7 +102,7 @@ impl <K, V> ScopedMap<K, V>
102102
None
103103
}
104104
}
105-
fn pop(&mut self, k: &K) -> Option<V> {
105+
pub fn pop(&mut self, k: &K) -> Option<V> {
106106
match self.map.get_mut(k).and_then(|x| x.pop()) {
107107
Some(v) => {
108108
let mut i = self.scopes.len() as int - 1;
@@ -117,13 +117,13 @@ impl <K, V> ScopedMap<K, V>
117117
None => None
118118
}
119119
}
120-
fn find_mut<'a>(&'a mut self, key: &K) -> Option<&'a mut V> {
120+
pub fn find_mut<'a>(&'a mut self, key: &K) -> Option<&'a mut V> {
121121
self.map.get_mut(key).and_then(|x| x.last_mut())
122122
}
123-
fn insert(&mut self, k: K, v: V) -> bool {
123+
pub fn insert(&mut self, k: K, v: V) -> bool {
124124
let vec = match self.map.entry(k.clone()) {
125125
Entry::Vacant(entry) => entry.insert(Vec::new()),
126-
Entry::Occupied(entry) => entry.get_mut()
126+
Entry::Occupied(entry) => entry.into_mut()
127127
};
128128
vec.push(v);
129129
self.scopes.push(Some(k));

typecheck.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl <'a> TypeEnvironment<'a> {
255255
}
256256
}
257257

258-
pub fn add_types(&'a mut self, types: &'a DataTypes) {
258+
pub fn add_types(&mut self, types: &'a DataTypes) {
259259
self.assemblies.push(types);
260260
}
261261

@@ -307,9 +307,10 @@ impl <'a> TypeEnvironment<'a> {
307307
self.namedTypes.insert(type_decl.name.clone(), t);
308308
}
309309
for binding in class.bindings.iter_mut() {
310+
let classname = &class.name;
310311
let decl = class.declarations.iter()
311312
.find(|decl| binding.name.name.as_slice().ends_with(decl.name.as_slice()))
312-
.unwrap_or_else(|| panic!("Could not find {:?} in class {:?}", binding.name, class.name));
313+
.unwrap_or_else(|| panic!("Could not find {:?} in class {:?}", binding.name, classname));
313314
binding.typ = decl.typ.clone();
314315
{
315316
let mut context = vec![];
@@ -349,8 +350,9 @@ impl <'a> TypeEnvironment<'a> {
349350
_ => ()
350351
}
351352
for binding in instance.bindings.iter_mut() {
353+
let classname = &instance.classname;
352354
let decl = class_decls.iter().find(|decl| binding.name.as_slice().ends_with(decl.name.as_slice()))
353-
.unwrap_or_else(|| panic!("Could not find {:?} in class {:?}", binding.name, instance.classname));
355+
.unwrap_or_else(|| panic!("Could not find {:?} in class {:?}", binding.name, classname));
354356
binding.typ = decl.typ.clone();
355357
replace_var(&mut binding.typ.value, class_var, &instance.typ);
356358
self.freshen_qualified_type(&mut binding.typ, HashMap::new());
@@ -448,7 +450,7 @@ impl <'a> TypeEnvironment<'a> {
448450
let old = constraint.variables[0].clone();
449451
let new = match mapping.entry(old.clone()) {
450452
Entry::Vacant(entry) => entry.insert(self.new_var_kind(old.kind.clone())),
451-
Entry::Occupied(entry) => entry.get_mut()
453+
Entry::Occupied(entry) => entry.into_mut()
452454
};
453455
constraint.variables[0] = new.var().clone();
454456
}
@@ -672,7 +674,7 @@ impl <'a> TypeEnvironment<'a> {
672674
Case(ref mut case_expr, ref mut alts) => {
673675
let mut match_type = self.typecheck(&mut **case_expr, subs);
674676
self.typecheck_pattern(&alts[0].pattern.location, subs, &alts[0].pattern.node, &mut match_type);
675-
match alts[0].where_bindings {
677+
match *&mut alts[0].where_bindings {
676678
Some(ref mut bindings) => self.typecheck_local_bindings(subs, &mut BindingsWrapper { value: &mut **bindings }),
677679
None => ()
678680
}
@@ -930,7 +932,8 @@ impl <'a> TypeEnvironment<'a> {
930932
}
931933
///Typechecks a group of local bindings (such as a let expression)
932934
fn typecheck_local_bindings(&mut self, subs: &mut Substitution, bindings: &mut Bindings) {
933-
self.typecheck_mutually_recursive_bindings(self.variable_age + 1, subs, bindings, false);
935+
let var = self.variable_age + 1;
936+
self.typecheck_mutually_recursive_bindings(var, subs, bindings, false);
934937
}
935938
///Typechecks a group of global bindings.
936939
fn typecheck_global_bindings(&mut self, start_var_age: int, subs: &mut Substitution, bindings: &mut Bindings) {
@@ -1406,9 +1409,9 @@ fn match_or_fail(env: &mut TypeEnvironment, subs: &mut Substitution, location: &
14061409
fn match_(env: &mut TypeEnvironment, subs: &mut Substitution, lhs: &mut Type, rhs: &Type) -> Result<(), TypeError> {
14071410
match (lhs, rhs) {
14081411
(&mut Type::Application(ref mut l1, ref mut r1), &Type::Application(ref l2, ref r2)) => {
1409-
match_(env, subs, &mut **l1, &mut **l2).and_then(|_| {
1412+
match_(env, subs, &mut **l1, &**l2).and_then(|_| {
14101413
replace(&mut env.constraints, &mut **r1, subs);
1411-
match_(env, subs, &mut **r1, &mut **r2)
1414+
match_(env, subs, &mut **r1, &**r2)
14121415
})
14131416
}
14141417
(&mut Type::Variable(ref mut lhs), &Type::Variable(ref rhs)) => {

types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,13 @@ impl <T: fmt::Show, I: fmt::Show> fmt::Show for Qualified<T, I> {
262262
}
263263
}
264264

265-
#[derive(PartialEq, PartialOrd)]
265+
#[derive(PartialEq, Copy, PartialOrd)]
266266
enum Prec_ {
267267
Top,
268268
Function,
269269
Constructor,
270270
}
271+
#[derive(Copy)]
271272
struct Prec<'a>(Prec_, &'a Type);
272273

273274
///If the type is a function it returns the type of the argument and the result type,

vm.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ fn primitive_float<'a, F>(stack: &mut Vec<Node<'a>>, f: F) where F: FnOnce(f64,
501501
}
502502
}
503503
fn primitive<F>(stack: &mut Vec<Node>, f: F) where F: FnOnce(int, int) -> int {
504-
primitive_int(stack, |l, r| Int(f(l, r)))
504+
primitive_int(stack, move |l, r| Int(f(l, r)))
505505
}
506506

507507
#[derive(PartialEq, Show)]
@@ -637,7 +637,7 @@ mod primitive {
637637
eval(vm, stack[0].clone());
638638
let aw = stack[0].borrow();
639639
let (a, rw) = match *aw {
640-
Constructor(_, ref args) => (args[0], args[1]),
640+
Constructor(_, ref args) => (&args[0], &args[1]),
641641
_ => panic!("pass exepected constructor")
642642
};
643643
Node::new(Application(Node::new(Application(stack[1].clone(), a.clone())), rw.clone()))
@@ -694,8 +694,8 @@ mod primitive {
694694
let mut node = Node::new(Constructor(0, vec!()));
695695
let first = node.clone();
696696
for c in s.chars() {
697-
node = match *node.borrow_mut() {
698-
Constructor(ref mut tag, ref mut args) => {
697+
node = match &mut *node.borrow_mut() {
698+
&mut Constructor(ref mut tag, ref mut args) => {
699699
*tag = 1;
700700
args.push(Node::new(Char(c)));
701701
args.push(Node::new(Constructor(0, Vec::new())));
@@ -708,14 +708,15 @@ mod primitive {
708708
}
709709
///Compares the tags of two constructors, returning an Ordering
710710
fn compare_tags<'a>(vm: &'a VM, stack: &[Node<'a>]) -> Node<'a> {
711+
use std::cmp::Ordering;
711712
assert_eq!(stack.len(), 2);
712713
let lhs = eval(vm, stack[0].clone());
713714
let rhs = eval(vm, stack[1].clone());
714715
let tag = match (&*lhs.borrow(), &*rhs.borrow()) {
715716
(&Constructor(lhs, _), &Constructor(rhs, _)) => match lhs.cmp(&rhs) {
716-
Less => 0,
717-
Equal => 1,
718-
Greater => 2
717+
Ordering::Less => 0,
718+
Ordering::Equal => 1,
719+
Ordering::Greater => 2
719720
},
720721
(_, _) => 1//EQ
721722
};

0 commit comments

Comments
 (0)