public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/100635] New: error: cannot bind non-const lvalue reference of type 'const volatile int&' to ...
@ 2021-05-17  9:04 redi at gcc dot gnu.org
  2021-05-17  9:30 ` [Bug c++/100635] " redi at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2021-05-17  9:04 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 100635
           Summary: error: cannot bind non-const lvalue reference of type
                    'const volatile int&' to ...
           Product: gcc
           Version: 11.1.1
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

void foo(const volatile int&) { }
volatile int&& declvol();
using X = decltype(foo(declvol()));

This code is invalid and is correctly rejected, but the diagnostic is bad:

vol.C:3:31: error: cannot bind non-const lvalue reference of type 'const
volatile int&' to an rvalue of type 'volatile int'
    3 | using X = decltype(foo(declvol()));
      |                        ~~~~~~~^~
vol.C:1:10: note:   initializing argument 1 of 'void foo(const volatile int&)'
    1 | void foo(const volatile int&) { }
      |          ^~~~~~~~~~~~~~~~~~~
vol.C:3:31: error: cannot bind non-const lvalue reference of type 'const
volatile int&' to an rvalue of type 'volatile int'
    3 | using X = decltype(foo(declvol()));
      |                        ~~~~~~~^~
vol.C:1:10: note:   initializing argument 1 of 'void foo(const volatile int&)'
    1 | void foo(const volatile int&) { }
      |          ^~~~~~~~~~~~~~~~~~~


The error says "non-const lvalue reference" but then shows a const volatile
lvalue reference, so "non-const" is wrong.

The error also shouldn't be printed twice.


I'm not sure about the duplicate error (something tries to perform the
conversion twice and both times fail?) but the first problem could probably be
fixed by simply omitting the "non-const" for the case of a
reference-to-volatile:

vol.C:3:31: error: cannot bind lvalue reference of type 'const volatile int&'
to an rvalue of type 'volatile int'

This is accurate, because the relevant rule is:

  Otherwise, if the reference is an lvalue reference to a type that is not
  const-qualified or is volatile-qualified, the program is ill-formed.

i.e. binding an lvalue reference to an rvalue is ill-formed for non-const types
(as the current diagnostic covers) and for volatile types (as in the testcase
above). For the latter case, simple saying "cannot bind lvalue reference" would
be fine.

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

* [Bug c++/100635] error: cannot bind non-const lvalue reference of type 'const volatile int&' to ...
  2021-05-17  9:04 [Bug c++/100635] New: error: cannot bind non-const lvalue reference of type 'const volatile int&' to redi at gcc dot gnu.org
@ 2021-05-17  9:30 ` redi at gcc dot gnu.org
  2021-05-17 11:16 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2021-05-17  9:30 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2021-05-17
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Alternatively, remove "non-const" from the ref_type and add "volatile" to the
expr type:

vol.C:3:31: error: cannot bind lvalue reference of type 'const volatile
int&' to an rvalue of volatile type 'volatile int'

I'll test this:

--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -7900,6 +7900,10 @@ convert_like_internal (conversion *convs, tree expr,
tree fn, int argnum,
                              "type %qH to a value of type %qI",
                              totype, next->type);
                  }
+               else if (CP_TYPE_CONST_P (TREE_TYPE (ref_type)))
+                 error_at (loc, "cannot bind lvalue reference of type "
+                           "%qH to an rvalue of volatile type %qI", totype,
+                           extype);
                else
                  error_at (loc, "cannot bind non-const lvalue reference of "
                            "type %qH to an rvalue of type %qI", totype,
extype);

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

* [Bug c++/100635] error: cannot bind non-const lvalue reference of type 'const volatile int&' to ...
  2021-05-17  9:04 [Bug c++/100635] New: error: cannot bind non-const lvalue reference of type 'const volatile int&' to redi at gcc dot gnu.org
  2021-05-17  9:30 ` [Bug c++/100635] " redi at gcc dot gnu.org
@ 2021-05-17 11:16 ` redi at gcc dot gnu.org
  2021-05-17 20:04 ` cvs-commit at gcc dot gnu.org
  2021-05-17 20:38 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2021-05-17 11:16 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |redi at gcc dot gnu.org

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Patch posted for review:
https://gcc.gnu.org/pipermail/gcc-patches/2021-May/570472.html

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

* [Bug c++/100635] error: cannot bind non-const lvalue reference of type 'const volatile int&' to ...
  2021-05-17  9:04 [Bug c++/100635] New: error: cannot bind non-const lvalue reference of type 'const volatile int&' to redi at gcc dot gnu.org
  2021-05-17  9:30 ` [Bug c++/100635] " redi at gcc dot gnu.org
  2021-05-17 11:16 ` redi at gcc dot gnu.org
@ 2021-05-17 20:04 ` cvs-commit at gcc dot gnu.org
  2021-05-17 20:38 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-05-17 20:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:5d93261bc03c9c6891ccd8c77ab22b2a09971905

commit r12-849-g5d93261bc03c9c6891ccd8c77ab22b2a09971905
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon May 17 10:53:56 2021 +0100

    c++: Fix diagnostic for binding lvalue reference to volatile rvalue [PR
100635]

    The current diagnostic assumes the reference binding fails because the
    reference is non-const, but it can also fail if the rvalue is volatile.

    Use the current diagnostic for non-const cases, and a modified
    diagnostic otherwise.

    gcc/cp/ChangeLog:

            PR c++/100635
            * call.c (convert_like_internal): Print different diagnostic if
            the lvalue reference is const.

    gcc/testsuite/ChangeLog:

            * g++.dg/conversion/pr100635.C: New test.

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

* [Bug c++/100635] error: cannot bind non-const lvalue reference of type 'const volatile int&' to ...
  2021-05-17  9:04 [Bug c++/100635] New: error: cannot bind non-const lvalue reference of type 'const volatile int&' to redi at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2021-05-17 20:04 ` cvs-commit at gcc dot gnu.org
@ 2021-05-17 20:38 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2021-05-17 20:38 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED
   Target Milestone|---                         |12.0

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Fixed for GCC 12.

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

end of thread, other threads:[~2021-05-17 20:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-17  9:04 [Bug c++/100635] New: error: cannot bind non-const lvalue reference of type 'const volatile int&' to redi at gcc dot gnu.org
2021-05-17  9:30 ` [Bug c++/100635] " redi at gcc dot gnu.org
2021-05-17 11:16 ` redi at gcc dot gnu.org
2021-05-17 20:04 ` cvs-commit at gcc dot gnu.org
2021-05-17 20:38 ` redi at gcc dot gnu.org

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).