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

[Master] Fix issues when there is union type in the select clause #40835

Merged
merged 16 commits into from
Aug 17, 2023

Conversation

LakshanWeerasinghe
Copy link
Contributor

Purpose

$subject

Fixes #40013
Fixes #40012

Related PRs #40271, #40726

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.

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

@SasinduDilshara
Copy link
Contributor

Found the issue #40974, which is related to the focused task in this PR, but this issue not happen with this PR, it is already happen in update 6 as well.

@codecov
Copy link

codecov bot commented Jul 11, 2023

Codecov Report

Patch coverage: 95.38% and no project coverage change.

Comparison is base (c55bb5f) 76.44% compared to head (6185bb3) 76.45%.
Report is 11 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##             master   #40835   +/-   ##
=========================================
  Coverage     76.44%   76.45%           
- Complexity    52244    52273   +29     
=========================================
  Files          2863     2863           
  Lines        197207   197280   +73     
  Branches      25628    25648   +20     
=========================================
+ Hits         150761   150836   +75     
+ Misses        38140    38136    -4     
- Partials       8306     8308    +2     
Files Changed Coverage Δ
.../compiler/semantics/analyzer/QueryTypeChecker.java 88.74% <94.17%> (+0.69%) ⬆️
...compiler/semantics/analyzer/IsolationAnalyzer.java 85.62% <100.00%> (+0.12%) ⬆️
...alang/compiler/semantics/analyzer/TypeChecker.java 92.20% <100.00%> (+<0.01%) ⬆️

... and 5 files with indirect coverage changes

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

@LakshanWeerasinghe
Copy link
Contributor Author

Found the issue #40974, which is related to the focused task in this PR, but this issue not happen with this PR, it is already happen in update 6 as well.

Since this has occurred previously will be addressed in another PR.

@@ -322,89 +323,149 @@ public BType resolveQueryType(SymbolEnv env, BLangExpression selectExp, BType ta
}
}

Copy link
Member

Choose a reason for hiding this comment

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

For

type Foo record {
    Baz[] a;
};

type Bar record {
    boolean[]|float[] b;
};

type Baz record {
    string c;
};

type Quux record {
    string c;
    int d?;
};

type Qux record {
    string c;
    int d;
};

function transform(Foo foo) returns Bar => {
    b: from var _ in foo.a
        select {
           c: "str"
        }
};

there seems to be an extra error now.

a type compatible with mapping constructor expressions not found in type 'boolean'

Can we avoid this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This error is there even before the fix. This error comes when we try to type check the select clause againsts the expected member type of the query result.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will try to find a proper solution to remove this misleading diagnostic message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since this error is expected logging is correct. The missing part was the error should be logged for all expected types. In this case a type compatible with mapping constructor expressions not found in type 'float' was missing. Added it in #6a7b01.

Copy link
Member

Choose a reason for hiding this comment

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

What are all the errors for this sample now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ambiguous type '(Baz|Qux)' will be the only error.

@github-actions
Copy link

github-actions bot commented Aug 5, 2023

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 Aug 5, 2023
@gimantha gimantha added Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. and removed Stale labels Aug 6, 2023
@@ -141,6 +145,65 @@ public void testQueryActionOrExprSemanticsNegative() {
validateError(negativeResult, i++, "action invocation as an expression not allowed here", 315, 23);
validateError(negativeResult, i++, "incompatible types: expected 'int', found 'other'", 316, 21);
validateError(negativeResult, i++, "action invocation as an expression not allowed here", 320, 50);
validateError(negativeResult, i++, "incompatible types: expected '(T3[]|T4[])', found '(T3|T4)[]'",
339, 13);
validateError(negativeResult, i++, "incompatible types: expected 'T3', " +
Copy link
Member

Choose a reason for hiding this comment

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

Why we should expect this error?
If we consider a normal ternary expression this is allowed

public function foo(string str) {
    T3|T4 _ = str == "" ? <T3>{
            str: ""
        } : <T4>{
            foo: false
        };
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In a normal expression this is correct. But In here we log the error inside the select clause. In the L.H.S it is expecting a T3[]|T4[]. So select clause type being a T3|T4 is incorrect.

Initially we agreed to log a single error for the whole query. But when reviewing this PR @MaryamZi pointed out a #case where we needed type check the select clause as well.

@@ -322,89 +323,149 @@ public BType resolveQueryType(SymbolEnv env, BLangExpression selectExp, BType ta
}
}

Copy link
Member

Choose a reason for hiding this comment

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

What are all the errors for this sample now?

Comment on lines 465 to 466
checkExpr(selectExp, env, expType, data);
selectExp.typeChecked = false;
Copy link
Member

Choose a reason for hiding this comment

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

Can't we clone and do this instead? Are we sure there's nothing else that needs to be reset?

To avoid such issues and side-effects we usually clone and type check (when checking for compatibility). Check mapping constructor type checking against unions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated in commit.

checkExpr(nodeCloner.cloneNode(selectExp), env, expType, data);
}
});
selectExp.typeChecked = true;
Copy link
Member

Choose a reason for hiding this comment

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

At this level selectExp hasn't been typeChecked isn't it? Is it correct to mark it as type-checked?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. There are case that can be where selectExpr not being type checked.

Copy link
Contributor Author

@LakshanWeerasinghe LakshanWeerasinghe Aug 15, 2023

Choose a reason for hiding this comment

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

Updated in 30e430.


function transform(T1 t1) returns T2 => {
t3OrT4: from var t3sItem in t1.t3s
select t3sItem.str == ""
Copy link
Member

Choose a reason for hiding this comment

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

This specific ternary expression is not correct. Just try running just that sample alone

@pcnfernando pcnfernando merged commit a1902ad into ballerina-platform:master Aug 17, 2023
17 checks passed
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
5 participants