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

Remove casts to imported anonymous types and add a validation to disallow the same #28462

Conversation

MaryamZi
Copy link
Member

@MaryamZi MaryamZi commented Feb 6, 2021

Purpose

$title, as a temporary fix for #28262.

Not sure if we can write a test for either change.

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 MaryamZi added the Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. label Feb 6, 2021
@MaryamZi MaryamZi marked this pull request as ready for review February 7, 2021 03:30
@MaryamZi MaryamZi added this to the Ballerina Swan Lake - Alpha2 milestone Feb 8, 2021
BLangExpression expr = valueAccessExpr.expr;
// Since `$result$` is always a record (with a single `value` field), add a cast to `map<any|error>`
// to avoid casting to an anonymous type - https://github.com/ballerina-platform/ballerina-lang/issues/28262.
valueAccessExpr.expr = addConversionExprIfRequired(expr, symTable.mapAllType);
Copy link
Member

Choose a reason for hiding this comment

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

Out of curiosity, what happens if we don't have this conversion here? Issues in jvm codegen or does it get re-added somehow via desugaring the desugared constructs?

Copy link
Member Author

Choose a reason for hiding this comment

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

So the type of expr ($result$) here is the union of a record and nil - record {|T value;|}?. Although semantically it is invalid to do $result$.value in such a scenario, desugaring proceeds as a field access on a record union due to the check at https://github.com/ballerina-platform/ballerina-lang/blob/master/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java#L5171. And then at org.wso2.ballerinalang.compiler.bir.codegen.JvmInstructionGen#generateMapLoadIns we don't seem to be adding a cast so probably works as is there?

Copy link
Contributor

@KRVPerera KRVPerera left a comment

Choose a reason for hiding this comment

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

Better if we can add a positive test case which was failing previously with anonemous type cast.

@MaryamZi
Copy link
Member Author

MaryamZi commented Feb 8, 2021

Better if we can add a positive test case which was failing previously with anonemous type cast.

Any suggestions on how we can add such a test?

There are several conditions that needs to be met. For example if we consider using foreach with an array

  1. need to have two compatible lang.array versions where both have different names for the anonymous types
  2. need to build the JAR using the pack with one version and then run it using a pack with the other

@MaryamZi
Copy link
Member Author

MaryamZi commented Feb 8, 2021

Better if we can add a positive test case which was failing previously with anonemous type cast.

Any suggestions on how we can add such a test?

There are several conditions that needs to be met. For example if we consider using foreach with an array

  1. need to have two compatible lang.array versions where both have different names for the anonymous types
  2. need to build the JAR using the pack with one version and then run it using a pack with the other

Will go ahead and merge this for now, will add a test in a separate PR if we can figure out a way to test this.

@MaryamZi MaryamZi merged commit e4e4f6a into ballerina-platform:master Feb 8, 2021
@MaryamZi MaryamZi deleted the remove-casts-to-external-anon-types branch February 8, 2021 13:56
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
Development

Successfully merging this pull request may close these issues.

3 participants