public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/92687] decltype of a structured binding to a tuple component is a reference type inside a template function
       [not found] <bug-92687-4@http.gcc.gnu.org/bugzilla/>
@ 2024-02-28 11:56 ` Christopher.Nerz at de dot bosch.com
  2024-02-28 12:15 ` jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 5+ messages in thread
From: Christopher.Nerz at de dot bosch.com @ 2024-02-28 11:56 UTC (permalink / raw)
  To: gcc-bugs

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

Christopher Nerz <Christopher.Nerz at de dot bosch.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |Christopher.Nerz at de dot bosch.c
                   |                            |om

--- Comment #2 from Christopher Nerz <Christopher.Nerz at de dot bosch.com> ---
Same error happens for all other gcc versions I checked, ranging from 8.3 to
13.2.
Note that the problem does not arise if you replace the tuple with a struct {
int a; int b;}.

Seems strongly related: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102116

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

* [Bug c++/92687] decltype of a structured binding to a tuple component is a reference type inside a template function
       [not found] <bug-92687-4@http.gcc.gnu.org/bugzilla/>
  2024-02-28 11:56 ` [Bug c++/92687] decltype of a structured binding to a tuple component is a reference type inside a template function Christopher.Nerz at de dot bosch.com
@ 2024-02-28 12:15 ` jakub at gcc dot gnu.org
  2024-02-28 13:44 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-02-28 12:15 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Testcase without using library:
namespace std {
  template<typename T> struct tuple_size;
  template<int, typename> struct tuple_element;
}

struct A {
  int i;
  template <int I> int& get() { return i; }
};

template<> struct std::tuple_size<A> { static const int value = 2; };
template<int I> struct std::tuple_element<I,A> { using type = int; };

template<typename T>
struct is_reference {
  static const bool value = false;
};

template<typename T>
struct is_reference<T&>
{
  static const bool value = true;
};

template<typename T>
struct is_reference<T&&>
{
  static const bool value = true;
};

template<int N>
void
foo ()
{
  auto [x, y] = A {};
  static_assert (!is_reference<decltype (x)>::value, "");
}

void
bar ()
{
  auto [x, y] = A {};
  static_assert (!is_reference<decltype (x)>::value, "");
}

template<typename T>
void
baz ()
{
  auto [x, y] = T {};
  static_assert (!is_reference<decltype (x)>::value, "");
}

void
qux ()
{
  foo<0> ();
  baz<A> ();
}

which shows it is only for the non-dependent structured binding in a template
case.

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

* [Bug c++/92687] decltype of a structured binding to a tuple component is a reference type inside a template function
       [not found] <bug-92687-4@http.gcc.gnu.org/bugzilla/>
  2024-02-28 11:56 ` [Bug c++/92687] decltype of a structured binding to a tuple component is a reference type inside a template function Christopher.Nerz at de dot bosch.com
  2024-02-28 12:15 ` jakub at gcc dot gnu.org
@ 2024-02-28 13:44 ` jakub at gcc dot gnu.org
  2024-02-28 14:31 ` jakub at gcc dot gnu.org
  2024-03-01 16:28 ` cvs-commit at gcc dot gnu.org
  4 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-02-28 13:44 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
finish_decltype_type does:
      /* decltype of a decomposition name drops references in the tuple case
         (unlike decltype of a normal variable) and keeps cv-qualifiers from
         the containing object in the other cases (unlike decltype of a member
         access expression).  */
      if (DECL_DECOMPOSITION_P (expr))
        {
          if (DECL_HAS_VALUE_EXPR_P (expr))
            /* Expr is an array or struct subobject proxy, handle
               bit-fields properly.  */
            return unlowered_expr_type (expr);
          else
            /* Expr is a reference variable for the tuple case.  */
            return lookup_decomp_type (expr);
        }
The problem is that if processing_template_decl (though, finish_decltype_type
has processing_template_decl temporarily cleared here) and expr is not
dependent (otherwise finish_decltype_type would defer handling it)
DECL_HAS_VALUE_EXPR_P is actually set on all the structured binding decls, not
just when it is array/struct/vector/complex etc. subobject proxy.

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

* [Bug c++/92687] decltype of a structured binding to a tuple component is a reference type inside a template function
       [not found] <bug-92687-4@http.gcc.gnu.org/bugzilla/>
                   ` (2 preceding siblings ...)
  2024-02-28 13:44 ` jakub at gcc dot gnu.org
@ 2024-02-28 14:31 ` jakub at gcc dot gnu.org
  2024-03-01 16:28 ` cvs-commit at gcc dot gnu.org
  4 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-02-28 14:31 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

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

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Created attachment 57561
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=57561&action=edit
gcc14-pr92687.patch

So var only very lightly tested fix.

Another possibility would be instead of the cp/*.cc changes in the patch change
lookup_decomp_type such that for NULL get it would return NULL_TREE, and either
always or just if ptds.saved try to call lookup_decomp_type and return its
result if it returned true, regardless of whether DECL_HAS_VALUE_EXPR_P or not.
Guess that would be cleaner, but slower.

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

* [Bug c++/92687] decltype of a structured binding to a tuple component is a reference type inside a template function
       [not found] <bug-92687-4@http.gcc.gnu.org/bugzilla/>
                   ` (3 preceding siblings ...)
  2024-02-28 14:31 ` jakub at gcc dot gnu.org
@ 2024-03-01 16:28 ` cvs-commit at gcc dot gnu.org
  4 siblings, 0 replies; 5+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-03-01 16:28 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:867cbadb912ab75d0eaf919a3f992595e508482b

