Skip to content

Commit

Permalink
Correct implementation of IdentityMostlySingleton
Browse files Browse the repository at this point in the history
  • Loading branch information
smillst committed Aug 5, 2020
1 parent a741394 commit 959f82e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
public abstract class AbstractMostlySingleton<T extends Object> implements Set<T> {

/** The possible states of the collection. */
/** The possible states of this set. */
public enum State {
/** An empty set. */
EMPTY,
Expand All @@ -29,8 +29,8 @@ public enum State {
protected State state;
/** The current value, non-null when the state is SINGLETON. */
protected @Nullable T value;
/** The wrapped collection, non-null when the state is ANY. */
protected @Nullable Collection<T> set;
/** The wrapped set, non-null when the state is ANY. */
protected @Nullable Set<T> set;

/** Create an AbstractMostlySingleton. */
protected AbstractMostlySingleton(State s) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.checkerframework.dataflow.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.IdentityHashMap;
import org.checkerframework.checker.interning.qual.FindDistinct;
import org.checkerframework.javacutil.BugInCF;

/**
Expand All @@ -21,15 +23,18 @@ public IdentityMostlySingleton(T value) {

@Override
@SuppressWarnings("fallthrough")
public boolean add(T e) {
public boolean add(@FindDistinct T e) {
switch (state) {
case EMPTY:
state = State.SINGLETON;
value = e;
return true;
case SINGLETON:
if (value == e) {
return false;
}
state = State.ANY;
set = new ArrayList<>();
set = Collections.newSetFromMap(new IdentityHashMap<>());
assert value != null : "@AssumeAssertion(nullness): previous add is non-null";
set.add(value);
value = null;
Expand Down
20 changes: 20 additions & 0 deletions framework/tests/nontopdefault/TestCasting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import testlib.nontopdefault.qual.NTDMiddle;

@SuppressWarnings("inconsistent.constructor.type") // Not the point of this test
public class TestCasting {
void repro(@NTDMiddle long startTime) {
try {
System.out.println("Inside try");
return;
} catch (Exception ex) {
long timeTaken = startTime;
@NTDMiddle double dblTimeTaken = timeTaken;

throw new IllegalArgumentException();
} finally {
long timeTaken2 = startTime;
// This assignment used to fail.
@NTDMiddle double dblTimeTaken2 = timeTaken2;
}
}
}

0 comments on commit 959f82e

Please sign in to comment.