[llvm-bugs] [Bug 48517] New: Accidental equality of classes templated by pointer to local static constant of templated function

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Dec 15 08:48:15 PST 2020


https://bugs.llvm.org/show_bug.cgi?id=48517

            Bug ID: 48517
           Summary: Accidental equality of classes templated by pointer to
                    local static constant of templated function
           Product: clang
           Version: 5.0
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++17
          Assignee: unassignedclangbugs at nondot.org
          Reporter: matthieum.147192 at gmail.com
                CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
                    llvm-bugs at lists.llvm.org, richard-llvm at metafoo.co.uk

Based on https://stackoverflow.com/q/65306562/147192.

The behavior of Clang (and GCC) is inconsistent (between compile-time and
run-time), however it is unclear to me whether the inconsistency is conforming
with the C++17 standard or not.

Furthermore, in -O0 mode, Clang generates an unused symbol (`create()::I`)
which causes the linker to fail, see https://godbolt.org/z/M4T7f3.


The following reduced program is expected to return 0 (invoking clang++ with
-std=c++17), it does not (https://godbolt.org/z/6r6vK3):

template <typename, typename>
struct is_same { static constexpr bool value = false; };

template <typename T>
struct is_same<T, T> { static constexpr bool value = true; };

template <typename T, typename U>
static constexpr bool is_same_v = is_same<T, U>::value;

using uintptr_t = unsigned long long;

template <int const* I>
struct Parameterized { int const* member; };

template <typename T>
auto create() {
    static constexpr int const I = 2;

    return Parameterized<&I>{ &I };
}

int main() {
    auto one = create<short>();
    auto two = create<int>();

    if (is_same_v<decltype(one), decltype(two)>) {
        return reinterpret_cast<uintptr_t>(one.member) ==
reinterpret_cast<uintptr_t>(two.member) ? 1 : 2;
    }

    return 0;
}

Yet, on all versions of Clang where it compiles (from 5.0.0 onwards), and for
all optimization levels (from -O1 to -O3), it returns 2, indicating:

- That `one` and `two` have the same type -- which according to 17.4
[temp.type] should mean that they point to the same object.
- Yet they point to different objects -- there are two instances of
`create<T>()::I`, one for `T = short` and one for `T = int`.

The assembly listing clearly contains 2 different instances of
`create<T>()::I`.


Notes:

- If `I` is initialized with `= sizeof(T)`, instead, then with -O1 to -O3 the
program returns 0 as expected.
- Even with `I` initialized with `= sizeof(T)`, with -O0 Clang still generates
an unused symbol which causes the linker to fail: `auto create<short>()::I` and
`auto create<int>()::I`, whereas the declared symbols do not have the leading
`auto`.

-- 
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/20201215/e9998ad6/attachment.html>


More information about the llvm-bugs mailing list