Skip to content

Commit

Permalink
improve error message on generic value decls (#6169)
Browse files Browse the repository at this point in the history
Co-authored-by: Yong He <yonghe@outlook.com>
  • Loading branch information
fairywreath and csyonghe authored Jan 24, 2025
1 parent ac174d2 commit 92c9fff
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
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()
{
}

0 comments on commit 92c9fff

Please sign in to comment.