From 2c4bfce49d9af2414f6a3f70f7221d6890a017e7 Mon Sep 17 00:00:00 2001 From: sricker-nvidia <115114531+sricker-nvidia@users.noreply.github.com> Date: Tue, 8 Jul 2025 16:15:51 -0700 Subject: 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 { } void bar, 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> --- .../generic-constraint-forward-reference.slang | 16 ++++++++++++++++ .../generic-constraint-forward-reference.slang.expected | 14 ++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/diagnostics/generic-constraint-forward-reference.slang create mode 100644 tests/diagnostics/generic-constraint-forward-reference.slang.expected (limited to 'tests/diagnostics') 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 {} +interface IBar {} + +// Test case 1: Simple forward reference in interface constraint +void test1, T : IFloat>() {} + +// Test case 2: Multiple forward references in a single constraint +void test2, B : IFloat, C : IFloat>() {} + +// Valid case that should NOT produce our error: +// Test case 3: Correct order - type parameter declared before use +void test3>() {} 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, 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, 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, B : IFloat, C : IFloat>() {} + ^~~~ +} +standard output = { +} -- cgit v1.2.3