Skip to content

Commit

Permalink
Include module name for definitions files (#861)
Browse files Browse the repository at this point in the history
Closes #818 

We set the `moduleName` of the source module to the provided
`packageName`.
This then populates the relevant `definitionModuleName`'s on
CTV/FTV/TTVs, so it allows us to lookup where they originated from.

The one place I can see this having an impact inside of Luau code is the
following:

https://github.com/Roblox/luau/blob/1fa8311a188a932233d2ce2bc6412e5bab37c035/Analysis/src/Error.cpp#L70-L90
Which will potentially change they way error messages are formatted.
Should there be a way to exclude definition files when calling
`getDefinitionModuleName`, so we can preserve this behaviour?
  • Loading branch information
JohnnyMorganz authored Nov 21, 2023
1 parent 74c5320 commit ae53051
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Analysis/src/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ LUAU_FASTFLAGVARIABLE(DebugLuauLogSolverToJson, false)
LUAU_FASTFLAGVARIABLE(DebugLuauReadWriteProperties, false)
LUAU_FASTFLAGVARIABLE(LuauTypecheckLimitControls, false)
LUAU_FASTFLAGVARIABLE(CorrectEarlyReturnInMarkDirty, false)
LUAU_FASTFLAGVARIABLE(LuauDefinitionFileSetModuleName, false)

namespace Luau
{
Expand Down Expand Up @@ -165,6 +166,11 @@ LoadDefinitionFileResult Frontend::loadDefinitionFile(GlobalTypes& globals, Scop
LUAU_TIMETRACE_SCOPE("loadDefinitionFile", "Frontend");

Luau::SourceModule sourceModule;
if (FFlag::LuauDefinitionFileSetModuleName)
{
sourceModule.name = packageName;
sourceModule.humanReadableName = packageName;
}
Luau::ParseResult parseResult = parseSourceForModule(source, sourceModule, captureComments);
if (parseResult.errors.size() > 0)
return LoadDefinitionFileResult{false, parseResult, sourceModule, nullptr};
Expand Down
23 changes: 23 additions & 0 deletions tests/TypeInfer.definitions.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,4 +441,27 @@ TEST_CASE_FIXTURE(Fixture, "class_definitions_reference_other_classes")
REQUIRE(result.success);
}

TEST_CASE_FIXTURE(Fixture, "definition_file_has_source_module_name_set")
{
ScopedFastFlag sff{"LuauDefinitionFileSetModuleName", true};

LoadDefinitionFileResult result = loadDefinition(R"(
declare class Foo
end
)");

REQUIRE(result.success);

CHECK_EQ(result.sourceModule.name, "@test");
CHECK_EQ(result.sourceModule.humanReadableName, "@test");

std::optional<TypeFun> fooTy = frontend.globals.globalScope->lookupType("Foo");
REQUIRE(fooTy);

const ClassType* ctv = get<ClassType>(fooTy->type);

REQUIRE(ctv);
CHECK_EQ(ctv->definitionModuleName, "@test");
}

TEST_SUITE_END();

0 comments on commit ae53051

Please sign in to comment.