From 7ba9d764dfdea96da470f612d360824269fda132 Mon Sep 17 00:00:00 2001 From: brandjon Date: Tue, 20 Mar 2018 10:47:31 -0700 Subject: Add ability to shallow-freeze individual objects Clarify that the IMMUTABLE Mutability should only be used for deeply immutable things, not tuples. Created a new SHALLOW_IMMUTABLE Mutability for them. Note that the new shallow-freezing functionality marks things as deeply IMMUTABLE without traversing its contents. I.e., it lies, and it is up to the caller to ensure this doesn't cause problems. RELNOTES: NONE PiperOrigin-RevId: 189767422 --- .../devtools/build/lib/syntax/MutabilityTest.java | 22 ++++++++++++++++++++++ .../devtools/build/lib/syntax/SkylarkListTest.java | 14 ++++++++++++++ 2 files changed, 36 insertions(+) (limited to 'src/test/java/com/google/devtools/build/lib/syntax') diff --git a/src/test/java/com/google/devtools/build/lib/syntax/MutabilityTest.java b/src/test/java/com/google/devtools/build/lib/syntax/MutabilityTest.java index 67c3e0ec8b..b9b896f2bc 100644 --- a/src/test/java/com/google/devtools/build/lib/syntax/MutabilityTest.java +++ b/src/test/java/com/google/devtools/build/lib/syntax/MutabilityTest.java @@ -233,4 +233,26 @@ public final class MutabilityTest { assertThat(expected).hasMessageThat().contains( "trying to check the lock of an object from a different context"); } + + @Test + public void checkUnsafeShallowFreezePrecondition_FailsWhenAlreadyFrozen() throws Exception { + Mutability mutability = Mutability.create("test").freeze(); + assertThrows( + IllegalArgumentException.class, + () -> Freezable.checkUnsafeShallowFreezePrecondition(new DummyFreezable(mutability))); + } + + @Test + public void checkUnsafeShallowFreezePrecondition_FailsWhenDisallowed() throws Exception { + Mutability mutability = Mutability.create("test"); + assertThrows( + IllegalArgumentException.class, + () -> Freezable.checkUnsafeShallowFreezePrecondition(new DummyFreezable(mutability))); + } + + @Test + public void checkUnsafeShallowFreezePrecondition_SucceedsWhenAllowed() throws Exception { + Mutability mutability = Mutability.createAllowingShallowFreeze("test"); + Freezable.checkUnsafeShallowFreezePrecondition(new DummyFreezable(mutability)); + } } diff --git a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkListTest.java b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkListTest.java index b2768390da..016c30f85a 100644 --- a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkListTest.java +++ b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkListTest.java @@ -278,6 +278,20 @@ public class SkylarkListTest extends EvaluationTestCase { } } + @Test + public void testCannotMutateAfterShallowFreeze() throws Exception { + Mutability mutability = Mutability.createAllowingShallowFreeze("test"); + MutableList list = MutableList.copyOf(mutability, ImmutableList.of(1, 2, 3)); + list.unsafeShallowFreeze(); + + try { + list.add(4, null, mutability); + fail("expected exception"); + } catch (EvalException e) { + assertThat(e).hasMessage("trying to mutate a frozen object"); + } + } + @Test public void testCopyOfTakesCopy() throws EvalException { ArrayList copyFrom = Lists.newArrayList("hi"); -- cgit v1.2.3