-
Notifications
You must be signed in to change notification settings - Fork 884
Disallow built-in internal types to be inferred by auto #8510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
acf7235
cc2eb8f
8a363cc
c7e29f4
69045d5
8dc7932
36aede0
6adf8a0
bf83962
d704acc
615a73e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4773,6 +4773,45 @@ class HLSLExternalSource : public ExternalSemaSource { | |
| return type; | ||
| } | ||
|
|
||
| bool IsTypeDeducibleWithAuto(QualType type) { | ||
| if (type.isNull()) | ||
| return false; | ||
|
|
||
| switch (GetTypeObjectKind(type)) { | ||
| // Types with no concrete, nameable form for 'auto' to bind to. | ||
| case AR_TOBJ_INVALID: | ||
| case AR_TOBJ_VOID: | ||
| case AR_TOBJ_INNER_OBJ: | ||
| case AR_TOBJ_DEPENDENT: | ||
| return false; | ||
|
|
||
| // Fully-formed types that 'auto' can deduce directly. | ||
| case AR_TOBJ_OBJECT: | ||
| case AR_TOBJ_BASIC: | ||
| case AR_TOBJ_COMPOUND: | ||
| case AR_TOBJ_INTERFACE: | ||
| case AR_TOBJ_STRING: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure STRING should be included here. |
||
| case AR_TOBJ_LINALG_MATRIX: | ||
| return true; | ||
|
|
||
| // Container types: deducible iff their wrapped type is deducible. | ||
| case AR_TOBJ_MATRIX: | ||
| case AR_TOBJ_VECTOR: | ||
| return IsTypeDeducibleWithAuto(GetMatrixOrVectorElementType(type)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why matrix/vector types aren't just always deducible. What element type is allowed with these types (we have separate checking for the allowed element template type) that would prevent this? |
||
| case AR_TOBJ_ARRAY: { | ||
| const ArrayType *arrayType = | ||
| GetStructuralForm(type)->getAsArrayTypeUnsafe(); | ||
| return IsTypeDeducibleWithAuto(arrayType->getElementType()); | ||
| } | ||
| case AR_TOBJ_POINTER: | ||
| return IsTypeDeducibleWithAuto(GetStructuralForm(type)->getPointeeType()); | ||
| case AR_TOBJ_QUALIFIER: | ||
| return IsTypeDeducibleWithAuto( | ||
| GetStructuralForm(type).getUnqualifiedType()); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /// <summary>Given a Clang type, return the ArBasicKind classification for its | ||
| /// contents.</summary> | ||
| ArBasicKind GetTypeElementKind(QualType type) { | ||
|
|
@@ -12787,6 +12826,10 @@ bool hlsl::DiagnoseTypeElements(Sema &S, SourceLocation Loc, QualType Ty, | |
| LongVecDiagContext, CheckedDecls, FD); | ||
| } | ||
|
|
||
| bool hlsl::IsTypeDeducibleWithAuto(Sema &S, QualType Ty) { | ||
| return HLSLExternalSource::FromSema(&S)->IsTypeDeducibleWithAuto(Ty); | ||
| } | ||
|
|
||
| bool hlsl::DiagnoseNodeStructArgument(Sema *self, TemplateArgumentLoc ArgLoc, | ||
| QualType ArgTy, bool &Empty, | ||
| const FieldDecl *FD) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // RUN: %dxc -T cs_6_0 -HV 202x -verify %s | ||
| // expected-no-diagnostics | ||
|
|
||
| struct MyStruct { | ||
| float a; | ||
| int b; | ||
| }; | ||
|
|
||
| Texture2D<float4> tex : register(t0); | ||
| SamplerState samp : register(s0); | ||
| RWBuffer<float> output : register(u0); | ||
|
|
||
| [numthreads(1,1,1)] | ||
| void main() { | ||
| // AR_TOBJ_BASIC | ||
| auto i = 5; | ||
| auto f = 1.5f; | ||
| auto b = true; | ||
|
|
||
| // AR_TOBJ_VECTOR -> element AR_TOBJ_BASIC | ||
| auto v = float4(1, 2, 3, 4); | ||
|
|
||
| // AR_TOBJ_MATRIX -> element AR_TOBJ_BASIC | ||
| float2x2 matInit = { 1, 2, 3, 4 }; | ||
| auto m = matInit; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about something like? auto row = m[0]; |
||
|
|
||
| // AR_TOBJ_COMPOUND | ||
| MyStruct s = { 1.0f, 2 }; | ||
| auto sCopy = s; | ||
|
|
||
| // AR_TOBJ_OBJECT: resource handles are copyable, so binding one is allowed. | ||
| auto t = tex; | ||
|
|
||
| // Use every value to prevent dead-code elimination. | ||
| output[0] = (float)i + f + (float)b + v.x + m._11 + sCopy.a + | ||
| (float)sCopy.b + t.SampleLevel(samp, float2(0, 0), 0).x; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // RUN: %dxc -T cs_6_0 -HV 202x -verify %s | ||
|
|
||
|
|
||
| void voidFunc() {} | ||
|
|
||
| Texture2D<float4> tex : register(t0); | ||
| Texture2DMS<float4> texMS : register(t1); | ||
|
|
||
| [numthreads(1,1,1)] | ||
| void main() { | ||
| int y = 1; | ||
|
|
||
| // AR_TOBJ_VOID: a void-returning call has no value type. | ||
| // expected-error@+1 {{'auto' cannot deduce type 'void'}} | ||
| auto x = voidFunc(); | ||
|
|
||
| // AR_TOBJ_VOID: an explicit cast to void is equally non-deducible. | ||
| // expected-error@+1 {{'auto' cannot deduce type 'void'}} | ||
| auto v = (void)y; | ||
|
|
||
| // AR_TOBJ_INNER_OBJ: .mips is an inner indexer object (Texture2D::mips_type). | ||
| // expected-error@+1 {{'auto' cannot deduce type}} | ||
| auto m = tex.mips; | ||
|
|
||
| // AR_TOBJ_INNER_OBJ: .sample is an inner indexer object | ||
| // (Texture2DMS::sample_type). | ||
| // expected-error@+1 {{'auto' cannot deduce type}} | ||
| auto s = texMS.sample; | ||
|
joaosaffran marked this conversation as resolved.
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the only two cases here that actually matter are
AR_TOBJ_INNER_OBJandAR_TOBJ_STRINGright?Any other case should result in an error from somewhere else in the compiler.
Why not just handle the string case and put an attribute on the inner object types that marks them as non-deducible?
If we also put that attribute implicitly on subobject types, we can flatten a lot of this to just a hasAttr check.