-
Notifications
You must be signed in to change notification settings - Fork 753
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41917 from heshanpadmasiri/fix/rec-store-optional
Remove unnecessary casting for record field set
- Loading branch information
Showing
11 changed files
with
413 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bir/RecordDesugarTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org). | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.ballerinalang.test.bir; | ||
|
||
import org.ballerinalang.test.BCompileUtil; | ||
import org.testng.Assert; | ||
import org.testng.annotations.AfterClass; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
import org.wso2.ballerinalang.compiler.bir.emit.BIREmitter; | ||
import org.wso2.ballerinalang.compiler.bir.model.BIRNode; | ||
import org.wso2.ballerinalang.compiler.util.CompilerContext; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* This class contains unit tests to validate various desugaring/optimizations on records produce the expected BIR. | ||
* | ||
* @since 2201.9.0 | ||
*/ | ||
public class RecordDesugarTest { | ||
|
||
private BIREmitter birEmitter; | ||
private BCompileUtil.BIRCompileResult result; | ||
|
||
@BeforeClass | ||
public void setup() { | ||
birEmitter = BIREmitter.getInstance(new CompilerContext()); | ||
result = BCompileUtil.generateBIR("test-src/bir/record_desugar.bal"); | ||
} | ||
|
||
@Test(description = "Test record field set") | ||
public void testRecordFieldSet() { | ||
List<String> functions = Arrays.asList("setRequiredField", "setNillableField", "setOptionalField"); | ||
result.getExpectedBIR().functions.stream().filter(function -> functions.contains(function.name.value)) | ||
.forEach(this::assertFunctions); | ||
} | ||
|
||
private void assertFunctions(BIRNode.BIRFunction function) { | ||
String actual = BIREmitter.emitFunction(function, 0); | ||
String expected = null; | ||
try { | ||
expected = readFile(function.name.value); | ||
} catch (IOException e) { | ||
Assert.fail("Failed to read the expected BIR file for function: " + function.name.value, e); | ||
} | ||
Assert.assertEquals(actual, expected); | ||
} | ||
|
||
private String readFile(String name) throws IOException { | ||
// The files in the bir-dump folder are named with the function name and contain the expected bir dump for | ||
// the function | ||
Path filePath = Paths.get("src", "test", "resources", "test-src", "bir", "bir-dump", name).toAbsolutePath(); | ||
if (Files.exists(filePath)) { | ||
StringBuilder contentBuilder = new StringBuilder(); | ||
|
||
Stream<String> stream = Files.lines(filePath, StandardCharsets.UTF_8); | ||
stream.forEach(s -> contentBuilder.append(s).append("\n")); | ||
|
||
return contentBuilder.toString().trim(); | ||
} | ||
Assert.fail("Expected BIR file not found for function: " + name); | ||
return null; | ||
} | ||
|
||
@AfterClass | ||
public void tearDown() { | ||
result = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/setNillableField
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
setNillableField function() -> () { | ||
%0(RETURN) (); | ||
%1(LOCAL) R2; | ||
%2(TEMP) typeDesc<any|error>; | ||
%4(TEMP) string; | ||
%5(TEMP) int|(); | ||
%6(TEMP) int; | ||
%12(TEMP) (); | ||
|
||
bb0 { | ||
%2 = newType R2; | ||
%4 = ConstLoad x; | ||
%6 = ConstLoad 1; | ||
%5 = <int|()> %6; | ||
%1 = NewMap %2{%4:%5}; | ||
%6 = ConstLoad 2; | ||
%5 = <int|()> %6; | ||
%4 = ConstLoad x; | ||
%1[%4] = %5; | ||
%12 = ConstLoad 0; | ||
%5 = <int|()> %12; | ||
%4 = ConstLoad x; | ||
%1[%4] = %5; | ||
%0 = ConstLoad 0; | ||
GOTO bb1; | ||
} | ||
bb1 { | ||
return; | ||
} | ||
|
||
|
||
} |
28 changes: 28 additions & 0 deletions
28
tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/setOptionalField
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
setOptionalField function() -> () { | ||
%0(RETURN) (); | ||
%1(LOCAL) R3; | ||
%2(TEMP) typeDesc<any|error>; | ||
%4(TEMP) int; | ||
%6(TEMP) string; | ||
%7(TEMP) int|(); | ||
%8(TEMP) (); | ||
|
||
bb0 { | ||
%2 = newType R3; | ||
%1 = NewMap %2{}; | ||
%4 = ConstLoad 2; | ||
%6 = ConstLoad x; | ||
%1[%6] = %4; | ||
%8 = ConstLoad 0; | ||
%7 = <int|()> %8; | ||
%6 = ConstLoad x; | ||
%1[%6] = %7; | ||
%0 = ConstLoad 0; | ||
GOTO bb1; | ||
} | ||
bb1 { | ||
return; | ||
} | ||
|
||
|
||
} |
24 changes: 24 additions & 0 deletions
24
tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/setRequiredField
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
setRequiredField function() -> () { | ||
%0(RETURN) (); | ||
%1(LOCAL) R1; | ||
%2(TEMP) typeDesc<any|error{map<ballerina/lang.value:0.0.0:Cloneable>}>; | ||
%4(TEMP) string; | ||
%5(TEMP) int; | ||
|
||
bb0 { | ||
%2 = newType R1; | ||
%4 = ConstLoad x; | ||
%5 = ConstLoad 1; | ||
%1 = NewMap %2{%4:%5}; | ||
%5 = ConstLoad 2; | ||
%4 = ConstLoad x; | ||
%1[%4] = %5; | ||
%0 = ConstLoad 0; | ||
GOTO bb1; | ||
} | ||
bb1 { | ||
return; | ||
} | ||
|
||
|
||
} |
28 changes: 28 additions & 0 deletions
28
tests/jballerina-unit-test/src/test/resources/test-src/bir/record_desugar.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
type R1 record {| | ||
int x; | ||
|}; | ||
|
||
type R2 record {| | ||
int? x; | ||
|}; | ||
|
||
type R3 record {| | ||
int x?; | ||
|}; | ||
|
||
function setRequiredField() { | ||
R1 r1 = {x: 1}; | ||
r1.x = 2; | ||
} | ||
|
||
function setNillableField() { | ||
R2 r2 = {x: 1}; | ||
r2.x = 2; | ||
r2.x = (); | ||
} | ||
|
||
function setOptionalField() { | ||
R3 r3 = {}; | ||
r3.x = 2; | ||
r3.x = (); | ||
} |
Oops, something went wrong.