diff options
| author | Tim Foley <tfoley@nvidia.com> | 2019-02-05 11:53:27 -0800 |
|---|---|---|
| committer | Tim Foley <tfoley@nvidia.com> | 2019-02-05 13:55:17 -0800 |
| commit | 43950e2b2f2b1109fe25e67fc678272af6dfb7ef (patch) | |
| tree | 0863941f34088e09e2e28b934df54388ca294b82 | |
| parent | 314795b5d8ff5845624f93e152face325659dd0c (diff) | |
Fix checking of interface conformances for nested types
Before this change, code like the following would crash the compiler:
```hlsl
interface IThing { /* ... */ }
struct Outer
{
struct Inner : IThing
{}
}
/* go on to use Outer.Inner */
```
The problem was that the front-end logic for checking interface conformances was *only* checking declarations at the top level of a module, or nested under a generic.
This change fixes the logic to recurse through the entire tree of declarations.
I have added a test case that uses a nested `struct` type to satisfy an associated type requirement, to confirm that the new check works as intended.
| -rw-r--r-- | source/slang/check.cpp | 52 | ||||
| -rw-r--r-- | tests/compute/assoctype-nested.slang | 61 | ||||
| -rw-r--r-- | tests/compute/assoctype-nested.slang.expected.txt | 4 |
3 files changed, 105 insertions, 12 deletions
diff --git a/source/slang/check.cpp b/source/slang/check.cpp index 921ef61e6..b6185344b 100644 --- a/source/slang/check.cpp +++ b/source/slang/check.cpp @@ -2883,6 +2883,45 @@ namespace Slang syntaxNode->modifiers.first = resultModifiers; } + /// Perform checking of interface conformaces for this decl and all its children + void checkInterfaceConformancesRec(Decl* decl) + { + // Any user-defined type may have declared interface conformances, + // which we should check. + // + if( auto aggTypeDecl = as<AggTypeDecl>(decl) ) + { + checkAggTypeConformance(aggTypeDecl); + } + // Conformances can also come via `extension` declarations, and + // we should check them against the type(s) being extended. + // + else if(auto extensionDecl = as<ExtensionDecl>(decl)) + { + checkExtensionConformance(extensionDecl); + } + + // We need to handle the recursive cases here, the first + // of which is a generic decl, where we want to recurivsely + // check the inner declaration. + // + if(auto genericDecl = as<GenericDecl>(decl)) + { + checkInterfaceConformancesRec(genericDecl->inner); + } + // For any other kind of container declaration, we will + // recurse into all of its member declarations, so that + // we can handle, e.g., nested `struct` types. + // + else if(auto containerDecl = as<ContainerDecl>(decl)) + { + for(auto member : containerDecl->Members) + { + checkInterfaceConformancesRec(member); + } + } + } + void visitModuleDecl(ModuleDecl* programNode) { // Try to register all the builtin decls @@ -2988,18 +3027,7 @@ namespace Slang if (pass == 0) { - // now we can check all interface conformances - for (auto & s : programNode->getMembersOfType<AggTypeDecl>()) - checkAggTypeConformance(s); - for (auto & s : programNode->getMembersOfType<ExtensionDecl>()) - checkExtensionConformance(s); - for (auto & g : programNode->getMembersOfType<GenericDecl>()) - { - if (auto innerAggDecl = as<AggTypeDecl>(g->inner)) - checkAggTypeConformance(innerAggDecl); - else if (auto innerExtDecl = as<ExtensionDecl>(g->inner)) - checkExtensionConformance(innerExtDecl); - } + checkInterfaceConformancesRec(programNode); } } } diff --git a/tests/compute/assoctype-nested.slang b/tests/compute/assoctype-nested.slang new file mode 100644 index 000000000..b3d96306b --- /dev/null +++ b/tests/compute/assoctype-nested.slang @@ -0,0 +1,61 @@ +// assoctype-nested.slang + +// Confirm that an associated type can be declared nested in its parent. + +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -dx12 +//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute + +interface IRandomGenerator +{ + [mutating] int generateVal(); +} + +interface IRandomStrategy +{ + associatedtype Generator : IRandomGenerator; + Generator makeGenerator(int seed); +} + +int helper<T:IRandomStrategy>(T strategy, int seed) +{ + var generator = strategy.makeGenerator(seed); + let val = generator.generateVal(); + return val; +} + +struct CounterStrategy : IRandomStrategy +{ + struct Generator : IRandomGenerator + { + int state; + [mutating] int generateVal() + { + return state++; + } + } + + Generator makeGenerator(int seed) + { + Generator generator = { seed }; + return generator; + } +} + +int test(int val) +{ + CounterStrategy strategy; + return helper(strategy, val); +} + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):dxbinding(0),glbinding(0),out +RWStructuredBuffer<int> gOutputBuffer; + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + uint tid = dispatchThreadID.x; + int inputVal = tid; + int outputVal = test(inputVal); + gOutputBuffer[tid] = outputVal; +}
\ No newline at end of file diff --git a/tests/compute/assoctype-nested.slang.expected.txt b/tests/compute/assoctype-nested.slang.expected.txt new file mode 100644 index 000000000..bc856dafa --- /dev/null +++ b/tests/compute/assoctype-nested.slang.expected.txt @@ -0,0 +1,4 @@ +0 +1 +2 +3 |
