public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/114410] New: compiler issue in reference_binding when using rvalue reference to member of const object
@ 2024-03-21  8:54 cvoica at gmail dot com
  2024-03-21  9:05 ` [Bug c++/114410] " pinskia at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: cvoica at gmail dot com @ 2024-03-21  8:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114410

            Bug ID: 114410
           Summary: compiler issue in reference_binding when using rvalue
                    reference to member of const object
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: cvoica at gmail dot com
  Target Milestone: ---

The issue "internal compiler error: in reference_binding, at cp/call.cc:2020"
is visible when compiling this program

#include<iostream>
#include<string>

template<typename T>
class Holder{
    typedef T value_type;
    value_type m_v;
public:
    Holder(const value_type& v):m_v(v){}
    operator const value_type() const{return m_v;}
    operator value_type(){return m_v;}

//    operator const value_type&() const{return m_v;}
//    operator value_type&(){return m_v;}
};

struct A {
    Holder<std::string> m_i;
    A(const std::string& i):m_i(i){}
};


void bad_const_access(const A* a){
    std::cout << (std::string&)a->m_i << std::endl; //crash in gcc-13 but OK in
gcc-12
}
void correct_const_access(const A* a){
    std::cout << (const std::string&)a->m_i << std::endl;
}
int main(){
    const A a("test");
    bad_const_access(&a);
    correct_const_access(&a);
    return 0;
}

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/114410] compiler issue in reference_binding when using rvalue reference to member of const object
  2024-03-21  8:54 [Bug c++/114410] New: compiler issue in reference_binding when using rvalue reference to member of const object cvoica at gmail dot com
@ 2024-03-21  9:05 ` pinskia at gcc dot gnu.org
  2024-03-21  9:07 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-21  9:05 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114410

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                URL|https://godbolt.org/#z:OYLg |
                   |hAFBqd5QCxAYwPYBMCmBRdBLAF1 |
                   |QCcAaPECAMzwBtMA7AQwFtMQByA |
                   |Rg9KtQYEAysib0QXACx8BBAKoBn |
                   |TAAUAHpwAMvAFYTStJg1DIApACY |
                   |AQuYukl9ZATwDKjdAGFUtAK4sGe |
                   |1wAyeAyYAHI%2BAEaYxCAAzACsp |
                   |AAOqAqETgwe3r56KWmOAkEh4SxR |
                   |MQm2mPYFDEIETMQEWT5%2BXJXVG |
                   |XUNBEVhkdFxiQr1jc05bcPdvSVl |
                   |gwCUtqhexMjsHOaxwcjeWCaxbk7 |
                   |DxJise9gmGgCCG1s7mHtuR8HAZx |
                   |fXVwSYLEkGnw8EAE8koxWJgANQA |
                   |FVeV22TAUCjBAAlPFhiCYAOxWK5 |
                   |gnFgwHArBUSFggBuYi8mAA%2Bvj |
                   |7rEsZdcaTyVSaWCWJSSXt6UkvBF |
                   |aHhkCA3ozkbRURA0AxhkzvCygfc |
                   |zAA2UlzEDskkQElzDFWdEAEWFuN |
                   |QwOITCIxDBkulZNl1PlEDmloEwx |
                   |1xwIywYbI5XIxBuxRpNZpIMopdu |
                   |BDtdmHdxE96p9%2BrebwA9EnGcb |
                   |okGLVaCCG5cDzIqHU6pQRI9HY96 |
                   |6b7k6mAxnzbmwwrC9rMW6PV7OVW |
                   |E1dq1Xe1cjl4HGCGTrDTjRaiHsN |
                   |0CAQE8jGcvXgff6cZcJc6c7P54u |
                   |XkqwXhVey8BBj%2BOe9cE/33neS |
                   |ag8OgwREmOhKdnKUxkKt4VuS6OA |
                   |BUYJMK29KMruKBLDmDwPGCECQfu |
                   |BZzEwAC0ZynmCsH7GCkGuLQXJgi |
                   |myCmgoCBHp6wA/qhXCxC%2BXg5g |
                   |A8gA0hRYJUcgNFmImV4Pk%2BTrE |
                   |McDgftuX4/pgf7ZkBIFgROuEEHO |
                   |UEMVh%2Bxwf%2B0qIQQxDPMhaEY |
                   |ZSeAqW4cF4Qw6AEbe1ZXMEOYsEw |
                   |wQRpiclSQyTAQOYZifC6ZhmNqt6 |
                   |Mq%2B76ft%2Bv4KO5SqgWuDK4mg |
                   |gmYMJwXiX%2BBaRf5uLtjGYIaFF |
                   |VmXBwCy0Jw8S8H4HBaKQqCcMZlj |
                   |WLhSwrOCGw8KQBCaPlCwANYgJIk |
                   |gAHQ9QNg1DfonCSCVbUVZwvAKCA |
                   |GgtW1CxwLASBoN8dDROQlCrUk60 |
                   |xMAtFtDQtCfMQM0QBEE0RMEDQAp |
                   |d13EACjERNo8WtdwvCrWwgiMQwt |
                   |C3WVvBYBEXjAG4Yi0DN72kFgdlG |
                   |OIAPQ3gQmOCSEkTZgqjxQxazlTZ |
                   |VQTfyESmg9HhYBNWl4CwC18AYwA |
                   |KAAangmAAO6McCpXNfwggiGI7BS |
                   |DIgiKCo6gI7obQGEYKDWNY%2Bh4 |
                   |BEM2QAsxo1JDqGznseqmDVlhmBo |
                   |YKoQA6uD%2BsGxjWlMCbBKGI4yC |
                   |8KgqOCU%2BaOK%2B0r0ZC4ZljK0 |
                   |pCBMEfSlAMbR5OkAhe7kqQhww0z |
                   |9DEExVG7AhdKMngtHodgJ7UIw9H |
                   |7MyB7YWdhxMWfRwHscLAo9WrBIB |
                   |VFeNCOVRwYKqAAHIqqGKpI7E/mC |
                   |tG9Vw8G4IQwZNXMvBvVocydSAPm |
                   |9T5c/zwviSFRwY2kKV5UN9Ns3zQ |
                   |Di0wIgIDQTyBCbRAmD4OaT56Fzw |
                   |iiOI/M30LagTWLpDM6aSScDwNcc |
                   |MVa8TQ3RiDFj5glQESFubcO5d2Q |
                   |D3WIfd4IeDWvQC0I8x4LSnrEOBm |
                   |DsE4NwSNFedcN5TVsNvce7V8FmE |
                   |IXbYhZDJ6k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CGw8KQBCaPlCwANYgJIkgAHQ9QNg1DfonCSCVbUVZwvAKCAGgtW1CxwLASBoN8dDROQlCrUk60xMAtFtDQtCfMQM0QBEE0RMEDQApd13EACjERNo8WtdwvCrWwgiMQwtC3WVvBYBEXjAG4Yi0DN72kFgdlGOIAPQ3gQmOCSEkTZgqjxQxazlTZVQTfyESmg9HhYBNWl4CwC18AYwAKAAangmAAO6McCpXNfwggiGI7BSDIgiKCo6gI7obQGEYKDWNY%2Bh4BEM2QAsxo1JDqGznseqmDVlhmBoYKoQA6uD%2BsGxjWlMCbBKGI4yC8KgqOCU%2BaOK%2B0r0ZC4ZljK0pCBMEfSlAMbR5OkAhe7kqQhww0z9DEExVG7AhdKMngtHodgJ7UIw9H7MyB7YWdhxMWfRwHscLAo9WrBIBVFeNCOVRwYKqAAHIqqGKpI7E/mCtG9Vw8G4IQwZNXMvBvVocydSAPm9T5c/zwviSFRwY2kKV5UN9Ns3zQDi0wIgIDQTyBCbRAmD4OaT56FzwiiOI/M30LagTWLpDM6aSScDwNccMVa8TQ3RiDFj5glQESFubcO5d2QD3WIfd4IeDWvQC0I8x4LSnrEOBmDsE4NwSNFedcN5TVsNvce7V8FmEIXbYhZDJ6kAdmkZwkggA%3D

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/114410] compiler issue in reference_binding when using rvalue reference to member of const object
  2024-03-21  8:54 [Bug c++/114410] New: compiler issue in reference_binding when using rvalue reference to member of const object cvoica at gmail dot com
  2024-03-21  9:05 ` [Bug c++/114410] " pinskia at gcc dot gnu.org
@ 2024-03-21  9:07 ` pinskia at gcc dot gnu.org
  2024-03-21  9:18 ` cvoica at gmail dot com
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-21  9:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114410

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
dup.

*** This bug has been marked as a duplicate of bug 113141 ***

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/114410] compiler issue in reference_binding when using rvalue reference to member of const object
  2024-03-21  8:54 [Bug c++/114410] New: compiler issue in reference_binding when using rvalue reference to member of const object cvoica at gmail dot com
  2024-03-21  9:05 ` [Bug c++/114410] " pinskia at gcc dot gnu.org
  2024-03-21  9:07 ` pinskia at gcc dot gnu.org
@ 2024-03-21  9:18 ` cvoica at gmail dot com
  2024-03-21  9:26 ` cvoica at gmail dot com
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: cvoica at gmail dot com @ 2024-03-21  9:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114410

--- Comment #3 from Cristian VOICA <cvoica at gmail dot com> ---
be aware that is initial issue mentions "If the type of r is changed to const
int&, the program correctly compiles with GCC"
I have this in my code as well but this is not always what programmer wants as
it creates a reference to a temporary object and extend the lifetime with const
not aan lvalue const reference.
If possible, a warning would be nice here

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/114410] compiler issue in reference_binding when using rvalue reference to member of const object
  2024-03-21  8:54 [Bug c++/114410] New: compiler issue in reference_binding when using rvalue reference to member of const object cvoica at gmail dot com
                   ` (2 preceding siblings ...)
  2024-03-21  9:18 ` cvoica at gmail dot com
@ 2024-03-21  9:26 ` cvoica at gmail dot com
  2024-03-21 10:25 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: cvoica at gmail dot com @ 2024-03-21  9:26 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114410