commit r14-9258-g867cbadb912ab75d0eaf919a3f992595e508482b
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Mar 1 16:59:08 2024 +0100

    c++: Fix up decltype of non-dependent structured binding decl in template
[PR92687]

    finish_decltype_type uses DECL_HAS_VALUE_EXPR_P (expr) check for
    DECL_DECOMPOSITION_P (expr) to determine if it is
    array/struct/vector/complex etc. subobject proxy case vs. structured
    binding using std::tuple_{size,element}.
    For non-templates or when templates are already instantiated, that works
    correctly, finalized DECL_DECOMPOSITION_P non-base vars indeed have
    DECL_VALUE_EXPR in the former case and don't have it in the latter.
    It works fine for dependent structured bindings as well, cp_finish_decomp
in
    that case creates DECLTYPE_TYPE tree and defers the handling until
    instantiation.
    As the testcase shows, this doesn't work for the non-dependent structured
    binding case in templates, because DECL_HAS_VALUE_EXPR_P is set in that
case
    always; cp_finish_decomp ends with:
      if (processing_template_decl)
        {
          for (unsigned int i = 0; i < count; i++)
            if (!DECL_HAS_VALUE_EXPR_P (v[i]))
              {
                tree a = build_nt (ARRAY_REF, decl, size_int (i),
                                   NULL_TREE, NULL_TREE);
                SET_DECL_VALUE_EXPR (v[i], a);
                DECL_HAS_VALUE_EXPR_P (v[i]) = 1;
              }
        }
    and those artificial ARRAY_REFs are used in various places during
    instantiation to find out what base the DECL_DECOMPOSITION_P VAR_DECLs
    have and their positions.

    The following patch fixes that by changing lookup_decomp_type, such that
    it doesn't ICE when called on a DECL_DECOMPOSITION_P var which isn't in a
    hash table, but returns NULL_TREE in that case, and for
processing_template_decl
    asserts DECL_HAS_VALUE_EXPR_P is non-NULL and just calls
lookup_decomp_type.
    If it returns non-NULL, it is a structured binding using tuple and its
result
    is returned, otherwise it falls through to returning unlowered_expr_type
(expr)
    because it is an array, structure etc. subobject proxy.
    For !processing_template_decl it keeps doing what it did before,
    DECL_HAS_VALUE_EXPR_P meaning it is an array/structure etc. subobject
proxy,
    otherwise the tuple case.

    2024-03-01  Jakub Jelinek  <jakub@redhat.com>

            PR c++/92687
            * decl.cc (lookup_decomp_type): Return NULL_TREE if
decomp_type_table
            doesn't have entry for V.
            * semantics.cc (finish_decltype_type): If ptds.saved, assert
            DECL_HAS_VALUE_EXPR_P is true and decide on tuple vs. non-tuple
based
            on if lookup_decomp_type is NULL or not.

            * g++.dg/cpp1z/decomp59.C: New test.

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

end of thread, other threads:[~2024-03-01 16:28 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-92687-4@http.gcc.gnu.org/bugzilla/>
2024-02-28 11:56 ` [Bug c++/92687] decltype of a structured binding to a tuple component is a reference type inside a template function Christopher.Nerz at de dot bosch.com
2024-02-28 12:15 ` jakub at gcc dot gnu.org
2024-02-28 13:44 ` jakub at gcc dot gnu.org
2024-02-28 14:31 ` jakub at gcc dot gnu.org
2024-03-01 16:28 ` cvs-commit 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).