summaryrefslogtreecommitdiffstats
path: root/source/slang
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2024-03-14 14:14:18 -0700
committerGitHub <noreply@github.com>2024-03-14 14:14:18 -0700
commit78d4df0644b20b8ba4eeff459c749c4e8d261345 (patch)
tree0fec98d03281134cc46a0cc96cfd48b6107b159f /source/slang
parentba65c212f71a9033c11657936f202f67b01abf23 (diff)
Add diagnostic on invalid type constraint. (#3769)
Diffstat (limited to 'source/slang')
-rw-r--r--source/slang/slang-check-conformance.cpp9
-rw-r--r--source/slang/slang-check-decl.cpp4
-rw-r--r--source/slang/slang-check-impl.h1
-rw-r--r--source/slang/slang-diagnostic-defs.h1
4 files changed, 15 insertions, 0 deletions
diff --git a/source/slang/slang-check-conformance.cpp b/source/slang/slang-check-conformance.cpp
index d6e73e798..ff4a40031 100644
--- a/source/slang/slang-check-conformance.cpp
+++ b/source/slang/slang-check-conformance.cpp
@@ -237,6 +237,15 @@ namespace Slang
return false;
}
+ bool SemanticsVisitor::isValidGenericConstraintType(Type* type)
+ {
+ if (auto andType = as<AndType>(type))
+ {
+ return isValidGenericConstraintType(andType->getLeft()) && isValidGenericConstraintType(andType->getRight());
+ }
+ return isInterfaceType(type);
+ }
+
bool SemanticsVisitor::isTypeDifferentiable(Type* type)
{
return isSubtype(type, m_astBuilder->getDiffInterfaceType());
diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp
index c08e8f7ef..88a707ed8 100644
--- a/source/slang/slang-check-decl.cpp
+++ b/source/slang/slang-check-decl.cpp
@@ -2206,6 +2206,10 @@ namespace Slang
CheckConstraintSubType(decl->sub);
decl->sub = TranslateTypeNodeForced(decl->sub);
decl->sup = TranslateTypeNodeForced(decl->sup);
+ if (!isValidGenericConstraintType(decl->sup) && !as<ErrorType>(decl->sub.type))
+ {
+ getSink()->diagnose(decl->sup.exp, Diagnostics::invalidTypeForConstraint, decl->sup);
+ }
}
void SemanticsDeclHeaderVisitor::visitGenericTypeParamDecl(GenericTypeParamDecl* decl)
diff --git a/source/slang/slang-check-impl.h b/source/slang/slang-check-impl.h
index b1618dfb6..28c145498 100644
--- a/source/slang/slang-check-impl.h
+++ b/source/slang/slang-check-impl.h
@@ -2017,6 +2017,7 @@ namespace Slang
SubtypeWitness* checkAndConstructSubtypeWitness(Type* subType, Type* superType);
bool isInterfaceType(Type* type);
+ bool isValidGenericConstraintType(Type* type);
bool isTypeDifferentiable(Type* type);
diff --git a/source/slang/slang-diagnostic-defs.h b/source/slang/slang-diagnostic-defs.h
index 8df51211a..5b66a0f91 100644
--- a/source/slang/slang-diagnostic-defs.h
+++ b/source/slang/slang-diagnostic-defs.h
@@ -476,6 +476,7 @@ DIAGNOSTIC(39901, Fatal , cannotProcessInclude, "internal compiler error: cannot
// 304xx: generics
DIAGNOSTIC(30400, Error, genericTypeNeedsArgs, "generic type '$0' used without argument")
+DIAGNOSTIC(30401, Error, invalidTypeForConstraint, "type '$0' cannot be used as a constraint.")
// 305xx: initializer lists
DIAGNOSTIC(30500, Error, tooManyInitializers, "too many initializers (expected $0, got $1)")