From 4b9cecf8beebb345eaa07ee9b9c09fe4d096d0c8 Mon Sep 17 00:00:00 2001 From: Kavindu Gimhan Zoysa Date: Tue, 19 Apr 2022 21:13:32 +0530 Subject: [PATCH 1/4] Add conformance tests for trap-expr --- conformance/lang/expressions/trap_expr.balt | 252 ++++++++++++++++++++ 1 file changed, 252 insertions(+) create mode 100644 conformance/lang/expressions/trap_expr.balt diff --git a/conformance/lang/expressions/trap_expr.balt b/conformance/lang/expressions/trap_expr.balt new file mode 100644 index 00000000..434475f5 --- /dev/null +++ b/conformance/lang/expressions/trap_expr.balt @@ -0,0 +1,252 @@ +Test-Case: output +Description: Test trap-expr when the expression is a nil-literal. +Labels: trap-expr, error-type, null, nil-literal, is-expr + +function init() { + ()|error t1 = trap (); + io:println(t1 is ()); // @output true + + error? t2 = trap null; + io:println(t2 is ()); // @output true +} + +Test-Case: output +Description: Test trap-expr when the expression is a boolean-literal. +Labels: trap-expr, error-type, boolean, boolean-literal + +function init() { + boolean|error t = trap false; + io:println(t); // @output false +} + +Test-Case: output +Description: Test trap-expr when the expression is a numeric-literal. +Labels: trap-expr, error-type, int, float, decimal + +function init() { + int|error t1 = trap 2; + io:println(t1); // @output 2 + float|error t2 = trap 2e3f; + io:println(t2); // @output 2000.0 + decimal|error t3 = trap 2e355d; + io:println(t3); // @output 2E+355 +} + +Test-Case: output +Description: Test trap-expr when the expression is a string-literal. +Labels: trap-expr, string + +function init() { + string|error t1 = trap "Hello"; + io:println(t1); // @output Hello + t1 = trap "Hello\tWorld"; + io:println(t1); // @output Hello World +} + +Test-Case: output +Description: Test trap-expr when the expression is a byte-array-literal. +Labels: trap-expr, error-type, byte, byte-array-literal + +function init() { + byte[]|error t1 = trap base16 `aeeecdefabcd12345567888822`; + io:println(t1); // @output [174,238,205,239,171,205,18,52,85,103,136,136,34] + t1 = base64 `aGVsbG8gYmFsbGVyaW5hICEhIQ==`; + io:println(t1); // @output [104,101,108,108,111,32,98,97,108,108,101,114,105,110,97,32,33,33,33] +} + +Test-Case: output +Description: Test trap-expr when the expression is a string-template-expr. +Labels: trap-expr, error-type, string-template-expr + +function init() { + string|error t1 = trap string `Hello World!`; + io:println(t1); // @output Hello World! + string s = string `Hello World!`; + string|error t2 = string `${s}`; + io:println(t2); // @output Hello World! +} + +Test-Case: output +Description: Test trap-expr when the expression is a xml-template-expr. +Labels: trap-expr, error-type, xml-template-expr + +function init() { + xml|error t1 = trap xml `Hello!`; + io:println(t1); // @output Hello! +} + +Test-Case: output +Description: Test trap-expr when the expression is a list-constructor-expr. +Labels: trap-expr, error-type, int, string, union-type, list-constructor-expr + +function init() { + int[]|error t1 = trap [1, 2, 3, 4]; + io:println(t1); // @output [1,2,3,4] + (int|string)[]|error t2 = trap [1, "2", 3, "4"]; + io:println(t2); // @output [1,"2",3,"4"] +} + +Test-Case: output +Description: Test trap-expr when the expression is a list-constructor-expr. +Labels: trap-expr, error-type, int, string, union-type, list-constructor-expr, type-cast-expr, function-call-expr + +function foo() returns int|string { + return "2"; +} + +function init() { + int[]|error t = trap [1, 2, foo(), 4]; + io:println(t); // @output error("{ballerina}TypeCastError",message="incompatible types: 'string' cannot be cast to 'int'") +} + +Test-Case: output +Description: Test trap-expr when the expression is a list-constructor-expr. +Labels: trap-expr, error-type, int, string, union-type, table-constructor-expr, multiplicative-expr + +function init() { + (table key(name))|error tbl1 = trap table key(name) [ + {name: "John Doe", id: 9223372036854775807*2} + ]; + io:println(tbl1); // @output error("{ballerina}NumberOverflow",message="int range overflow") +} + +Test-Case: output +Description: Test trap-expr when the expression is a object-constructor-expr. +Labels: trap-expr, error-type, object-constructor-expr, method-call-expr, panic + +function init() { + object { function bar(); } obj = object { + error? i = (); + function bar() { + self.i = self.foo(); + } + function foo() { + panic error("msg"); + } + }; + error? e = trap obj.bar(); + io:println(e); // @output error("msg") +} + +Test-Case: output +Description: Test trap-expr when the expression is a new-expr. +Labels: trap-expr, error-type, new-expr, method-call-expr, panic, error-constructor-expr + +class Obj { + function bar() { + panic error("msg"); + } +} + +function init() { + Obj obj = new(); + error? t = trap obj.bar(); + io:println(t); // @output error("msg") +} + +Test-Case: output +Description: Test trap-expr when the expression is a member-access-expr. +Labels: trap-expr, error-type, int, array-type, member-access-expr + +function init() { + int[] i = []; + io:println(trap i[3]); // @output error("{ballerina/lang.array}IndexOutOfRange",message="array index out of range: index: 3, size: 0") +} + +Test-Case: output +Description: Test trap-expr when the expression is a function-call-expr. +Labels: trap-expr, error-type, int, function-call-expr, array-type, union-type, error-constructor-expr + +function foo() returns int { + int[] i = []; + return i[3]; +} + +function bar() { + panic error("msg"); +} + +function init() { + int|error t1 = trap foo(); + io:println(t1); // @output error("{ballerina/lang.array}IndexOutOfRange",message="array index out of range: index: 3, size: 0") + error? t2 = trap bar(); + io:println(t2); // @output error("msg") +} + +Test-Case: output +Description: Test trap-expr when the expression is a type-cast-expr. +Labels: trap-expr, error-type, int, string, function-call-expr, union-type, type-cast-expr + +function foo() returns int|string { + return "str"; +} + +function init() { + int|error t1 = trap foo(); + io:println(t1); // @output error("{ballerina}TypeCastError",message="incompatible types: 'string' cannot be cast to 'int'") +} + +Test-Case: output +Description: Test trap-expr when the expression is a unary-logical-expr. +Labels: trap-expr, error-type, int, boolean, function-call-expr, union-type + +function foo() returns int|boolean { + return 23; +} + +function init() { + boolean|error t1 = trap !foo(); + io:println(t1); // @output error("{ballerina}TypeCastError",message="incompatible types: 'int' cannot be cast to 'boolean'") +} + +Test-Case: output +Description: Test trap-expr when the expression is a multiplicative-expr. +Labels: trap-expr, error-type, int, boolean, function-call-expr, multiplicative-expr + +function foo() returns int { + return 0; +} + +function init() { + int|error t1 = trap 2 / foo(); + io:println(t1); // @output error("{ballerina}DivisionByZero",message=" / by zero") +} + +Test-Case: output +Description: Test trap-expr when the expression is a trap-expr. +Labels: trap-expr, error-type, int, boolean, function-call-expr, multiplicative-expr + +function foo() returns int { + return 0; +} + +function init() { + int|error t1 = trap trap 2 / foo(); + io:println(t1); // @output error("{ballerina}DivisionByZero",message=" / by zero") +} + +Test-Case: error +Description: Check the type of trap-expr. +Labels: trap-expr, error-type, int, function-call-expr, multiplicative-expr + +function foo() returns int { + return 0; +} + +function errorFunction() { + int _ = trap 2 / foo(); // @error incompatible types: expected 'int', found '(int|error)' + error e = trap 2 / foo(); // @error incompatible types: expected 'error', found 'int' +} + +Test-Case: output +Description: Test trap-expr when the type of expression is never. +Labels: trap-expr, error-type, int, boolean, function-call-expr, error-constructor-expr + +function funcNeverReturns() returns never { + panic error("msg"); +} + +function init() { + never|error e = trap funcNeverReturns(); + io:println(e); // @output error("msg") +} From fb67a16e338638967729af712bba438c521e280a Mon Sep 17 00:00:00 2001 From: Kavindu Gimhan Zoysa Date: Wed, 8 Jun 2022 13:51:28 +0530 Subject: [PATCH 2/4] Fix review suggestions --- conformance/lang/expressions/trap_expr.balt | 217 ++++++++++++++------ 1 file changed, 158 insertions(+), 59 deletions(-) diff --git a/conformance/lang/expressions/trap_expr.balt b/conformance/lang/expressions/trap_expr.balt index 434475f5..9ecde133 100644 --- a/conformance/lang/expressions/trap_expr.balt +++ b/conformance/lang/expressions/trap_expr.balt @@ -1,6 +1,70 @@ Test-Case: output -Description: Test trap-expr when the expression is a nil-literal. -Labels: trap-expr, error-type, null, nil-literal, is-expr +Description: Test the result of the trap expression being `e`, + when evaluation of the expression completes abruptly with panic with associated value `e`. +Labels: error-type, function-call-expr, function-defn, optional-type, panic, trap-expr + +function foo() { + panic error("foo panics"); +} + +function init() { + error? e1 = trap foo(); + io:println(e1); // @output error("foo panics") +} + +Test-Case: output +Description: Test the result of the trap expression being `v`, + when evaluation of the expression does not complete abruptly with panic. +Labels: error-type, function-call-expr, function-defn, int, nil-literal, nil-type, optional-type, trap-expr, union-type, variable-reference-expr + +function init() { + int? nilVal = (); + int|error? e = trap nilVal; + io:println(e is ()); // @output true +} + +Test-Case: error +Description: If type of expr is T, then type of trap expr is T|error. +Labels: error-type, int, local-init-var-decl-stmt, multiplicative-expr, nil-literal, optional-type, string, trap-expr, union-type, variable-reference-expr + +function errorFunction() { + int zero = 0; + int? nilVal = (); + int _ = trap 2 / zero; // @error incompatible types: expected 'int', found '(int|error)' + string|error? x = trap nilVal; // @error incompatible types: expected '(string|error)?', found 'int?' + int _ = trap 2; // @error incompatible types: expected 'int', found '(int|error)' + string _ = trap "str"; // @error incompatible types: expected 'string', found '(string|error)' + float _ = trap 2.3f; // @error incompatible types: expected 'float', found '(float|error)' +} + +Test-Case: error +Description: If type of expr is T, then type of trap expr is T|error. +Fail-Issue: ballerina-platform/ballerina-lang#36472 +Labels: error-type, int, local-init-var-decl-stmt, multiplicative-expr, trap-expr, variable-reference-expr + +function errorFunction() { + int zero = 0; + error e = trap 2 / zero; // @error incompatible types: expected 'error', found 'int' +} + +Test-Case: output +Description: Test trap expression when the type of expression is never. +Labels: boolean, error-constructor-expr, error-type, function-call-expr, function-defn, int, never, panic, return-stmt, trap-expr, union-type + +function funcNeverReturns() returns never { + panic error("msg"); +} + +function init() { + never|error e1 = trap funcNeverReturns(); + io:println(e1); // @output error("msg") + error e2 = trap funcNeverReturns(); + io:println(e2); // @output error("msg") +} + +Test-Case: output +Description: Test trap expression when the expression is a nil-literal and it does not panic. +Labels: error-type, is-expr, nil-literal, nil-type, null, optional-type, trap-expr, union-type function init() { ()|error t1 = trap (); @@ -11,8 +75,8 @@ function init() { } Test-Case: output -Description: Test trap-expr when the expression is a boolean-literal. -Labels: trap-expr, error-type, boolean, boolean-literal +Description: Test trap expression when the expression is a boolean-literal and it does not panic. +Labels: boolean, boolean-literal, error-type, trap-expr, union-type function init() { boolean|error t = trap false; @@ -20,8 +84,8 @@ function init() { } Test-Case: output -Description: Test trap-expr when the expression is a numeric-literal. -Labels: trap-expr, error-type, int, float, decimal +Description: Test trap expression when the expression is a numeric-literal and it does not panic. +Labels: decimal, error-type, float, int, trap-expr, union-type function init() { int|error t1 = trap 2; @@ -33,8 +97,8 @@ function init() { } Test-Case: output -Description: Test trap-expr when the expression is a string-literal. -Labels: trap-expr, string +Description: Test trap expression when the expression is a string-literal and it does not panic. +Labels: string, trap-expr, union-type function init() { string|error t1 = trap "Hello"; @@ -44,31 +108,31 @@ function init() { } Test-Case: output -Description: Test trap-expr when the expression is a byte-array-literal. -Labels: trap-expr, error-type, byte, byte-array-literal +Description: Test trap expression when the expression is a byte-array-literal and it does not panic. +Labels: byte, byte-array-literal, error-type, trap-expr, union-type function init() { byte[]|error t1 = trap base16 `aeeecdefabcd12345567888822`; io:println(t1); // @output [174,238,205,239,171,205,18,52,85,103,136,136,34] - t1 = base64 `aGVsbG8gYmFsbGVyaW5hICEhIQ==`; + t1 = trap base64 `aGVsbG8gYmFsbGVyaW5hICEhIQ==`; io:println(t1); // @output [104,101,108,108,111,32,98,97,108,108,101,114,105,110,97,32,33,33,33] } Test-Case: output -Description: Test trap-expr when the expression is a string-template-expr. -Labels: trap-expr, error-type, string-template-expr +Description: Test trap expression when the expression is a string-template-expr and it does not panic. +Labels: error-type, string-template-expr, trap-expr, union-type function init() { string|error t1 = trap string `Hello World!`; io:println(t1); // @output Hello World! string s = string `Hello World!`; - string|error t2 = string `${s}`; + string|error t2 = trap string `${s}`; io:println(t2); // @output Hello World! } Test-Case: output -Description: Test trap-expr when the expression is a xml-template-expr. -Labels: trap-expr, error-type, xml-template-expr +Description: Test trap expression when the expression is a xml-template-expr and it does not panic. +Labels: error-type, trap-expr, union-type, xml-template-expr function init() { xml|error t1 = trap xml `Hello!`; @@ -76,8 +140,8 @@ function init() { } Test-Case: output -Description: Test trap-expr when the expression is a list-constructor-expr. -Labels: trap-expr, error-type, int, string, union-type, list-constructor-expr +Description: Test trap expression when the expression is a list constructor expression. Test 1. +Labels: array-type, error-type, int, list-constructor-expr, string, trap-expr, union-type function init() { int[]|error t1 = trap [1, 2, 3, 4]; @@ -87,8 +151,8 @@ function init() { } Test-Case: output -Description: Test trap-expr when the expression is a list-constructor-expr. -Labels: trap-expr, error-type, int, string, union-type, list-constructor-expr, type-cast-expr, function-call-expr +Description: Test trap expression when the expression is a list constructor expression. Test 2. +Labels: array-type, error-type, function-call-expr, function-defn, int, list-constructor-expr, return-stmt, string, trap-expr, type-cast-expr, union-type function foo() returns int|string { return "2"; @@ -100,8 +164,8 @@ function init() { } Test-Case: output -Description: Test trap-expr when the expression is a list-constructor-expr. -Labels: trap-expr, error-type, int, string, union-type, table-constructor-expr, multiplicative-expr +Description: Test trap expression when the expression is a table constructor expression. +Labels: error-type, int, multiplicative-expr, record-type, record-type-readonly-field, string, table-constructor-expr, union-type, trap-expr function init() { (table key(name))|error tbl1 = trap table key(name) [ @@ -111,8 +175,8 @@ function init() { } Test-Case: output -Description: Test trap-expr when the expression is a object-constructor-expr. -Labels: trap-expr, error-type, object-constructor-expr, method-call-expr, panic +Description: Test trap expression when the expression is a object constructor expression. +Labels: error-type, function-type, method-call-expr, object-constructor-expr, optional-type, panic, trap-expr function init() { object { function bar(); } obj = object { @@ -129,8 +193,8 @@ function init() { } Test-Case: output -Description: Test trap-expr when the expression is a new-expr. -Labels: trap-expr, error-type, new-expr, method-call-expr, panic, error-constructor-expr +Description: Test trap expression when the expression is a new expression. +Labels: error-constructor-expr, error-type, function-defn, method-call-expr, module-class-defn, new-expr, optional-type, panic, trap-expr class Obj { function bar() { @@ -145,8 +209,8 @@ function init() { } Test-Case: output -Description: Test trap-expr when the expression is a member-access-expr. -Labels: trap-expr, error-type, int, array-type, member-access-expr +Description: Test trap expression when the expression is a member access expression. +Labels: array-type, error-type, int, member-access-expr, trap-expr function init() { int[] i = []; @@ -154,8 +218,8 @@ function init() { } Test-Case: output -Description: Test trap-expr when the expression is a function-call-expr. -Labels: trap-expr, error-type, int, function-call-expr, array-type, union-type, error-constructor-expr +Description: Test trap expression when the expression is a function call expression. +Labels: array-type, error-constructor-expr, error-type, function-call-expr, function-defn, int, optional-type, return-stmt, trap-expr, union-type function foo() returns int { int[] i = []; @@ -174,8 +238,8 @@ function init() { } Test-Case: output -Description: Test trap-expr when the expression is a type-cast-expr. -Labels: trap-expr, error-type, int, string, function-call-expr, union-type, type-cast-expr +Description: Test trap expression when the expression is a type cast expression. +Labels: error-type, function-call-expr, function-defn, int, return-stmt, string, trap-expr, type-cast-expr, union-type function foo() returns int|string { return "str"; @@ -184,11 +248,13 @@ function foo() returns int|string { function init() { int|error t1 = trap foo(); io:println(t1); // @output error("{ballerina}TypeCastError",message="incompatible types: 'string' cannot be cast to 'int'") + string|error t2 = trap foo(); + io:println(t2); // @output str } Test-Case: output -Description: Test trap-expr when the expression is a unary-logical-expr. -Labels: trap-expr, error-type, int, boolean, function-call-expr, union-type +Description: Test trap expression when the expression is a unary logical expression. +Labels: boolean, error-type, function-call-expr, function-defn, int, return-stmt, trap-expr, union-type function foo() returns int|boolean { return 23; @@ -200,53 +266,86 @@ function init() { } Test-Case: output -Description: Test trap-expr when the expression is a multiplicative-expr. -Labels: trap-expr, error-type, int, boolean, function-call-expr, multiplicative-expr +Description: Test trap expression when the expression is a multiplicative expression. +Labels: boolean, error-type, function-call-expr, function-defn, int, local-init-var-decl-stmt, multiplicative-expr, trap-expr, union-type, variable-reference-expr -function foo() returns int { - return 0; +function init() { + int zero = 0; + int|error t1 = trap 2 / zero; + io:println(t1); // @output error("{ballerina}DivisionByZero",message=" / by zero") + int|error t2 = trap int:MAX_VALUE * int:MAX_VALUE; + io:println(t2); // @output error("{ballerina}NumberOverflow",message="int range overflow") } +Test-Case: output +Description: Test trap expression when the expression is a trap expression. +Labels: boolean, error-type, function-call-expr, function-defn, int, local-init-var-decl-stmt, multiplicative-expr, trap-expr, union-type, variable-reference-expr + function init() { - int|error t1 = trap 2 / foo(); + int zero = 0; + int|error t1 = trap trap 2 / zero; io:println(t1); // @output error("{ballerina}DivisionByZero",message=" / by zero") } Test-Case: output -Description: Test trap-expr when the expression is a trap-expr. -Labels: trap-expr, error-type, int, boolean, function-call-expr, multiplicative-expr +Description: Test trap expression when the expression is a checking expression. +Labels: boolean, check, checkpanic, error-type, int, local-init-var-decl-stmt, multiplicative-expr, trap-expr, union-type, variable-reference-expr -function foo() returns int { - return 0; +function init() { + int zero = 0; + int|error e1 = trap (check 2 / zero); + io:println(e1); // @output error("{ballerina}DivisionByZero",message=" / by zero") + int|error e2 = trap (checkpanic 2 / zero); + io:println(e2); // @output error("{ballerina}DivisionByZero",message=" / by zero") } +Test-Case: output +Description: Test trap expression when the expression is a char and it does not panic. +Labels: boolean, error-type, local-init-var-decl-stmt, string:Char, trap-expr, union-type, variable-reference-expr + function init() { - int|error t1 = trap trap 2 / foo(); - io:println(t1); // @output error("{ballerina}DivisionByZero",message=" / by zero") + string:Char c = "c"; + string:Char|error e1 = trap c; + io:println(e1); // @output c } -Test-Case: error -Description: Check the type of trap-expr. -Labels: trap-expr, error-type, int, function-call-expr, multiplicative-expr +Test-Case: output +Description: Test trap expression when the expression is a byte value and it does not panic. +Labels: boolean, byte, error-type, local-init-var-decl-stmt, trap-expr, union-type, variable-reference-expr -function foo() returns int { - return 0; +function init() { + byte b = 0; + byte|error e = trap b; + io:println(e); // @output 0 } -function errorFunction() { - int _ = trap 2 / foo(); // @error incompatible types: expected 'int', found '(int|error)' - error e = trap 2 / foo(); // @error incompatible types: expected 'error', found 'int' +Test-Case: output +Description: Test trap expression when the expression is a byte value and it does not panic. +Labels: boolean, error-type, int:Signed32, int:Unsigned32, local-init-var-decl-stmt, trap-expr, union-type, variable-reference-expr + +function init() { + int:Signed32 i1 = -12; + int:Signed32|error e1 = trap i1; + io:println(e1); // @output -12 + int:Unsigned32 i2 = 1234; + int:Unsigned32|error e2 = trap i2; + io:println(e2); // @output 1234 } Test-Case: output -Description: Test trap-expr when the type of expression is never. -Labels: trap-expr, error-type, int, boolean, function-call-expr, error-constructor-expr +Description: Test trap expression when the expression is a query expression +Labels: array-type, error-type, list-constructor-expr, query-expr, trap-expr, union-type -function funcNeverReturns() returns never { - panic error("msg"); +function init() { + int[]|error res = trap from var item in [1, 0, 4] + select 5/item; + io:println(res); // @output error("{ballerina}DivisionByZero",message=" / by zero") } +Test-Case: output +Description: Test trap expression when the expression is a transactional expression and it does not panic. +Labels: transactional-expr, trap-expr + function init() { - never|error e = trap funcNeverReturns(); - io:println(e); // @output error("msg") + io:println(trap transactional); // @output false } From d67f285b313ec3dc600e5a710d4e5aae7ee23c18 Mon Sep 17 00:00:00 2001 From: Kavindu Gimhan Zoysa Date: Mon, 17 Oct 2022 20:25:22 +0530 Subject: [PATCH 3/4] Address new review suggestions --- conformance/lang/expressions/trap_expr.balt | 58 +++++++++------------ 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/conformance/lang/expressions/trap_expr.balt b/conformance/lang/expressions/trap_expr.balt index 9ecde133..bdf6d168 100644 --- a/conformance/lang/expressions/trap_expr.balt +++ b/conformance/lang/expressions/trap_expr.balt @@ -15,7 +15,7 @@ function init() { Test-Case: output Description: Test the result of the trap expression being `v`, when evaluation of the expression does not complete abruptly with panic. -Labels: error-type, function-call-expr, function-defn, int, nil-literal, nil-type, optional-type, trap-expr, union-type, variable-reference-expr +Labels: error-type, function-call-expr, function-defn, int, nil-literal, nil-type, optional-type, trap-expr, union-type function init() { int? nilVal = (); @@ -25,41 +25,42 @@ function init() { Test-Case: error Description: If type of expr is T, then type of trap expr is T|error. -Labels: error-type, int, local-init-var-decl-stmt, multiplicative-expr, nil-literal, optional-type, string, trap-expr, union-type, variable-reference-expr +Labels: error-type, int, multiplicative-expr, nil-literal, optional-type, string, trap-expr, union-type function errorFunction() { int zero = 0; int? nilVal = (); int _ = trap 2 / zero; // @error incompatible types: expected 'int', found '(int|error)' - string|error? x = trap nilVal; // @error incompatible types: expected '(string|error)?', found 'int?' + string|error? x = trap nilVal; // @error incompatible types: expected '(string|error)?', found '(int|error)?' int _ = trap 2; // @error incompatible types: expected 'int', found '(int|error)' string _ = trap "str"; // @error incompatible types: expected 'string', found '(string|error)' float _ = trap 2.3f; // @error incompatible types: expected 'float', found '(float|error)' } Test-Case: error -Description: If type of expr is T, then type of trap expr is T|error. +Description: If type of `expr` is `T`, then type of `trap expr` is `T|error`. Fail-Issue: ballerina-platform/ballerina-lang#36472 -Labels: error-type, int, local-init-var-decl-stmt, multiplicative-expr, trap-expr, variable-reference-expr +Labels: error-type, int, multiplicative-expr, trap-expr function errorFunction() { int zero = 0; - error e = trap 2 / zero; // @error incompatible types: expected 'error', found 'int' + error e1 = trap 2 / zero; // @error incompatible types: expected 'error', found 'int' + error e2 = trap zero;; // @error incompatible types: expected 'error', found 'int' } Test-Case: output Description: Test trap expression when the type of expression is never. Labels: boolean, error-constructor-expr, error-type, function-call-expr, function-defn, int, never, panic, return-stmt, trap-expr, union-type -function funcNeverReturns() returns never { - panic error("msg"); +function funcNeverReturns(string message) returns never { + panic error(message); } function init() { - never|error e1 = trap funcNeverReturns(); - io:println(e1); // @output error("msg") - error e2 = trap funcNeverReturns(); - io:println(e2); // @output error("msg") + never|error e1 = trap funcNeverReturns("message1"); + io:println(e1); // @output error("message1") + error e2 = trap funcNeverReturns("message2"); + io:println(e2); // @output error("message2") } Test-Case: output @@ -140,18 +141,7 @@ function init() { } Test-Case: output -Description: Test trap expression when the expression is a list constructor expression. Test 1. -Labels: array-type, error-type, int, list-constructor-expr, string, trap-expr, union-type - -function init() { - int[]|error t1 = trap [1, 2, 3, 4]; - io:println(t1); // @output [1,2,3,4] - (int|string)[]|error t2 = trap [1, "2", 3, "4"]; - io:println(t2); // @output [1,"2",3,"4"] -} - -Test-Case: output -Description: Test trap expression when the expression is a list constructor expression. Test 2. +Description: Test trap expression when the expression is a list constructor expression. Labels: array-type, error-type, function-call-expr, function-defn, int, list-constructor-expr, return-stmt, string, trap-expr, type-cast-expr, union-type function foo() returns int|string { @@ -159,8 +149,10 @@ function foo() returns int|string { } function init() { - int[]|error t = trap [1, 2, foo(), 4]; - io:println(t); // @output error("{ballerina}TypeCastError",message="incompatible types: 'string' cannot be cast to 'int'") + (int|string)[]|error t1 = trap [1, "2", 3, "4"]; + io:println(t1); // @output [1,"2",3,"4"] + int[]|error t2 = trap [1, 2, foo(), 4]; + io:println(t2); // @output error("{ballerina}TypeCastError",message="incompatible types: 'string' cannot be cast to 'int'") } Test-Case: output @@ -267,7 +259,7 @@ function init() { Test-Case: output Description: Test trap expression when the expression is a multiplicative expression. -Labels: boolean, error-type, function-call-expr, function-defn, int, local-init-var-decl-stmt, multiplicative-expr, trap-expr, union-type, variable-reference-expr +Labels: boolean, error-type, function-call-expr, function-defn, int, multiplicative-expr, trap-expr, union-type function init() { int zero = 0; @@ -279,7 +271,7 @@ function init() { Test-Case: output Description: Test trap expression when the expression is a trap expression. -Labels: boolean, error-type, function-call-expr, function-defn, int, local-init-var-decl-stmt, multiplicative-expr, trap-expr, union-type, variable-reference-expr +Labels: boolean, error-type, function-call-expr, function-defn, int, multiplicative-expr, trap-expr, union-type function init() { int zero = 0; @@ -289,7 +281,7 @@ function init() { Test-Case: output Description: Test trap expression when the expression is a checking expression. -Labels: boolean, check, checkpanic, error-type, int, local-init-var-decl-stmt, multiplicative-expr, trap-expr, union-type, variable-reference-expr +Labels: boolean, check, checkpanic, error-type, int, multiplicative-expr, trap-expr, union-type function init() { int zero = 0; @@ -300,8 +292,8 @@ function init() { } Test-Case: output -Description: Test trap expression when the expression is a char and it does not panic. -Labels: boolean, error-type, local-init-var-decl-stmt, string:Char, trap-expr, union-type, variable-reference-expr +Description: Test trap expression when the expression is a character and it does not panic. +Labels: boolean, error-type, string:Char, trap-expr, union-type function init() { string:Char c = "c"; @@ -311,7 +303,7 @@ function init() { Test-Case: output Description: Test trap expression when the expression is a byte value and it does not panic. -Labels: boolean, byte, error-type, local-init-var-decl-stmt, trap-expr, union-type, variable-reference-expr +Labels: boolean, byte, error-type, trap-expr, union-type function init() { byte b = 0; @@ -321,7 +313,7 @@ function init() { Test-Case: output Description: Test trap expression when the expression is a byte value and it does not panic. -Labels: boolean, error-type, int:Signed32, int:Unsigned32, local-init-var-decl-stmt, trap-expr, union-type, variable-reference-expr +Labels: boolean, error-type, int:Signed32, int:Unsigned32, trap-expr, union-type function init() { int:Signed32 i1 = -12; From 5303f62061d4c9f05d3292185a6988ae3261a1a4 Mon Sep 17 00:00:00 2001 From: Kavindu Gimhan Zoysa Date: Mon, 31 Oct 2022 10:10:27 +0530 Subject: [PATCH 4/4] Move literals to function calls --- conformance/lang/expressions/trap_expr.balt | 112 ++++++++++++-------- 1 file changed, 69 insertions(+), 43 deletions(-) diff --git a/conformance/lang/expressions/trap_expr.balt b/conformance/lang/expressions/trap_expr.balt index bdf6d168..78232424 100644 --- a/conformance/lang/expressions/trap_expr.balt +++ b/conformance/lang/expressions/trap_expr.balt @@ -64,52 +64,52 @@ function init() { } Test-Case: output -Description: Test trap expression when the expression is a nil-literal and it does not panic. +Description: Test trap expression when the expression is a nil literal and it does not panic. Labels: error-type, is-expr, nil-literal, nil-type, null, optional-type, trap-expr, union-type -function init() { - ()|error t1 = trap (); - io:println(t1 is ()); // @output true - - error? t2 = trap null; - io:println(t2 is ()); // @output true +function foo(boolean nilLiteral) returns () { + if nilLiteral { + return null; + } else { + panic error("Don't return boolean"); + } } -Test-Case: output -Description: Test trap expression when the expression is a boolean-literal and it does not panic. -Labels: boolean, boolean-literal, error-type, trap-expr, union-type - function init() { - boolean|error t = trap false; - io:println(t); // @output false + ()|error t = trap foo(true); + io:println(t is ()); // @output true + t = trap foo(false); + io:println(t); // @output error("Don't return boolean") } Test-Case: output -Description: Test trap expression when the expression is a numeric-literal and it does not panic. -Labels: decimal, error-type, float, int, trap-expr, union-type +Description: Test trap expression when the expression is a function call which returns int or panic. +Labels: decimal, error-type, float, function-call-expr, int, trap-expr, union-type + +function foo(string str) returns int { + return checkpanic int:fromString(str); +} function init() { - int|error t1 = trap 2; - io:println(t1); // @output 2 - float|error t2 = trap 2e3f; - io:println(t2); // @output 2000.0 - decimal|error t3 = trap 2e355d; - io:println(t3); // @output 2E+355 + int|error a = trap foo("1"); + io:println(a); // @output 1 + a = trap foo("a"); + io:println(a); // @output error("{ballerina/lang.int}NumberParsingError",message="'string' value 'a' cannot be converted to 'int'") } Test-Case: output -Description: Test trap expression when the expression is a string-literal and it does not panic. +Description: Test trap expression when the expression is a function call which returns string or panic. Labels: string, trap-expr, union-type function init() { - string|error t1 = trap "Hello"; - io:println(t1); // @output Hello - t1 = trap "Hello\tWorld"; - io:println(t1); // @output Hello World + string|error e = trap string:fromCodePointInt(100); + io:println(e); // @output d + e = trap string:fromCodePointInt(1000000000000000); + io:println(e); // @output error("Invalid codepoint: 1000000000000000") } Test-Case: output -Description: Test trap expression when the expression is a byte-array-literal and it does not panic. +Description: Test trap expression when the expression is a byte array literal and it does not panic. Labels: byte, byte-array-literal, error-type, trap-expr, union-type function init() { @@ -120,7 +120,7 @@ function init() { } Test-Case: output -Description: Test trap expression when the expression is a string-template-expr and it does not panic. +Description: Test trap expression when the expression is a string template expr and it does not panic. Labels: error-type, string-template-expr, trap-expr, union-type function init() { @@ -132,7 +132,7 @@ function init() { } Test-Case: output -Description: Test trap expression when the expression is a xml-template-expr and it does not panic. +Description: Test trap expression when the expression is a xml template expr and it does not panic. Labels: error-type, trap-expr, union-type, xml-template-expr function init() { @@ -292,36 +292,62 @@ function init() { } Test-Case: output -Description: Test trap expression when the expression is a character and it does not panic. -Labels: boolean, error-type, string:Char, trap-expr, union-type +Description: Test trap expression when the expression is a function call which returns char or panic. +Labels: boolean, error-type, function-call-expr, string:Char, trap-expr, union-type + +function foo(boolean character) returns string:Char { + if character { + return "c"; + } else { + panic error("Don't return char"); + } +} function init() { string:Char c = "c"; - string:Char|error e1 = trap c; - io:println(e1); // @output c + string:Char|error e = trap foo(true); + io:println(e); // @output c + e = trap foo(false); + io:println(e); // @output error("Don't return char") } Test-Case: output -Description: Test trap expression when the expression is a byte value and it does not panic. -Labels: boolean, byte, error-type, trap-expr, union-type +Description: Test trap expression when the expression is a function call which returns byte or panic. +Labels: boolean, byte, error-type, function-call-expr, trap-expr, union-type + +function foo(boolean byteValue) returns byte { + if byteValue { + return 0; + } else { + panic error("Don't return byte"); + } +} function init() { byte b = 0; - byte|error e = trap b; + byte|error e = trap foo(true); io:println(e); // @output 0 + e = trap foo(false); + io:println(e); // @output error("Don't return byte") } Test-Case: output -Description: Test trap expression when the expression is a byte value and it does not panic. -Labels: boolean, error-type, int:Signed32, int:Unsigned32, trap-expr, union-type +Description: Test trap expression when the expression is a function call which returns subtype of int or panic. +Labels: boolean, error-type, function-call-expr, int:Signed32, int:Unsigned32, trap-expr, union-type + +function foo(boolean signedInt) returns int:Signed32 { + if signedInt { + return -12; + } else { + panic error("Don't return signed int"); + } +} function init() { - int:Signed32 i1 = -12; - int:Signed32|error e1 = trap i1; + int:Signed32|error e1 = trap foo(true); io:println(e1); // @output -12 - int:Unsigned32 i2 = 1234; - int:Unsigned32|error e2 = trap i2; - io:println(e2); // @output 1234 + e1 = trap foo(false); + io:println(e1); // @output error("Don't return signed int") } Test-Case: output