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

Commit b806c15

Browse files
committed
Instance functions correctly have the same uid as the class they implement
1 parent ecad43b commit b806c15

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

renamer.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ impl Renamer {
9797
let mut names = module.dataDefinitions.iter()
9898
.flat_map(|data| data.constructors.iter().map(|ctor| ctor.name))
9999
.chain(module.newtypes.iter().map(|newtype| newtype.constructor_name))
100-
.chain(module.instances.iter()
101-
.flat_map(|instance| binding_groups(instance.bindings.as_slice()).map(|binds| binds[0].name)))
102100
.chain(module.classes.iter().flat_map(|class|
103101
Some(class.name).move_iter()
104102
.chain(class.declarations.iter().map(|decl| decl.name))
@@ -107,6 +105,12 @@ impl Renamer {
107105
for name in names {
108106
self.declare_global(name, uid);
109107
}
108+
for instance in module.instances.iter() {
109+
let class_uid = self.get_name(instance.classname).uid;
110+
for binds in binding_groups(instance.bindings.as_slice()) {
111+
self.declare_global(binds[0].name, class_uid);
112+
}
113+
}
110114
}
111115

112116
fn rename_bindings(&mut self, bindings: ~[Binding<InternedStr>], is_global: bool) -> ~[Binding<Name>] {

vm.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,24 @@ main = (test (0 :: Int) 2) && not (test (1 :: Int) 0)")
10461046
.unwrap_or_else(|err| fail!("{}", err));
10471047
assert_eq!(result, Some(ConstructorResult(0, Vec::new())));
10481048
}
1049+
#[test]
1050+
fn implement_class() {
1051+
let result = execute_main_string(
1052+
r"
1053+
import Prelude
1054+
data AB = A | B
1055+
1056+
instance Eq AB where
1057+
(==) A A = True
1058+
(==) B B = True
1059+
(==) _ _ = False
1060+
1061+
test x y = x == y
1062+
1063+
main = A == B && test A A")
1064+
.unwrap_or_else(|err| fail!("{}", err));
1065+
assert_eq!(result, Some(ConstructorResult(1, Vec::new())));
1066+
}
10491067

10501068
#[test]
10511069
fn deriving_eq() {

0 commit comments

Comments
 (0)