diff --git a/ets2panda/checker/ETSchecker.h b/ets2panda/checker/ETSchecker.h index 06de9f47df7fc3cd76ba9417daf3c146cc31b52d..095c0cfa6e3d0172b8546d73096e6952bd42ae02 100644 --- a/ets2panda/checker/ETSchecker.h +++ b/ets2panda/checker/ETSchecker.h @@ -944,6 +944,7 @@ private: void ValidateNewClassInstanceIdentifier(ir::Identifier *const ident); void ValidateMemberIdentifier(ir::Identifier *const ident); void ValidateAssignmentIdentifier(ir::Identifier *const ident, Type *const type); + void ValidateReturnStatementIdentifier(ir::Identifier *const ident, Type *const type); bool ValidateBinaryExpressionIdentifier(ir::Identifier *const ident, Type *const type); ETSFunctionType *ResolveAccessorTypeByFlag(ir::MemberExpression *const memberExpr, ETSFunctionType *propType, ETSFunctionType *funcType, PropertySearchFlags searchFlag); diff --git a/ets2panda/checker/ets/validateHelpers.cpp b/ets2panda/checker/ets/validateHelpers.cpp index f3ad9a385b9c4e0619ab024c454748e9602ecc35..65e6650e02d2a626a3728bdd464004fcf0548e5b 100644 --- a/ets2panda/checker/ets/validateHelpers.cpp +++ b/ets2panda/checker/ets/validateHelpers.cpp @@ -121,6 +121,12 @@ void ETSChecker::ValidateAssignmentIdentifier(ir::Identifier *const ident, Type WrongContextErrorClassifyByType(ident); return; } + + if (assignmentExpr->Right() == ident && (resolved != nullptr) && type->IsETSFunctionType() && + resolved->Declaration()->IsTypeAliasDecl()) { + // assign a typealias: type RF = (p: int) => int; x = RF; + WrongContextErrorClassifyByType(ident); + } } bool ETSChecker::ValidateBinaryExpressionIdentifier(ir::Identifier *const ident, Type *const type) @@ -148,6 +154,18 @@ bool ETSChecker::ValidateBinaryExpressionIdentifier(ir::Identifier *const ident, return isFinished; } +void ETSChecker::ValidateReturnStatementIdentifier(ir::Identifier *const ident, Type *const type) +{ + varbinder::Variable *const resolved = ident->Variable(); + if (resolved != nullptr && !resolved->Declaration()->PossibleTDZ() && !type->IsETSFunctionType()) { + WrongContextErrorClassifyByType(ident); + } + if (type->IsETSFunctionType() && resolved->Declaration()->IsTypeAliasDecl()) { + // return a typealias + WrongContextErrorClassifyByType(ident); + } +} + void ETSChecker::ValidateResolvedIdentifier(ir::Identifier *const ident) { varbinder::Variable *const resolved = ident->Variable(); @@ -181,6 +199,9 @@ void ETSChecker::ValidateResolvedIdentifier(ir::Identifier *const ident) WrongContextErrorClassifyByType(ident); } break; + case ir::AstNodeType::RETURN_STATEMENT: + ValidateReturnStatementIdentifier(ident, resolvedType); + break; case ir::AstNodeType::ASSIGNMENT_EXPRESSION: ValidateAssignmentIdentifier(ident, resolvedType); break; diff --git a/ets2panda/test/ast/parser/ets/InvalidExpressions.ets b/ets2panda/test/ast/parser/ets/InvalidExpressions.ets index 4a96a1fe0bef86b137e37cc7ba6366f84e05f169..a8bd89bf5663dc459b6e337d486e7b7a10d381c1 100644 --- a/ets2panda/test/ast/parser/ets/InvalidExpressions.ets +++ b/ets2panda/test/ast/parser/ets/InvalidExpressions.ets @@ -57,56 +57,51 @@ function f6(a: int = 9 @@): void; function f7(a: (b: int = 0) => int): void { } +/* @@? TypeError: Postcondition check failed for DefaultParametersLowering */ /* @@? 16:35 Error SyntaxError: Rest parameter cannot have the default value. */ /* @@? 16:37 Error SyntaxError: Identifier is needed here. */ -/* @@? 16:40 Error TypeError: Cannot find type 'bool'. */ -/* @@? 17:20 Error TypeError: Unresolved reference b */ /* @@? 17:21 Error SyntaxError: Unexpected token '...'. */ /* @@? 17:21 Error SyntaxError: Unexpected token, expected ',' or ')'. */ +/* @@? 17:21 Error SyntaxError: Unexpected token '...'. */ /* @@? 17:24 Error SyntaxError: Unexpected token ')'. */ -/* @@? 18:13 Error TypeError: Bad operand type, the types of the operands must be numeric, same enumeration, or boolean type. */ -/* @@? 18:13 Error TypeError: Circular dependency detected for identifier: x */ -/* @@? 18:14 Error TypeError: Variable 'x' is accessed before it's initialization. */ /* @@? 18:35 Error SyntaxError: Unexpected token, expected '>'. */ /* @@? 18:36 Error SyntaxError: Unexpected token ';'. */ -/* @@? 18:36 Error SyntaxError: Unexpected token, expected ')'. */ -/* @@? 19:12 Error TypeError: Bad operand type, the types of the operands must be same type. */ -/* @@? 19:25 Error Warning: Type parameter is erased from type 'A' when used in instanceof expression. */ +/* @@? 19:5 Error SyntaxError: Unexpected token, expected ')'.*/ +/* @@? 19:12 Error SyntaxError: Unexpected token 'x'. */ /* @@? 22:23 Error SyntaxError: Not enable default value with default undefined. */ -/* @@? 24:10 Error TypeError: Only abstract or native methods can't have body. */ -/* @@? 24:22 Error SyntaxError: You didn't set the value. */ /* @@? 24:22 Error SyntaxError: Unexpected token ')'. */ -/* @@? 27:5 Error TypeError: Invalid left-hand side of assignment expression */ -/* @@? 27:8 Error SyntaxError: Invalid left-hand side in assignment expression. */ -/* @@? 28:7 Error SyntaxError: Invalid left-hand side operator. */ -/* @@? 30:5 Error TypeError: Invalid left-hand side of assignment expression */ -/* @@? 30:5 Error SyntaxError: Invalid left-hand side in prefix operation. */ -/* @@? 30:8 Error SyntaxError: Invalid left-hand side in assignment expression. */ +/* @@? 24:22 Error SyntaxError: You didn't set the value. */ +/* @@? 24:23 Error SyntaxError: Unexpected token, expected ',' or ')'. */ +/* @@? 27:8 Error SyntaxError: Invalid left-hand side in assignment expression.*/ +/* @@? 27:10 Error SyntaxError: Unexpected token 'int'. */ +/* @@? 28:7 Error SyntaxError: Invalid left-hand side operator.*/ +/* @@? 30:5 Error SyntaxError: Invalid left-hand side in prefix operation.*/ +/* @@? 30:8 Error SyntaxError: Invalid left-hand side in assignment expression.*/ /* @@? 33:27 Error SyntaxError: Rest parameter cannot have the default value. */ /* @@? 33:29 Error SyntaxError: Identifier is needed here. */ /* @@? 33:41 Error SyntaxError: Not enable default value with default undefined. */ /* @@? 35:10 Error SyntaxError: Unexpected token ':'. */ -/* @@? 35:10 Error SyntaxError: Unexpected token, expected ')'. */ -/* @@? 35:12 Error SyntaxError: Unexpected token 'int'. */ +/* @@? 35:12 Error SyntaxError: Unexpected token, expected ')'. */ /* @@? 35:16 Error SyntaxError: Unexpected token '=>'. */ -/* @@? 37:5 Error TypeError: Variable 'x' has already been declared. */ /* @@? 37:15 Error SyntaxError: Unexpected token '=>'. */ -/* @@? 38:5 Error TypeError: Variable 'x' has already been declared. */ -/* @@? 38:9 Error TypeError: This expression is not constructible. */ -/* @@? 39:5 Error TypeError: Variable 'x' has already been declared. */ -/* @@? 39:9 Error TypeError: This expression is not constructible. */ +/* @@? 37:18 Error SyntaxError: Unexpected token '77'. */ /* @@? 39:16 Error SyntaxError: Unexpected token '{'. */ /* @@? 47:15 Error SyntaxError: Unexpected token, expected '('. */ /* @@? 47:17 Error SyntaxError: Unexpected token, expected an identifier. */ -/* @@? 48:5 Error SyntaxError: Unexpected token, expected ',' or ')'. */ -/* @@? 48:5 Error SyntaxError: Expected '=>', got '}'. */ -/* @@? 48:5 Error SyntaxError: Unexpected token '}'. */ +/* @@? 48:5 Error SyntaxError: Unexpected token, expected ',' or ')'.*/ +/* @@? 48:5 Error SyntaxError: Expected '=>', got '}'.*/ +/* @@? 48:5 Error SyntaxError: Unexpected token '}'.*/ /* @@? 51:13 Error SyntaxError: Unexpected token '.99'. */ /* @@? 51:16 Error SyntaxError: Invalid left-hand side operator. */ -/* @@? 55:10 Error TypeError: Only abstract or native methods can't have body. */ +/* @@? 55:1 Error SyntaxError: Unexpected token 'function'.*/ /* @@? 55:24 Error SyntaxError: Unexpected token '@@'. */ /* @@? 55:24 Error SyntaxError: Unexpected token, expected ',' or ')'. */ +/* @@? 55:24 Error SyntaxError: There is no any node to insert at the placeholder position. */ +/* @@? 55:24 Error SyntaxError: Unexpected token '@@'. */ /* @@? 55:26 Error SyntaxError: Unexpected token ')'. */ /* @@? 55:27 Error SyntaxError: Unexpected token ':'. */ -/* @@? 55:29 Error TypeError: Unresolved reference void */ +/* @@? 55:29 Error SyntaxError: Unexpected token 'void'. */ +/* @@? 57:1 Error SyntaxError: Unexpected token 'function'.*/ /* @@? 57:26 Error SyntaxError: Default value is allowed only for optional parameters. */ +/* @@? 113:1 Error SyntaxError: Expected '}', got 'end of stream'. */ + diff --git a/ets2panda/test/ast/parser/ets/lambdaWithWrongOptionalParameter.ets b/ets2panda/test/ast/parser/ets/lambdaWithWrongOptionalParameter.ets index aa61413645ae8fc766f4015100cc9f4535838559..194fe1cf869e4b0dbc8fb136b1c5175f4bb3d79f 100644 --- a/ets2panda/test/ast/parser/ets/lambdaWithWrongOptionalParameter.ets +++ b/ets2panda/test/ast/parser/ets/lambdaWithWrongOptionalParameter.ets @@ -21,5 +21,4 @@ function main() { foo(2,3); } -/* @@? 17:25 Error TypeError: Expected initializer for parameter b. */ -/* @@? 1:1 Error SyntaxError: Required parameter follows default parameter(s). */ +/* @@? 17:25 Error TypeError: A required parameter cannot follow an optional parameter. */ diff --git a/ets2panda/test/test-lists/astchecker/astchecker-ets-ignored.txt b/ets2panda/test/test-lists/astchecker/astchecker-ets-ignored.txt index 3888be099f06c2f41a1d1aefc8f37af562268910..604e9d0ba7b43c4f1c0c3659831dcfb4d376dea7 100644 --- a/ets2panda/test/test-lists/astchecker/astchecker-ets-ignored.txt +++ b/ets2panda/test/test-lists/astchecker/astchecker-ets-ignored.txt @@ -90,13 +90,6 @@ ast/parser/ets/lambda-type-inference-neg2.ets # after the fix the test fails to detect another type error ast/parser/ets/unexpected_token_51.ets - -# Issue: #23134 -# Can't insert "/* TypeError: Postcondition check failed for DefaultParametersLowering */" test directive -ast/parser/ets/InvalidExpressions.ets -# falls to post-checker lowerings after type errors are detected! -ast/parser/ets/lambdaWithWrongOptionalParameter.ets - # Issue: #24253 ast/compiler/ets/lambda_infer_type/lambda_param_type_cannot_be_determined.ets