"null" is now a valid assignment/argument type

This commit is contained in:
bigguy345 committed 2025-12-29 02:12:12 +02:00
1 parent 595efb529c
commit 2ab48129b3
4 files changed
+21 -5

No files matched your search

@@ -2277,7 +2277,7 @@ public class ScriptDocument {
// Null literal
if (expr.equals("null")) {
return null; // null is compatible with any reference type
return TypeInfo.unresolved("null", "<null>"); // null is compatible with any reference type
}
// Numeric literals with precision checking
@@ -2687,6 +2687,11 @@ public class ScriptDocument {
ExpressionTypeResolver resolver = new ExpressionTypeResolver(context);
TypeInfo result = resolver.resolve(expr);
// Special case: null literal type is "unresolved" but valid
if (result != null && "<null>".equals(result.getFullName())) {
return result;
}
// If the expression resolver couldn't resolve it, fall back to simple resolution
if (result == null || !result.isResolved()) {
// Try the simple path in case the operator detection was a false positive
@@ -2712,7 +2717,7 @@ public class ScriptDocument {
return TypeInfo.fromPrimitive("boolean");
}
if ("null".equals(name)) {
return null;
return TypeInfo.unresolved("null", "<null>");
}
// Try to resolve as a variable
@@ -59,7 +59,9 @@ public abstract class ExpressionNode {
public static class NullLiteralNode extends ExpressionNode {
public NullLiteralNode(int start, int end) { super(start, end); }
public TypeInfo resolveType(TypeResolverContext resolver) { return null; }
public TypeInfo resolveType(TypeResolverContext resolver) {
return TypeInfo.unresolved("null", "<null>");
}
}
public static class IdentifierNode extends ExpressionNode {
@@ -51,7 +51,7 @@ public class ExpressionTypeResolver {
if (node instanceof ExpressionNode.BooleanLiteralNode) return TypeInfo.fromPrimitive("boolean");
if (node instanceof ExpressionNode.CharLiteralNode) return TypeInfo.fromPrimitive("char");
if (node instanceof ExpressionNode.StringLiteralNode) return TypeInfo.fromClass(String.class);
if (node instanceof ExpressionNode.NullLiteralNode) return null;
if (node instanceof ExpressionNode.NullLiteralNode) return TypeInfo.unresolved("null", "<null>");
if (node instanceof ExpressionNode.IdentifierNode) {
return context.resolveIdentifier(((ExpressionNode.IdentifierNode) node).getName());
@@ -132,7 +132,7 @@ public class ExpressionTypeResolver {
return new ExpressionNode.TypeResolverContext() {
public TypeInfo resolveIdentifier(String name) {
if ("true".equals(name) || "false".equals(name)) return TypeInfo.fromPrimitive("boolean");
if ("null".equals(name)) return null;
if ("null".equals(name)) return TypeInfo.unresolved("null", "<null>");
return null;
}
public TypeInfo resolveMemberAccess(TypeInfo targetType, String memberName) { return null; }
@@ -144,6 +144,15 @@ public class TypeRules {
if (thenType == null && elseType == null) return null;
if (thenType == null) return elseType;
if (elseType == null) return thenType;
// Handle null literal type
boolean thenIsNull = "<null>".equals(thenType.getFullName());
boolean elseIsNull = "<null>".equals(elseType.getFullName());
if (thenIsNull && elseIsNull) return thenType; // both null -> null
if (thenIsNull) return elseType; // null : T -> T
if (elseIsNull) return thenType; // T : null -> T
if (!thenType.isResolved()) return elseType.isResolved() ? elseType : null;
if (!elseType.isResolved()) return thenType;