summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexey Panteleev <alpanteleev@nvidia.com>2022-04-01 10:56:02 -0700
committerGitHub <noreply@github.com>2022-04-01 10:56:02 -0700
commit2ddd252db192ab4376994d34cb9be862f97b5449 (patch)
treeca828032001de1b465e84a6c76b80177a4036649
parent255fd5873f65a6b01d5385c277d55612dc3cc587 (diff)
Improved type printing (#2172)
Improved the type printing function to include the generic substitutions and parent types. Added a test for it, mismatching-types.slang
-rw-r--r--source/slang/slang-syntax.cpp64
-rw-r--r--tests/diagnostics/mismatching-types.slang77
-rw-r--r--tests/diagnostics/mismatching-types.slang.expected35
-rw-r--r--tests/doc/doc.slang.expected14
4 files changed, 177 insertions, 13 deletions
diff --git a/source/slang/slang-syntax.cpp b/source/slang/slang-syntax.cpp
index 6b2140056..b9a5b8cd5 100644
--- a/source/slang/slang-syntax.cpp
+++ b/source/slang/slang-syntax.cpp
@@ -1132,17 +1132,69 @@ Index getFilterCountImpl(const ReflectClassInfo& clsInfo, MemberFilterStyle filt
return builder;
}
- void DeclRefBase::toText(StringBuilder& out) const
+ // Prints a partially qualified type name with generic substitutions.
+ static void _printNestedDecl(const Substitutions* substitutions, Decl* decl, StringBuilder& out)
{
- if (decl)
+ // If there is a parent scope for the declaration, print it first.
+ // Exclude top-level namespaces like `tu0` or `core`.
+ if (decl->parentDecl && !Slang::as<ModuleDecl>(decl->parentDecl))
{
- auto name = decl->getName();
- if (name)
+ auto parentGeneric = Slang::as<GenericDecl>(decl->parentDecl);
+
+ // Exclude function or operator names.
+ // Avoids excessively verbose messages like `func<T>(func::T)`
+ if (!(parentGeneric && Slang::as<CallableDecl>(parentGeneric->inner)))
{
- // TODO: need to print out substitutions too!
- out << name->text;
+ _printNestedDecl(substitutions, decl->parentDecl, out);
+
+ // If the parent is a generic for this type, skip *this* type.
+ // Avoids duplicate types like `MyType<T>::MyType`
+ if (parentGeneric && parentGeneric->inner == decl)
+ return;
+
+ out << ".";
}
}
+
+ // Print this type's name.
+ auto name = decl->getName();
+ if (name)
+ {
+ out << name->text;
+ }
+
+ // Look for generic substitutions on this type.
+ for (const Substitutions* subst = substitutions; subst; subst = subst->outer)
+ {
+ auto genericSubstitution = Slang::as<GenericSubstitution>(subst);
+ if (!genericSubstitution)
+ continue;
+
+ // If the substitution is for this type, print it.
+ if (genericSubstitution->genericDecl == decl)
+ {
+ out << "<";
+ bool isFirst = true;
+ for (const auto& it : genericSubstitution->args)
+ {
+ if (!isFirst)
+ out << ", ";
+ isFirst = false;
+ it->toText(out);
+ }
+ out << ">";
+
+ break;
+ }
+ }
+ }
+
+ void DeclRefBase::toText(StringBuilder& out) const
+ {
+ if (decl)
+ {
+ _printNestedDecl(substitutions, decl, out);
+ }
}
bool SubstitutionSet::equals(const SubstitutionSet& substSet) const
diff --git a/tests/diagnostics/mismatching-types.slang b/tests/diagnostics/mismatching-types.slang
new file mode 100644
index 000000000..15fc1d0e3
--- /dev/null
+++ b/tests/diagnostics/mismatching-types.slang
@@ -0,0 +1,77 @@
+// mismatching-types.slang
+//DIAGNOSTIC_TEST:SIMPLE:-target hlsl
+
+Texture1D<float> tex;
+
+struct GenericOuter<T>
+{
+ struct GenericInner<Q>
+ {
+ T val;
+ };
+
+ struct NonGenericInner
+ {
+ T val;
+ };
+
+ GenericInner<T> g;
+ NonGenericInner ng;
+};
+
+struct NonGenericOuter
+{
+ struct GenericInner<T>
+ {
+ T val;
+
+ struct ReallyNested
+ {
+ T val;
+ };
+
+ ReallyNested n;
+ };
+
+ GenericInner<int> i;
+ GenericInner<float> f;
+};
+
+
+[shader("compute")]
+[numthreads(1, 1, 1)]
+void main(uint3 dispatchThreadID : SV_DispatchThreadID)
+{
+ GenericOuter<int> a;
+ GenericOuter<float> b;
+ NonGenericOuter c;
+ NonGenericOuter.GenericInner<int> d;
+
+ // expected an expression of type 'GenericOuter<int>', got 'int'
+ a = 0;
+ // expected an expression of type 'GenericOuter<int>.GenericInner<int>', got 'int'
+ a.g = 0;
+ // expected an expression of type 'GenericOuter<int>.NonGenericInner', got 'int'
+ a.ng = 0;
+ // expected an expression of type 'GenericOuter<int>.GenericInner<int>', got 'GenericOuter<float>.GenericInner<float>'
+ a.g = b.g;
+ // expected an expression of type 'GenericOuter<int>.NonGenericInner', got 'GenericOuter<float>.NonGenericInner'
+ a.ng = b.ng;
+ // expected an expression of type 'NonGenericOuter.GenericInner<int>', got 'int'
+ c.i = 0;
+ // expected an expression of type 'NonGenericOuter.GenericInner<int>', got 'NonGenericOuter.GenericInner<float>'
+ c.i = c.f;
+ // expected an expression of type 'NonGenericOuter.GenericInner<int>.ReallyNested', got 'int'
+ c.i.n = 0;
+ // OK
+ c.i.n.val = 0;
+ // OK
+ d.val = 0;
+ // OK
+ c.i = d;
+
+ // expected an expression of type 'Texture1D<int>', got 'Texture1D<float>'
+ Texture1D<int> t1 = tex;
+ // expected an expression of type 'Texture2D<float>', got 'Texture1D<float>'
+ Texture2D<float> t2 = tex;
+} \ No newline at end of file
diff --git a/tests/diagnostics/mismatching-types.slang.expected b/tests/diagnostics/mismatching-types.slang.expected
new file mode 100644
index 000000000..2186a8d10
--- /dev/null
+++ b/tests/diagnostics/mismatching-types.slang.expected
@@ -0,0 +1,35 @@
+result code = -1
+standard error = {
+tests/diagnostics/mismatching-types.slang(51): error 30019: expected an expression of type 'GenericOuter<int>', got 'int'
+ a = 0;
+ ^
+tests/diagnostics/mismatching-types.slang(53): error 30019: expected an expression of type 'GenericOuter<int>.GenericInner<int>', got 'int'
+ a.g = 0;
+ ^
+tests/diagnostics/mismatching-types.slang(55): error 30019: expected an expression of type 'GenericOuter<int>.NonGenericInner', got 'int'
+ a.ng = 0;
+ ^
+tests/diagnostics/mismatching-types.slang(57): error 30019: expected an expression of type 'GenericOuter<int>.GenericInner<int>', got 'GenericOuter<float>.GenericInner<float>'
+ a.g = b.g;
+ ^
+tests/diagnostics/mismatching-types.slang(59): error 30019: expected an expression of type 'GenericOuter<int>.NonGenericInner', got 'GenericOuter<float>.NonGenericInner'
+ a.ng = b.ng;
+ ^
+tests/diagnostics/mismatching-types.slang(61): error 30019: expected an expression of type 'NonGenericOuter.GenericInner<int>', got 'int'
+ c.i = 0;
+ ^
+tests/diagnostics/mismatching-types.slang(63): error 30019: expected an expression of type 'NonGenericOuter.GenericInner<int>', got 'NonGenericOuter.GenericInner<float>'
+ c.i = c.f;
+ ^
+tests/diagnostics/mismatching-types.slang(65): error 30019: expected an expression of type 'NonGenericOuter.GenericInner<int>.ReallyNested', got 'int'
+ c.i.n = 0;
+ ^
+tests/diagnostics/mismatching-types.slang(74): error 30019: expected an expression of type 'Texture1D<int>', got 'Texture1D<float>'
+ Texture1D<int> t1 = tex;
+ ^~~
+tests/diagnostics/mismatching-types.slang(76): error 30019: expected an expression of type 'Texture2D<float>', got 'Texture1D<float>'
+ Texture2D<float> t2 = tex;
+ ^~~
+}
+standard output = {
+}
diff --git a/tests/doc/doc.slang.expected b/tests/doc/doc.slang.expected
index 95b3381b5..df51728e7 100644
--- a/tests/doc/doc.slang.expected
+++ b/tests/doc/doc.slang.expected
@@ -49,7 +49,7 @@ A useless method hey ho
## Signature
```
-T ParentStruct<T>.ChildStruct<S>.getValue(S v);
+ParentStruct.T ParentStruct<T>.ChildStruct<S>.getValue(ParentStruct<ParentStruct.T>.ChildStruct.S v);
```
## Parameters
@@ -77,7 +77,7 @@ T ParentStruct<T>.ChildStruct<S>.getValue(S v);
## Signature
```
-T GenericStruct<T>.getValue();
+GenericStruct.T GenericStruct<T>.getValue();
```
--------------------------------------------------------------------------------
@@ -120,7 +120,7 @@ void Hey::doAnotherThing(int a);
# inputBuffer
```
-RWStructuredBuffer inputBuffer
+RWStructuredBuffer<int> inputBuffer
```
## Description
@@ -160,9 +160,9 @@ Add two integers
## Signature
```
-V IDoThing.add(
- V a,
- V b);
+IDoThing.V IDoThing.add(
+ IDoThing.V a,
+ IDoThing.V b);
```
## Parameters
@@ -330,7 +330,7 @@ An enum
# outputBuffer
```
-RWStructuredBuffer outputBuffer
+RWStructuredBuffer<int> outputBuffer
```
## Description