From f0d886cea4f7afb474da771df723b4ffc9d6c6fc Mon Sep 17 00:00:00 2001 From: Mohit Mishra Date: Fri, 20 Nov 2026 09:08:30 +0530 Subject: [PATCH 1/3] Give co_await the value category of await_resume(). [GH #447] --- src/expr.c | 19 +++++- .../gh-447-co-await-value-category.txt | 15 +++++ tests/expectations/edg_x86_64/edg.log | 2 +- .../tests/changes/.GH447.rto/default.1.1.txt | 1 + tests/tests/changes/GH447.sft.cpp | 62 +++++++++++++++++++ .../edg/gnats/.21769.rto/default.1.1.txt | 12 ++-- 6 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 src/pending_changes/gh-447-co-await-value-category.txt create mode 100644 tests/tests/changes/.GH447.rto/default.1.1.txt create mode 100644 tests/tests/changes/GH447.sft.cpp diff --git a/src/expr.c b/src/expr.c index 358c42d016..3093affaa2 100644 --- a/src/expr.c +++ b/src/expr.c @@ -49254,7 +49254,24 @@ suspend point. } /* if */ node->variant.await_info.ready_resume_suspend->next->next = make_node_from_operand(&suspend_call); - make_expression_operand(node, operand); + if (is_a_glvalue(&resume_call)) { + /* await_resume returns a reference, so the co_await expression is a + glvalue too (an lvalue or xvalue, like the await_resume call). As + for a call, give the node the reference type and put a reference + indirection on top of it. */ + an_expr_node_ptr call = strip_ref_indirect( + node->variant.await_info.ready_resume_suspend->next, + /*parens_also=*/FALSE); + if (is_any_reference_type(call->type)) { + node->type = call->type; + node = add_ref_indirection_to_node(node); + if (is_an_xvalue(&resume_call)) { + node->is_lvalue = FALSE; + node->is_xvalue = TRUE; + } /* if */ + } /* if */ + } /* if */ + make_lvalue_or_rvalue_expression_operand(node, operand); done:; } /* add_await_to_operand */ diff --git a/src/pending_changes/gh-447-co-await-value-category.txt b/src/pending_changes/gh-447-co-await-value-category.txt new file mode 100644 index 0000000000..18def731e5 --- /dev/null +++ b/src/pending_changes/gh-447-co-await-value-category.txt @@ -0,0 +1,15 @@ +[GH #447] +Value category of co_await and co_yield expressions + +A co_await or co_yield expression was always treated as a prvalue, even +when the await_resume call returns a reference. It now has the value +category of the await_resume call, so code like this is accepted: + + struct bool_ref_awaiter { + bool await_ready() noexcept; + void await_suspend(std::coroutine_handle<>) noexcept; + bool &await_resume() noexcept; + }; + task f() { + (co_await bool_ref_awaiter{}) = false; + } diff --git a/tests/expectations/edg_x86_64/edg.log b/tests/expectations/edg_x86_64/edg.log index 7cf72b28e0..6d47a8fdb0 100644 --- a/tests/expectations/edg_x86_64/edg.log +++ b/tests/expectations/edg_x86_64/edg.log @@ -50,7 +50,7 @@ gnats/21112.sft.cpp:fp:-DTEST_NUMBER=1:FAIL: gnats/21435.sft.cpp:cp:-DTEST_NUMBER=1:FAIL: gnats/21436.sft.cpp:fn:-DTEST_NUMBER=1:FAIL: gnats/21543.sft.cpp:fn:-DTEST_NUMBER=1:FAIL: -gnats/21769.sft.cpp:cp:-DTEST_NUMBER=1:FAIL: +gnats/21769.sft.cpp:cp:-DTEST_NUMBER=1:BADC: gnats/21821.sft.cpp:fn:-DTEST_NUMBER=1:FAIL: gnats/21822.sft.cpp:fn:-DTEST_NUMBER=1:FAIL: gnats/21824.sft.cpp:fn:-DTEST_NUMBER=1:FAIL: diff --git a/tests/tests/changes/.GH447.rto/default.1.1.txt b/tests/tests/changes/.GH447.rto/default.1.1.txt new file mode 100644 index 0000000000..4163d8bb9a --- /dev/null +++ b/tests/tests/changes/.GH447.rto/default.1.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=1 --c++23 Test_name.c diff --git a/tests/tests/changes/GH447.sft.cpp b/tests/tests/changes/GH447.sft.cpp new file mode 100644 index 0000000000..d7494c7b4b --- /dev/null +++ b/tests/tests/changes/GH447.sft.cpp @@ -0,0 +1,62 @@ +//type:fp +//options:--c++23 +//remark:[GH #447] co_await has the value category of await_resume() +// +// A co_await or co_yield expression was always treated as a prvalue, so +// assigning to the result of an await_resume() that returns an lvalue +// reference was rejected. + +namespace std { + template struct coroutine_handle; + template <> struct coroutine_handle<> { void *address(); }; + template struct coroutine_handle { + static coroutine_handle from_address(void *); + operator coroutine_handle<>() noexcept; + }; + template struct coroutine_traits { + using promise_type = typename R::promise_type; + }; + template struct is_same { static constexpr bool value = false; }; + template struct is_same { static constexpr bool value = true; }; +} // namespace std + +struct suspend { + bool await_ready() noexcept; + void await_suspend(std::coroutine_handle<>) noexcept; + void await_resume() noexcept; +}; + +template struct awaiter { + bool await_ready() noexcept; + void await_suspend(std::coroutine_handle<>) noexcept; + R await_resume() noexcept; +}; + +struct task; + +struct promise { + suspend initial_suspend() noexcept; + suspend final_suspend() noexcept; + void return_void(); + void unhandled_exception(); + task get_return_object(); + awaiter yield_value(int); +}; + +struct task { + using promise_type = promise; +}; + +task f() { + (co_await awaiter{}) = false; + bool *p = &co_await awaiter{}; + decltype(auto) a = co_await awaiter{}; + decltype(auto) b = co_await awaiter{}; + decltype(auto) c = co_await awaiter{}; + static_assert(std::is_same::value); + static_assert(std::is_same::value); + static_assert(std::is_same::value); + int &r = co_yield 1; + decltype(auto) d = co_yield 1; + static_assert(std::is_same::value); +} diff --git a/tests/tests/edg/gnats/.21769.rto/default.1.1.txt b/tests/tests/edg/gnats/.21769.rto/default.1.1.txt index 121285ace9..4484bf0595 100644 --- a/tests/tests/edg/gnats/.21769.rto/default.1.1.txt +++ b/tests/tests/edg/gnats/.21769.rto/default.1.1.txt @@ -1,6 +1,8 @@ cpfe -DTEST_NUMBER=1 --set_flag coroutines --no_ms_perm --microsoft_v 1923 Test_name.c -"Test_name.c", line 51: error: initial value of reference to non-const must be an lvalue - Immovable &r1 = co_await Aw{}; - ^ - -1 error detected in the compilation of "Test_name.c". +eccp: diagnostics generated from compilation of Test_name.int.c: +Test_name.c: In function '_Z1fv': +Test_name.c:51:14: error: 'co_await__T9' undeclared (first use in this function) +Test_name.c:51:14: note: each undeclared identifier is reported only once for each function it appears in +Test_name.c:51:172: error: '__T18' undeclared (first use in this function); did you mean '__T17'? +eccp: end of diagnostics from compilation of Test_name.int.c +eccp: gcc compilation of Test_name.int.c returned an exit status of 1 From fe59fc9d19e93d399a6c9f99a1bdfeb83847378c Mon Sep 17 00:00:00 2001 From: Mohit Mishra Date: Sat, 21 Nov 2026 20:03:26 +0530 Subject: [PATCH 2/3] Do not clone a folded co_await operand for await_suspend. [GH #447] --- src/expr.c | 65 ++++++++++++++++--- .../gh-447-co-await-value-category.txt | 5 ++ tests/tests/changes/GH447.sft.cpp | 20 +++++- 3 files changed, 81 insertions(+), 9 deletions(-) diff --git a/src/expr.c b/src/expr.c index 3093affaa2..90911dad29 100644 --- a/src/expr.c +++ b/src/expr.c @@ -31,6 +31,7 @@ expr.c -- Expression scanning routines. #include "interpret.h" #include "layout.h" #include "ifc_modules.h" +#include "il_walk.h" #if MICROSOFT_EXTENSIONS_ALLOWED /* The Microsoft-specific predefined identifier __FUNCDNAME__ refers to the mangled name of the current function. Hence, we may need access to the @@ -49053,6 +49054,55 @@ variable. #endif /* MICROSOFT_EXTENSIONS_ALLOWED */ +/* The expression node add_await_to_operand is looking for; see + call_contains_await_operand. */ +static an_expr_node_ptr await_operand_sought; + +static void find_await_operand(an_expr_node_ptr expr, + an_expr_or_stmt_traversal_block_ptr tblock) +/* +Called from traverse_expr: stop with a TRUE result if expr is the node being +looked for. +*/ +{ + if (expr == await_operand_sought) { + tblock->result = TRUE; + tblock->terminate = TRUE; + } /* if */ +} /* find_await_operand */ + + +static a_boolean call_contains_await_operand(an_operand *call, + an_operand *await_operand) +/* +Return TRUE if the result of one of the calls generated for a co_await (call) +still contains the expression of the awaiter operand (await_operand), i.e., +if the call was not folded away. A call folded to a constant does not, and +neither does a folded call returning a reference to a variable, whose result +is just an lvalue for that variable. +*/ +{ + a_boolean result; + + if (is_constant_operand(call)) { + result = FALSE; + } else if (!is_expression_operand(call) || + !is_expression_operand(await_operand)) { + /* Keep the earlier behavior: assume the call uses the operand. */ + result = TRUE; + } else { + an_expr_or_stmt_traversal_block tblock; + clear_expr_or_stmt_traversal_block(&tblock); + tblock.process_expr = find_await_operand; + await_operand_sought = await_operand->variant.expression; + traverse_expr(call->variant.expression, &tblock); + await_operand_sought = NULL; + result = tblock.result; + } /* if */ + return result; +} /* call_contains_await_operand */ + + void add_await_to_operand(an_operand_ptr operand, a_source_position *pos, a_token_sequence_number tok_seq_number, @@ -49192,31 +49242,30 @@ suspend point. (a_template_arg_ptr)NULL, (an_arg_list_elem_ptr)NULL, &orig_operand, &ready_call); - if (!is_constant_operand(&ready_call)) { + if (call_contains_await_operand(&ready_call, &orig_operand)) { /* We can't use the original operand because it's been incorporated into the await_ready call, so make a clone for use in the await_resume call. */ clone_operand(&orig_operand, &resume_operand, /*vars_can_change=*/TRUE, &temp_init_used, /*treat_as_potential_prvalue=*/TRUE); } else { - /* The await_ready call folded to a constant, so the original operand - isn't yet in the IL; just use it directly for the await_resume - call. */ + /* The await_ready call was folded, so the original operand isn't yet + in the IL; just use it directly for the await_resume call. */ copy_operand(&orig_operand, &resume_operand); } /* if */ call_named_member_function(&resume_operand, "await_resume", (a_template_arg_ptr)NULL, (an_arg_list_elem_ptr)NULL, &orig_operand, &resume_call); - if (!is_constant_operand(&ready_call) || - !is_constant_operand(&resume_call)) { + if (call_contains_await_operand(&ready_call, &orig_operand) || + call_contains_await_operand(&resume_call, &orig_operand)) { /* The original operand has been incorporated into one of the preceding calls, so clone it for use in the await_suspend call. */ clone_operand(&orig_operand, &suspend_operand, /*vars_can_change=*/TRUE, &temp_init_used, /*treat_as_potential_prvalue=*/TRUE); } else { - /* Both preceding calls folded to constants, so the original operand - is usable in the await_suspend call. */ + /* Both preceding calls were folded, so the original operand is + usable in the await_suspend call. */ copy_operand(&orig_operand, &suspend_operand); } /* if */ /* await_suspend takes an argument that is the handle of the coroutine. */ diff --git a/src/pending_changes/gh-447-co-await-value-category.txt b/src/pending_changes/gh-447-co-await-value-category.txt index 18def731e5..e9b86fb8f3 100644 --- a/src/pending_changes/gh-447-co-await-value-category.txt +++ b/src/pending_changes/gh-447-co-await-value-category.txt @@ -13,3 +13,8 @@ category of the await_resume call, so code like this is accepted: task f() { (co_await bool_ref_awaiter{}) = false; } + +Also, an internal error in lowering is fixed for a co_await whose +await_ready and await_resume are both constexpr when await_resume returns a +reference (both calls are then folded, and the awaiter was wrongly cloned +for the await_suspend call). diff --git a/tests/tests/changes/GH447.sft.cpp b/tests/tests/changes/GH447.sft.cpp index d7494c7b4b..9899174059 100644 --- a/tests/tests/changes/GH447.sft.cpp +++ b/tests/tests/changes/GH447.sft.cpp @@ -4,7 +4,8 @@ // // A co_await or co_yield expression was always treated as a prvalue, so // assigning to the result of an await_resume() that returns an lvalue -// reference was rejected. +// reference was rejected. Also covers awaiters whose await_ready and +// await_resume calls are folded. namespace std { template struct coroutine_handle; @@ -60,3 +61,20 @@ task f() { decltype(auto) d = co_yield 1; static_assert(std::is_same::value); } + +// When await_ready and await_resume are both constexpr and await_resume +// returns a reference, both calls are folded. The awaiter must then not be +// cloned for await_suspend (lowering used to fail with an internal error). +bool flag; +template struct constexpr_ref_awaiter { + constexpr bool await_ready() noexcept { return Ready; } + void await_suspend(std::coroutine_handle<>) noexcept; + constexpr bool &await_resume() noexcept { return flag; } +}; + +task g() { + (co_await constexpr_ref_awaiter{}) = false; + (co_await constexpr_ref_awaiter{}) = false; + bool x = co_await constexpr_ref_awaiter{}; + (void)x; +} From 0c66807fa1c02f1037a560c450a484f17aa45043 Mon Sep 17 00:00:00 2001 From: Mohit Mishra Date: Sun, 22 Nov 2026 08:50:50 +0530 Subject: [PATCH 3/3] Use the folded flag of the co_await calls; handle function references. [GH #447] Co-authored-by: Christof Meerwald --- src/expr.c | 71 ++++++------------------------- src/exprutil.c | 8 ++-- src/exprutil.h | 3 +- tests/tests/changes/GH447.sft.cpp | 12 ++++++ 4 files changed, 31 insertions(+), 63 deletions(-) diff --git a/src/expr.c b/src/expr.c index 90911dad29..19d4ccf19d 100644 --- a/src/expr.c +++ b/src/expr.c @@ -31,7 +31,6 @@ expr.c -- Expression scanning routines. #include "interpret.h" #include "layout.h" #include "ifc_modules.h" -#include "il_walk.h" #if MICROSOFT_EXTENSIONS_ALLOWED /* The Microsoft-specific predefined identifier __FUNCDNAME__ refers to the mangled name of the current function. Hence, we may need access to the @@ -49054,55 +49053,6 @@ variable. #endif /* MICROSOFT_EXTENSIONS_ALLOWED */ -/* The expression node add_await_to_operand is looking for; see - call_contains_await_operand. */ -static an_expr_node_ptr await_operand_sought; - -static void find_await_operand(an_expr_node_ptr expr, - an_expr_or_stmt_traversal_block_ptr tblock) -/* -Called from traverse_expr: stop with a TRUE result if expr is the node being -looked for. -*/ -{ - if (expr == await_operand_sought) { - tblock->result = TRUE; - tblock->terminate = TRUE; - } /* if */ -} /* find_await_operand */ - - -static a_boolean call_contains_await_operand(an_operand *call, - an_operand *await_operand) -/* -Return TRUE if the result of one of the calls generated for a co_await (call) -still contains the expression of the awaiter operand (await_operand), i.e., -if the call was not folded away. A call folded to a constant does not, and -neither does a folded call returning a reference to a variable, whose result -is just an lvalue for that variable. -*/ -{ - a_boolean result; - - if (is_constant_operand(call)) { - result = FALSE; - } else if (!is_expression_operand(call) || - !is_expression_operand(await_operand)) { - /* Keep the earlier behavior: assume the call uses the operand. */ - result = TRUE; - } else { - an_expr_or_stmt_traversal_block tblock; - clear_expr_or_stmt_traversal_block(&tblock); - tblock.process_expr = find_await_operand; - await_operand_sought = await_operand->variant.expression; - traverse_expr(call->variant.expression, &tblock); - await_operand_sought = NULL; - result = tblock.result; - } /* if */ - return result; -} /* call_contains_await_operand */ - - void add_await_to_operand(an_operand_ptr operand, a_source_position *pos, a_token_sequence_number tok_seq_number, @@ -49235,14 +49185,16 @@ suspend point. an_operand orig_operand; an_operand resume_operand; an_operand suspend_operand; + a_boolean ready_call_folded, resume_call_folded; copy_operand(operand, &orig_operand); /* Call the await_ready, await_suspend, and await_resume member functions. */ call_named_member_function(operand, "await_ready", (a_template_arg_ptr)NULL, (an_arg_list_elem_ptr)NULL, - &orig_operand, &ready_call); - if (call_contains_await_operand(&ready_call, &orig_operand)) { + &orig_operand, &ready_call, + &ready_call_folded); + if (!ready_call_folded) { /* We can't use the original operand because it's been incorporated into the await_ready call, so make a clone for use in the await_resume call. */ @@ -49256,9 +49208,9 @@ suspend point. call_named_member_function(&resume_operand, "await_resume", (a_template_arg_ptr)NULL, (an_arg_list_elem_ptr)NULL, - &orig_operand, &resume_call); - if (call_contains_await_operand(&ready_call, &orig_operand) || - call_contains_await_operand(&resume_call, &orig_operand)) { + &orig_operand, &resume_call, + &resume_call_folded); + if (!ready_call_folded || !resume_call_folded) { /* The original operand has been incorporated into one of the preceding calls, so clone it for use in the await_suspend call. */ clone_operand(&orig_operand, &suspend_operand, /*vars_can_change=*/TRUE, @@ -49303,11 +49255,12 @@ suspend point. } /* if */ node->variant.await_info.ready_resume_suspend->next->next = make_node_from_operand(&suspend_call); - if (is_a_glvalue(&resume_call)) { + if (is_a_glvalue(&resume_call) || is_a_function_designator(&resume_call)) { /* await_resume returns a reference, so the co_await expression is a - glvalue too (an lvalue or xvalue, like the await_resume call). As - for a call, give the node the reference type and put a reference - indirection on top of it. */ + glvalue too (an lvalue or xvalue, like the await_resume call; a + reference to a function gives an lvalue even if it is an rvalue + reference). As for a call, give the node the reference type and put a + reference indirection on top of it. */ an_expr_node_ptr call = strip_ref_indirect( node->variant.await_info.ready_resume_suspend->next, /*parens_also=*/FALSE); diff --git a/src/exprutil.c b/src/exprutil.c index ec79386de2..680aa4cd87 100644 --- a/src/exprutil.c +++ b/src/exprutil.c @@ -24626,7 +24626,8 @@ void call_named_member_function(an_operand *selector_operand, a_template_arg_ptr templ_arg_list, an_arg_list_elem_ptr alep, an_operand *orig_operand, - an_operand *result) + an_operand *result, + a_boolean *p_folded) /* Create in *result the representation of a call of the form @@ -24641,6 +24642,7 @@ the explicit template arguments (if any) are represented by templ_arg_list, and a1, a2, ... is represented by alep (possibly NULL). Operand details other than the bound_function and virtual_function flags will be copied from orig_operand to the function operand created before assembling the final call. +If p_folded is non-NULL, *p_folded indicates whether the call was folded. */ { a_type_ptr class_type; @@ -24650,6 +24652,7 @@ orig_operand to the function operand created before assembling the final call. an_expr_node_ptr arg_node_list = NULL; an_operand function_operand; + if (p_folded != NULL) *p_folded = FALSE; class_type = skip_typerefs_not_dependent_decltypes(selector_operand->type); if (class_type->kind == (a_type_kind)tk_typeref) { /* A dependent "decltype": Use an associated proxy class. */ @@ -24742,8 +24745,7 @@ orig_operand to the function operand created before assembling the final call. /*start_position=*/&null_source_position, /*operator_position=*/&selector_operand->position, /*end_position=*/&null_source_position, - result, /*p_folded=*/(a_boolean*)NULL, - &func_call_node); + result, p_folded, &func_call_node); if (nonreal_case && func_call_node != NULL && node_operator_is(func_call_node, eok_call)) { func_call_node->variant.operation.kind = diff --git a/src/exprutil.h b/src/exprutil.h index ccfaed99cc..2245b0d29b 100644 --- a/src/exprutil.h +++ b/src/exprutil.h @@ -2022,7 +2022,8 @@ void call_named_member_function(an_operand *selector_operand, a_template_arg_ptr templ_arg_list, an_arg_list_elem_ptr alep, an_operand *orig_operand, - an_operand *result); + an_operand *result, + a_boolean *p_folded = NULL); extern void call_adl_named_function(a_const_char *func_name, diff --git a/tests/tests/changes/GH447.sft.cpp b/tests/tests/changes/GH447.sft.cpp index 9899174059..ef95f9aac2 100644 --- a/tests/tests/changes/GH447.sft.cpp +++ b/tests/tests/changes/GH447.sft.cpp @@ -78,3 +78,15 @@ task g() { bool x = co_await constexpr_ref_awaiter{}; (void)x; } + +// A reference to a function gives an lvalue, even for an rvalue reference. +void fn(); +task h() { + void (&l)() = co_await awaiter{}; + void (&r)() = co_await awaiter{}; + decltype(auto) d = co_await awaiter{}; + static_assert(std::is_same::value); + l(); + r(); + d(); +}