From 455da693ca47d7b19e498c83ab47b82a0bb2bbb7 Mon Sep 17 00:00:00 2001 From: karl-police Date: Wed, 11 Sep 2024 01:34:12 +0200 Subject: [PATCH] Autocomplete Free State TableType entryMap change --- Analysis/src/Autocomplete.cpp | 23 +++++++++++++++++++ tests/Autocomplete.test.cpp | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/Analysis/src/Autocomplete.cpp b/Analysis/src/Autocomplete.cpp index ee865edd1..f3fb457b7 100644 --- a/Analysis/src/Autocomplete.cpp +++ b/Analysis/src/Autocomplete.cpp @@ -303,6 +303,29 @@ static void autocompleteProps( else type = follow(prop.type()); + + // If a "TableType" is a "Free State" + // Somehow readTy only properties get added into it + // This part removes them if writeTy is empty + // + // This gets rid of unassigned properties in the autocomplete. + if (FFlag::LuauSolverV2) + { + if (auto tblTy = get(rootTy)) + { + // This probably won't assure for nested tables? + if (tblTy->state == TableState::Free) + { + // If this property was never written to. + if (prop.writeTy == std::nullopt) + { + continue; // Skip this + } + } + } + } + + TypeCorrectKind typeCorrect = indexType == PropIndexType::Key ? TypeCorrectKind::Correct : checkTypeCorrectKind(module, typeArena, builtinTypes, nodes.back(), {{}, {}}, type); diff --git a/tests/Autocomplete.test.cpp b/tests/Autocomplete.test.cpp index 7f020b18b..5fffc671c 100644 --- a/tests/Autocomplete.test.cpp +++ b/tests/Autocomplete.test.cpp @@ -650,6 +650,48 @@ TEST_CASE_FIXTURE(ACFixture, "dont_offer_any_suggestions_from_within_a_broken_co CHECK_EQ(ac.context, AutocompleteContext::Unknown); } +TEST_CASE_FIXTURE(ACBuiltinsFixture, "autocomplete_freetable_shows_nullopt_writeTy_outsideOfFuncScope_fix") +{ + TypeArena arena; + frontend.globals.globalScope->exportedTypeBindings["FreeTable"] = TypeFun{{}, arena.addType(TableType{TableState::Free, TypeLevel{}})}; + + // This fix only works for the new type solver. + if (!FFlag::LuauSolverV2) + return; + + CheckResult check1 = check(R"( +local tbl_A = {} :: FreeTable +tbl_A.abc = 1 + +print(tbl_A.notWritingTo) + +function test(a) + a.@3 + if (a.propertyTest) then return true end + return false +end + +test({@2}) + +tbl_A.@1 +)"); + + auto test1 = toString(requireType("tbl_A")); + auto test2 = requireType("tbl_A"); + + auto ac1 = autocomplete('1'); + + auto ac2 = autocomplete('2'); + auto ac3 = autocomplete('3'); + + // tbl_A indexing, the main problem that is to fix. + CHECK_EQ(ac1.entryMap.count("abc"), 1); + CHECK_EQ(ac1.entryMap.count("notWritingTo"), 0); + + // when within function({}) + CHECK_EQ(ac2.entryMap.count("propertyTest"), 1); +} + TEST_CASE_FIXTURE(ACFixture, "autocomplete_for_middle_keywords") { check(R"(