Skip to content

Commit a6850a9

Browse files
committed
Preserve explicit compiler pool scopes instead of owner fallbacks
Withdraw the currentOr and poolInUse changes in response to review. Document the existing host boundary contract and exercise scope restoration, executor isolation, cross-pool operations, and scoped method inspection without changing production behavior.
1 parent dd136d3 commit a6850a9

12 files changed

Lines changed: 111 additions & 139 deletions

File tree

‎javatools/src/main/java/org/xvm/api/InterpreterConnector.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ public void invoke0(MethodStructure method, String... asArg) {
8484
throw new IllegalStateException("The container has not been started");
8585
}
8686

87-
// the container binds a pool to the thread it starts on; a caller invoking from another
88-
// thread would otherwise find none, so fall back to the container's own
89-
ConstantPool pool = ConstantPool.currentOr(getConstantPool());
87+
ConstantPool pool = ConstantPool.getCurrentPool();
9088
TypeConstant typeStrings = pool.ensureArrayType(pool.typeString());
9189
ObjectHandle[] ahArg = Utils.OBJECTS_NONE;
9290

‎javatools/src/main/java/org/xvm/asm/Constant.java‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -461,19 +461,6 @@ public ConstantPool getConstantPool() {
461461
return (ConstantPool) getContaining();
462462
}
463463

