public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/115378] New: [ice-on-valid] code using friend injection is crashing gcc since 14
@ 2024-06-06 22:03 eric.niebler at gmail dot com
  2024-06-06 22:13 ` [Bug c++/115378] " pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: eric.niebler at gmail dot com @ 2024-06-06 22:03 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 115378
           Summary: [ice-on-valid] code using friend injection is crashing
                    gcc since 14
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: eric.niebler at gmail dot com
  Target Milestone: ---

the following valid code crashes gcc-14 and gcc-trunk. clang accepts it, as
does gcc-13. see https://godbolt.org/z/s9frvq3Pv


#include <algorithm>
#include <cassert>
#include <concepts>
#include <functional>
#include <ranges>
#include <tuple>
#include <type_traits>

#if defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic ignored "-Wunknown-warning-option"
#pragma GCC diagnostic ignored "-Wnon-template-friend"
#endif

namespace std::execution {
  template <class Env, class Query, class... Args>
  concept __has_query =
    requires (const Env& __env, Args&&... __args) {
      __env.query(Query(), std::forward<Args>(__args)...);
    };

  // specialization for key/value pairs
  template <class Query, class Value>
  struct prop {
    [[no_unique_address]] Query __query;
    [[no_unique_address]] Value __value;

    [[nodiscard]] constexpr const Value & query(Query) const noexcept {
      return __value;
    }
  };

  template <class Query, class Value>
  prop(Query, Value) -> prop<Query, unwrap_reference_t<Value>>;

  template <class Env>
  struct __ref;

  template <class Env>
  struct __ref<Env&> {
    Env& __env;

    constexpr __ref(reference_wrapper<Env> __r) noexcept : __env(__r) {}

    template <class Query, class... Args>
      requires __has_query<Env, Query, Args...>
    constexpr decltype(auto) query(Query __q, Args&&... args) const
      noexcept(noexcept(__env.query(__q, std::forward<Args>(args)...))) {
      return __env.query(__q, std::forward<Args>(args)...);
    }
  };

  template<class Slot, size_t N>
  struct __reader {
    friend auto __counted_flag(__reader<Slot, N>);
  };

  template<class Slot, size_t N>
  struct __writer {
    friend auto __counted_flag(__reader<Slot, N>) {}
    static constexpr size_t __n = N;
  };

  template<class Slot, auto Tag, size_t NextVal = 0>
  consteval auto __counter_impl() {
    constexpr bool __counted_past_value = requires(__reader<Slot, NextVal> __r)
{
      __counted_flag(__r);
    };

    if constexpr (__counted_past_value) {
      return __counter_impl<Slot, Tag, NextVal + 1>();
    } else {
      return __writer<Slot, NextVal>().__n;
    }
  }

  template<class Slot, auto Tag = []{}, auto Val = __counter_impl<Slot, Tag>()>
  constexpr auto __counter = Val;

  template<class...> struct __list;

  template <class, size_t>
  struct __ignore {
    constexpr __ignore(auto&&) {}
    auto query(auto) const = delete;
  };

  template <class Env>
  using __wrap = conditional_t<is_reference_v<Env>, __ref<Env>, Env>;

  template <class Child, size_t Counter>
  using _as_base_ = conditional_t<Counter == 0, __wrap<Child>, __ignore<Child,
Counter>>;

  template <class Parent, class Child, size_t Counter =
__counter<__list<Parent, Child>>>
  using _as_base = _as_base_<Child, Counter>;

  // utility for joining multiple environments
  template <class... Envs>
  struct env : _as_base<env<Envs...>, Envs>... {
    template <class Query, class... Args>
    constexpr decltype(auto) __get_1st() const noexcept {
      constexpr bool __flags[] = {__has_query<Envs, Query, Args...>...};
      constexpr size_t __idx = ranges::find(__flags, true) - __flags;
      auto __tup = tie(static_cast<const __wrap<Envs>&>(*this)...);
      return get<__idx>(__tup);
    }

    template <class Query, class... Args>
      requires (__has_query<Envs, Query, Args...> ||...)
    constexpr decltype(auto) query(Query __q, Args&&... args) const
      noexcept(noexcept(__get_1st<Query, Args...>().query(__q,
std::forward<Args>(args)...))) {
      return __get_1st<Query, Args...>().query(__q,
std::forward<Args>(args)...);
    }
  };

  template <class... Envs>
  env(Envs...) -> env<unwrap_reference_t<Envs>...>;
} // std::execution

// Test code:
template <size_t>
struct get_value_t {
  auto operator()(const auto& env) const noexcept -> decltype(env.query(*this))
{
    static_assert(noexcept(env.query(*this)));
    return env.query(*this);
  }
};

template <size_t I>
inline constexpr get_value_t<I> get_value{};

int main() {
  using namespace std::execution;

  // can create an environment out of a query and a value
  auto env1 = prop(get_value<0>, 42);
  auto val = get_value<0>(env1);
  assert(val == 42);

  // can store a value by reference:
  int i = 42;
  auto env2 = prop(get_value<0>, std::ref(i));
  int& j = get_value<0>(env2);
  ++j;
  assert(i == 43);

  // Can create an env from several envs with duplicates.
  // Queries are resolved by asking the nested envs first to last.
  auto env3 = env(prop(get_value<0>, 42),
                  prop(get_value<1>, 43),
                  prop(get_value<1>, 44),
                  prop(get_value<0>, 45),
                  prop(get_value<0>, 46));
  assert(get_value<0>(env3) == 42);
  assert(get_value<1>(env3) == 43);

  // nested envs can be held by reference also:
  auto env4 = env(prop(get_value<1>, 42), std::cref(env2));
  assert(get_value<0>(env4) == 43);
  assert(get_value<1>(env4) == 42);
}

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

* [Bug c++/115378] [ice-on-valid] code using friend injection is crashing gcc since 14
  2024-06-06 22:03 [Bug c++/115378] New: [ice-on-valid] code using friend injection is crashing gcc since 14 eric.niebler at gmail dot com
@ 2024-06-06 22:13 ` pinskia at gcc dot gnu.org
  2024-06-06 22:14 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-06 22:13 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note it has nothing to do with `friend injection` but rather variadic
