Skip to content
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 2 commits into from
Nov 12, 2021

Conversation

MaryamZi
Copy link
Member

Purpose

$title.

Fixes #33162

Check List

  • Read the Contributing Guide
  • Updated Change Log
  • Checked Tooling Support (#)
  • Added necessary tests
    • Unit Tests
    • Spec Conformance Tests
    • Integration Tests
    • Ballerina By Example Tests
  • Increased Test Coverage
  • Added necessary documentation
    • API documentation
    • Module documentation in Module.md files
    • Ballerina By Examples

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.
@MaryamZi MaryamZi added the Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. label Nov 11, 2021
@MaryamZi MaryamZi added this to the Ballerina Swan Lake - Beta5 milestone Nov 11, 2021
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;
Copy link
Member Author

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?

Copy link
Contributor

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.

Copy link
Member Author

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.

@codecov
Copy link

codecov bot commented Nov 11, 2021

Codecov Report

Merging #33767 (72dc954) into master (b7c30a6) will decrease coverage by 0.00%.
The diff coverage is 95.45%.

Impacted file tree graph

@@             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              
Impacted Files Coverage Δ
...alang/compiler/semantics/analyzer/TypeChecker.java 87.06% <92.30%> (+0.03%) ⬆️
...llerinalang/compiler/util/ImmutableTypeCloner.java 91.03% <100.00%> (-0.15%) ⬇️
...lerinalang/compiler/util/TypeDefBuilderHelper.java 89.91% <100.00%> (+0.63%) ⬆️
...runtime/internal/scheduling/WorkerDataChannel.java 84.71% <0.00%> (-4.46%) ⬇️
.../ballerina/runtime/internal/scheduling/Strand.java 79.89% <0.00%> (-2.58%) ⬇️
...alang/langserver/diagnostic/DiagnosticsHelper.java 72.91% <0.00%> (-1.05%) ⬇️
.../compiler/bir/codegen/interop/JMethodResolver.java 78.59% <0.00%> (-0.27%) ⬇️
...viders/context/ListenerDeclarationNodeContext.java 74.23% <0.00%> (+0.61%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b7c30a6...72dc954. Read the comment docs.

@MaryamZi MaryamZi merged commit 6c96b7a into ballerina-platform:master Nov 12, 2021
@MaryamZi MaryamZi deleted the fix-33162 branch November 12, 2021 02:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times.
Projects
None yet
2 participants