Skip to content

C++ back end drops required parentheses in coroutine expressions #546

Closed

Description

@nv-cmeerw

Issue Summary

With PARENS_IN_IL=0 and ALL_TEMPLATE_INFO_IN_IL=1, the C++-generating back end can omit parentheses needed to preserve the meaning of co_await and co_yield expressions. This affects both a coroutine expression used as the operand of another operator and operands of co_await or co_yield whose grammar requires grouping. Configurations with PARENS_IN_IL=1 preserve the source parentheses and are unaffected by these cases.

A complete generator fix must cover both nondependent coroutine expressions and a dependent co_yield path that is emitted with the wrong precedence. The resulting C++ can either be rejected or compile with different semantics from the original source.

Reproducing Source Code

namespace std {
  template<typename = void> struct coroutine_handle;
  template<> struct coroutine_handle<> { void *address(); };
  template<typename> struct coroutine_handle {
    static coroutine_handle from_address(void *);
    operator coroutine_handle<>() noexcept;
  };
  template<typename R, typename...> struct coroutine_traits {
    using promise_type = typename R::promise_type;
  };
}

struct suspend {
  bool await_ready() noexcept;
  void await_suspend(std::coroutine_handle<>) noexcept;
  void await_resume() noexcept;
};

template<typename R>
struct awaiter {
  bool await_ready() noexcept;
  void await_suspend(std::coroutine_handle<>) noexcept;
  R await_resume() noexcept;
};

struct S {
  int m;
  void operator=(int);
};

struct task;
struct promise {
  suspend initial_suspend() noexcept;
  suspend final_suspend() noexcept;
  void return_void();
  void unhandled_exception();
  task get_return_object();
  awaiter<S> yield_value(S);
};
struct task { using promise_type = promise; };

task outer() {
  int i = (co_await awaiter<S>{}).m;
  (co_yield S{}) = 1;
}

bool c;
awaiter<S> x, y;
task await_operand() {
  co_await (c ? x : y);
}

int first();
S second();
task yield_operand() {
  co_yield (first(), second());
}

template<typename T>
task dependent() {
  (co_yield T{}) = 1;
}
template task dependent<S>();

Command-line Options

--c++20

Type of Issue

[Front End] This code SHOULD compile with these options.

Additional Details

Only configurations with PARENS_IN_IL=0 and ALL_TEMPLATE_INFO_IN_IL=1 are affected; configurations retaining parentheses in the IL (PARENS_IN_IL=1) do not reproduce these cases.

Reproducing (Standard) Configuration(s)

linux-gcc-debug

Reproducing (Non-standard) Configuration

No response

I acknowledge that:

  • I have checked open issues and existing discussions before filing this report.
  • The reproducing source code that I have provided does not depend on headers that I have not included with my submission.
  • I have reduced the reproducing source code to the best of my ability.
  • I have selected at least one standard configuration or uploaded a non-standard configuration dump.
  • I understand code shared via this form is NOT confidential.
  • I have permission to share the reproducing source code and grant permission for the project to use the source code (or derivatives of the source code) to create tests.
  • I agree that any tests created using the reproducing source code (or deriatives of it) will be licensed under project license unless I included a LICENSE.txt file in my .zip or .tar.gz upload. If I included a LICENSE.txt file, any tests created using the reproducing source code will be governed by the terms of the license I provided instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions