Comments (4)
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.
from ballerina-lang.
@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.
@chiranSachintha @HindujaB any update on this?
from ballerina-lang.
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 ofint
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)
- [Task]: Investigate into the ballerina library level 5 failure for PR #43303 HOT 3
- [Task]: Fix failing langlib tests for Java 21 migration
- [Task]: Refactor ballerina integration tests
- [Task]: Fix failing integration tests for Java 21 migration
- [Task]: Implement Java 21 Virtual threads based Runtime Concurrency model
- [Improvement]: Properly distinguish between type descriptor and type in runtime HOT 1
- [Bug]: Getting `missing single backtick token` warning for the doc comment containing backticks
- Lang artifacts are not publishing to the local Maven repository in PR builds HOT 1
- [Bug]: Getting NPE when accessing a field with a default value which is generated from rest binding with the optional field HOT 3
- [Bug]: Regular expression with `toJson` method panics HOT 1
- [Bug]: Compiler crash in a nested query with innermost query having inline record definition with default value
- [Bug]: The compiler crashes when a variable is passed dynamically to a TCP connection service initializer
- [Task]: Publish traces to DataDog
- [Bug]: Intermittent test failures in the daily full build pipeline HOT 1
- [Bug]: decimal:fromString() panics HOT 1
- [Bug]: Update document code action not available for undocumented parameter and return value
- [Bug]: Re-enable the LS test cases that are failing intermittently
- [Bug]: Re-enable jBallerina test cases that are failing intermittently
- [Improvement]: Add jetbrains annotations to the classpath dependencies for `@NotNull` and `@Nullable` annotations HOT 1
- [Task]: Anaylze the vs-code slowness in a multi project scenario HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ballerina-lang.