464-
/**
465-
* The pool to work in when resolving other constants from this one.
466-
*
467-
* <p>The thread's pool where the compiler has bound one, and this constant's own otherwise, so
468-
* that a constant asked to do something outside a compilation - printed by a debugger, used
469-
* by a test - answers rather than throwing. See {@link ConstantPool#currentOr}.
470-
*
471-
* @return the pool to work in; never null
472-
*/
473-
protected ConstantPool poolInUse() {
474-
return ConstantPool.currentOr(getConstantPool());
475-
}
476-
477464
@Override
478465
public boolean isModified() {
479466
// it's a constant; it can't be modified

‎javatools/src/main/java/org/xvm/asm/ConstantPool.java‎

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3175,7 +3175,7 @@ private Relation checkFunctionOrMethodCompatibility(TypeConstant typeLeft, TypeC
31753175
// the number of returns on the left must not exceed the number of returns on the right;
31763176
// the only exception: "void f(X)" is allowed to be assigned to "Tuple<> f(x)"
31773177
if (cLR > cRR) {
3178-
return cRR == 0 && cLR == 1 && typeLR.getParamType(0).equals(currentOr(this).typeTuple0())
3178+
return cRR == 0 && cLR == 1 && typeLR.getParamType(0).equals(getCurrentPool().typeTuple0())
31793179
? Relation.IS_A
31803180
: Relation.INCOMPATIBLE;
31813181
}
@@ -3433,33 +3433,13 @@ private TypeInfo computeNakedRefInfo(TypeConstant typeReferent) {
34333433
}
34343434

34353435
/**
3436-
* The pool to work in: the one bound to this thread if there is one, and the given fallback
3437-
* otherwise.
3436+
* Return the working pool associated with the current thread.
34383437
*
3439-
* <p>{@link #getCurrentPool} is a thread-local, bound by {@link #withPool} around stretches of
3440-
* compilation and by the runtime container. Outside those it is simply null - on a thread
3441-
* that is driving the compiler from ordinary Java code, in a test, in a debugger evaluating a
3442-
* watch - and code that dereferenced it threw there. Two such NullPointerExceptions were
3443-
* found by accident while doing something else, one of them in the code meant to describe a
3444-
* structure for a log line.
3438+
* <p>Hosts entering compiler operations that depend on this context must establish a
3439+
* {@link #withPool} scope. A constant's owning pool is not necessarily the working pool:
3440+
* cross-pool operations may intentionally resolve or register constants in another pool.
34453441
*
3446-
* <p>The ambient pool is preferred rather than ignored, because it is not always the pool a
3447-
* given constant belongs to: {@link #withPool} exists precisely because the compiler works
3448-
* across pools, and a well-known constant fetched from the wrong one answers wrongly rather
3449-
* than not at all. So this changes nothing where a pool is bound, and answers instead of
3450-
* throwing where none is.
3451-
*
3452-
* @param poolFallback the pool to use when no pool is bound to this thread
3453-
*
3454-
* @return the pool to work in; null only if the fallback is null
3455-
*/
3456-
public static ConstantPool currentOr(ConstantPool poolFallback) {
3457-
ConstantPool pool = getCurrentPool();
3458-
return pool == null ? poolFallback : pool;
3459-
}
3460-
3461-
/**
3462-
* @return a ContextPool associated with the current thread
3442+
* @return the working pool, or null if none is associated with this thread
34633443
*/
34643444
public static ConstantPool getCurrentPool() {
34653445
return s_tloPool.get()[0];
@@ -3478,6 +3458,10 @@ public static void setCurrentPool(ConstantPool pool) {
34783458
* Temporarily update the current ConstantPool, restoring it when the returned AutoCloseable
34793459
* is closed.
34803460
*
3461+
* <p>Use try-with-resources at the operation boundary, including when inspecting compiler
3462+
* results after compilation. Open and close the scope on the same thread. Work dispatched to
3463+
* another thread must establish its own scope; the context is not inherited.
3464+
*
34813465
* @param pool the new pool
34823466
*
34833467
* @return an Auto which will revert to the prior pool

‎javatools/src/main/java/org/xvm/asm/constants/ByteConstant.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ public Constant apply(Token.Id op, Constant that) {
287287
case "Int8^Int8":
288288
return validate(this.m_nVal ^ ((ByteConstant) that).m_nVal);
289289
case "Int8..Int8":
290-
return poolInUse().ensureRangeConstant(this, that);
290+
return ConstantPool.getCurrentPool().ensureRangeConstant(this, that);
291291
case "Int8..<Int8":
292-
return poolInUse().ensureRangeConstant(this, false, that, true);
292+
return ConstantPool.getCurrentPool().ensureRangeConstant(this, false, that, true);
293293

294294
case "Int8<<Int64":
295295
return validate(this.m_nVal << ((IntConstant) that).getValue().and(new PackedInteger(7)).getInt());
@@ -362,13 +362,13 @@ public Constant apply(Token.Id op, Constant that) {
362362
return validate(this.m_nVal ^ ((ByteConstant) that).m_nVal);
363363
case "UInt8..UInt8":
364364
case "Nibble..Nibble":
365-
return poolInUse().ensureRangeConstant(this, that);
365+
return ConstantPool.getCurrentPool().ensureRangeConstant(this, that);
366366
case "UInt8..<UInt8":
367-
return poolInUse().ensureRangeConstant(this, false, that, true);
367+
return ConstantPool.getCurrentPool().ensureRangeConstant(this, false, that, true);
368368
case "UInt8>..UInt8":
369-
return poolInUse().ensureRangeConstant(this, true, that, false);
369+
return ConstantPool.getCurrentPool().ensureRangeConstant(this, true, that, false);
370370
case "UInt8>..<UInt8":
371-
return poolInUse().ensureRangeConstant(this, true, that, true);
371+
return ConstantPool.getCurrentPool().ensureRangeConstant(this, true, that, true);
372372

373373
case "UInt8<<Int64":
374374
return validate(this.m_nVal << ((IntConstant) that).getValue().and(new PackedInteger(7)).getInt());

‎javatools/src/main/java/org/xvm/asm/constants/IdentityConstant.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ public int compareTo(NestedIdentity that) {
502502
}
503503

504504
private Object resolve(Object element) {
505-
ConstantPool pool = poolInUse();
505+
ConstantPool pool = ConstantPool.getCurrentPool();
506506
return m_resolver != null && element instanceof SignatureConstant sig
507507
? sig.resolveGenericTypes(pool, m_resolver)
508508
: element;

‎javatools/src/main/java/org/xvm/asm/constants/IntConstant.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ public Constant apply(Token.Id op, Constant that) {
718718
case "UInt64..UInt64":
719719
case "UInt128..UInt128":
720720
case "UIntN..UIntN":
721-
return poolInUse().ensureRangeConstant(this, that);
721+
return ConstantPool.getCurrentPool().ensureRangeConstant(this, that);
722722

723723
case "Int..<Int":
724724
case "UInt..<UInt":
@@ -732,7 +732,7 @@ public Constant apply(Token.Id op, Constant that) {
732732
case "UInt64..<UInt64":
733733
case "UInt128..<UInt128":
734734
case "UIntN..<UIntN":
735-
return poolInUse().ensureRangeConstant(this, false, that, true);
735+
return ConstantPool.getCurrentPool().ensureRangeConstant(this, false, that, true);
736736

737737
case "Int>..Int":
738738
case "UInt>..UInt":
@@ -746,7 +746,7 @@ public Constant apply(Token.Id op, Constant that) {
746746
case "UInt64>..UInt64":
747747
case "UInt128>..UInt128":
748748
case "UIntN>..UIntN":
749-
return poolInUse().ensureRangeConstant(this, true, that, false);
749+
return ConstantPool.getCurrentPool().ensureRangeConstant(this, true, that, false);
750750

751751
case "Int>..<Int":
752752
case "UInt>..<UInt":
@@ -760,7 +760,7 @@ public Constant apply(Token.Id op, Constant that) {
760760
case "UInt64>..<UInt64":
761761
case "UInt128>..<UInt128":
762762
case "UIntN>..<UIntN":
763-
return poolInUse().ensureRangeConstant(this, true, that, true);
763+
return ConstantPool.getCurrentPool().ensureRangeConstant(this, true, that, true);
764764

765765
case "Int<<Int":
766766
case "Int16<<Int64":

‎javatools/src/main/java/org/xvm/asm/constants/MethodBody.java‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -685,19 +685,10 @@ public boolean isOp(String sName, String sOp, int cParams) {
685685
}
686686

687687
/**
688-
* The pool to resolve well-known constants against, such as the {@code @Op} annotation class.
689-
*
690-
* <p>The ambient one when there is one - the compiler pushes it for the pool it is working in,
691-
* which is not always the pool this body's identity belongs to. But it is a thread-local, so
692-
* it is simply null on any thread that has never had one pushed, and this is reached from
693-
* {@link #isOp()} and from {@code toString()}: a debugger, a log line or a test that so much
694-
* as printed a TypeInfo threw a NullPointerException out of it. Failing that, a constant
695-
* knows its own pool.
696-
*
697-
* @return the pool; never null
688+
* @return the ConstantPool
698689
*/
699690
private ConstantPool pool() {
700-
return ConstantPool.currentOr(m_id.getConstantPool());
691+
return ConstantPool.getCurrentPool();
701692
}
702693

703694
// ----- Object methods ------------------------------------------------------------------------

‎javatools/src/main/java/org/xvm/asm/constants/MethodInfo.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1511,7 +1511,7 @@ public int getRank() {
15111511
* @return the ConstantPool
15121512
*/
15131513
private ConstantPool pool() {
1514-
return ConstantPool.currentOr(getIdentity().getConstantPool());
1514+
return ConstantPool.getCurrentPool();
15151515
}
15161516

15171517
// ----- JIT support ---------------------------------------------------------------------------

‎javatools/src/main/java/org/xvm/asm/constants/PropertyInfo.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ public boolean isIdentityValid(PropertyConstant idProp) {
678678
// allowable for interfaces
679679
Component parent = idProp.getNamespace().getComponent();
680680
if (parent == null) {
681-
ConstantPool pool = ConstantPool.currentOr(idProp.getConstantPool());
681+
ConstantPool pool = ConstantPool.getCurrentPool();
682682
if (idProp.isShared(pool)) {
683683
idProp = pool.register(idProp);
684684
parent = idProp.getNamespace().getComponent();

‎javatools/src/main/java/org/xvm/asm/constants/TypeConstant.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6326,7 +6326,7 @@ public boolean isCovariantReturn(TypeConstant typeBase, TypeConstant typeCtx) {
63266326
return true;
63276327
}
63286328

6329-
ConstantPool pool = poolInUse();
6329+
ConstantPool pool = ConstantPool.getCurrentPool();
63306330
if (typeCtx instanceof UnionTypeConstant typeUnion) {
63316331
if (this.containsAutoNarrowing(true) || typeBase.containsAutoNarrowing(true)) {
63326332
boolean fCovariant = isCovariantReturn(typeBase, pool.ensureIntersectionTypeConstant(
@@ -6407,7 +6407,7 @@ public boolean isContravariantParameter(TypeConstant typeBase, TypeConstant type
64076407
return true;
64086408
}
64096409

6410-
ConstantPool pool = poolInUse();
6410+
ConstantPool pool = ConstantPool.getCurrentPool();
64116411

64126412
TypeConstant typeThisR = this.containsAutoNarrowing(true)
64136413
? this.resolveAutoNarrowing(pool, false, typeCtx, null)

0 commit comments

Comments
 (0)