templates.

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

* [Bug c++/115378] [ice-on-valid] code using friend injection is crashing gcc since 14
  2024-06-06 22:03 [Bug c++/115378] New: [ice-on-valid] code using friend injection is crashing gcc since 14 eric.niebler at gmail dot com
  2024-06-06 22:13 ` [Bug c++/115378] " pinskia at gcc dot gnu.org
@ 2024-06-06 22:14 ` pinskia at gcc dot gnu.org
  2024-06-06 22:34 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-06 22:14 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Created attachment 58375
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58375&action=edit
Slightly reduced

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

* [Bug c++/115378] [ice-on-valid] code using friend injection is crashing gcc since 14
  2024-06-06 22:03 [Bug c++/115378] New: [ice-on-valid] code using friend injection is crashing gcc since 14 eric.niebler at gmail dot com
  2024-06-06 22:13 ` [Bug c++/115378] " pinskia at gcc dot gnu.org
  2024-06-06 22:14 ` pinskia at gcc dot gnu.org
@ 2024-06-06 22:34 ` pinskia at gcc dot gnu.org
  2024-06-06 22:40 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-06 22:34 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |c++-lambda,
                   |                            |ice-on-valid-code

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
It is looking related to use of the lambda function as a default template
argument even.

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

* [Bug c++/115378] [ice-on-valid] code using friend injection is crashing gcc since 14
  2024-06-06 22:03 [Bug c++/115378] New: [ice-on-valid] code using friend injection is crashing gcc since 14 eric.niebler at gmail dot com
                   ` (2 preceding siblings ...)
  2024-06-06 22:34 ` pinskia at gcc dot gnu.org
@ 2024-06-06 22:40 ` pinskia at gcc dot gnu.org
  2024-06-06 22:42 ` [Bug c++/115378] [14/15 Regression] ICE with lambda function as a default template argument with variadic templates in some cases pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-06 22:40 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
  Attachment #58375|0                           |1
        is obsolete|                            |

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Created attachment 58376
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58376&action=edit
Reduced testcase

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

* [Bug c++/115378] [14/15 Regression] ICE with lambda function as a default template argument with variadic templates in some cases
  2024-06-06 22:03 [Bug c++/115378] New: [ice-on-valid] code using friend injection is crashing gcc since 14 eric.niebler at gmail dot com
                   ` (3 preceding siblings ...)
  2024-06-06 22:40 ` pinskia at gcc dot gnu.org
@ 2024-06-06 22:42 ` pinskia at gcc dot gnu.org
  2024-06-07 13:49 ` ppalka at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-06 22:42 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2024-06-06
            Summary|[ice-on-valid] code using   |[14/15 Regression] ICE with
                   |friend injection is         |lambda function as a
                   |crashing gcc since 14       |default template argument
                   |                            |with variadic templates in
                   |                            |some cases
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
   Target Milestone|---                         |14.2

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note I don't think it was exactly working in GCC 13 either even though it was
not ICEing.
With my reduced testcase we get the following (wrong) warning for GCC 13:
<source>:4:33: warning: no return statement in function returning non-void
[-Wreturn-type]
    4 | template<class Slot, auto Tag = []{}>
      |                                 ^~~~

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

* [Bug c++/115378] [14/15 Regression] ICE with lambda function as a default template argument with variadic templates in some cases
  2024-06-06 22:03 [Bug c++/115378] New: [ice-on-valid] code using friend injection is crashing gcc since 14 eric.niebler at gmail dot com
                   ` (4 preceding siblings ...)
  2024-06-06 22:42 ` [Bug c++/115378] [14/15 Regression] ICE with lambda function as a default template argument with variadic templates in some cases pinskia at gcc dot gnu.org
@ 2024-06-07 13:49 ` ppalka at gcc dot gnu.org
  2024-06-07 16:14 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: ppalka at gcc dot gnu.org @ 2024-06-07 13:49 UTC (permalink / raw)
  To: gcc-bugs

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

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Keywords|needs-bisection             |
                 CC|                            |ppalka at gcc dot gnu.org
           Assignee|unassigned at gcc dot gnu.org      |ppalka at gcc dot gnu.org

