https://bugs.llvm.org/show_bug.cgi?id=39916
Bug ID: 39916
Summary: Invalid code generation and ICE when template
references local static constexpr variable by pointer
Product: clang
Version: trunk
Hardware: PC
OS: Linux
Status: NEW
Severity: release blocker
Priority: P
Component: C++'17
Assignee: unassignedclangb...@nondot.org
Reporter: andyg1...@hotmail.co.uk
CC: blitzrak...@gmail.com, erik.pilking...@gmail.com,
llvm-bugs@lists.llvm.org, richard-l...@metafoo.co.uk
The following minimised code fragment generates incorrect code in all clang
version from 4.0 (oldest I have) up to trunk (tested using godbolt and -O2
-std=c++1z):
template <const int* X>
struct S
{
static int Value() { return *X; }
};
template <typename T>
int fn()
{
static constexpr int value = 10;
return S<&value>::Value();
}
int test()
{
return fn<int>(); // will return 0 not 10
}
There are ways that the code can be made to produce correct values:
1. Move the 'static constexpr int value = 10' line up to the global scope
2. Remove the template from 'int fn()' to make it a plain function
3. Change the template parameter in struct S from 'const int* X' to 'const
int& X'
So, for example, the following compiles correctly...
template <const int& X>
struct S
{
static int Value() { return X; }
};
template <typename T>
int fn()
{
static constexpr int value = 10;
return S<value>::Value();
}
int test()
{
return fn<int>(); // will return 0 not 10
}
The following, slightly more complex example, crashes the compiler:
template <const int* X>
struct S
{
static int Value() { return *X; }
};
template <typename T>
int fn()
{
const int c = ([]() {
static constexpr int value = 10;
return S<&value>::Value();
})();
return c;
}
int test()
{
return fn<int>();
}
Again, any one of the three "solutions" above solves the problem. So the issue
seems to be in the specific use of pointers in the template for S when called
from another templated function.
However, while we're here, this also crashes the compiler:
template <const int& X>
struct S
{
static int Value() { return X; }
};
template <typename T>
int fn()
{
const int c = ([]() {
static int value = 10; // not const or constexpr!
return S<value>::Value();
})();
return c;
}
int test()
{
return fn<int>();
}
But utilising a second "solution" from above means this is ok again:
template <const int& X>
struct S
{
static int Value() { return X; }
};
int fn()
{
const int c = ([]() {
static int value = 10;
return S<value>::Value();
})();
return c;
}
int test()
{
return fn();
}
--
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs