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

Add support for record literals with mapping binding patterns and fix using mapping-binding pattern within a list binding pattern against an open record #40283

Merged
merged 21 commits into from
Aug 2, 2024

Conversation

dulajdilshan
Copy link
Contributor

@dulajdilshan dulajdilshan commented Apr 24, 2023

Purpose

$title

Fixes #39324
Fixes #19804

Original PR: #40113 (invalidated)

Approach

Describe how you are implementing the solutions along with the design details.

Samples

Provide high-level details about the samples related to this feature.

Remarks

List any other known issues, related PRs, TODO items, or any other notes related to the PR.

Encountered the following issue

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

@MaryamZi
Copy link
Member

Please update the title to reflect what's actually being fixed.

@dulajdilshan dulajdilshan changed the title Fix/39324 Fix using mapping-binding pattern within a list binding pattern against an open record Apr 24, 2023
@codecov
Copy link

codecov bot commented Apr 24, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 77.33%. Comparing base (1922024) to head (3de0d3e).
Report is 94 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master   #40283      +/-   ##
============================================
- Coverage     77.35%   77.33%   -0.03%     
- Complexity    58436    58545     +109     
============================================
  Files          3454     3460       +6     
  Lines        219598   219990     +392     
  Branches      28870    28914      +44     
============================================
+ Hits         169880   170136     +256     
- Misses        40330    40449     +119     
- Partials       9388     9405      +17     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@MaryamZi MaryamZi added the Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. label May 15, 2023
@dulajdilshan
Copy link
Contributor Author

dulajdilshan commented Jun 8, 2023

Experiencing an NPE in ballerina runtime for the following

public function main() {
  int a;
  int b;
  [{a, b}] = [{a: 10, b: 11}];
}

Working on fixing it

@github-actions
Copy link

This PR has been open for more than 15 days with no activity. This will be closed in 3 days unless the stale label is removed or commented.

@dulajdilshan
Copy link
Contributor Author

dulajdilshan commented Jun 28, 2023

Experiencing an NPE in ballerina runtime for the following

public function main() {
  int a;
  int b;
  [{a, b}] = [{a: 10, b: 11}];
}

Working on fixing it

This scenario seems to be fixe via #37184

@dulajdilshan
Copy link
Contributor Author

This scenario seems to be fixed via #37184

Had a chat with @MaryamZi and @chiranSachintha. It seems this scenario needs to be disallowed

@github-actions
Copy link

This PR has been open for more than 15 days with no activity. This will be closed in 3 days unless the stale label is removed or commented.

@github-actions github-actions bot added the Stale label Jul 13, 2023
@github-actions
Copy link

Closed PR due to inactivity for more than 18 days.

@github-actions github-actions bot closed this Jul 17, 2023
@MaryamZi
Copy link
Member

Please fix the build/test failures.

name: fName,
age,
married,
...theMap
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

theMap isn't asserted

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's intentional. We don't need to assert it. We have asserted it in the other tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can remove this test. It seems this test is not necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's intentional. We don't need to assert it. We have asserted it in the other tests.

Why can't we just assert it? We either need to assert it or remove that binding.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed via 6568a25

@MaryamZi
Copy link
Member

MaryamZi commented Jul 3, 2024

Still not supported with typed binding patterns so I would update the title and add a comment on #19804 saying this is only enabled for destructuring assignments in this PR. Ideally, it should be straightforward to support the same with typed binding patterns too.

@@ -2199,11 +2199,6 @@ public void visit(BLangSimpleVariableDef varDefNode, AnalyzerData data) {

@Override
public void visit(BLangRecordVariableDef varDefNode, AnalyzerData data) {
// TODO: 10/18/18 Need to support record literals as well
if (varDefNode.var.expr != null && varDefNode.var.expr.getKind() == RECORD_LITERAL_EXPR) {
dlog.error(varDefNode.pos, DiagnosticErrorCode.INVALID_LITERAL_FOR_TYPE, "record binding pattern");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this DiagnosticErrorCode removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed via 08f5e57c

@@ -2199,11 +2199,6 @@ public void visit(BLangSimpleVariableDef varDefNode, AnalyzerData data) {

@Override
public void visit(BLangRecordVariableDef varDefNode, AnalyzerData data) {
// TODO: 10/18/18 Need to support record literals as well
if (varDefNode.var.expr != null && varDefNode.var.expr.getKind() == RECORD_LITERAL_EXPR) {
dlog.error(varDefNode.pos, DiagnosticErrorCode.INVALID_LITERAL_FOR_TYPE, "record binding pattern");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where have the tests been added for this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noticed that they've been added in RecordVariableReferenceTest.java. That's not the correct class. Should go in RecordVariableDefinitionTest.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack


var {country, ...rest} = {country: "USA", zipCode: 80001};
assertEquality("USA", country);
assertEquality({zipCode: 80001}, rest);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add more tests including where the member binding patterns are in turn structured binding patterns.

Also add negative tests for unspecified fields, optional field binding, etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dulajdilshan can you please point to the negative tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in edebb78

Copy link
Member

@MaryamZi MaryamZi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Title needs to be changed too.

assertEquality("Alabama", state);
assertEquality(35004, postalCode);

record {|string street; float...;|} {street, ...coordinates} = {street: "Highway 61", "lat": 37.30, "lon": -90.40};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add for when some of the required fields are bound as rest.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in edebb78

@dulajdilshan dulajdilshan changed the title Add support for record literals within record destructuring statement and fix using mapping-binding pattern within a list binding pattern against an open record Add support for record literals with mapping binding patterns and fix using mapping-binding pattern within a list binding pattern against an open record Jul 24, 2024
int b;
int c;
[{a, b}]= l1;
[int, [record {int b?;}]] l2= [1, [{b: 1}]];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[int, [record {int b?;}]] l2= [1, [{b: 1}]];
[int, [record {int b?;}]] l2 = [1, [{b: 1}]];

Fix everywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed via 3de0d3e

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use underscores for file names.

Also proper terms for new files/code - e.g., record_bp_in_list_bp_destructuring_assignment_negative.bal

@dulajdilshan dulajdilshan merged commit 1caf979 into ballerina-platform:master Aug 2, 2024
17 of 18 checks passed
@dulajdilshan dulajdilshan deleted the fix/39324 branch August 2, 2024 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/Compiler Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times.
Projects
None yet
4 participants