From 5c69b9c0e2e762264f9ea8f72077f9331fc96a33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20Gel=C3=A1nyi?= Date: Thu, 12 Oct 2023 17:37:50 +0200 Subject: [PATCH] Add proper error message to override keyw used in interfaces MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add proper error message to override keyword used in interfaces Create new test for override keyword used in interfaces Add override keyword to TokentoString method Signed-off-by: András Gelányi --- lexer/token/token.cpp | 2 ++ parser/TypedParser.cpp | 4 +++ .../interface-override-function-expected.txt | 1 + .../ets/interface-override-function.ets | 27 +++++++++++++++++++ 4 files changed, 34 insertions(+) create mode 100644 test/parser/ets/interface-override-function-expected.txt create mode 100644 test/parser/ets/interface-override-function.ets diff --git a/lexer/token/token.cpp b/lexer/token/token.cpp index 1a991e17a..610b0ce81 100644 --- a/lexer/token/token.cpp +++ b/lexer/token/token.cpp @@ -421,6 +421,8 @@ const char *TokenToString(TokenType type) // NOLINT(readability-function-size) return "eos"; case TokenType::KEYW_OUT: return "out"; + case TokenType::KEYW_OVERRIDE: + return "override"; default: return ""; } diff --git a/parser/TypedParser.cpp b/parser/TypedParser.cpp index 240509d81..9e00c82fe 100644 --- a/parser/TypedParser.cpp +++ b/parser/TypedParser.cpp @@ -594,6 +594,10 @@ ArenaVector TypedParser::ParseTypeLiteralOrInterface() ArenaVector members(Allocator()->Adapter()); + if (Lexer()->GetToken().Type() == lexer::TokenType::KEYW_OVERRIDE) { + ThrowSyntaxError("'override' modifier cannot appear in interfaces"); + } + while (Lexer()->GetToken().Type() != lexer::TokenType::PUNCTUATOR_RIGHT_BRACE) { ir::AstNode *member = ParseTypeLiteralOrInterfaceMember(); diff --git a/test/parser/ets/interface-override-function-expected.txt b/test/parser/ets/interface-override-function-expected.txt new file mode 100644 index 000000000..e885c51ad --- /dev/null +++ b/test/parser/ets/interface-override-function-expected.txt @@ -0,0 +1 @@ +SyntaxError: 'override' modifier cannot appear in interfaces [interface-override-function.ets:23:5] diff --git a/test/parser/ets/interface-override-function.ets b/test/parser/ets/interface-override-function.ets new file mode 100644 index 000000000..d1570405b --- /dev/null +++ b/test/parser/ets/interface-override-function.ets @@ -0,0 +1,27 @@ +/* + * 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 K { + foo(): int { + return 1; + } +} + +interface J extends K { + override foo(): int { + return 0; + } +} + -- Gitee