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

Rewrite BUnionType's getMemberTypes() usages using SemTypes #43341

Open
wants to merge 28 commits into
base: nutcracker
Choose a base branch
from

Conversation

lochana-chathura
Copy link
Member

@lochana-chathura lochana-chathura commented Aug 30, 2024

Purpose

$subject

Fixes #43344
Fixes #43437

Approach

n/a

Samples

n/a

Remarks

n/a

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

@lochana-chathura lochana-chathura added Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Area/SemtypeIntegration Issue related to integrating semtype engine labels Aug 30, 2024
Copy link

codecov bot commented Aug 30, 2024

Codecov Report

Attention: Patch coverage is 91.78082% with 12 lines in your changes missing coverage. Please review.

Project coverage is 77.55%. Comparing base (ce35671) to head (ff06641).
Report is 1 commits behind head on nutcracker.

Files with missing lines Patch % Lines
...alang/compiler/semantics/analyzer/TypeChecker.java 79.31% 2 Missing and 4 partials ⚠️
...ballerinalang/compiler/desugar/ASTBuilderUtil.java 0.00% 0 Missing and 1 partial ⚠️
...2/ballerinalang/compiler/desugar/QueryDesugar.java 83.33% 0 Missing and 1 partial ⚠️
...mpiler/semantics/analyzer/ConstantTypeChecker.java 50.00% 0 Missing and 1 partial ⚠️
.../compiler/semantics/analyzer/SemanticAnalyzer.java 87.50% 0 Missing and 1 partial ⚠️
...alang/compiler/semantics/analyzer/SymbolEnter.java 91.66% 1 Missing ⚠️
...compiler/semantics/analyzer/TypeParamAnalyzer.java 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@               Coverage Diff                @@
##             nutcracker   #43341      +/-   ##
================================================
+ Coverage         77.47%   77.55%   +0.08%     
+ Complexity        58442    58128     -314     
================================================
  Files              3449     3449              
  Lines            219783   218626    -1157     
  Branches          28815    28377     -438     
================================================
- Hits             170269   169549     -720     
+ Misses            40047    39690     -357     
+ Partials           9467     9387      -80     

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

@lochana-chathura lochana-chathura force-pushed the semIntegration/cleanup3 branch 2 times, most recently from 6d652a5 to dfb9b06 Compare September 4, 2024 16:44
@lochana-chathura lochana-chathura force-pushed the semIntegration/cleanup3 branch 5 times, most recently from 9af942a to f8d8a47 Compare October 3, 2024 06:35
@lochana-chathura lochana-chathura force-pushed the semIntegration/cleanup3 branch 4 times, most recently from ad2e562 to f503052 Compare October 23, 2024 06:08
@lochana-chathura lochana-chathura force-pushed the semIntegration/cleanup3 branch 3 times, most recently from 81e9418 to 6b38208 Compare November 7, 2024 04:20
@lochana-chathura lochana-chathura changed the title Eliminate BUnionType's getMemberTypes() usages Rewrite BUnionType's getMemberTypes() usages using SemTypes Nov 7, 2024
@lochana-chathura lochana-chathura marked this pull request as ready for review November 7, 2024 07:04
Comment on lines 118 to 120
SemType otherTy = null;
for (SemType s : ((BUnionType) sourceType).getMemberSemTypes()) {
if (PredefinedType.NIL.equals(s)) {
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason why we can't do containsBasicType here? (since nil don't have subtypes)

Copy link
Member Author

@lochana-chathura lochana-chathura Nov 11, 2024

Choose a reason for hiding this comment

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

Fixed in e2ce145

int foundError = 0;
for (BType bType : ((BUnionType) sourceType).getMemberTypes()) {
if (bType.tag == TypeTags.ERROR) {
for (SemType s : ((BUnionType) sourceType).getMemberSemTypes()) {
Copy link
Member

Choose a reason for hiding this comment

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

If I understand this correctly what we are actually trying to check is if we have a is E where E is a error subtype and error part of a is a subtype of E. (In which case if a is a BError above type check passes so we just do instanceof check). If that is the case wouldn't it be easier to,

  1. Check target is isSubTypeSimple(error)
  2. Check SemType errorPart = intersect(sourceType, error); -> errorPart is not never
  3. Check if errorPart is a subtype of target

Copy link
Member Author

@lochana-chathura lochana-chathura Nov 11, 2024

Choose a reason for hiding this comment

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

Thanks. Fixed in 445ecc9

}

return true;
return !Core.isEmpty(types.typeCtx(), Core.diff(returnType.semType(), PredefinedType.ERROR));
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this same as !Core.isSubtyp(returnType.semType(), PredefinedType.ERROR))? Also in the previous code we had handled the case where returnType could be null don't we need to handle it here a well?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed the former. I think the null check we had previously is redundant/obsolete. No test for that. Hence, removed.

Screenshot 2024-11-11 at 18 15 07

* When the type is mutated we need to reset resolved semType.
*/
public void resetSemType() {
this.semType = null;
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we also clear the memberSemTypes as well?

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 repopulate the memberSemTypes here, whenever the semtype is null.

public void populateMemberSemTypes(boolean ignoreTypeIds) {
LinkedHashSet<SemType> memberSemTypes = new LinkedHashSet<>();
for (BType memberType : this.memberTypes) {
populateMemberSemTypes(memberType, memberSemTypes, ignoreTypeIds);
}
this.memberSemTypes = memberSemTypes;

Since ballerina-platform#35886 has been addressed, we should be able to remove the temporary shape check workaround.
Please refer to ballerina-platform#35872 (comment)
Previously ballerina `readonly` return type allowed for java void methods. However, `error?` was not allowed with java void. This inconsistency is due to type checking not recognizing the fact that readonly type does include the error type. The error given in the latter case too is wrong because error is optional.
Previously this was implemented with a temporary workaround which was inefficient as we recalculated the semtypes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/SemtypeIntegration Issue related to integrating semtype engine Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants