Bug 114219 - [11/12/13/14 Regression] [expr.const] lvalue-to-rvalue conversion is not diagnosed to disqualify constant expressions for empty classes
Summary: [11/12/13/14 Regression] [expr.const] lvalue-to-rvalue conversion is not diag...
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 14.0
: P3 normal
Target Milestone: 11.5
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid, needs-bisection
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2024-03-03 08:05 UTC by Jan Schultke
Modified: 2024-03-03 18:08 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 5.1.0, 6.1.0, 6.4.0
Known to fail: 7.1.0
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Schultke 2024-03-03 08:05:52 UTC
https://godbolt.org/z/jhcP8WPn8

Code to reproduce
=================

struct S {};

constexpr int f(S s) {
    constexpr S _ = s;
    return 0;
}

S s;
constexpr int _ = f(s);



Issue description
=================

This code compiles, but should produce errors for both constexpr initializations.

According to [expr.const] p5.9, the initializers of _ cannot be constant expression because they contain lvalue-to-rvalue conversion of s, whose lifetime did not begin within the evaluation of the constant expression, and which is not usable in constant expressions.

It shouldn't matter whether S is an empty class or has members; the lvalue-to-rvalue conversion in itself disqualifies expressions from being constant expressions.
Comment 1 Andrew Pinski 2024-03-03 08:15:35 UTC
Hmm, clang also accepts this ...
Comment 2 Jan Schultke 2024-03-03 08:18:12 UTC
Corresponding LLVM bug: https://github.com/llvm/llvm-project/issues/83712
Comment 3 Andrew Pinski 2024-03-03 08:21:56 UTC
https://gcc.gnu.org/pipermail/gcc-patches/2020-February/539673.html


hmm reading https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91953#c3 implies there is no lvalue-to-rvalue conversion happening ...
Comment 4 Jan Schultke 2024-03-03 08:33:14 UTC
I don't see how lvalue-to-rvalue conversion would be bypassed here.

https://eel.is/c++draft/conv.lval#:conversion,lvalue-to-rvalue has no special provision for empty classes.

https://eel.is/c++draft/dcl.init.general#16.9 would necessitate lvalue-to-rvalue conversion because the initializer has to be converted to a prvalue. I couldn't find any special rule for empty classes.
Comment 5 Jan Schultke 2024-03-03 18:08:57 UTC
Looks like the existing comments were right.

Lvalue-to-rvalue conversion very rarely takes place for class types, and if it does, that may be a wording bug.

Instead of lvalue-to-rvalue conversion, this case calls the implicitly-defined copy constructor, which can be used in constant expressions.