From 2605502a81bc4cd97e6ed2731bd585d03fd89e7f Mon Sep 17 00:00:00 2001 From: dulaj Date: Fri, 7 Jun 2024 11:57:15 +0530 Subject: [PATCH 1/5] Fix getting non-accessible symbol error --- .../compiler/semantics/analyzer/Types.java | 3 +- .../intersection/IntersectionTypeTest.java | 6 +++ .../error-intersection-access/Ballerina.toml | 7 +++ .../error_intersection_access_test.bal | 43 +++++++++++++++++++ .../error_intersection_mod.bal | 33 ++++++++++++++ 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 tests/jballerina-unit-test/src/test/resources/test-src/types/intersection/error-intersection-access/Ballerina.toml create mode 100644 tests/jballerina-unit-test/src/test/resources/test-src/types/intersection/error-intersection-access/error_intersection_access_test.bal create mode 100644 tests/jballerina-unit-test/src/test/resources/test-src/types/intersection/error-intersection-access/modules/error_intersection_mod/error_intersection_mod.bal diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java index d602a103edbf..3ad7f4e67044 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java @@ -5695,7 +5695,8 @@ private BErrorType createErrorType(BType lhsType, BType rhsType, BType detailTyp public BErrorType createErrorType(BType detailType, long flags, SymbolEnv env) { String name = anonymousModelHelper.getNextAnonymousIntersectionErrorTypeName(env.enclPkg.packageID); - BErrorTypeSymbol errorTypeSymbol = Symbols.createErrorSymbol(flags | Flags.ANONYMOUS, Names.fromString(name), + BErrorTypeSymbol errorTypeSymbol = Symbols.createErrorSymbol(flags | Flags.ANONYMOUS | Flags.PUBLIC, + Names.fromString(name), env.enclPkg.symbol.pkgID, null, env.scope.owner, symTable.builtinPos, VIRTUAL); errorTypeSymbol.scope = new Scope(errorTypeSymbol); diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/intersection/IntersectionTypeTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/intersection/IntersectionTypeTest.java index 6c8a1629b734..5df620c06eb2 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/intersection/IntersectionTypeTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/intersection/IntersectionTypeTest.java @@ -264,6 +264,12 @@ public void testUnsupportedIntersectionNegative() { assertEquals(result.getErrorCount(), index); } + @Test + public void testErrorIntersectionAccessTest() { + CompileResult result = BCompileUtil.compile("test-src/types/intersection/error-intersection-access"); + assertEquals(result.getDiagnostics().length, 0); + } + @AfterClass public void tearDown() { readOnlyIntersectionResults = null; diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/types/intersection/error-intersection-access/Ballerina.toml b/tests/jballerina-unit-test/src/test/resources/test-src/types/intersection/error-intersection-access/Ballerina.toml new file mode 100644 index 000000000000..71d5616b4ab1 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/resources/test-src/types/intersection/error-intersection-access/Ballerina.toml @@ -0,0 +1,7 @@ +[package] +org = "testorg" +name = "error_intersection_access" +version = "0.1.0" + +[build-options] +observabilityIncluded = true diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/types/intersection/error-intersection-access/error_intersection_access_test.bal b/tests/jballerina-unit-test/src/test/resources/test-src/types/intersection/error-intersection-access/error_intersection_access_test.bal new file mode 100644 index 000000000000..a9ba3fe465f3 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/resources/test-src/types/intersection/error-intersection-access/error_intersection_access_test.bal @@ -0,0 +1,43 @@ +// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import error_intersection_access.error_intersection_mod as err_lib; + +function testErrorIntersectionAccessTest(int code) returns error? { + string reason = "response error"; + + if code > 500 { + return error err_lib:RemoteServerError(reason); + } + + if code == 500 { + return error err_lib:ApplicationResponseError(reason); + } + + if code > 400 { + if code == 404 { + return error err_lib:ClientRequestErrorWithStatusCode(reason, code = code); + } + + return error err_lib:ApplicationResponseErrorWithStatusCode(reason, code = code); + } + + if code == 400 { + return error err_lib:BadRequestError(reason, code = 0); + } + + return error err_lib:Error(reason); +} diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/types/intersection/error-intersection-access/modules/error_intersection_mod/error_intersection_mod.bal b/tests/jballerina-unit-test/src/test/resources/test-src/types/intersection/error-intersection-access/modules/error_intersection_mod/error_intersection_mod.bal new file mode 100644 index 000000000000..869c6bd61ea7 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/resources/test-src/types/intersection/error-intersection-access/modules/error_intersection_mod/error_intersection_mod.bal @@ -0,0 +1,33 @@ +// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +public type Status record { + int code; +}; + +public type Error distinct error; + +public type ClientRequestError distinct Error & error; + +public type RemoteServerError distinct error & error; + +public type ApplicationResponseError distinct ClientRequestError & RemoteServerError; + +public type ApplicationResponseErrorWithStatusCode distinct ApplicationResponseError & error; + +public type ClientRequestErrorWithStatusCode distinct ClientRequestError & error; + +public type BadRequestError distinct ApplicationResponseErrorWithStatusCode & ClientRequestErrorWithStatusCode; From 6420b3ed1ee37fbfd6e3e707867cdc621d83dede Mon Sep 17 00:00:00 2001 From: dulaj Date: Fri, 7 Jun 2024 12:44:34 +0530 Subject: [PATCH 2/5] Changed to errorCount() --- .../test/types/intersection/IntersectionTypeTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/intersection/IntersectionTypeTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/intersection/IntersectionTypeTest.java index 5df620c06eb2..0af630feff23 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/intersection/IntersectionTypeTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/intersection/IntersectionTypeTest.java @@ -267,7 +267,7 @@ public void testUnsupportedIntersectionNegative() { @Test public void testErrorIntersectionAccessTest() { CompileResult result = BCompileUtil.compile("test-src/types/intersection/error-intersection-access"); - assertEquals(result.getDiagnostics().length, 0); + assertEquals(result.getErrorCount(), 0); } @AfterClass From db0b569086556549c57b3faaea93208b03cdb9c5 Mon Sep 17 00:00:00 2001 From: dulaj Date: Mon, 10 Jun 2024 09:54:42 +0530 Subject: [PATCH 3/5] Address review comments --- .../ballerinalang/compiler/semantics/analyzer/Types.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java index 3ad7f4e67044..24b542d353ab 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java @@ -5685,8 +5685,9 @@ private BErrorType createErrorType(BType lhsType, BType rhsType, BType detailTyp BErrorType lhsErrorType = (BErrorType) lhsType; BErrorType rhsErrorType = (BErrorType) rhsType; - BErrorType errorType = createErrorType(detailType, lhsType.flags, env); - errorType.tsymbol.flags |= rhsType.flags; + long flags = lhsType.flags | rhsType.flags | Flags.PUBLIC; // Anonymous error intersection types must be public + + BErrorType errorType = createErrorType(detailType, flags, env); errorType.typeIdSet = BTypeIdSet.getIntersection(lhsErrorType.typeIdSet, rhsErrorType.typeIdSet); @@ -5695,8 +5696,7 @@ private BErrorType createErrorType(BType lhsType, BType rhsType, BType detailTyp public BErrorType createErrorType(BType detailType, long flags, SymbolEnv env) { String name = anonymousModelHelper.getNextAnonymousIntersectionErrorTypeName(env.enclPkg.packageID); - BErrorTypeSymbol errorTypeSymbol = Symbols.createErrorSymbol(flags | Flags.ANONYMOUS | Flags.PUBLIC, - Names.fromString(name), + BErrorTypeSymbol errorTypeSymbol = Symbols.createErrorSymbol(flags | Flags.ANONYMOUS, Names.fromString(name), env.enclPkg.symbol.pkgID, null, env.scope.owner, symTable.builtinPos, VIRTUAL); errorTypeSymbol.scope = new Scope(errorTypeSymbol); From 863c1dee2735080f213ef0116491d5324596b694 Mon Sep 17 00:00:00 2001 From: dulaj Date: Fri, 28 Jun 2024 14:42:33 +0530 Subject: [PATCH 4/5] Address review suggestions --- .../compiler/semantics/analyzer/Types.java | 3 +- .../error_intersection_access_test.bal | 46 ++++++++++++------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java index 24b542d353ab..b6f80c1f19cf 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java @@ -5685,8 +5685,7 @@ private BErrorType createErrorType(BType lhsType, BType rhsType, BType detailTyp BErrorType lhsErrorType = (BErrorType) lhsType; BErrorType rhsErrorType = (BErrorType) rhsType; - long flags = lhsType.flags | rhsType.flags | Flags.PUBLIC; // Anonymous error intersection types must be public - + long flags = lhsType.flags | rhsType.flags | Flags.PUBLIC; // Anonymous (generated) types are marked as public. BErrorType errorType = createErrorType(detailType, flags, env); errorType.typeIdSet = BTypeIdSet.getIntersection(lhsErrorType.typeIdSet, rhsErrorType.typeIdSet); diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/types/intersection/error-intersection-access/error_intersection_access_test.bal b/tests/jballerina-unit-test/src/test/resources/test-src/types/intersection/error-intersection-access/error_intersection_access_test.bal index a9ba3fe465f3..dae071db9529 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/types/intersection/error-intersection-access/error_intersection_access_test.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/types/intersection/error-intersection-access/error_intersection_access_test.bal @@ -16,28 +16,42 @@ import error_intersection_access.error_intersection_mod as err_lib; -function testErrorIntersectionAccessTest(int code) returns error? { - string reason = "response error"; +public function testErrorIntersectionAccessTest() { + err_lib:RemoteServerError remoteServerErr = error err_lib:RemoteServerError("remote server error"); + assertEquality("remote server error", remoteServerErr.message()); - if code > 500 { - return error err_lib:RemoteServerError(reason); - } + err_lib:ApplicationResponseError applResponeErr = error err_lib:ApplicationResponseError("application response server error"); + assertEquality("application response server error", applResponeErr.message()); - if code == 500 { - return error err_lib:ApplicationResponseError(reason); - } + err_lib:ClientRequestErrorWithStatusCode clientReqErrWithStatusCode = + error err_lib:ClientRequestErrorWithStatusCode("client request error", code = 404); + assertEquality("client request error", clientReqErrWithStatusCode.message()); + assertEquality(404, clientReqErrWithStatusCode.detail().code); + + err_lib:ApplicationResponseErrorWithStatusCode applResponseErrWithStatusCode = + error err_lib:ApplicationResponseErrorWithStatusCode("application response error", code = 401); + assertEquality("application response error", applResponseErrWithStatusCode.message()); + assertEquality(401, applResponseErrWithStatusCode.detail().code); - if code > 400 { - if code == 404 { - return error err_lib:ClientRequestErrorWithStatusCode(reason, code = code); - } + err_lib:BadRequestError badReqErr = error err_lib:BadRequestError("bad request error", code = 400); + assertEquality("bad request error", badReqErr); + assertEquality(400, badReqErr.detail().code); + + err_lib:Error err = error err_lib:Error("response error"); + assertEquality("error", err.message()); +} - return error err_lib:ApplicationResponseErrorWithStatusCode(reason, code = code); +function assertEquality(any|error actual, any|error expected) { + if expected is anydata && actual is anydata && expected == actual { + return; } - if code == 400 { - return error err_lib:BadRequestError(reason, code = 0); + if expected === actual { + return; } - return error err_lib:Error(reason); + string expectedValAsString = expected is error ? expected.toString() : expected.toString(); + string actualValAsString = actual is error ? actual.toString() : actual.toString(); + panic error("AssertionError", + message = "expected '" + expectedValAsString + "', found '" + actualValAsString + "'"); } From 6eed512cc5f1deefcd6b4e8f1597d190f8b14f99 Mon Sep 17 00:00:00 2001 From: dulaj Date: Mon, 1 Jul 2024 13:55:42 +0530 Subject: [PATCH 5/5] Update test --- .../intersection/IntersectionTypeTest.java | 1 + .../error_intersection_access_test.bal | 53 +++++++++---------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/intersection/IntersectionTypeTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/intersection/IntersectionTypeTest.java index 0af630feff23..93d7588fe1f7 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/intersection/IntersectionTypeTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/intersection/IntersectionTypeTest.java @@ -268,6 +268,7 @@ public void testUnsupportedIntersectionNegative() { public void testErrorIntersectionAccessTest() { CompileResult result = BCompileUtil.compile("test-src/types/intersection/error-intersection-access"); assertEquals(result.getErrorCount(), 0); + BRunUtil.invoke(result, "testErrorIntersectionFromImportedModule"); } @AfterClass diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/types/intersection/error-intersection-access/error_intersection_access_test.bal b/tests/jballerina-unit-test/src/test/resources/test-src/types/intersection/error-intersection-access/error_intersection_access_test.bal index dae071db9529..0f49c8555bd3 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/types/intersection/error-intersection-access/error_intersection_access_test.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/types/intersection/error-intersection-access/error_intersection_access_test.bal @@ -16,42 +16,41 @@ import error_intersection_access.error_intersection_mod as err_lib; -public function testErrorIntersectionAccessTest() { - err_lib:RemoteServerError remoteServerErr = error err_lib:RemoteServerError("remote server error"); - assertEquality("remote server error", remoteServerErr.message()); +const MESSAGE = "Message"; - err_lib:ApplicationResponseError applResponeErr = error err_lib:ApplicationResponseError("application response server error"); - assertEquality("application response server error", applResponeErr.message()); +public function testErrorIntersectionFromImportedModule() { + err_lib:RemoteServerError remoteServerErr = error err_lib:RemoteServerError(MESSAGE); + assertError(remoteServerErr); - err_lib:ClientRequestErrorWithStatusCode clientReqErrWithStatusCode = - error err_lib:ClientRequestErrorWithStatusCode("client request error", code = 404); - assertEquality("client request error", clientReqErrWithStatusCode.message()); - assertEquality(404, clientReqErrWithStatusCode.detail().code); + err_lib:ApplicationResponseError applResponeErr = error err_lib:ApplicationResponseError(MESSAGE); + assertError(applResponeErr); - err_lib:ApplicationResponseErrorWithStatusCode applResponseErrWithStatusCode = - error err_lib:ApplicationResponseErrorWithStatusCode("application response error", code = 401); - assertEquality("application response error", applResponseErrWithStatusCode.message()); - assertEquality(401, applResponseErrWithStatusCode.detail().code); + err_lib:ClientRequestErrorWithStatusCode clientReqErrWithStatusCode = error (MESSAGE, code = 404); + assertError(clientReqErrWithStatusCode, 404); - err_lib:BadRequestError badReqErr = error err_lib:BadRequestError("bad request error", code = 400); - assertEquality("bad request error", badReqErr); - assertEquality(400, badReqErr.detail().code); + err_lib:ApplicationResponseErrorWithStatusCode appRespErrWithStatusCode = error (MESSAGE, code = 401); + assertError(appRespErrWithStatusCode, 401); - err_lib:Error err = error err_lib:Error("response error"); - assertEquality("error", err.message()); + err_lib:BadRequestError badReqErr = error err_lib:BadRequestError(MESSAGE, code = 400); + assertError(badReqErr, 400); + + err_lib:Error err = error err_lib:Error(MESSAGE); + assertError(err); } -function assertEquality(any|error actual, any|error expected) { - if expected is anydata && actual is anydata && expected == actual { - return; +function assertError(any|error actual, int? code = ()) { + if actual !is error { + panic error(string `expected an error, found '${actual.toString()}'`); } - if expected === actual { - return; + if MESSAGE != actual.message() { + panic error(string `expected message: '${MESSAGE}', found: '${actual.message()}'`); } - string expectedValAsString = expected is error ? expected.toString() : expected.toString(); - string actualValAsString = actual is error ? actual.toString() : actual.toString(); - panic error("AssertionError", - message = "expected '" + expectedValAsString + "', found '" + actualValAsString + "'"); + if code != () { + var detail = actual.detail(); + if code != detail.code { + panic error(string `expected code: '${code}', found: '${detail.code}'`); + } + } }