Skip to content

Commit

Permalink
Move null checks to asserts as rascal tends to catch these already an…
Browse files Browse the repository at this point in the history
…d they were showing up in profiles

The asserts should still trigger during our unit tests.

Fixes #233
  • Loading branch information
DavyLandman committed Feb 21, 2024
1 parent a07b754 commit 7fdb016
Showing 1 changed file with 55 additions and 37 deletions.
92 changes: 55 additions & 37 deletions src/main/java/io/usethesource/vallang/type/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.lang.reflect.InvocationTargetException;
import java.math.BigInteger;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
Expand Down Expand Up @@ -175,7 +176,7 @@ public Type boolType() {
* @return an object equal to externalType but possibly older
*/
public Type externalType(Type externalType) {
checkNull(externalType);
assert !isNull(externalType) : "external type cannot be null";
return getFromCache(externalType);
}

Expand Down Expand Up @@ -228,7 +229,7 @@ public Type tupleEmpty() {
* @return a tuple type or `void` in case one of the field types was void
*/
public Type tupleType(Type... fieldTypes) {
checkNull((Object[]) fieldTypes);
assert !anyNull((Object[]) fieldTypes) : "tuple field types should not be null";

// tuple values with elements of type void (like tuple[void,void])
// can not exist, so the type that represents that empty set of values is `void`,
Expand Down Expand Up @@ -310,8 +311,8 @@ public Type tupleType(Object... fieldTypesAndLabels) throws FactTypeDeclarationE
*/
@Deprecated
public Type tupleType(Type[] types, String[] labels) {
checkNull((Object[]) types);
checkNull((Object[]) labels);
assert !anyNull((Object[]) types) : "tuples types should not contain nulls";
assert !anyNull((Object[]) labels): "label types should not contain nulls";
assert types.length == labels.length;

if (types.length == 0) {
Expand All @@ -338,7 +339,7 @@ public Type tupleType(Type[] types, String[] labels) {
* @return a tuple type
*/
public Type tupleType(IValue... elements) {
checkNull((Object[]) elements);
assert !anyNull((Object[]) elements) : "tuple type elements should not contain any nulls";
int N = elements.length;
Type[] fieldTypes = new Type[N];
for (int i = N - 1; i >= 0; i--) {
Expand Down Expand Up @@ -374,10 +375,10 @@ public Type functionType(Type returnType, Type argumentTypesTuple, Type keywordP
* Construct a function type with labeled parameter and keyword field types
*/
public Type functionType(Type returnType, Type[] argumentTypes, String[] argumentLabels, Type[] keywordParameterTypes, String[] keywordParameterLabels) {
checkNull((Object[]) argumentTypes);
checkNull((Object[]) argumentLabels);
checkNull((Object[]) keywordParameterTypes);
checkNull((Object[]) keywordParameterLabels);
assert !anyNull((Object[]) argumentTypes) : "argument types should not be null";
assert !anyNull((Object[]) argumentLabels) : "argument labels should not contain nulls";
assert !anyNull((Object[]) keywordParameterTypes) : "keyword parameter types should not contain nulls";
assert !anyNull((Object[]) keywordParameterLabels) : "keyword parameter types should not contain nulls";
return getFromCache(new FunctionType(returnType,
(TupleType) getFromCache(new TupleTypeWithFieldNames(argumentTypes, argumentLabels)),
(TupleType) getFromCache(new TupleTypeWithFieldNames(keywordParameterTypes, keywordParameterLabels))
Expand All @@ -388,8 +389,8 @@ public Type functionType(Type returnType, Type[] argumentTypes, String[] argumen
* Construct a function type with unlabeled parameter and keyword field types
*/
public Type functionType(Type returnType, Type[] argumentTypes, Type[] keywordParameterTypes) {
checkNull((Object[]) argumentTypes);
checkNull((Object[]) keywordParameterTypes);
assert !anyNull((Object[]) argumentTypes) : "argument types should not be null";
assert !anyNull((Object[]) keywordParameterTypes) : "keyword parameter types should not contain nulls";
return getFromCache(new FunctionType(returnType,
(TupleType) getFromCache(new TupleType(argumentTypes)),
(TupleType) getFromCache(new TupleType(keywordParameterTypes))
Expand All @@ -404,17 +405,17 @@ public Type functionType(Type returnType, Type[] argumentTypes, Type[] keywordPa
* @return a set type
*/
public Type setType(Type eltType) {
checkNull(eltType);
assert !isNull(eltType) : "set type should not be null";
return getFromCache(new SetType(eltType));
}

public Type relTypeFromTuple(Type tupleType) {
checkNull(tupleType);
assert !isNull(tupleType) : "rel type should not be null";
return setType(tupleType);
}

public Type lrelTypeFromTuple(Type tupleType) {
checkNull(tupleType);
assert !isNull(tupleType) : "lrel type should not be null";

return listType(tupleType);
}
Expand All @@ -427,7 +428,7 @@ public Type lrelTypeFromTuple(Type tupleType) {
* @return a relation type
*/
public Type relType(Type... fieldTypes) {
checkNull((Object[]) fieldTypes);
assert !anyNull((Object[]) fieldTypes) : "rel types should not contain nulls";
return setType(tupleType(fieldTypes));
}

Expand All @@ -451,7 +452,7 @@ public Type relType(Object... fieldTypesAndLabels) {
* @return a list relation type
*/
public Type lrelType(Type... fieldTypes) {
checkNull((Object[]) fieldTypes);
assert !anyNull((Object[]) fieldTypes) : "lrel types should not contain nulls";
return listType(tupleType(fieldTypes));
}

Expand Down Expand Up @@ -487,8 +488,10 @@ public Type lrelType(Object... fieldTypesAndLabels) {
*/
public Type aliasType(TypeStore store, String name, Type aliased, Type... parameters)
throws FactTypeDeclarationException {
checkNull(store, name, aliased);
checkNull((Object[]) parameters);
assert !isNull(store) : "alias store should not be null";
assert !isNull(name) : "alias name should not be null";
assert !isNull(aliased) : "alias aliased should not be null";
assert !anyNull((Object[]) parameters): "alias parameters should not be null";

Type paramType;
if (parameters.length == 0) {
Expand All @@ -502,10 +505,10 @@ public Type aliasType(TypeStore store, String name, Type aliased, Type... parame

public Type aliasTypeFromTuple(TypeStore store, String name, Type aliased, Type params)
throws FactTypeDeclarationException {
checkNull(store);
checkNull(name);
checkNull(aliased);
checkNull(params);
assert !isNull(store) : "alias store should not be null";
assert !isNull(name) : "alias name should not be null";
assert !isNull(aliased) : "alias aliased should not be null";
assert !isNull(params): "alias params should not be null";

if (!isIdentifier(name)) {
throw new IllegalIdentifierException(name);
Expand Down Expand Up @@ -540,8 +543,9 @@ public Type nodeType() {
* @throws IllegalIdentifierException
*/
public Type abstractDataType(TypeStore store, String name, Type... parameters) throws FactTypeDeclarationException {
checkNull(store, name);
checkNull((Object[]) parameters);
assert !isNull(store) : "adt store should not be null";
assert !isNull(name) : "adt name should not be null";
assert !anyNull((Object[]) parameters) : "adt parameters should not contain a null";

Type paramType = voidType();
if (parameters.length != 0) {
Expand All @@ -552,7 +556,9 @@ public Type abstractDataType(TypeStore store, String name, Type... parameters) t
}

public Type abstractDataTypeFromTuple(TypeStore store, String name, Type params) throws FactTypeDeclarationException {
checkNull(store, name, params);
assert !isNull(store) : "adt store should not be null";
assert !isNull(name) : "adt name should not be null";
assert !isNull(params): "adt params should not be null";

if (!isIdentifier(name)) {
throw new IllegalIdentifierException(name);
Expand Down Expand Up @@ -589,7 +595,10 @@ public Type abstractDataTypeFromTuple(TypeStore store, String name, Type params)
* RedeclaredFieldNameException, RedeclaredConstructorException
*/
public Type constructorFromTuple(TypeStore store, Type adt, String name, Type tupleType) throws FactTypeDeclarationException {
checkNull(store, adt, name, tupleType);
assert !isNull(store) : "constructor store should not be null";
assert !isNull(adt): "constructor adt should not be null";
assert !isNull(name) : "constructor name should not be null";
assert !isNull(tupleType) : "constructor type should not be null";

if (!isIdentifier(name)) {
throw new IllegalIdentifierException(name);
Expand Down Expand Up @@ -658,7 +667,7 @@ public Type constructor(TypeStore store, Type nodeType, String name, Object... c
* @return a list type
*/
public Type listType(Type elementType) {
checkNull(elementType);
assert !isNull(elementType) : "list type should not be null";
return getFromCache(new ListType(elementType));
}

Expand All @@ -672,12 +681,13 @@ public Type listType(Type elementType) {
* @return a map type
*/
public Type mapType(Type key, Type value) {
checkNull(key, value);
assert !isNull(key) : "map key type should not be null";
assert !isNull(value) : "map key type should not be null";
return getFromCache(new MapType(key, value));
}

public Type mapTypeFromTuple(Type fields) {
checkNull(fields);
assert !isNull(fields) : "map tuple type should not be null";

if (fields.isBottom()) {
return mapType(voidType(), voidType());
Expand All @@ -697,7 +707,8 @@ public Type mapTypeFromTuple(Type fields) {
}

public Type mapType(Type key, String keyLabel, Type value, String valueLabel) {
checkNull(key, value);
assert !isNull(key) : "map key type should not be null";
assert !isNull(value) : "map key type should not be null";
if ((keyLabel != null && valueLabel == null) || (valueLabel != null && keyLabel == null)) {
throw new IllegalArgumentException("Key and value labels must both be non-null or null: " + keyLabel + ", "
+ valueLabel);
Expand All @@ -715,7 +726,8 @@ public Type mapType(Type key, String keyLabel, Type value, String valueLabel) {
* @return a parameter type
*/
public Type parameterType(String name, Type bound) {
checkNull(name, bound);
assert !isNull(name) : "parameter name should not be null";
assert !isNull(bound) : "parameter type should not be null";
return getFromCache(new ParameterType(name, bound));
}

Expand Down Expand Up @@ -764,15 +776,21 @@ public Type fromString(TypeStore store, Reader reader) throws IOException {
* @return a parameter type
*/
public Type parameterType(String name) {
checkNull(name);
assert !isNull(name) : "parameter name should not be null";
return getFromCache(new ParameterType(name));
}

private void checkNull(Object... os) {
for (int i = os.length - 1; i >= 0; i--) {
if (os[i] == null)
throw new NullTypeException();
private boolean anyNull(@Nullable Object... os) {
for (@Nullable Object o: os) {
if (isNull(o)) {
return true;
}
}
return false;
}

private boolean isNull(@Nullable Object os) {
return Objects.isNull(os);
}

/**
Expand All @@ -783,7 +801,7 @@ private void checkNull(Object... os) {
* @return true if the string is a valid identifier
*/
public boolean isIdentifier(String str) {
checkNull(str);
assert !isNull(str) : "str should not be null";

int len = str.length();
if (len == 0) {
Expand Down

0 comments on commit 7fdb016

Please sign in to comment.