DirectXShaderCompiler icon indicating copy to clipboard operation
DirectXShaderCompiler copied to clipboard

Templated Class inherting from Templated Base cannot call shadowed template method

Open devshgraphicsprogramming opened this issue 1 year ago • 2 comments

Description HLSL counterpart to perfectly valid C++ template code ( https://godbolt.org/z/eh6ePvGWE ) fails to compile.

Steps to Reproduce Compile this godbolt: https://godbolt.org/z/48Eed5fvc

with -E PSMain -T ps_6_7 -HV 2021

Note that the bug happens for DXIL as well.

Actual Behavior I crash in SemaOverload.cpp in method:

static ImplicitConversionSequence
TryObjectArgumentInitialization(Sema &S, QualType FromType,
                                Expr::Classification FromClassification,
                                CXXMethodDecl *Method,
                                CXXRecordDecl *ActingContext)

on line:

  // First check the qualifiers.
  QualType FromTypeCanon = S.Context.getCanonicalType(FromType);

Environment

  • DXC version : commit 3ac44bc1ffaaaa45784d57e59a6ce8fe89f87298, so pretty much almost HEAD
  • Host Operating System: Windows

I've cut it down to absolute minimum, no language keywords and no reserved identifiers (like double _)

C++: https://godbolt.org/z/58Yrcqzh4 HLSL: https://godbolt.org/z/Wj3bnsrWj

If you remove any of the two templates (around class or around method), it compiles.

It no longer segfaults on latest trunk but gives this error

not viable: no known conversion from 'Derived<false> &' to 'Base<false>' for object argument

its because it seems to "not know" that we're calling the base class non-static method and this was supposed to reference

Somehow when the Base method is not templates it knows to pass this of type Derived<false>& by reference and casted to a Base<false>&

The code you're trying to compile isn't actually valid in C++ 98, it was only valid with C++ 11 and later. While this is odd, HLSL has C++ 98-based templates by design, so newer features in C++ 11+ are not supported. For that reason, I'm closing this as behaving correctly.

Good news for the future! HLSL in Clang has adopted a C++ 11-based base, which includes C++ 11 templates (https://github.com/microsoft/hlsl-specs/blob/main/proposals/0023-cxx11-base.md). This is planned as a feature for HLSL 202y, but will be available in HLSL 202x in clang only.

llvm-beanz avatar Oct 28 '24 17:10 llvm-beanz

Why are you closing an issue that reports a crash? You haven't even told me why its not valid C++98

I should at least get an intelligible error.

Also Clang 3.7 from which DXC forked, compiles .template method (I guess thats the C++11 feature) with a warning but without issue https://godbolt.org/z/TPv1f8sqh

I think this issue stems from how DXC changed the semantics of this to not be a reference.