diff --git a/Ast/src/Parser.cpp b/Ast/src/Parser.cpp index e26df1fa..ee339423 100644 --- a/Ast/src/Parser.cpp +++ b/Ast/src/Parser.cpp @@ -1523,7 +1523,11 @@ AstType* Parser::parseFunctionTypeTail(const Lexeme& begin, AstArray parts(scratchType); - parts.push_back(type); + + if (type != nullptr) + { + parts.push_back(type); + } incrementRecursionCounter("type annotation"); @@ -1622,11 +1626,17 @@ AstTypeOrPack Parser::parseTypeOrPack() AstType* Parser::parseType(bool inDeclarationContext) { + Location begin = lexer.current().location; + + Lexeme::Type c = lexer.current().type; + if (c == '|' || c == '&') + { + return parseTypeSuffix(nullptr, begin); + } + unsigned int oldRecursionCount = recursionCounter; // recursion counter is incremented in parseSimpleType - Location begin = lexer.current().location; - AstType* type = parseSimpleType(/* allowPack= */ false, /* in declaration context */ inDeclarationContext).type; recursionCounter = oldRecursionCount; diff --git a/tests/Parser.test.cpp b/tests/Parser.test.cpp index b178f539..d5d22faa 100644 --- a/tests/Parser.test.cpp +++ b/tests/Parser.test.cpp @@ -3167,4 +3167,14 @@ TEST_CASE_FIXTURE(Fixture, "read_write_table_properties") LUAU_ASSERT(pr.errors.size() == 0); } +TEST_CASE_FIXTURE(Fixture, "can_parse_leading_bar_unions_successfully") +{ + parse(R"(type A = | "Hello" | "World")"); +} + +TEST_CASE_FIXTURE(Fixture, "can_parse_leading_ampersand_intersections_successfully") +{ + parse(R"(type A = & { string } & { number })"); +} + TEST_SUITE_END();