--- Comment #4 from Cristian VOICA <cvoica at gmail dot com> ---
I'm also trying to understand another thing. 
Perhaps you can point me to where to read more about it as I was not expecting
casting to a non-const reference to work for a member in a const object.

Is there a warning for (std::string&)a->m_i on "const A* a" ?

Thx!

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/114410] compiler issue in reference_binding when using rvalue reference to member of const object
  2024-03-21  8:54 [Bug c++/114410] New: compiler issue in reference_binding when using rvalue reference to member of const object cvoica at gmail dot com
                   ` (3 preceding siblings ...)
  2024-03-21  9:26 ` cvoica at gmail dot com
@ 2024-03-21 10:25 ` redi at gcc dot gnu.org
  2024-03-21 10:45 ` cvoica at gmail dot com
  2024-03-21 11:43 ` cvoica at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2024-03-21 10:25 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114410

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
No, because a (T) style cast can do almost anything. In this case it's
equivalent to

const_cast<std::string&>(static_cast<const std::string&>(a->m_i))

which is valid code.

-Wold-style-cast will warn about using any form of (T) cast, which can
encourage you to be more careful and precise about what you're trying to do.

If you had written it like this:

std::cout << static_cast<std::string&>(a->m_i) << std::endl;

Then it wouldn't even compile.

tl;dr don't use C-style casts if you want the compiler to tell you when you're
doing something wrong.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/114410] compiler issue in reference_binding when using rvalue reference to member of const object
  2024-03-21  8:54 [Bug c++/114410] New: compiler issue in reference_binding when using rvalue reference to member of const object cvoica at gmail dot com
                   ` (4 preceding siblings ...)
  2024-03-21 10:25 ` redi at gcc dot gnu.org
@ 2024-03-21 10:45 ` cvoica at gmail dot com
  2024-03-21 11:43 ` cvoica at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: cvoica at gmail dot com @ 2024-03-21 10:45 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114410

--- Comment #6 from Cristian VOICA <cvoica at gmail dot com> ---
makes sense, thx!

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/114410] compiler issue in reference_binding when using rvalue reference to member of const object
  2024-03-21  8:54 [Bug c++/114410] New: compiler issue in reference_binding when using rvalue reference to member of const object cvoica at gmail dot com
                   ` (5 preceding siblings ...)
  2024-03-21 10:45 ` cvoica at gmail dot com
@ 2024-03-21 11:43 ` cvoica at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: cvoica at gmail dot com @ 2024-03-21 11:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114410

