diff --git a/checker/ETSchecker.h b/checker/ETSchecker.h index 92065ea4b63452ab64a5ab6dacb8d1893a6f7dde..8cc5626b3570bdcb5b2bff22be854c6bf9bda80c 100644 --- a/checker/ETSchecker.h +++ b/checker/ETSchecker.h @@ -286,7 +286,8 @@ public: [[nodiscard]] bool IsReturnTypeSubstitutable(Signature *s1, Signature *s2); void CheckStaticHide(Signature *target, Signature *source); void CheckThrowMarkers(Signature *source, Signature *target); - void ValidateSignatureAccessibility(ETSObjectType *callee, Signature *signature, const lexer::SourcePosition &pos); + void ValidateSignatureAccessibility(ETSObjectType *callee, [[maybe_unused]] ir::CallExpression *call_expr, + Signature *signature, const lexer::SourcePosition &pos); void CreateLambdaObjectForLambdaReference(ir::ArrowFunctionExpression *lambda, ETSObjectType *functional_interface); ir::ClassProperty *CreateLambdaCapturedField(const binder::Variable *captured_var, binder::ClassScope *scope, size_t &idx, const lexer::SourcePosition &pos); @@ -435,6 +436,7 @@ public: Type *GetTypeFromTypeAnnotation(ir::TypeNode *type_annotation); void AddNullParamsForDefaultParams(const Signature *signature, ArenaVector &arguments, ETSChecker *checker); + ir::ScriptFunction *EnclosingFunction(ir::Expression *expr); // Exception ETSObjectType *CheckExceptionOrErrorType(checker::Type *type, lexer::SourcePosition pos); diff --git a/checker/ets/function.cpp b/checker/ets/function.cpp index 22047ad76c87613ddf6812e1cbe69c99633f41b9..467878d944611ba5eeb68dc78c37924f03e6b62f 100644 --- a/checker/ets/function.cpp +++ b/checker/ets/function.cpp @@ -1065,12 +1065,30 @@ Signature *ETSChecker::GetSignatureFromMethodDefinition(const ir::MethodDefiniti return nullptr; } -void ETSChecker::ValidateSignatureAccessibility(ETSObjectType *callee, Signature *signature, - const lexer::SourcePosition &pos) +void ETSChecker::ValidateSignatureAccessibility(ETSObjectType *callee, [[maybe_unused]] ir::CallExpression *call_expr, + Signature *signature, const lexer::SourcePosition &pos) { if (signature->HasSignatureFlag(SignatureFlags::PRIVATE) || signature->HasSignatureFlag(SignatureFlags::PROTECTED)) { - ASSERT(callee->GetDeclNode() && callee->GetDeclNode()->IsClassDefinition()); + ASSERT(callee->GetDeclNode() && + (callee->GetDeclNode()->IsClassDefinition() || callee->GetDeclNode()->IsTSInterfaceDeclaration())); + + if (callee->GetDeclNode()->IsTSInterfaceDeclaration()) { + if (call_expr->Callee()->IsMemberExpression() && + call_expr->Callee()->AsMemberExpression()->Object()->IsThisExpression()) { + const auto *enclosing_func = EnclosingFunction(call_expr); + if (signature->Function()->IsPrivate() && !enclosing_func->IsPrivate()) { + ThrowTypeError({"Cannot reference 'this' in this context."}, enclosing_func->Start()); + } + } + + if (Context().ContainingClass() == callee->GetDeclNode()->AsTSInterfaceDeclaration()->TsType() && + callee->GetDeclNode()->AsTSInterfaceDeclaration()->TsType()->AsETSObjectType()->IsSignatureInherited( + signature)) { + return; + } + } + if (Context().ContainingClass() == callee->GetDeclNode()->AsClassDefinition()->TsType() && callee->GetDeclNode()->AsClassDefinition()->TsType()->AsETSObjectType()->IsSignatureInherited(signature)) { return; diff --git a/checker/ets/helpers.cpp b/checker/ets/helpers.cpp index 62ce4503fa04b1b3084d956b22c11362b1f4cad1..ad8dfc8ecb4d98ebb6cc9e046421f9d426da71f6 100644 --- a/checker/ets/helpers.cpp +++ b/checker/ets/helpers.cpp @@ -1826,6 +1826,21 @@ Type *ETSChecker::GetTypeFromTypeAnnotation(ir::TypeNode *const type_annotation) return type; } +ir::ScriptFunction *ETSChecker::EnclosingFunction(ir::Expression *expr) +{ + ir::AstNode *iter = expr; + + while (iter != nullptr) { + if (iter->IsScriptFunction()) { + return iter->AsScriptFunction(); + } + + iter = iter->Parent(); + } + + return nullptr; +} + void ETSChecker::CheckValidGenericTypeParameter(Type *const arg_type, const lexer::SourcePosition &pos) { if (!arg_type->IsETSEnumType()) { diff --git a/checker/ets/object.cpp b/checker/ets/object.cpp index 5160a1fd23a350a4e5dd74b1a4e48538f28d8150..df389e67efbab42557b4d18ae97bcc1b2052c623 100644 --- a/checker/ets/object.cpp +++ b/checker/ets/object.cpp @@ -931,7 +931,7 @@ ETSObjectType *ETSChecker::CheckThisOrSuperAccess(ir::Expression *node, ETSObjec ThrowTypeError({"'", msg, "' cannot be referenced from a static context"}, node->Start()); } - if (class_type->GetDeclNode()->AsClassDefinition()->IsGlobal()) { + if (class_type->GetDeclNode()->IsClassDefinition() && class_type->GetDeclNode()->AsClassDefinition()->IsGlobal()) { ThrowTypeError({"Cannot reference '", msg, "' in this context."}, node->Start()); } diff --git a/ir/ets/etsNewClassInstanceExpression.cpp b/ir/ets/etsNewClassInstanceExpression.cpp index 129c83819674b80997606b97330a52623cf0614d..a2e933e66a148d717b3b45cab975702b5a995419 100644 --- a/ir/ets/etsNewClassInstanceExpression.cpp +++ b/ir/ets/etsNewClassInstanceExpression.cpp @@ -149,7 +149,7 @@ checker::Type *ETSNewClassInstanceExpression::Check([[maybe_unused]] checker::ET signature_ = checker->ResolveConstructExpression(callee_obj, arguments_, Start()); checker->CheckObjectLiteralArguments(signature_, arguments_); - checker->ValidateSignatureAccessibility(callee_obj, signature_, Start()); + checker->ValidateSignatureAccessibility(callee_obj, nullptr, signature_, Start()); ASSERT(signature_->Function() != nullptr); diff --git a/ir/expressions/callExpression.cpp b/ir/expressions/callExpression.cpp index 6527b52e111fe2c01ca4c8617a813a327b5dfe78..81009d21d118492eb1d98cfbf4e840278291b9cb 100644 --- a/ir/expressions/callExpression.cpp +++ b/ir/expressions/callExpression.cpp @@ -349,7 +349,7 @@ checker::Type *CallExpression::Check(checker::ETSChecker *checker) callee_obj = callee_->AsMemberExpression()->ObjType(); } - checker->ValidateSignatureAccessibility(callee_obj, signature_, Start()); + checker->ValidateSignatureAccessibility(callee_obj, this, signature_, Start()); } ASSERT(signature_->Function() != nullptr); diff --git a/ir/expressions/memberExpression.cpp b/ir/expressions/memberExpression.cpp index 909371b7159d0d762e7389eb6f658b66dc5d44c8..53ebababd70bd487fa74e87443d9670bd23ebf5c 100644 --- a/ir/expressions/memberExpression.cpp +++ b/ir/expressions/memberExpression.cpp @@ -400,10 +400,10 @@ checker::Type *MemberExpression::Check(checker::ETSChecker *checker) for (auto *sig : func_type->CallSignatures()) { if (sig->Function()->IsSetter()) { AddMemberKind(ir::MemberExpressionKind::SETTER); - checker->ValidateSignatureAccessibility(obj_type_, sig, Start()); + checker->ValidateSignatureAccessibility(obj_type_, nullptr, sig, Start()); } else if (sig->Function()->IsGetter()) { AddMemberKind(ir::MemberExpressionKind::GETTER); - checker->ValidateSignatureAccessibility(obj_type_, sig, Start()); + checker->ValidateSignatureAccessibility(obj_type_, nullptr, sig, Start()); } } } diff --git a/ir/expressions/objectExpression.cpp b/ir/expressions/objectExpression.cpp index 684a1564b9ec0cad95589ff916fd065becd3fb16..4a93b58bf13bf8e9c17a1b8e2eef1610dbd31c4c 100644 --- a/ir/expressions/objectExpression.cpp +++ b/ir/expressions/objectExpression.cpp @@ -763,7 +763,7 @@ checker::Type *ObjectExpression::Check(checker::ETSChecker *checker) for (checker::Signature *sig : obj_type->ConstructSignatures()) { if (sig->Params().empty()) { have_empty_constructor = true; - checker->ValidateSignatureAccessibility(obj_type, sig, Start()); + checker->ValidateSignatureAccessibility(obj_type, nullptr, sig, Start()); break; } } diff --git a/test/parser/ets/InterfacePrivateMethod-expected.txt b/test/parser/ets/InterfacePrivateMethod-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..839e44e90316d7cc7d84895df5f369410d775f4c --- /dev/null +++ b/test/parser/ets/InterfacePrivateMethod-expected.txt @@ -0,0 +1,1158 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSInterfaceDeclaration", + "body": { + "type": "TSInterfaceBody", + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "getHorsePower3", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "kind": "method", + "accessibility": "private", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "getHorsePower3", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "rpm", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 33 + }, + "end": { + "line": 17, + "column": 36 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 28 + }, + "end": { + "line": 17, + "column": 36 + } + } + }, + { + "type": "Identifier", + "name": "trq", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 43 + }, + "end": { + "line": 17, + "column": 46 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 38 + }, + "end": { + "line": 17, + "column": 46 + } + } + } + ], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 49 + }, + "end": { + "line": 17, + "column": 52 + } + } + }, + "declare": true, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "BinaryExpression", + "operator": "/", + "left": { + "type": "BinaryExpression", + "operator": "*", + "left": { + "type": "Identifier", + "name": "rpm", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "right": { + "type": "Identifier", + "name": "trq", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 23 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 16 + }, + "end": { + "line": 18, + "column": 27 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 5252, + "loc": { + "start": { + "line": 18, + "column": 30 + }, + "end": { + "line": 18, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 16 + }, + "end": { + "line": 18, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 35 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 52 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 27 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 27 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "getPwrIndex", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 16 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "getPwrIndex", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 16 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "trq", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 21, + "column": 22 + }, + "end": { + "line": 21, + "column": 25 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 17 + }, + "end": { + "line": 21, + "column": 25 + } + } + } + ], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 21, + "column": 28 + }, + "end": { + "line": 21, + "column": 31 + } + } + }, + "declare": true, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 20 + } + } + }, + "property": { + "type": "Identifier", + "name": "getHorsePower3", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 21 + }, + "end": { + "line": 22, + "column": 35 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 35 + } + } + }, + "arguments": [ + { + "type": "NumberLiteral", + "value": 5252, + "loc": { + "start": { + "line": 22, + "column": 36 + }, + "end": { + "line": 22, + "column": 40 + } + } + }, + { + "type": "Identifier", + "name": "trq", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 42 + }, + "end": { + "line": 22, + "column": 45 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 47 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 31 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 16 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 16 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 23, + "column": 6 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "id": { + "type": "Identifier", + "name": "Vehicle", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 18 + } + } + }, + "extends": [], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 26, + "column": 6 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Car", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 7 + }, + "end": { + "line": 26, + "column": 10 + } + } + }, + "superClass": null, + "implements": [ + { + "type": "TSClassImplements", + "expression": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Vehicle", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 22 + }, + "end": { + "line": 26, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 22 + }, + "end": { + "line": 26, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 22 + }, + "end": { + "line": 26, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 22 + }, + "end": { + "line": 26, + "column": 30 + } + } + } + ], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 31 + }, + "end": { + "line": 26, + "column": 31 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 29 + }, + "end": { + "line": 26, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 1 + }, + "end": { + "line": 26, + "column": 31 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 10 + }, + "end": { + "line": 28, + "column": 14 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 10 + }, + "end": { + "line": 28, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 28, + "column": 18 + }, + "end": { + "line": 28, + "column": 22 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Car", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 16 + }, + "end": { + "line": 29, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 16 + }, + "end": { + "line": 29, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 16 + }, + "end": { + "line": 29, + "column": 20 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 29, + "column": 12 + }, + "end": { + "line": 29, + "column": 22 + } + } + }, + "property": { + "type": "Identifier", + "name": "getPwrIndex", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 22 + }, + "end": { + "line": 29, + "column": 33 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 29, + "column": 12 + }, + "end": { + "line": 29, + "column": 33 + } + } + }, + "arguments": [ + { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 29, + "column": 34 + }, + "end": { + "line": 29, + "column": 35 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 29, + "column": 12 + }, + "end": { + "line": 29, + "column": 36 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 29, + "column": 40 + }, + "end": { + "line": 29, + "column": 41 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 12 + }, + "end": { + "line": 29, + "column": 41 + } + } + }, + "second": null, + "loc": { + "start": { + "line": 29, + "column": 5 + }, + "end": { + "line": 29, + "column": 42 + } + } + } + ], + "loc": { + "start": { + "line": 28, + "column": 23 + }, + "end": { + "line": 30, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 14 + }, + "end": { + "line": 30, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 14 + }, + "end": { + "line": 30, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 1 + }, + "end": { + "line": 30, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 31, + "column": 1 + } + } +} +TypeError: Cannot reference 'this' in this context. [InterfacePrivateMethod.ets:21:16] diff --git a/test/parser/ets/InterfacePrivateMethod.ets b/test/parser/ets/InterfacePrivateMethod.ets new file mode 100644 index 0000000000000000000000000000000000000000..36f491b499a24fd85547f0c5dab61adde8cba788 --- /dev/null +++ b/test/parser/ets/InterfacePrivateMethod.ets @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed 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. + */ + +interface Vehicle { + private getHorsePower3(rpm: int, trq: int): int{ + return (rpm * trq) / 5252; + } + + getPwrIndex(trq: int): int{ + return this.getHorsePower3(5252, trq); + } +} + +class Car implements Vehicle{} + +function main(): void { + assert new Car().getPwrIndex(1) == 1; +} diff --git a/test/runtime/ets/InterfacePrivateMethod2.ets b/test/runtime/ets/InterfacePrivateMethod2.ets new file mode 100644 index 0000000000000000000000000000000000000000..d493737f77652e871c9f3baac802662fb73ca981 --- /dev/null +++ b/test/runtime/ets/InterfacePrivateMethod2.ets @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed 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. + */ + +interface Vehicle { + getHorsePower3(rpm: int, trq: int): int{ + return (rpm * trq) / 5252; + } + + getPwrIndex(trq: int): int{ + return this.getHorsePower3(5252, trq); + } + + getHorsePower3(rpm: int): int{ + return rpm; + } + + getPwrIndex(trq: int, second: int): int{ + return this.getHorsePower3(trq + second); + } +} + +class Car implements Vehicle{} + +function main(): void { + assert new Car().getPwrIndex(1) == 1; + assert new Car().getPwrIndex(1,1) == 2; +}