Skip to content

Commit

Permalink
Fix checkValueEqual
Browse files Browse the repository at this point in the history
  • Loading branch information
heshanpadmasiri committed Dec 17, 2024
1 parent ab2a1b5 commit c24a754
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public final class Context {
private int drainedPermits = 0;
private int nTypeChecking = 0;
private Phase phase = Phase.INIT;
// List<PhaseData> typeCheckPhases = new ArrayList<>();
List<PhaseData> typeResolutionPhases = new ArrayList<>();

private Context(Env env) {
this.env = env;
Expand Down Expand Up @@ -100,6 +100,7 @@ public void enterTypeResolutionPhase(MutableSemType type) throws InterruptedExce
case INIT -> {
env.enterTypeResolutionPhase(this, type);
phase = Phase.TYPE_RESOLUTION;
typeResolutionPhases.add(new PhaseData());
}
case TYPE_RESOLUTION -> {
}
Expand All @@ -115,13 +116,13 @@ public void exitTypeResolutionPhaseAbruptly(Exception ex) {

public void enterTypeCheckingPhase(SemType t1, SemType t2) {
nTypeChecking += 1;
// typeCheckPhases.add(new PhaseData(phase, nTypeChecking, Thread.currentThread().getStackTrace()));
switch (phase) {
case INIT -> {
// This can happen if both types are immutable semtypes
phase = Phase.TYPE_CHECKING;
}
case TYPE_RESOLUTION -> {
typeResolutionPhases.removeLast();
drainedPermits = env.enterTypeCheckingPhase(this, t1, t2);
phase = Phase.TYPE_CHECKING;
}
Expand All @@ -130,9 +131,14 @@ public void enterTypeCheckingPhase(SemType t1, SemType t2) {
}
}

public void exitTypeResolutionPhase() {
if (phase == Phase.TYPE_RESOLUTION) {
env.exitTypeResolutionPhase(this);
}
}

public void exitTypeCheckingPhase() {
nTypeChecking -= 1;
// typeCheckPhases.removeLast();
switch (phase) {
case INIT -> throw new IllegalStateException("Cannot exit type checking phase without entering it");
case TYPE_RESOLUTION ->
Expand Down Expand Up @@ -227,17 +233,15 @@ enum Phase {
INIT, TYPE_RESOLUTION, TYPE_CHECKING
}

int getNTypeChecking() {
return nTypeChecking;
}
record PhaseData(StackTraceElement[] stackTrace) {

record PhaseData(Phase phase, int nTypeCheck, StackTraceElement[] stackTrace) {
PhaseData() {
this(Thread.currentThread().getStackTrace());
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Phase: ").append(phase).append("\n");
builder.append("N type checking: ").append(nTypeCheck).append("\n");
for (StackTraceElement element : stackTrace) {
builder.append("\tat ").append(element).append("\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ public void registerAbruptTypeCheckEnd(Context cx, Exception ex) {
}
}

@Override
public void registerTypeResolutionExit(Context cx) {
synchronized (pendingTypeResolutions) {
pendingTypeResolutions.removeIf(data -> data.cx == cx);
}
}

private static String withIdentity(Object o) {
return o + "[" + System.identityHashCode(o) + "]";
}
Expand Down Expand Up @@ -174,6 +181,8 @@ private static String pendingTypeResolutionsToString(Collection<TypeResolutionDa
for (StackTraceElement element : data.t.getStackTrace()) {
logBuilder.append("\tat ").append(element).append("\n");
}
logBuilder.append("Entry points\n");
data.cx.typeResolutionPhases.forEach(each -> logBuilder.append(each.toString()).append("\n"));
});
return logBuilder.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,12 @@ void enterTypeResolutionPhase(Context cx, MutableSemType t) throws InterruptedEx
synchronized (this) {
if (typeResolutionPhaser.isTerminated()) {
typeResolutionPhaser = new Phaser();
typeResolutionPhaser.register();
}
}
} else {
typeResolutionPhaser.register();
}
typeResolutionPhaser.register();
this.selfDiagnosticsRunner.registerTypeResolutionStart(cx, t);
}

Expand All @@ -348,6 +350,12 @@ private void releaseLock(ReentrantReadWriteLock lock) {
}
}

void exitTypeResolutionPhase(Context cx) {
typeResolutionSemaphore.release();
typeResolutionPhaser.arriveAndDeregister();
this.selfDiagnosticsRunner.registerTypeResolutionExit(cx);
}

int enterTypeCheckingPhase(Context cx, SemType t1, SemType t2) {
int drained = typeResolutionSemaphore.drainPermits();
typeResolutionPhaser.awaitAdvance(typeResolutionPhaser.arriveAndDeregister());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public void registerAbruptTypeResolutionEnd(Context cx, Exception ex) {
public void registerAbruptTypeCheckEnd(Context cx, Exception ex) {

}

@Override
public void registerTypeResolutionExit(Context cx) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ interface TypeCheckSelfDiagnosticsRunner {
void registerAbruptTypeResolutionEnd(Context cx, Exception ex);

void registerAbruptTypeCheckEnd(Context cx, Exception ex);

void registerTypeResolutionExit(Context cx);
}
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ public static boolean isEqual(Object lhsValue, Object rhsValue, Set<ValuePair> c
private static boolean checkValueEqual(Context cx, Object lhsValue, Object rhsValue, Set<ValuePair> checkedValues) {
SemType lhsShape = ShapeAnalyzer.inherentTypeOf(cx, lhsValue).orElseThrow();
SemType rhsShape = ShapeAnalyzer.inherentTypeOf(cx, rhsValue).orElseThrow();
cx.exitTypeResolutionPhase();
Predicate<SemType> belongToSameBasicType = (basicType) -> Core.containsBasicType(lhsShape, basicType) &&
Core.containsBasicType(rhsShape, basicType);
if (belongToSameBasicType.test(Builder.getStringType()) ||
Expand Down

0 comments on commit c24a754

Please sign in to comment.