--- Comment #7 from Cristian VOICA <cvoica at gmail dot com> ---
I've found a flag that sounds closer to my needs: -Wcast-qual
As the issue seems to me to come from the need to stay compatible with C I
think this flag does a pretty decent job to hint at the problem.

<source>: In function 'void bad_const_access(const A*)':
<source>:24:18: warning: cast from type 'const
Holder<std::__cxx11::basic_string<char> >*' to type 'std::string*' {aka
'std::__cxx11::basic_string<char>*'} casts away qualifiers [-Wcast-qual]
   24 |     std::cout << (std::string&)a->m_i << std::endl; //crash in gcc-13
but OK in gcc-12
      |                  ^~~~~~~~~~~~~~~~~~~~
<source>:24:35: internal compiler error: in reference_binding, at
cp/call.cc:2020
   24 |     std::cout << (std::string&)a->m_i << std::endl; //crash in gcc-13
but OK in gcc-12
      |                                   ^~~

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-03-21 11:43 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-21  8:54 [Bug c++/114410] New: compiler issue in reference_binding when using rvalue reference to member of const object cvoica at gmail dot com
2024-03-21  9:05 ` [Bug c++/114410] " pinskia at gcc dot gnu.org
2024-03-21  9:07 ` pinskia at gcc dot gnu.org
2024-03-21  9:18 ` cvoica at gmail dot com
2024-03-21  9:26 ` cvoica at gmail dot com
2024-03-21 10:25 ` redi at gcc dot gnu.org
2024-03-21 10:45 ` cvoica at gmail dot com
2024-03-21 11:43 ` cvoica at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).