llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.34k stars 11.7k forks source link

error: non-type template argument is not a constant expression with 32-bit address space #82490

Open ararmine opened 7 months ago

ararmine commented 7 months ago

The following C++ code is not compiled with clang x86-64 v17.0.1 with MS extensions enabled, while it compiles successfully with X86 msvc v19.
Steps to reproduce:

  1. cat test.cpp
    
    typedef void                OpcUa_Void;

typedef OpcUa_Void (__ptr32 PfnClear)(OpcUa_Void __ptr32);

template<class T, PfnClear fctClear> class UaStackTypeArray {};

OpcUa_Void ClearNothing(void* __ptr32);

template class UaStackTypeArray<bool, ClearNothing>;

2. clang test.cpp -S -fms-extensions

test.cpp:12:39: error: non-type template argument is not a constant expression 12 | template class UaStackTypeArray<bool, ClearNothing>; | ^~~~ test.cpp:12:39: note: cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression 1 error generated.

llvmbot commented 7 months ago

@llvm/issue-subscribers-clang-frontend

Author: None (ararmine)

The following C++ code is not compiled with clang x86-64 v17.0.1 with MS extensions enabled, while it compiles successfully with X86 msvc v19. Steps to reproduce: 1. cat test.cpp ```c++ typedef void OpcUa_Void; typedef OpcUa_Void (*__ptr32 PfnClear)(OpcUa_Void* __ptr32); template<class T, PfnClear fctClear> class UaStackTypeArray {}; OpcUa_Void ClearNothing(void* __ptr32); template class UaStackTypeArray<bool, ClearNothing>; ``` 2. clang test.cpp -S -fms-extensions ``` test.cpp:12:39: error: non-type template argument is not a constant expression 12 | template class UaStackTypeArray<bool, ClearNothing>; | ^~~~~~~~~~~~ test.cpp:12:39: note: cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression 1 error generated. ```
shafik commented 7 months ago

This looks very similar to https://github.com/llvm/llvm-project/issues/12488 which was closed as won't fix.

CC @AaronBallman for second set of 👀

zygoloid commented 7 months ago

I think this is different -- this isn't an arbitrary reinterpret_cast conversion, it's just a CK_AddressSpaceConversion, which we could in principle permit in constant evaluation (perhaps just in this case, or perhaps in general). It'd be a bit of work to do, including IR generation changes, but probably not too hard.

I think the bigger problem is that the backend doesn't support these constants either: https://godbolt.org/z/4GW9cbenr