diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 74631ae9763e2b213ee071ef4526bb980238433b..87787c5d78dc5ef19f1d1dbd7d329734a97d962a 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -16,6 +16,7 @@ #include "ETSAnalyzer.h" #include "checker/ETSchecker.h" +#include "compiler/lowering/util.h" #include "generated/diagnostic.h" #include "checker/types/globalTypesHolder.h" #include "checker/types/ets/etsTupleType.h" @@ -23,7 +24,6 @@ #include "evaluate/scopedDebugInfoPlugin.h" #include "types/signature.h" #include "compiler/lowering/ets/setJumpTarget.h" -#include "compiler/lowering/util.h" #include "checker/types/ets/etsAsyncFuncReturnType.h" #include "types/ts/nullType.h" #include "types/type.h" @@ -107,7 +107,18 @@ checker::Type *ETSAnalyzer::Check(ir::ClassProperty *st) const ETSChecker *checker = GetETSChecker(); if (st->Id()->Variable() == nullptr) { - st->Id()->Check(checker); + // Now invalid or dummy nodes obtaining after parsing don't have associated variables at all, that leads to + // incorrect AST and multiple reported errors in AST verifier. Need to create and bind [special]? variables for + // them with default TypeError set[?]. Why can't we directly check the 'Id'? During the process of + // resolveIdentifier, we might obtain the wrong variable, which breaks the consistency between the variable and + // its tsType. see wrong_variable_binding.ets for more details. + auto ident = st->Id(); + auto [decl, var] = checker->VarBinder()->NewVarDecl( + ident->Start(), compiler::GenName(checker->ProgramAllocator()).View()); + var->SetScope(checker->VarBinder()->GetScope()); + ident->SetVariable(var); + decl->BindNode(ident); + ident->SetTsType(var->SetTsType(checker->GlobalTypeError())); } ES2PANDA_ASSERT(st->Id()->Variable() != nullptr); diff --git a/ets2panda/checker/ets/utilityTypeHandlers.cpp b/ets2panda/checker/ets/utilityTypeHandlers.cpp index d8d65c3a544707e40472c8522be805192795cf92..93f6b5eccc737df9ed001d1f9d02975aa04d2d76 100644 --- a/ets2panda/checker/ets/utilityTypeHandlers.cpp +++ b/ets2panda/checker/ets/utilityTypeHandlers.cpp @@ -123,11 +123,7 @@ static T *CloneNodeIfNotNullptr(T *node, ArenaAllocator *allocator) Type *ETSChecker::CreatePartialType(Type *const typeToBePartial) { ES2PANDA_ASSERT(typeToBePartial->IsETSReferenceType()); - if (typeToBePartial->IsTypeError()) { - return typeToBePartial; - } - - if (typeToBePartial->IsETSAnyType()) { + if (typeToBePartial->IsTypeError() || typeToBePartial->IsETSNeverType() || typeToBePartial->IsETSAnyType()) { return typeToBePartial; } diff --git a/ets2panda/test/ast/compiler/ets/bad_call_setter.ets b/ets2panda/test/ast/compiler/ets/bad_call_setter.ets index 2bfd2c9474fc77f2e8e5bb70fec88ac0dd2ba02f..0e68c90948a0b0e4020b1ba1fc04c6a4ea1b623b 100644 --- a/ets2panda/test/ast/compiler/ets/bad_call_setter.ets +++ b/ets2panda/test/ast/compiler/ets/bad_call_setter.ets @@ -31,5 +31,4 @@ function main() { /* @@? 23:5 Error SyntaxError: Unexpected token 'this'. */ /* @@? 23:9 Error SyntaxError: Unexpected token '.'. */ /* @@? 23:10 Error TypeError: Variable 'text' has already been declared. */ -/* @@? 23:10 Error TypeError: Property 'text' must be accessed through 'this' */ /* @@? 23:17 Error TypeError: Property 's' must be accessed through 'this' */ diff --git a/ets2panda/test/ast/compiler/ets/same_name_field_err.ets b/ets2panda/test/ast/compiler/ets/same_name_field_err.ets index 8c8f75dc32dc87b44c657e16073796c8834474a2..fdfa470be384a0a806dab24a940e14e528ea91e6 100644 --- a/ets2panda/test/ast/compiler/ets/same_name_field_err.ets +++ b/ets2panda/test/ast/compiler/ets/same_name_field_err.ets @@ -24,4 +24,3 @@ function main(): void { } /* @@? 19:12 Error TypeError: Variable 'a' has already been declared. */ -/* @@? 19:12 Error TypeError: Property 'a' must be accessed through 'this' */ diff --git a/ets2panda/test/ast/compiler/ets/unresolve_class_with_type_infer.ets b/ets2panda/test/ast/compiler/ets/unresolve_class_with_type_infer.ets new file mode 100644 index 0000000000000000000000000000000000000000..9347301cc3f96cac0dba997c2b30dd0fec9a5b9e --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/unresolve_class_with_type_infer.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2025 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. + */ + +function foo>(record: Record>){ + +} + +foo({}) + +/* @@? 16:39 Error TypeError: Cannot find type 'A'. */ diff --git a/ets2panda/test/ast/compiler/ets/wrong_variable_binding.ets b/ets2panda/test/ast/compiler/ets/wrong_variable_binding.ets new file mode 100644 index 0000000000000000000000000000000000000000..c9f2d161ac777d0a94186bbc33fbac1ace9ae92b --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/wrong_variable_binding.ets @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2025 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. + */ + + +class C { + + public C; + + public C: "+v)"; + + public constructor() {} + + } + + class D extends C{} + +/* @@? 19:13 Error SyntaxError: Field type annotation expected. */ +/* @@? 21:12 Error TypeError: Variable 'C' has already been declared. */ \ No newline at end of file diff --git a/ets2panda/test/ast/parser/ets/FixedArray/spreadexpr_in_newexpr_neg01.ets b/ets2panda/test/ast/parser/ets/FixedArray/spreadexpr_in_newexpr_neg01.ets index ff2284a2983df75e6b7338f194c91987d71a4485..7eef9e9c85e61c440dcd6661ba7e7a5e12fc73a2 100644 --- a/ets2panda/test/ast/parser/ets/FixedArray/spreadexpr_in_newexpr_neg01.ets +++ b/ets2panda/test/ast/parser/ets/FixedArray/spreadexpr_in_newexpr_neg01.ets @@ -33,6 +33,5 @@ function main() { /* @@? 19:9 Error SyntaxError: Unexpected token 'this'. */ /* @@? 19:13 Error SyntaxError: Unexpected token '.'. */ /* @@? 19:14 Error TypeError: Variable 'fld' has already been declared. */ -/* @@? 19:14 Error TypeError: Property 'fld' must be accessed through 'this' */ /* @@? 19:20 Error TypeError: Unresolved reference p */ /* @@? 21:1 Error SyntaxError: Unexpected token '}'. */ diff --git a/ets2panda/test/ast/parser/ets/class-instance-field-redeclaration.ets b/ets2panda/test/ast/parser/ets/class-instance-field-redeclaration.ets index ef8d91f4ec84dbd0bdf6199e3a05f264d41c0dd5..5724caccac56b6e47b75648daeaaed243d5b1a28 100644 --- a/ets2panda/test/ast/parser/ets/class-instance-field-redeclaration.ets +++ b/ets2panda/test/ast/parser/ets/class-instance-field-redeclaration.ets @@ -19,4 +19,3 @@ class C { } /* @@@ label Error TypeError: Variable 'foo' has already been declared. */ -/* @@@ label Error TypeError: Property 'foo' must be accessed through 'this' */ diff --git a/ets2panda/test/ast/parser/ets/class-static-field-redeclaration.ets b/ets2panda/test/ast/parser/ets/class-static-field-redeclaration.ets index 87471276b09cc49968c4d2105a525345c4dabd4e..7951f410c99a0826dd9d00f62d92d5022f853a9c 100644 --- a/ets2panda/test/ast/parser/ets/class-static-field-redeclaration.ets +++ b/ets2panda/test/ast/parser/ets/class-static-field-redeclaration.ets @@ -19,4 +19,3 @@ class C { } /* @@@ label Error TypeError: Variable 'foo' has already been declared. */ -/* @@@ label Error TypeError: Static property 'foo' must be accessed through it's class 'C' */ diff --git a/ets2panda/test/ast/parser/ets/enum22.ets b/ets2panda/test/ast/parser/ets/enum22.ets index 1205a10d2bd28bec3efe710fa465df924a2036c3..b052216cadf5f248d42a0991f53e6cafe16ce94e 100644 --- a/ets2panda/test/ast/parser/ets/enum22.ets +++ b/ets2panda/test/ast/parser/ets/enum22.ets @@ -18,4 +18,3 @@ enum duplicateKeysStringCase { /* @@ label */Gray = "#ff808080" } /* @@@ label Error TypeError: Variable 'Gray' has already been declared. */ -/* @@@ label Error TypeError: Static property 'Gray' must be accessed through it's class 'duplicateKeysStringCase' */ diff --git a/ets2panda/test/ast/parser/ets/enum23.ets b/ets2panda/test/ast/parser/ets/enum23.ets index 800c35446e74cec12e57ace55229ce00085a88af..fbea4d6c3b28da754b46afcf9aefa975a6e94f11 100644 --- a/ets2panda/test/ast/parser/ets/enum23.ets +++ b/ets2panda/test/ast/parser/ets/enum23.ets @@ -17,5 +17,4 @@ enum duplicateKeysNumCase { Red = 1, /* @@ label */Red = 1 } -/* @@@ label Error TypeError: Variable 'Red' has already been declared. */ -/* @@@ label Error TypeError: Static property 'Red' must be accessed through it's class 'duplicateKeysNumCase' */ +/* @@@ label Error TypeError: Variable 'Red' has already been declared. */ \ No newline at end of file diff --git a/ets2panda/test/ast/parser/ets/fields.ets b/ets2panda/test/ast/parser/ets/fields.ets index 414efc3575499d93614f562d11a6059d086b8b90..31cdc48edbacaced6e755ac2afbb339edea4b54f 100644 --- a/ets2panda/test/ast/parser/ets/fields.ets +++ b/ets2panda/test/ast/parser/ets/fields.ets @@ -28,4 +28,3 @@ class C { } /* @@@ label Error TypeError: Variable 'f' has already been declared. */ -/* @@@ label Error TypeError: Static property 'f' must be accessed through it's class 'C' */ diff --git a/ets2panda/test/ast/parser/ets/spreadexpr_in_newexpr_neg01.ets b/ets2panda/test/ast/parser/ets/spreadexpr_in_newexpr_neg01.ets index 2a6b971d872d7eac87a6a3dfe1b4be0454b952f7..346e038a118c139cdee0e38a75a5b3360deb2467 100644 --- a/ets2panda/test/ast/parser/ets/spreadexpr_in_newexpr_neg01.ets +++ b/ets2panda/test/ast/parser/ets/spreadexpr_in_newexpr_neg01.ets @@ -33,6 +33,5 @@ function main() { /* @@? 19:9 Error SyntaxError: Unexpected token 'this'. */ /* @@? 19:13 Error SyntaxError: Unexpected token '.'. */ /* @@? 19:14 Error TypeError: Variable 'fld' has already been declared. */ -/* @@? 19:14 Error TypeError: Property 'fld' must be accessed through 'this' */ /* @@? 19:20 Error TypeError: Unresolved reference p */ -/* @@? 21:1 Error SyntaxError: Unexpected token '}'. */ +/* @@? 21:1 Error SyntaxError: Unexpected token '}'. */ \ No newline at end of file