--- Comment #6 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Started with r14-9943.

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

* [Bug c++/115378] [14/15 Regression] ICE with lambda function as a default template argument with variadic templates in some cases
  2024-06-06 22:03 [Bug c++/115378] New: [ice-on-valid] code using friend injection is crashing gcc since 14 eric.niebler at gmail dot com
                   ` (5 preceding siblings ...)
  2024-06-07 13:49 ` ppalka at gcc dot gnu.org
@ 2024-06-07 16:14 ` cvs-commit at gcc dot gnu.org
  2024-06-10 14:16 ` cvs-commit at gcc dot gnu.org
  2024-06-10 14:16 ` ppalka at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-06-07 16:14 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:

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

commit r15-1103-g5c761395402a730535983a5e49ef1775561ebc61
Author: Patrick Palka <ppalka@redhat.com>
Date:   Fri Jun 7 12:12:30 2024 -0400

    c++: lambda in pack expansion [PR115378]

    Here find_parameter_packs_r is incorrectly treating the 'auto' return
    type of a lambda as a parameter pack due to Concepts-TS specific logic
    added in r6-4517, leading to confusion later when expanding the pattern.

    Since we intend on removing Concepts TS support soon anyway, this patch
    fixes this by restricting the problematic logic with flag_concepts_ts.
    Doing so revealed that add_capture was relying on this logic to set
    TEMPLATE_TYPE_PARAMETER_PACK for the 'auto' type of an pack expansion
    init-capture, which we now need to do explicitly.

            PR c++/115378

    gcc/cp/ChangeLog:

            * lambda.cc (lambda_capture_field_type): Set
            TEMPLATE_TYPE_PARAMETER_PACK on the auto type of an init-capture
            pack expansion.
            * pt.cc (find_parameter_packs_r) <case TEMPLATE_TYPE_PARM>:
            Restrict TEMPLATE_TYPE_PARAMETER_PACK promotion with
            flag_concepts_ts.

    gcc/testsuite/ChangeLog:

            * g++.dg/cpp1y/decltype-auto-103497.C: Adjust expected diagnostic.
            * g++.dg/template/pr95672.C: Likewise.
            * g++.dg/cpp2a/lambda-targ5.C: New test.

    Reviewed-by: Jason Merrill <jason@redhat.com>

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

* [Bug c++/115378] [14/15 Regression] ICE with lambda function as a default template argument with variadic templates in some cases
  2024-06-06 22:03 [Bug c++/115378] New: [ice-on-valid] code using friend injection is crashing gcc since 14 eric.niebler at gmail dot com
                   ` (6 preceding siblings ...)
  2024-06-07 16:14 ` cvs-commit at gcc dot gnu.org
@ 2024-06-10 14:16 ` cvs-commit at gcc dot gnu.org
  2024-06-10 14:16 ` ppalka at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-06-10 14:16 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-14 branch has been updated by Patrick Palka
<ppalka@gcc.gnu.org>:

https://gcc.gnu.org/g:ff8105b4910f7dbee326cb36b01c16ac9bf10c4b

commit r14-10301-gff8105b4910f7dbee326cb36b01c16ac9bf10c4b
Author: Patrick Palka <ppalka@redhat.com>
Date:   Fri Jun 7 12:12:30 2024 -0400

    c++: lambda in pack expansion [PR115378]

    Here find_parameter_packs_r is incorrectly treating the 'auto' return
    type of a lambda as a parameter pack due to Concepts-TS specific logic
    added in r6-4517, leading to confusion later when expanding the pattern.

    Since we intend on removing Concepts TS support soon anyway, this patch
    fixes this by restricting the problematic logic with flag_concepts_ts.
    Doing so revealed that add_capture was relying on this logic to set
    TEMPLATE_TYPE_PARAMETER_PACK for the 'auto' type of an pack expansion
    init-capture, which we now need to do explicitly.

            PR c++/115378

    gcc/cp/ChangeLog:

            * lambda.cc (lambda_capture_field_type): Set
            TEMPLATE_TYPE_PARAMETER_PACK on the auto type of an init-capture
            pack expansion.
            * pt.cc (find_parameter_packs_r) <case TEMPLATE_TYPE_PARM>:
            Restrict TEMPLATE_TYPE_PARAMETER_PACK promotion with
            flag_concepts_ts.

    gcc/testsuite/ChangeLog:

            * g++.dg/cpp1y/decltype-auto-103497.C: Adjust expected diagnostic.
            * g++.dg/template/pr95672.C: Likewise.
            * g++.dg/cpp2a/lambda-targ5.C: New test.

    Reviewed-by: Jason Merrill <jason@redhat.com>
    (cherry picked from commit 5c761395402a730535983a5e49ef1775561ebc61)

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

* [Bug c++/115378] [14/15 Regression] ICE with lambda function as a default template argument with variadic templates in some cases
  2024-06-06 22:03 [Bug c++/115378] New: [ice-on-valid] code using friend injection is crashing gcc since 14 eric.niebler at gmail dot com
                   ` (7 preceding siblings ...)
  2024-06-10 14:16 ` cvs-commit at gcc dot gnu.org
@ 2024-06-10 14:16 ` ppalka at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: ppalka at gcc dot gnu.org @ 2024-06-10 14:16 UTC (permalink / raw)
  To: gcc-bugs

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

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #9 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Fixed for GCC 14.2

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

end of thread, other threads:[~2024-06-10 14:16 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-06 22:03 [Bug c++/115378] New: [ice-on-valid] code using friend injection is crashing gcc since 14 eric.niebler at gmail dot com
2024-06-06 22:13 ` [Bug c++/115378] " pinskia at gcc dot gnu.org
2024-06-06 22:14 ` pinskia at gcc dot gnu.org
2024-06-06 22:34 ` pinskia at gcc dot gnu.org
2024-06-06 22:40 ` pinskia at gcc dot gnu.org
2024-06-06 22:42 ` [Bug c++/115378] [14/15 Regression] ICE with lambda function as a default template argument with variadic templates in some cases pinskia at gcc dot gnu.org
2024-06-07 13:49 ` ppalka at gcc dot gnu.org
2024-06-07 16:14 ` cvs-commit at gcc dot gnu.org
2024-06-10 14:16 ` cvs-commit at gcc dot gnu.org
2024-06-10 14:16 ` ppalka 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).