From 2fe4bf91cfae3a160d9e99fc7fe7ec426bcd0d28 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Wed, 5 Jul 2023 12:05:35 +0100 Subject: [PATCH] Add FFlag and move test case --- Analysis/src/TypeInfer.cpp | 3 +- tests/TypeInfer.intersectionTypes.test.cpp | 30 ++++++++++++ tests/TypeInfer.tables.test.cpp | 55 +++++++++++++++------- 3 files changed, 70 insertions(+), 18 deletions(-) diff --git a/Analysis/src/TypeInfer.cpp b/Analysis/src/TypeInfer.cpp index 857bc2b9..3b87f718 100644 --- a/Analysis/src/TypeInfer.cpp +++ b/Analysis/src/TypeInfer.cpp @@ -41,6 +41,7 @@ LUAU_FASTFLAGVARIABLE(LuauTinyControlFlowAnalysis, false) LUAU_FASTFLAGVARIABLE(LuauTypecheckClassTypeIndexers, false) LUAU_FASTFLAGVARIABLE(LuauAlwaysCommitInferencesOfFunctionCalls, false) LUAU_FASTFLAG(LuauParseDeclareClassIndexer) +LUAU_FASTFLAGVARIABLE(LuauIndexTableIntersectionStringExpr, false) namespace Luau { @@ -3412,7 +3413,7 @@ TypeId TypeChecker::checkLValueBinding(const ScopePtr& scope, const AstExprIndex return prop->type(); } } - else if (get(exprType)) + else if (FFlag::LuauIndexTableIntersectionStringExpr && get(exprType)) { Name name = std::string(value->value.data, value->value.size); diff --git a/tests/TypeInfer.intersectionTypes.test.cpp b/tests/TypeInfer.intersectionTypes.test.cpp index 012dc7b4..45d127ab 100644 --- a/tests/TypeInfer.intersectionTypes.test.cpp +++ b/tests/TypeInfer.intersectionTypes.test.cpp @@ -880,4 +880,34 @@ TEST_CASE_FIXTURE(Fixture, "less_greedy_unification_with_intersection_types_2") CHECK_EQ("({| x: number |} & {| x: string |}) -> never", toString(requireType("f"))); } +TEST_CASE_FIXTURE(BuiltinsFixture, "index_property_table_intersection_1") +{ + CheckResult result = check(R"( +type Foo = { + Bar: string, +} & { Baz: number } + +local x: Foo = { Bar = "1", Baz = 2 } +local y = x.Bar + )"); + + LUAU_REQUIRE_NO_ERRORS(result); +} + +TEST_CASE_FIXTURE(BuiltinsFixture, "index_property_table_intersection_2") +{ + ScopedFastFlag sff{"LuauIndexTableIntersectionStringExpr", true}; + + CheckResult result = check(R"( +type Foo = { + Bar: string, +} & { Baz: number } + +local x: Foo = { Bar = "1", Baz = 2 } +local y = x["Bar"] + )"); + + LUAU_REQUIRE_NO_ERRORS(result); +} + TEST_SUITE_END(); diff --git a/tests/TypeInfer.tables.test.cpp b/tests/TypeInfer.tables.test.cpp index 9e17813f..ead1854f 100644 --- a/tests/TypeInfer.tables.test.cpp +++ b/tests/TypeInfer.tables.test.cpp @@ -216,9 +216,11 @@ TEST_CASE_FIXTURE(Fixture, "used_dot_instead_of_colon") local a = T.method() )"); - auto it = std::find_if(result.errors.begin(), result.errors.end(), [](const TypeError& e) { - return nullptr != get(e); - }); + auto it = std::find_if(result.errors.begin(), result.errors.end(), + [](const TypeError& e) + { + return nullptr != get(e); + }); REQUIRE(it != result.errors.end()); } @@ -261,9 +263,11 @@ TEST_CASE_FIXTURE(Fixture, "used_colon_instead_of_dot") local a = T:method() )"); - auto it = std::find_if(result.errors.begin(), result.errors.end(), [](const TypeError& e) { - return nullptr != get(e); - }); + auto it = std::find_if(result.errors.begin(), result.errors.end(), + [](const TypeError& e) + { + return nullptr != get(e); + }); REQUIRE(it != result.errors.end()); } @@ -3642,34 +3646,51 @@ end LUAU_REQUIRE_NO_ERRORS(result); } -TEST_CASE_FIXTURE(BuiltinsFixture, "index_property_table_intersection_1") +TEST_CASE_FIXTURE(BuiltinsFixture, "write_common_property_to_table_unions") { CheckResult result = check(R"( type Foo = { - Bar: string, -} & { Baz: number } + Enabled: boolean, + Size: number, +} -local x: Foo = { Bar = "1", Baz = 2 } -local y = x.Bar +type Bar = { + Enabled: boolean, + Width: number, + Height: number, +} + +local x: Foo | Bar = { Enabled = true, Size = 5 } + +x.Enabled = false )"); LUAU_REQUIRE_NO_ERRORS(result); } -TEST_CASE_FIXTURE(BuiltinsFixture, "index_property_table_intersection_2") +TEST_CASE_FIXTURE(BuiltinsFixture, "write_common_property_to_table_unions_2") { CheckResult result = check(R"( type Foo = { - Bar: string, -} & { Baz: number } + Enabled: boolean, + Size: number, +} -local x: Foo = { Bar = "1", Baz = 2 } -local y = x["Bar"] +type Bar = { + Enabled: boolean, + Width: number, + Height: number, +} + +local x: Foo | Bar = { Enabled = true, Size = 5 } + +x.Size = 6 )"); - LUAU_REQUIRE_NO_ERRORS(result); + LUAU_REQUIRE_ERROR_COUNT(1, result); + CHECK_EQ("error", toString(result.errors[0])); // TODO } TEST_SUITE_END();