Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message on generic value parameters decls #6169

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions source/slang/slang-ast-support-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class SyntaxNode;
SourceLoc getDiagnosticPos(SyntaxNode const* syntax);
SourceLoc getDiagnosticPos(TypeExp const& typeExp);
SourceLoc getDiagnosticPos(DeclRefBase* declRef);
SourceLoc getDiagnosticPos(Decl* decl);

typedef NodeBase* (*SyntaxParseCallback)(Parser* parser, void* userData);

Expand Down
9 changes: 8 additions & 1 deletion source/slang/slang-check-decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1775,7 +1775,14 @@ void SemanticsDeclHeaderVisitor::checkVarDeclCommon(VarDeclBase* varDecl)
{
if (!varDecl->type.type)
{
getSink()->diagnose(varDecl, Diagnostics::varWithoutTypeMustHaveInitializer);
if (as<GenericValueParamDecl>(varDecl))
{
getSink()->diagnose(varDecl, Diagnostics::genericValueParameterMustHaveType);
}
else
{
getSink()->diagnose(varDecl, Diagnostics::varWithoutTypeMustHaveInitializer);
}
varDecl->type.type = m_astBuilder->getErrorType();
}
}
Expand Down
6 changes: 5 additions & 1 deletion source/slang/slang-diagnostic-defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,11 @@ DIAGNOSTIC(
ambiguousDefaultInitializerForType,
"more than one default initializer was found for type '$0'")
DIAGNOSTIC(30623, Error, cannotHaveInitializer, "'$0' cannot have an initializer because it is $1")

DIAGNOSTIC(
30623,
Error,
genericValueParameterMustHaveType,
"a generic value parameter must be given an explicit type")

// 307xx: parameters
DIAGNOSTIC(
Expand Down
13 changes: 12 additions & 1 deletion source/slang/slang-syntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,18 @@ SourceLoc getDiagnosticPos(DeclRefBase* declRef)
{
if (!declRef)
return SourceLoc();
return declRef->getDecl()->loc;
return getDiagnosticPos(declRef->getDecl());
}

SourceLoc getDiagnosticPos(Decl* decl)
{
if (!decl)
return SourceLoc();
if (decl->getNameLoc().isValid())
{
return decl->getNameLoc();
}
return decl->loc;
}

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!! Free functions !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Expand Down
21 changes: 21 additions & 0 deletions tests/diagnostics/generic-value-parameter-must-have-type.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): -target spirv

interface ITest
{
static const uint kValue0;
static const uint kValue1;
};

// `TValue1` does not have an explicit type, this should fail to compile.
// CHECK: error 30623: a generic value parameter
// Make sure erroneous code is printed out.
// CHECK: let TValue1>
struct TestImpl<let TValue0 : uint, let TValue1> : ITest
{
static const uint kValue0 = TValue0;
static const uint kValue1 = TValue1;
};

void computeMain()
{
}