diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index f46c3706ceb4ab90c682689abae2b818260a9839..9af20f441f6dd012a57dcef538f6e9bcdb0756c4 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -2569,17 +2569,26 @@ void ETSChecker::InferTypesForLambda(ir::ScriptFunction *lambda, ir::ETSFunction continue; } auto *const lambdaParam = lambda->Params().at(i)->AsETSParameterExpression()->Ident(); - if (lambdaParam->TypeAnnotation() == nullptr) { - // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) - Type *inferredType = calleeType->Params()[i]->AsETSParameterExpression()->TypeAnnotation()->Check(this); - bool isPrimitive = inferredType != nullptr && inferredType->IsETSPrimitiveType(); - if (!isPrimitive && maybeSubstitutedFunctionSig != nullptr) { - ES2PANDA_ASSERT(maybeSubstitutedFunctionSig->Params().size() == calleeType->Params().size()); + if (lambdaParam->TypeAnnotation() != nullptr) { + continue; + } + + // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) + Type *inferredType = calleeType->Params()[i]->AsETSParameterExpression()->TypeAnnotation()->Check(this); + bool isPrimitive = inferredType != nullptr && inferredType->IsETSPrimitiveType(); + if (!isPrimitive && maybeSubstitutedFunctionSig != nullptr) { + auto sigParamSize = maybeSubstitutedFunctionSig->Params().size(); + ES2PANDA_ASSERT( + sigParamSize == calleeType->Params().size() || + (maybeSubstitutedFunctionSig->HasRestParameter() && sigParamSize <= calleeType->Params().size())); + if (i < sigParamSize) { inferredType = maybeSubstitutedFunctionSig->Params()[i]->TsType(); + } else if (!maybeSubstitutedFunctionSig->RestVar()->TsType()->IsETSTupleType()) { + inferredType = GetElementTypeOfArray(maybeSubstitutedFunctionSig->RestVar()->TsType()); } - lambdaParam->Variable()->SetTsType(inferredType); - lambdaParam->SetTsType(inferredType); } + lambdaParam->Variable()->SetTsType(inferredType); + lambdaParam->SetTsType(inferredType); } if (lambda->ReturnTypeAnnotation() == nullptr) { diff --git a/ets2panda/checker/ets/object.cpp b/ets2panda/checker/ets/object.cpp index 2593bb7321606f8cafc89fa06f99c925821912a9..7bc41f6a680901f3565e1dc177ef2e72e378fdcd 100644 --- a/ets2panda/checker/ets/object.cpp +++ b/ets2panda/checker/ets/object.cpp @@ -1848,7 +1848,8 @@ ETSObjectType *ETSChecker::CheckThisOrSuperAccess(ir::Expression *node, ETSObjec return classType; } - if (classType->GetDeclNode()->IsClassDefinition() && classType->GetDeclNode()->AsClassDefinition()->IsGlobal()) { + if (classType == nullptr || + (classType->GetDeclNode()->IsClassDefinition() && classType->GetDeclNode()->AsClassDefinition()->IsGlobal())) { LogError(diagnostic::CTOR_REF_INVALID_CTX_GLOBAL, {msg}, node->Start()); return GlobalBuiltinErrorType(); } diff --git a/ets2panda/checker/types/ets/etsObjectType.cpp b/ets2panda/checker/types/ets/etsObjectType.cpp index 4dfd0e2fbcea0bc08dd2bcbf6666e0f5c81ffae7..4bf8a2f7c12e504a9444cd6f11cb62e1de1d65c8 100644 --- a/ets2panda/checker/types/ets/etsObjectType.cpp +++ b/ets2panda/checker/types/ets/etsObjectType.cpp @@ -648,15 +648,14 @@ void ETSObjectType::SubstitutePartialTypes(TypeRelation *relation, Type *other) void ETSObjectType::IdenticalUptoTypeArguments(TypeRelation *relation, Type *other) { relation->Result(false); + if (!other->IsETSObjectType() || !CheckIdenticalFlags(other->AsETSObjectType())) { + return; + } if (IsPartial()) { SubstitutePartialTypes(relation, other); } - if (!other->IsETSObjectType() || !CheckIdenticalFlags(other->AsETSObjectType())) { - return; - } - // NOTE: (DZ) only both Partial types can be compatible. if (static_cast(static_cast(IsPartial()) ^ static_cast(other->AsETSObjectType()->IsPartial()))) { diff --git a/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp b/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp index 803b3945c0816bcab014d699999fdede9f35b120..497cf9a0b99e8f4376ef2a8774d8a36c0c88a520 100644 --- a/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp +++ b/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp @@ -1272,10 +1272,11 @@ static bool IsInTSEnumMemberInit(const ir::AstNode *n) ir::AstNode *ConstantExpressionLowering::UnfoldResolvedReference(ir::AstNode *resolved, ir::AstNode *node) { - if (unfoldingSet_.count(resolved) > 0) { + checker::RecursionPreserver rPreserver(unfoldingSet_, resolved); + if (*rPreserver) { + isSelfDependence_ = true; return node; } - unfoldingSet_.insert(resolved); ir::AstNode *resNode = nullptr; if (resolved->IsClassProperty()) { @@ -1300,7 +1301,11 @@ ir::AstNode *ConstantExpressionLowering::UnfoldResolvedReference(ir::AstNode *re if (resNode != nullptr) { auto res = MaybeUnfold(resNode); - unfoldingSet_.erase(resolved); + if (isSelfDependence_) { + isSelfDependence_ = false; + return node; + } + return res; } diff --git a/ets2panda/compiler/lowering/ets/constantExpressionLowering.h b/ets2panda/compiler/lowering/ets/constantExpressionLowering.h index 531871759fc22641437a042d9ec846634f106f64..ee985d150aa5ecaaf932e05ebfd31d0fde23c577 100644 --- a/ets2panda/compiler/lowering/ets/constantExpressionLowering.h +++ b/ets2panda/compiler/lowering/ets/constantExpressionLowering.h @@ -55,6 +55,7 @@ private: public_lib::Context *context_ {nullptr}; parser::Program *program_ {nullptr}; varbinder::ETSBinder *varbinder_ {nullptr}; + bool isSelfDependence_ = {false}; std::unordered_set unfoldingSet_; }; diff --git a/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.cpp b/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.cpp index e36ae97b91d21132b6e00a4ec2d7afd9ee9ccf41..b97fd7aed28f1faec6a85f3618f521220149f18a 100644 --- a/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.cpp +++ b/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.cpp @@ -238,6 +238,15 @@ void ScopesInitPhase::VisitVariableDeclarator(ir::VariableDeclarator *varDecl) auto init = varDecl->Id(); std::vector bindings = util::Helpers::CollectBindingNames(VarBinder(), init); for (auto *binding : bindings) { + auto name = binding->Name(); + if (binding->Variable() == nullptr && !name.Is(ERROR_LITERAL) && VarBinder()->IsETSBinder()) { + auto var = VarBinder()->GetScope()->FindLocal(name, varbinder::ResolveBindingOptions::ALL_VARIABLES); + if (var != nullptr) { + VarBinder()->ThrowRedeclaration(binding->Start(), name, var->Declaration()->Type()); + continue; + } + } + auto [decl, var] = AddOrGetVarDecl(varDecl->Flag(), binding); BindVarDecl(binding, init, decl, var); } @@ -498,7 +507,7 @@ std::tuple ScopesInitPhase::AddOrGetVa name = compiler::GenName(Allocator()).View(); } else if (VarBinder()->IsETSBinder()) { if (auto var = scope->FindLocal(name, varbinder::ResolveBindingOptions::ALL_VARIABLES); var != nullptr) { - VarBinder()->ThrowRedeclaration(id->Start(), name, var->Declaration()->Type()); + ES2PANDA_ASSERT(ctx_->diagnosticEngine->IsAnyError()); return {var->Declaration(), var}; } } diff --git a/ets2panda/test/ast/compiler/ets/block_namespace_interface_name_conflict.ets b/ets2panda/test/ast/compiler/ets/block_namespace_interface_name_conflict.ets new file mode 100644 index 0000000000000000000000000000000000000000..b0a58f1f5848d181784d5697817b2975b65171df --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/block_namespace_interface_name_conflict.ets @@ -0,0 +1,25 @@ + +/* + * 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. + */ + +{ + interface A { + } + namespace A { + } +} + +/* @@? 18:5 Error SyntaxError: Illegal start of INTERFACE expression. */ +/* @@? 20:5 Error SyntaxError: Namespace is allowed only at the top level or inside a namespace. */ diff --git a/ets2panda/test/ast/compiler/ets/class_without_closing_parentheses.ets b/ets2panda/test/ast/compiler/ets/class_without_closing_parentheses.ets index 4c4d1ca68c2f3f6c8f275eeda94077be514aa2bf..2926286b817fccaa6a16213dcdb5454341873c23 100644 --- a/ets2panda/test/ast/compiler/ets/class_without_closing_parentheses.ets +++ b/ets2panda/test/ast/compiler/ets/class_without_closing_parentheses.ets @@ -37,4 +37,4 @@ export default class TreeMap implements ReadonlyTreeMap { /* @@? 21:15 Error TypeError: Variable 'buffer' has already been declared. */ /* @@? 21:15 Error TypeError: Merging declarations is not supported, please keep all definitions of classes, interfaces and enums compact in the codebase! */ /* @@? 24:32 Error TypeError: Indexed access is not supported for such expression type. */ -/* @@? 26:20 Error TypeError: Cannot find type 'buffer'. */ +/* @@? 26:20 Error TypeError: Namespace 'buffer' cannot be used as a type. */ diff --git a/ets2panda/test/ast/compiler/ets/const_unfold_self_dependence01.ets b/ets2panda/test/ast/compiler/ets/const_unfold_self_dependence01.ets new file mode 100644 index 0000000000000000000000000000000000000000..7317988cd7d970e2846351140271eb6c7c544d00 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/const_unfold_self_dependence01.ets @@ -0,0 +1,18 @@ +/* + * 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. + */ + +const a = a + 1; + +/* @@? 16:7 Error TypeError: Circular dependency detected for identifier: a */ diff --git a/ets2panda/test/ast/compiler/ets/const_unfold_self_dependence02.ets b/ets2panda/test/ast/compiler/ets/const_unfold_self_dependence02.ets new file mode 100644 index 0000000000000000000000000000000000000000..c702b429150baf91107b070a4bdb267bf5de6fd7 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/const_unfold_self_dependence02.ets @@ -0,0 +1,23 @@ +/* + * 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. + */ + +const b = c + 1; +const c = d + 1; +const d = e + 1; +const e = f + 1; +const f = g + 1; +const g = b + 1; + +/* @@? 16:7 Error TypeError: Circular dependency detected for identifier: b */ diff --git a/ets2panda/test/ast/compiler/ets/lambda_type_infer_to_rest_type.ets b/ets2panda/test/ast/compiler/ets/lambda_type_infer_to_rest_type.ets new file mode 100644 index 0000000000000000000000000000000000000000..1a449142937d531082b2386380346a1e3776f39b --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/lambda_type_infer_to_rest_type.ets @@ -0,0 +1,20 @@ +/* + * 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. + */ + +declare function testRest(a: (t: T, t1: T, ...ts: T[]) => void): T + +testRest((t1:number, t2:boolean, t3) => {}) + +/* @@? 18:10 Error TypeError: Type '(t1: Double, t2: Boolean, t3: never) => void' is not compatible with type '(t: never, t1: never, ...ts: Array) => void' at index 1 */ diff --git a/ets2panda/test/ast/compiler/ets/redecl_func_arow_param_in_body.ets b/ets2panda/test/ast/compiler/ets/redecl_func_arow_param_in_body.ets new file mode 100644 index 0000000000000000000000000000000000000000..a0f98280f500bc543bcfad24cadfd07042e1dc8b --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/redecl_func_arow_param_in_body.ets @@ -0,0 +1,31 @@ +/* + * 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 ermissions and + * limitatioLicense. + * 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(bar: () => int) { + let x , bar(); + } + + function main(): void { + foo(() => {}); + } + +/* @@? 18:11 Error SyntaxError: Variable must be initialized or it's type must be declared. */ +/* @@? 18:13 Error TypeError: Variable 'bar' has already been declared. */ +/* @@? 18:16 Error SyntaxError: Variable must be initialized or it's type must be declared. */ +/* @@? 18:16 Error SyntaxError: Unexpected token '('. */ +/* @@? 18:17 Error SyntaxError: Unexpected token ')'. */ +/* @@? 18:18 Error SyntaxError: Unexpected token, expected ')'. */ +/* @@? 22:14 Error TypeError: Type 'void' is not compatible with the enclosing method's return type 'Int' */ diff --git a/ets2panda/test/ast/compiler/ets/this_in_wrong_context.ets b/ets2panda/test/ast/compiler/ets/this_in_wrong_context.ets new file mode 100644 index 0000000000000000000000000000000000000000..c1d9cb8eaf92d6d67cb3547ac8911f6a35e04c43 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/this_in_wrong_context.ets @@ -0,0 +1,24 @@ +/* + * 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. + */ + +export declare @interface ClassAuthor { + color: Color[] = [this.listener !== undefined] +} + +/* @@? 1:3 Error TypeError: Cannot find type 'Color'. */ +/* @@? 17:5 Error TypeError: Invalid annotation field type. Only numeric, boolean, string, enum, or arrays of these types are permitted for annotation fields. */ +/* @@? 17:22 Error TypeError: Invalid value for annotation field, expected a constant literal. */ +/* @@? 17:23 Error TypeError: Cannot reference 'this' in this context. */ +/* @@? 17:28 Error TypeError: Property 'listener' does not exist on type 'Error' */ diff --git a/ets2panda/test/ast/compiler/ets/union_type_of_partial_type.ets b/ets2panda/test/ast/compiler/ets/union_type_of_partial_type.ets new file mode 100644 index 0000000000000000000000000000000000000000..b024428363cc3f69df19ac62f9c781c0332e5f3f --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/union_type_of_partial_type.ets @@ -0,0 +1,28 @@ +/* + * 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: * + * 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. + */ + +abstract class A>{ + foo({ }, init: Partial | undefined): void { + } +} + +/* @@? 16:9 Error SyntaxError: Unexpected token, expected an identifier. */ +/* @@? 16:9 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ +/* @@? 16:9 Error SyntaxError: Unexpected token, expected ',' or ')'. */ +/* @@? 16:12 Error SyntaxError: Unexpected token ','. */ +/* @@? 16:42 Error SyntaxError: Unexpected token ')'. */ +/* @@? 16:43 Error SyntaxError: Unexpected token ':'. */ +/* @@? 16:45 Error SyntaxError: void is a predefined type, cannot be used as an identifier */ +/* @@? 16:50 Error SyntaxError: Unexpected token '{'. */ +/* @@? 18:1 Error SyntaxError: Unexpected token '}'. */ diff --git a/ets2panda/varbinder/scope.cpp b/ets2panda/varbinder/scope.cpp index 5060dc2a00421b4892ba402dda32ef35ee25c0b9..6edef25fd9ac9d89502a87cdc43dd867c8e858de 100644 --- a/ets2panda/varbinder/scope.cpp +++ b/ets2panda/varbinder/scope.cpp @@ -238,8 +238,13 @@ Variable *Scope::AddLocalVar(ArenaAllocator *allocator, Decl *newDecl) Variable *Scope::AddLocalInterfaceVariable(ArenaAllocator *allocator, Decl *newDecl) { - auto *var = bindings_.insert({newDecl->Name(), allocator->New(newDecl, VariableFlags::INTERFACE)}) - .first->second; + auto [iter, inserted] = + bindings_.try_emplace(newDecl->Name(), allocator->New(newDecl, VariableFlags::INTERFACE)); + if (!inserted) { + return nullptr; + } + + auto *var = iter->second; if (newDecl->Node() != nullptr) { newDecl->Node()->AsTSInterfaceDeclaration()->Id()->SetVariable(var); } @@ -261,7 +266,12 @@ Variable *Scope::AddLocalClassVariable(ArenaAllocator *allocator, Decl *newDecl) VariableFlags flag = isNamespaceTransformed ? VariableFlags::NAMESPACE : isEnumTransformed ? VariableFlags::ENUM_LITERAL : VariableFlags::CLASS; - auto *var = bindings_.insert({newDecl->Name(), allocator->New(newDecl, flag)}).first->second; + auto [iter, inserted] = bindings_.try_emplace(newDecl->Name(), allocator->New(newDecl, flag)); + if (!inserted) { + return nullptr; + } + + auto *var = iter->second; newDecl->Node()->AsClassDefinition()->Ident()->SetVariable(var); return var; }