Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct implementation of IdentityMostlySingleton. #3550

Merged
merged 7 commits into from
Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
}
}