Bug 98440 - [10 Regression] Accepts ill-formed reinterpret_cast<int&&>(1)
Summary: [10 Regression] Accepts ill-formed reinterpret_cast<int&&>(1)
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.2.0
: P2 normal
Target Milestone: 11.0
Assignee: Jason Merrill
URL:
Keywords: accepts-invalid
: 86633 (view as bug list)
Depends on:
Blocks:
 
Reported: 2020-12-24 21:38 UTC by Andrew Schepler
Modified: 2023-07-07 09:20 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work: 11.0, 8.3.0
Known to fail: 10.1.0, 10.5.0, 9.2.0
Last reconfirmed: 2020-12-27 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Schepler 2020-12-24 21:38:53 UTC
g++ does not give any error or warning for a reinterpret_cast when the target type is an rvalue reference type and the expression operand is a prvalue, for example reinterpret_cast<int&&>(1).

In standard section [expr.reinterpret.cast], casts to reference types are described only by paragraph 11, which allows casting a glvalue to a reference type, but specifically says "No temporary is created". So the temporary materialization conversion can't be implicitly used like in most expressions, and the paragraph can't apply.

Clang++, MSVC, and ICC all produce error messages for the expression reinterpret_cast<int&&>(1).
Comment 1 Jonathan Wakely 2020-12-27 20:19:19 UTC
This was correctly rejected until r260622:

    Fix cast to rvalue reference from prvalue.
    
            * cvt.c (diagnose_ref_binding): Handle rvalue reference.
            * rtti.c (build_dynamic_cast_1): Don't try to build a reference to
            non-class type.  Handle xvalue argument.
            * typeck.c (build_reinterpret_cast_1): Allow cast from prvalue to
            rvalue reference.
            * semantics.c (finish_compound_literal): Do direct-initialization,
            not cast, to initialize a reference.
Comment 2 GCC Commits 2021-04-05 21:27:31 UTC
The master branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:07f56824fd4da14a48030e698c8eb58de951c741

commit r11-7994-g07f56824fd4da14a48030e698c8eb58de951c741
Author: Jason Merrill <jason@redhat.com>
Date:   Mon Apr 5 15:50:48 2021 -0400

    c++: reinterpret_cast from prvalue to rvalue ref [PR98440]
    
    In r260622 I allowed this under the general principle that [basic.lval]
    "Whenever a prvalue appears as an operand of an operator that expects a
    glvalue for that operand, the temporary materialization conversion (7.3.4)
    is applied to convert the expression to an xvalue."  But
    [expr.reinterpret.cast] specifically excludes creating a temporary in this
    case.
    
    gcc/cp/ChangeLog:
    
            PR c++/98440
            * typeck.c (build_reinterpret_cast_1): Don't perform
            temporary materialization.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp0x/rv-cast6.C: Expect reinterpret_cast error.
            * g++.dg/cpp0x/reinterpret_cast2.C: Adjust message.
            * g++.old-deja/g++.jason/rvalue3.C: Likewise.
Comment 3 Jason Merrill 2021-04-05 21:30:03 UTC
Fixed for 11 so far.
Comment 4 Richard Biener 2021-06-01 08:19:17 UTC
GCC 9.4 is being released, retargeting bugs to GCC 9.5.
Comment 5 Andrew Pinski 2021-07-27 22:22:42 UTC
*** Bug 86633 has been marked as a duplicate of this bug. ***
Comment 6 Richard Biener 2022-05-27 09:44:06 UTC
GCC 9 branch is being closed
Comment 7 Jakub Jelinek 2022-06-28 10:42:56 UTC
GCC 10.4 is being released, retargeting bugs to GCC 10.5.
Comment 8 Richard Biener 2023-07-07 09:20:02 UTC
Fixed in GCC 11.