-
Notifications
You must be signed in to change notification settings - Fork 758
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
Fix defaults from the applicable contextually-expected type not being used when a mapping constructor has readonly
fields
#33767
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
52 changes: 52 additions & 0 deletions
52
...test/java/org/ballerinalang/test/bala/record/ReadOnlyMappingConstructorFieldBalaTest.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,52 @@ | ||
/* | ||
* Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. | ||
* | ||
* WSO2 Inc. 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.bala.record; | ||
|
||
import org.ballerinalang.test.BCompileUtil; | ||
import org.ballerinalang.test.BRunUtil; | ||
import org.ballerinalang.test.CompileResult; | ||
import org.testng.annotations.AfterClass; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* Bala test for readonly fields in mapping constructor expressions. | ||
* | ||
* @since 2.0.0 | ||
*/ | ||
public class ReadOnlyMappingConstructorFieldBalaTest { | ||
|
||
private CompileResult result; | ||
|
||
@BeforeClass | ||
public void setup() { | ||
BCompileUtil.compileAndCacheBala("test-src/bala/test_projects/test_project"); | ||
result = BCompileUtil.compile("test-src/record/mapping_constructor_with_readonly_fields_in_bala.bal"); | ||
} | ||
|
||
@Test | ||
public void testDefaultValueFromCETBeingUsedWithReadOnlyFieldsInTheMappingConstructor() { | ||
BRunUtil.invoke(result, "testDefaultValueFromCETBeingUsedWithReadOnlyFieldsInTheMappingConstructor"); | ||
} | ||
|
||
@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
21 changes: 21 additions & 0 deletions
21
...t/src/test/resources/test-src/bala/test_projects/test_project/modules/records/records.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,21 @@ | ||
// Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. | ||
// | ||
// WSO2 Inc. 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. | ||
|
||
public type Corge record {| | ||
string a = "hello"; | ||
string b = "world"; | ||
string[] c = <readonly> ["x", "y"]; | ||
|}; |
47 changes: 47 additions & 0 deletions
47
...t/src/test/resources/test-src/record/mapping_constructor_with_readonly_fields_in_bala.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,47 @@ | ||
// Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. | ||
// | ||
// WSO2 Inc. 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. | ||
|
||
import testorg/foo.records; | ||
|
||
function testDefaultValueFromCETBeingUsedWithReadOnlyFieldsInTheMappingConstructor() { | ||
records:Corge d = {readonly b: "ballerina"}; | ||
string[] & readonly e = ["x", "y"]; | ||
assertEquality({a: "hello", b: "ballerina", c: e}, d); | ||
assertTrue(d is record {|string a; readonly string b; string[] c;|}); | ||
assertFalse(d is record {|string a; readonly string b; readonly string[] c;|}); | ||
|
||
records:Corge f = {readonly b: "val", readonly c: ["a", "b", "c"]}; | ||
assertEquality({a: "hello", b: "val", c: <readonly> ["a", "b", "c"]}, f); | ||
assertTrue(f is record {|string a; readonly string b; string[] c;|}); | ||
assertTrue(f is record {|string a; readonly string b; readonly string[] c;|}); | ||
assertFalse(f is record {|readonly string a; readonly string b; readonly string[] c;|}); | ||
} | ||
|
||
function assertTrue(anydata actual) { | ||
assertEquality(true, actual); | ||
} | ||
|
||
function assertFalse(anydata actual) { | ||
assertEquality(false, actual); | ||
} | ||
|
||
function assertEquality(anydata expected, anydata actual) { | ||
if expected == actual { | ||
return; | ||
} | ||
|
||
panic error("expected '" + expected.toString() + "', found '" + actual.toString() + "'"); | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it is not possible to call record field default closures individually atm, the fix done for the time-being is adding the original type as an included type to make its init method (that initiates the fields) get called.
However this will cause issues for something like
This does not panic in Beta3/Beta4 but will panic with this fix.
Related to #24077 in a way.
Also panics at runtime atm.
But if we don't fix this, defaults won't be used in cases like
@hasithaa, thoughts please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for now. will @chiranSachintha fix for closures address this? I guess not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to introduce similar closures for record default values, right? Created #33771 to track fixing it. #32012 also depends on it.
Created #33773 to track fixing this specific case.