Skip to content

Commit 319fa92

Browse files
committed
Fix type speculation for Boolean stored in Object slot.
1 parent 8e0b3e0 commit 319fa92

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

truffle/src/com.oracle.truffle.api.object.test/src/com/oracle/truffle/object/ext/test/ObjectModelRegressionTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
import java.util.List;
5858
import java.util.Map;
5959

60+
import org.hamcrest.CoreMatchers;
61+
import org.hamcrest.MatcherAssert;
6062
import org.junit.Test;
6163
import org.junit.runner.RunWith;
6264
import org.junit.runners.Parameterized;
@@ -473,6 +475,25 @@ public void testTryMergeShapes2() {
473475
assertSame(b.getShape(), a.getShape());
474476
}
475477

478+
@Test
479+
public void testBooleanLocationTypeAssumption() {
480+
assumeExtLayout();
481+
482+
Shape emptyShape = Shape.newBuilder().build();
483+
484+
DynamicObject obj = new TestDynamicObject(emptyShape);
485+
486+
DynamicObjectLibrary library = createLibrary(DynamicObjectLibrary.class, obj);
487+
488+
library.put(obj, "b1", true);
489+
library.put(obj, "b2", true);
490+
library.put(obj, "b2", false);
491+
492+
Shape shape = obj.getShape();
493+
MatcherAssert.assertThat(shape.getProperty("b1").getLocation().toString(), CoreMatchers.containsString("Boolean"));
494+
MatcherAssert.assertThat(shape.getProperty("b2").getLocation().toString(), CoreMatchers.containsString("Boolean"));
495+
}
496+
476497
/**
477498
* Tests that onPropertyTransition is called by replace and remove property transitions.
478499
*/

