Coder Social home page Coder Social logo

Comments (4)

HindujaB avatar HindujaB commented on September 25, 2024

This happens due to an unchecked cast in the generated code.
The BIR of the above code is as follows,

 bb2 {
        %19 = ConstLoad String;
        %21 = newType int|error;
        %26 = get(%1, %19, %21) -> bb3;
    }
    bb3 {
        %17 = <int|error> %26; // Compiler added type cast expression
        %27 = %17 is int;
        %27? bb4 : bb6;
    }

This is generated as an unchecked cast due to the checkTypes boolean is set to false at compilation. Therefore, it fails at the next line (int i = value + 1;).

Seems like we need to handle the union-type conversions separately when adding type-conversion.
Screenshot 2024-05-21 at 09 43 40

from ballerina-lang.

MaryamZi avatar MaryamZi commented on September 25, 2024

@chiranSachintha, @HindujaB, if this isn't dependently-typed, does it result in an error at

int|error value = foo.get("String"); 

Anyway, IIRC, at least with dependently-typed functions, we expect the developer to ensure they return the value belonging to the expected type. For example, the following is valid code, and a cast (even if added) won't/can't identify the issue here.

typedesc<anydata> td = int;
anydata x = foo.get("String", td);
public class Foo {
    private final map<any> attributes = {};

    public function set(string 'key, any value) {
        self.attributes['key] = value;
    }

    function get(string str, typedesc returnType = <>) returns returnType = @java:Method {
        'class: "io.ballerina.Sample"
    } external;
}

Foo foo = new;
foo.set("String", "Ballerina"); // Setting a string value in the map.

from ballerina-lang.

MaryamZi avatar MaryamZi commented on September 25, 2024

@chiranSachintha @HindujaB any update on this?

from ballerina-lang.

HindujaB avatar HindujaB commented on September 25, 2024

The issue only happens for dependently typed functions, because if it is a function with a specific type the check cast is called at the return of that function.

    function get(string key) returns int|error = @java:Method {
        'class: "org.ballerinalang.nativeimpl.jvm.runtime.api.tests.Async"
    } external; 

I agree that we expect the user to provide a value with the expected type for dependently typed functions.
In both cases, it has to fail with a runtime type checking error. But, we need to discuss in which statement it should fail.

IMO, we have to fail at the int|error assignment for the first case as the returned RHS value is not that type.
To identify such cases at code generation, currently, we use the 'checkTypes` boolean. @chiranSachintha is there any way to identify these cases?

int|error value = foo.get("String"); 

For anydata that is not the case since int is a subtype ofanydata.

  • Note : Apart from this, the is check of int does not fail because we optimize the type checking a value with union of error to a non error check.
int|error x = ...;
if (x is int) { 
// refactored into if !(x is error)

}

from ballerina-lang.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.