Skip to content

Commit

Permalink
Validate supported intersection type
Browse files Browse the repository at this point in the history
  • Loading branch information
prakanth97 committed Apr 2, 2024
1 parent b46c0ee commit 18a8490
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 5 deletions.
29 changes: 28 additions & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.8.6"
distribution-version = "2201.9.0-20240326-110600-aca0cc0c"

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -42,6 +42,26 @@ modules = [
{org = "ballerina", packageName = "jballerina.java", moduleName = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "lang.__internal"
version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.object"}
]

[[package]]
org = "ballerina"
name = "lang.array"
version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.__internal"}
]

[[package]]
org = "ballerina"
name = "lang.error"
Expand All @@ -51,6 +71,12 @@ dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "lang.object"
version = "0.0.0"
scope = "testOnly"

[[package]]
org = "ballerina"
name = "lang.value"
Expand All @@ -70,6 +96,7 @@ version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.array"},
{org = "ballerina", name = "lang.error"}
]
modules = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,16 @@ public void testComplexUnionTypeAsExpectedType() {
Assert.assertEquals(errorDiagnosticsList.get(1).diagnosticInfo().messageFormat(),
"unsupported union type: union type does not support multiple complex types");
}

@Test
public void testComplexUnionTypeAsMemberOfIntersection() {
DiagnosticResult diagnosticResult =
CompilerPluginTestUtils.loadPackage("sample_package_8").getCompilation().diagnosticResult();
List<Diagnostic> errorDiagnosticsList = diagnosticResult.diagnostics().stream()
.filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR))
.collect(Collectors.toList());
Assert.assertEquals(errorDiagnosticsList.size(), 1);
Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(),
"unsupported union type: union type does not support multiple complex types");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "jsondata_test"
name = "sample_8"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import ballerina/data.jsondata;

type UnionType record {|int a;|}|record {|string b;|};

type IntersectionType UnionType & readonly;

public function main() returns error? {
string str = string `{"a": 1, "b": "str"}`;
IntersectionType _ = check jsondata:parseString(str);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.ballerina.compiler.api.symbols.AnnotationAttachmentSymbol;
import io.ballerina.compiler.api.symbols.AnnotationSymbol;
import io.ballerina.compiler.api.symbols.ArrayTypeSymbol;
import io.ballerina.compiler.api.symbols.IntersectionTypeSymbol;
import io.ballerina.compiler.api.symbols.ModuleSymbol;
import io.ballerina.compiler.api.symbols.RecordFieldSymbol;
import io.ballerina.compiler.api.symbols.RecordTypeSymbol;
Expand Down Expand Up @@ -145,6 +146,7 @@ private void validateExpectedType(TypeSymbol typeSymbol, SyntaxNodeAnalysisConte
case ARRAY -> validateExpectedType(((ArrayTypeSymbol) typeSymbol).memberTypeDescriptor(), ctx);
case TUPLE -> validateTupleType((TupleTypeSymbol) typeSymbol, ctx);
case TYPE_REFERENCE -> validateExpectedType(((TypeReferenceTypeSymbol) typeSymbol).typeDescriptor(), ctx);
case INTERSECTION -> validateExpectedType(getRawType(typeSymbol), ctx);
}
}

Expand Down Expand Up @@ -179,7 +181,7 @@ private void validateUnionType(UnionTypeSymbol unionTypeSymbol, Optional<Locatio
boolean isNilOrErrorPresent = false;
List<TypeSymbol> memberTypeSymbols = unionTypeSymbol.memberTypeDescriptors();
for (TypeSymbol memberTypeSymbol : memberTypeSymbols) {
TypeSymbol referredSymbol = getReferredSymbol(memberTypeSymbol);
TypeSymbol referredSymbol = getRawType(memberTypeSymbol);
if (referredSymbol.typeKind() == TypeDescKind.NIL || referredSymbol.typeKind() == TypeDescKind.ERROR) {
isNilOrErrorPresent = true;
continue;
Expand Down Expand Up @@ -215,9 +217,22 @@ private boolean isSupportedUnionMemberType(TypeSymbol typeSymbol) {
}
}

private TypeSymbol getReferredSymbol(TypeSymbol typeSymbol) {
return typeSymbol.typeKind() == TypeDescKind.TYPE_REFERENCE ?
((TypeReferenceTypeSymbol) typeSymbol).typeDescriptor() : typeSymbol;
public static TypeSymbol getRawType(TypeSymbol typeDescriptor) {
if (typeDescriptor.typeKind() == TypeDescKind.INTERSECTION) {
return getRawType(((IntersectionTypeSymbol) typeDescriptor).effectiveTypeDescriptor());
}
if (typeDescriptor.typeKind() == TypeDescKind.TYPE_REFERENCE) {
TypeReferenceTypeSymbol typeRef = (TypeReferenceTypeSymbol) typeDescriptor;
if (typeRef.typeDescriptor().typeKind() == TypeDescKind.INTERSECTION) {
return getRawType(((IntersectionTypeSymbol) typeRef.typeDescriptor()).effectiveTypeDescriptor());

Check warning on line 227 in compiler-plugin/src/main/java/io/ballerina/lib/data/jsondata/compiler/JsondataTypeValidator.java

View check run for this annotation

Codecov / codecov/patch

compiler-plugin/src/main/java/io/ballerina/lib/data/jsondata/compiler/JsondataTypeValidator.java#L227

Added line #L227 was not covered by tests
}
TypeSymbol rawType = typeRef.typeDescriptor();
if (rawType.typeKind() == TypeDescKind.TYPE_REFERENCE) {
return getRawType(rawType);

Check warning on line 231 in compiler-plugin/src/main/java/io/ballerina/lib/data/jsondata/compiler/JsondataTypeValidator.java

View check run for this annotation

Codecov / codecov/patch

compiler-plugin/src/main/java/io/ballerina/lib/data/jsondata/compiler/JsondataTypeValidator.java#L231

Added line #L231 was not covered by tests
}
return rawType;
}
return typeDescriptor;
}

private void reportDiagnosticInfo(SyntaxNodeAnalysisContext ctx, Optional<Location> location,
Expand Down

0 comments on commit 18a8490

Please sign in to comment.