truffle/src/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/ExtAllocator.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ public ExtLocation declaredLocation(Object value) {
107107
protected Location moveLocation(Location oldLocation) {
108108
final boolean decorateFinal = false;
109109
if (oldLocation instanceof IntLocation) {
110-
return newIntLocation(decorateFinal, oldLocation);
110+
return newIntLocation(decorateFinal, oldLocation, NO_VALUE);
111111
} else if (oldLocation instanceof DoubleLocation) {
112-
return newDoubleLocation(decorateFinal, ((DoubleLocation) oldLocation).isImplicitCastIntToDouble(), oldLocation);
112+
return newDoubleLocation(decorateFinal, ((DoubleLocation) oldLocation).isImplicitCastIntToDouble(), oldLocation, NO_VALUE);
113113
} else if (oldLocation instanceof LongLocation) {
114-
return newLongLocation(decorateFinal, ((LongLocation) oldLocation).isImplicitCastIntToLong(), oldLocation);
114+
return newLongLocation(decorateFinal, ((LongLocation) oldLocation).isImplicitCastIntToLong(), oldLocation, NO_VALUE);
115115
} else if (oldLocation instanceof BooleanLocation) {
116-
return newBooleanLocation(decorateFinal, oldLocation);
116+
return newBooleanLocation(decorateFinal, oldLocation, NO_VALUE);
117117
} else if (oldLocation instanceof AbstractObjectFieldLocation) {
118118
return newObjectLocation(decorateFinal, oldLocation, NO_VALUE);
119119
} else if (oldLocation instanceof AbstractObjectArrayLocation) {
@@ -266,10 +266,10 @@ private static int tryAllocatePrimitiveSlot(ExtLayout l, int startIndex, final i
266266

267267
@Override
268268
public Location newIntLocation(boolean useFinal) {
269-
return newIntLocation(false, null);
269+
return newIntLocation(false, null, NO_VALUE);
270270
}
271271

272-
private Location newIntLocation(boolean decorateFinal, Location oldLocation) {
272+
private Location newIntLocation(boolean decorateFinal, Location oldLocation, Object value) {
273273
if (PrimitiveLocations && IntegerLocations) {
274274
ExtLayout l = getLayout();
275275
if (InObjectFields) {
@@ -288,15 +288,15 @@ private Location newIntLocation(boolean decorateFinal, Location oldLocation) {
288288
return advance(location);
289289
}
290290
}
291-
return newObjectLocation(decorateFinal, null, NO_VALUE);
291+
return newObjectLocation(decorateFinal, oldLocation, value);
292292
}
293293

294294
@Override
295295
public Location newDoubleLocation(boolean useFinal) {
296-
return newDoubleLocation(false, getLayout().isAllowedIntToDouble(), null);
296+
return newDoubleLocation(false, getLayout().isAllowedIntToDouble(), null, NO_VALUE);
297297
}
298298

299-
private Location newDoubleLocation(boolean decorateFinal, boolean allowIntToDouble, Location oldLocation) {
299+
private Location newDoubleLocation(boolean decorateFinal, boolean allowIntToDouble, Location oldLocation, Object value) {
300300
if (PrimitiveLocations && DoubleLocations) {
301301
ExtLayout l = getLayout();
302302
if (InObjectFields) {
@@ -316,7 +316,7 @@ private Location newDoubleLocation(boolean decorateFinal, boolean allowIntToDoub
316316
return advance(location);
317317
}
318318
}
319-
return newObjectLocation(decorateFinal, null, NO_VALUE);
319+
return newObjectLocation(decorateFinal, oldLocation, value);
320320
}
321321

322322
/**
@@ -339,10 +339,10 @@ private static int alignArrayIndex(int index, int slotSize) {
339339

340340
@Override
341341
public Location newLongLocation(boolean useFinal) {
342-
return newLongLocation(false, getLayout().isAllowedIntToLong(), null);
342+
return newLongLocation(false, getLayout().isAllowedIntToLong(), null, NO_VALUE);
343343
}
344344

345-
private Location newLongLocation(boolean decorateFinal, boolean allowIntToLong, Location oldLocation) {
345+
private Location newLongLocation(boolean decorateFinal, boolean allowIntToLong, Location oldLocation, Object value) {
346346
if (PrimitiveLocations && LongLocations) {
347347
ExtLayout l = getLayout();
348348
if (InObjectFields) {
@@ -362,15 +362,15 @@ private Location newLongLocation(boolean decorateFinal, boolean allowIntToLong,
362362
return advance(location);
363363
}
364364
}
365-
return newObjectLocation(decorateFinal, null, NO_VALUE);
365+
return newObjectLocation(decorateFinal, oldLocation, value);
366366
}
367367

368368
@Override
369369
public Location newBooleanLocation(boolean useFinal) {
370-
return newBooleanLocation(false, null);
370+
return newBooleanLocation(false, null, NO_VALUE);
371371
}
372372

373-
private Location newBooleanLocation(boolean decorateFinal, Location oldLocation) {
373+
private Location newBooleanLocation(boolean decorateFinal, Location oldLocation, Object value) {
374374
if (PrimitiveLocations && BooleanLocations && InObjectFields) {
375375
ExtLayout l = getLayout();
376376
int fieldIndex = tryAllocatePrimitiveSlot(l, primitiveFieldSize, Integer.BYTES);
@@ -383,7 +383,7 @@ private Location newBooleanLocation(boolean decorateFinal, Location oldLocation)
383383
}
384384
}
385385
}
386-
return newObjectLocation(decorateFinal, null, NO_VALUE);
386+
return newObjectLocation(decorateFinal, oldLocation, value);
387387
}
388388

389389
@Override
@@ -411,9 +411,9 @@ protected Location locationForValueUpcast(Object value, Location oldLocation, in
411411
boolean allowedIntToDouble = getLayout().isAllowedIntToDouble() || Flags.isImplicitCastIntToDouble(putFlags);
412412
boolean allowedIntToLong = getLayout().isAllowedIntToLong() || Flags.isImplicitCastIntToLong(putFlags);
413413
if (value instanceof Double && allowedIntToDouble) {
414-
newLocation = newDoubleLocation(decorateFinal, allowedIntToDouble, oldLocation);
414+
newLocation = newDoubleLocation(decorateFinal, allowedIntToDouble, oldLocation, NO_VALUE);
415415
} else if (value instanceof Long && allowedIntToLong) {
416-
newLocation = newLongLocation(decorateFinal, allowedIntToLong, oldLocation);
416+
newLocation = newLongLocation(decorateFinal, allowedIntToLong, oldLocation, NO_VALUE);
417417
}
418418
}
419419

@@ -456,14 +456,14 @@ protected Location locationForValue(Object value, boolean nonNull, int putFlags)
456456
}
457457
boolean decorateFinal = true;
458458
if (value instanceof Integer) {
459-
return newIntLocation(decorateFinal, null);
459+
return newIntLocation(decorateFinal, null, value);
460460
} else if (value instanceof Double) {
461-
return newDoubleLocation(decorateFinal, getLayout().isAllowedIntToDouble(), null);
461+
return newDoubleLocation(decorateFinal, getLayout().isAllowedIntToDouble(), null, value);
462462
} else if (value instanceof Long) {
463-
return newLongLocation(decorateFinal, getLayout().isAllowedIntToLong(), null);
463+
return newLongLocation(decorateFinal, getLayout().isAllowedIntToLong(), null, value);
464464
} else if (value instanceof Boolean) {
465-
return newBooleanLocation(decorateFinal, null);
466-
} else if (TypedObjectLocations && value != null && value.getClass() != null) {
465+
return newBooleanLocation(decorateFinal, null, value);
466+
} else if (TypedObjectLocations && value != null) {
467467
return newTypedObjectLocation(value.getClass(), nonNull, decorateFinal, null, value);
468468
}
469469
return newObjectLocation(decorateFinal, null, value);

0 commit comments

Comments
 (0)