-
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
Conversation
Since it is not possible to call record field defaults individually atm, the original type is added as an included type to make its init method (that initiates the fields) get called.
recordTypeNode.initFunction = TypeDefBuilderHelper.createInitFunctionForRecordType(recordTypeNode, env, | ||
names, symTable); | ||
TypeDefBuilderHelper.addTypeDefinition(recordType, recordSymbol, recordTypeNode, env); | ||
|
||
if (applicableMappingType.tag == TypeTags.MAP) { | ||
if (applicableMappingType.tag == TypeTags.RECORD) { | ||
BRecordType applicableRecordType = (BRecordType) applicableMappingType; |
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
type Foo record {
string[] x = ["foo", "bar"];
};
// Panics now with `invalid value for record field 'x': expected value of type 'string[] & readonly', found 'string[]'`,
// since `x` is initialized using the mutable value from `Foo` before the mapping constructor's value.
Foo _ = {readonly x: [""]};
This does not panic in Beta3/Beta4 but will panic with this fix.
Related to #24077 in a way.
type Foo record {
int[] x = [];
};
Foo & readonly _ = {};
Also panics at runtime atm.
But if we don't fix this, defaults won't be used in cases like
import ballerina/io;
type Foo record {
int i;
string s = "default";
};
public function main() {
Foo f = {readonly i: 1};
io:println(f); // `{"i":1,"s":null}` and not `{"i":1,"s":"default"}`
}
@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.
Codecov Report
@@ Coverage Diff @@
## master #33767 +/- ##
============================================
- Coverage 74.06% 74.05% -0.01%
+ Complexity 44670 44666 -4
============================================
Files 3130 3130
Lines 174290 174302 +12
Branches 22453 22454 +1
============================================
- Hits 129087 129086 -1
- Misses 37562 37575 +13
Partials 7641 7641
Continue to review full report at Codecov.
|
Purpose
$title.
Fixes #33162
Check List