diff options
| author | sricker-nvidia <115114531+sricker-nvidia@users.noreply.github.com> | 2025-07-08 16:15:51 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-08 23:15:51 +0000 |
| commit | 2c4bfce49d9af2414f6a3f70f7221d6890a017e7 (patch) | |
| tree | f92644117c10e6d7a34f56b0499b0365e881d6f1 /tests | |
| parent | 0ab515159e9da15e0077d55c761ca0f326361257 (diff) | |
Add error for forward references in generic constraints (#7615)
* Add error for forward references in generic constraints
Change addresses issue #6545.
The slang compiler's type checker is unable to support cases where
a generic type parameter is referenced as a constraint before it is
declared. For example code like:
````
interface IFoo<T : IFloat>
{
}
void bar<Foo : IFoo<T>, T : IFloat>()
{
}
````
Is not supported, but will currently report a generic error like,
"(0): error 99999: Slang compilation aborted due to an exception
of class Slang::InternalError: unexpected: generic type constraint
during lowering"
This change adds a new check for this kind of code and reports an
error like,
"error 30117: generic constraint for parameter 'Foo' references type
parameter 'T' before it is declared"
when detected. Basic testing of this error is also added in a new
diagnostic test.
* Add error for forward refs in generic constiants update 1
* Add error for forward refs in generic constraints update 2
Revised algo in checkForwardReferencesInGenericConstraint to run
in O(n) instead of the previous O(n^2) using HashSets.
* Add error for forward refs in generic constraints update 3
-Update logic adding referenced decl's in collectReferencedDecls.
-Simplified error string logic.
* Add error for forward refs in generic constraints update 4
-Declare collectReferencedDecls in slang-check.h such that it can
be used as a general utility function.
* format code
---------
Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/diagnostics/generic-constraint-forward-reference.slang | 16 | ||||
| -rw-r--r-- | tests/diagnostics/generic-constraint-forward-reference.slang.expected | 14 |
2 files changed, 30 insertions, 0 deletions
diff --git a/tests/diagnostics/generic-constraint-forward-reference.slang b/tests/diagnostics/generic-constraint-forward-reference.slang new file mode 100644 index 000000000..08a6c3478 --- /dev/null +++ b/tests/diagnostics/generic-constraint-forward-reference.slang @@ -0,0 +1,16 @@ +//DIAGNOSTIC_TEST:SIMPLE:
+// Test forward references in generic constraints
+
+interface IFloat {}
+interface IFoo<T : IFloat> {}
+interface IBar<T, U> {}
+
+// Test case 1: Simple forward reference in interface constraint
+void test1<Foo : IFoo<T>, T : IFloat>() {}
+
+// Test case 2: Multiple forward references in a single constraint
+void test2<A : IBar<B, C>, B : IFloat, C : IFloat>() {}
+
+// Valid case that should NOT produce our error:
+// Test case 3: Correct order - type parameter declared before use
+void test3<T : IFloat, Foo : IFoo<T>>() {}
diff --git a/tests/diagnostics/generic-constraint-forward-reference.slang.expected b/tests/diagnostics/generic-constraint-forward-reference.slang.expected new file mode 100644 index 000000000..5c6b2707b --- /dev/null +++ b/tests/diagnostics/generic-constraint-forward-reference.slang.expected @@ -0,0 +1,14 @@ +result code = -1 +standard error = { +tests/diagnostics/generic-constraint-forward-reference.slang(9): error 30117: generic constraint for parameter 'Foo' references type parameter 'T' before it is declared +void test1<Foo : IFoo<T>, T : IFloat>() {} + ^~~~ +tests/diagnostics/generic-constraint-forward-reference.slang(12): error 30117: generic constraint for parameter 'A' references type parameter 'B' before it is declared +void test2<A : IBar<B, C>, B : IFloat, C : IFloat>() {} + ^~~~ +tests/diagnostics/generic-constraint-forward-reference.slang(12): error 30117: generic constraint for parameter 'A' references type parameter 'C' before it is declared +void test2<A : IBar<B, C>, B : IFloat, C : IFloat>() {} + ^~~~ +} +standard output = { +} |
