[llvm-bugs] [Bug 39916] New: Invalid code generation and ICE when template references local static constexpr variable by pointer
via llvm-bugs
llvm-bugs at lists.llvm.org
Fri Dec 7 08:29:56 PST 2018
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: unassignedclangbugs at nondot.org
Reporter: andyg1001 at hotmail.co.uk
CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
llvm-bugs at lists.llvm.org, richard-llvm at 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20181207/b8fa2c15/attachment-0001.html>
More information about the llvm-bugs
mailing list