Skip to content

Commit

Permalink
Proper error when building a record with duplicated features
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Oct 21, 2016
1 parent 83715fd commit 869f803
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
14 changes: 14 additions & 0 deletions vm/src/org/mozartoz/truffle/nodes/builtins/RecordBuiltins.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ protected Object makeDynamic(Object label, DynamicObject contents) {
for (int i = 0; i < size; i++) {
Object feature = derefNode.executeDerefIfBound(contents.get((long) i * 2 + 1));
Object value = derefNode.executeDerefIfBound(contents.get((long) i * 2 + 2));
if (map.containsKey(feature)) {
throw kernelError("recordConstruction", label, buildPairs(contents));
}
map.put(feature, value);
}

Expand All @@ -253,6 +256,17 @@ protected Object makeDynamic(Object label, DynamicObject contents) {
}
}

private Object buildPairs(DynamicObject contents) {
int size = OzRecord.getArity(contents).getWidth() / 2;
Object list = "nil";
for (int i = size - 1; i >= 0; i--) {
Object feature = derefNode.executeDerefIfBound(contents.get((long) i * 2 + 1));
Object value = derefNode.executeDerefIfBound(contents.get((long) i * 2 + 2));
list = new OzCons(Arity.PAIR_FACTORY.newRecord(feature, value), list);
}
return list;
}

}

@GenerateNodeFactory
Expand Down
2 changes: 2 additions & 0 deletions vm/src/org/mozartoz/truffle/runtime/Arity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Arity {
static final Shape CONS_SHAPE = CONS_ARITY.getShape();

public static final Arity PAIR_ARITY = Arity.build("#", 1L, 2L);
public static final RecordFactory PAIR_FACTORY = PAIR_ARITY.createFactory();

private final Object label;
private final Shape shape;
Expand Down Expand Up @@ -148,6 +149,7 @@ public static Arity build(Object label, Object... features) {
Shape shape = Arity.BASE;
for (Object feature : features) {
assert OzGuards.isFeature(feature);
assert !shape.hasProperty(feature) : "duplicated feature " + feature;
shape = shape.defineProperty(feature, SOME_OBJECT, 0);
}
return new Arity(label, shape);
Expand Down

0 comments on commit 869f803

Please sign in to comment.