diff options
| -rw-r--r-- | source/slang/slang-check-conformance.cpp | 9 | ||||
| -rw-r--r-- | source/slang/slang-check-decl.cpp | 4 | ||||
| -rw-r--r-- | source/slang/slang-check-impl.h | 1 | ||||
| -rw-r--r-- | source/slang/slang-diagnostic-defs.h | 1 | ||||
| -rw-r--r-- | tests/diagnostics/invalid-constraint.slang | 27 |
5 files changed, 42 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)") diff --git a/tests/diagnostics/invalid-constraint.slang b/tests/diagnostics/invalid-constraint.slang new file mode 100644 index 000000000..667077ffb --- /dev/null +++ b/tests/diagnostics/invalid-constraint.slang @@ -0,0 +1,27 @@ +//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): + +interface IFoo +{ + This foo(); +} + +struct Foo : IFoo +{ + float x; + This foo() + { + This f; + f.x = 0.0; + return f; + } +} + +RWStructuredBuffer<float> output; + +// CHECK: ([[# @LINE+1]]): error 30401 +float compute<T:Foo>(T f) // error, should be IFoo here. +{ +} + +void main() +{} |
