Skip to content

Commit

Permalink
Add an error when importing a symbol not existing in module
Browse files Browse the repository at this point in the history
  • Loading branch information
SirLynix committed Aug 11, 2024
1 parent de4a36a commit 46aad44
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
1 change: 1 addition & 0 deletions include/NZSL/Lang/ErrorList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ NZSL_SHADERLANG_COMPILER_ERROR(FunctionCallUnmatchingParameterType, "function {}
NZSL_SHADERLANG_COMPILER_ERROR(FunctionDeclarationInsideFunction, "a function cannot be defined inside another function")
NZSL_SHADERLANG_COMPILER_ERROR(IdentifierAlreadyUsed, "identifier {} is already used", std::string)
NZSL_SHADERLANG_COMPILER_ERROR(ImportIdentifierAlreadyPresent, "{} identifier was already imported", std::string)
NZSL_SHADERLANG_COMPILER_ERROR(ImportIdentifierNotFound, "identifier {} not found in module {}", std::string, std::string)
NZSL_SHADERLANG_COMPILER_ERROR(ImportMultipleWildcard, "only one wildcard can be present in an import directive")
NZSL_SHADERLANG_COMPILER_ERROR(ImportWildcardRename, "wildcard cannot be renamed")
NZSL_SHADERLANG_COMPILER_ERROR(IndexRequiresIntegerIndices, "index access requires integer indices (got {})", std::string)
Expand Down
27 changes: 18 additions & 9 deletions src/NZSL/Ast/SanitizeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2580,20 +2580,23 @@ namespace nzsl::Ast
std::vector<DeclareAliasStatementPtr> aliasStatements;
std::vector<DeclareConstStatementPtr> constStatements;

std::vector<std::string> wildcardImport{ std::string{} };

auto CheckImport = [&](const std::string& identifier) -> std::pair<bool, const std::vector<std::string>*>
auto CheckImport = [&](const std::string& identifier) -> std::pair<bool, std::vector<std::string>>
{
auto it = importedSymbols.find(identifier);
if (it == importedSymbols.end())
{
if (!importEverythingElse)
return { false, nullptr };
return { false, {} };

return { true, &wildcardImport };
return { true, { std::string{} } };
}
else
return { true, &it->second };
{
std::vector<std::string> imports = std::move(it->second);
importedSymbols.erase(it);

return { true, std::move(imports) };
}
};

ExportVisitor::Callbacks callbacks;
Expand All @@ -2617,7 +2620,7 @@ namespace nzsl::Ast
return ShaderBuilder::Constant(*node.constIndex, GetConstantType(*value));
};

for (const std::string& aliasName : *aliasesName)
for (const std::string& aliasName : aliasesName)
{
if (aliasName.empty())
{
Expand All @@ -2644,7 +2647,7 @@ namespace nzsl::Ast
if (moduleData.dependenciesVisitor)
moduleData.dependenciesVisitor->MarkFunctionAsUsed(*node.funcIndex);

for (const std::string& aliasName : *aliasesName)
for (const std::string& aliasName : aliasesName)
{
if (aliasName.empty())
{
Expand All @@ -2671,7 +2674,7 @@ namespace nzsl::Ast
if (moduleData.dependenciesVisitor)
moduleData.dependenciesVisitor->MarkStructAsUsed(*node.structIndex);

for (const std::string& aliasName : *aliasesName)
for (const std::string& aliasName : aliasesName)
{
if (aliasName.empty())
{
Expand All @@ -2690,6 +2693,12 @@ namespace nzsl::Ast
ExportVisitor exportVisitor;
exportVisitor.Visit(*m_context->currentModule->importedModules[moduleIndex].module->rootNode, callbacks);

if (!importedSymbols.empty())
{
for (const auto& [identifier, aliasesName] : importedSymbols)
throw CompilerImportIdentifierNotFoundError{ node.sourceLocation, identifier, node.moduleName };
}

if (aliasStatements.empty() && constStatements.empty())
return ShaderBuilder::NoOp();

Expand Down
17 changes: 15 additions & 2 deletions tests/src/Tests/ErrorsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,26 +842,39 @@ module Module;
external
{
[binding(0)]
data: mat4[f32]
}
)";

std::string_view shaderSource = R"(
std::string_view wildcardImportSource = R"(
[nzsl_version("1.0")]
module;
import * from Module;
)";

nzsl::Ast::ModulePtr shaderModule = nzsl::Parse(shaderSource);
nzsl::Ast::ModulePtr shaderModule;

auto directoryModuleResolver = std::make_shared<nzsl::FilesystemModuleResolver>();
directoryModuleResolver->RegisterModule(importedSource);

nzsl::Ast::SanitizeVisitor::Options sanitizeOpt;
sanitizeOpt.moduleResolver = directoryModuleResolver;

shaderModule = nzsl::Parse(wildcardImportSource);
CHECK_THROWS_WITH(nzsl::Ast::Sanitize(*shaderModule, sanitizeOpt), "(5,1 -> 21): CModuleFeatureMismatch error: module Module requires feature primitive_externals");

std::string_view nonExistentImportShaderSource = R"(
[nzsl_version("1.0")]
[feature(primitive_externals)]
module;
import Foo from Module;
)";

shaderModule = nzsl::Parse(nonExistentImportShaderSource);
CHECK_THROWS_WITH(nzsl::Ast::Sanitize(*shaderModule, sanitizeOpt), "(6,1 -> 23): CImportIdentifierNotFound error: identifier Foo not found in module Module");
}

/************************************************************************/
Expand Down

0 comments on commit 46aad44

Please sign in to comment.