Skip to content

Commit

Permalink
Fix a compiler assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
Gene Gleyzer committed Sep 12, 2023
1 parent e67918d commit 294948c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 35 deletions.
11 changes: 11 additions & 0 deletions javatools/src/main/java/org/xvm/compiler/ast/NewExpression.java
Expand Up @@ -823,6 +823,17 @@ else if (fNestMate && typeTarget.isVirtualChild() || typeTarget.isInnerChildClas
m_mapRegisters = ctxAnon.ensureRegisterMap();
m_fInstanceChild = ctxAnon.isInstanceChild();
m_ctxCapture = null;

// make sure the capture names don't collide
ClassStructure clzAnon = ctxAnon.getThisClass();
for (String sName : m_mapCapture.keySet())
{
if (clzAnon.getChild(sName) != null)
{
log(errs, Severity.ERROR, Compiler.NAME_COLLISION, sName);
return null;
}
}
}

m_plan = plan;
Expand Down
54 changes: 19 additions & 35 deletions manualTests/src/main/x/TestSimple.x
@@ -1,40 +1,24 @@
module TestSimple {
@Inject Console console;
module TestSimple {
@Inject Console console;

void run() {
Callback[] callbacks = new Array();
callbacks.add(new MyCallbackOne());
callbacks.add(new MyCallbackTwo());
void run() {
}

// the line below used to throw TypeMismatch at run-time; it no longer compiles
// Callback[] filtered = callbacks.filter(c -> !c.is(MyCallbackOne));
Collection<Callback> filtered = callbacks.filter(c -> !c.is(MyCallbackOne));
for (Callback callback : filtered) {
callback.call();
}

for (Callback callback : callbacks.filter(c -> c.is(MyCallbackOne))) {
callback.call();
}
}

interface Callback {
void call();
}

const MyCallbackOne
implements Callback {
@Override
void call() {
console.print("one");
}
}
Iterator<Int> test(Int next) { // the name collision used to assert the compiler
return new Iterator<Int>() {
private Int nextValue = next;
private Boolean hasNext = True;

const MyCallbackTwo
implements Callback {
@Override
void call() {
console.print("two");
conditional Element next() {
if (hasNext) {
Int value = nextValue;
hasNext = False;
return True, value;
} else {
return False;
}
}
}
}
};
}
}

0 comments on commit 294948c

Please sign in to comment.