From ddde3d833bec373081a3c0f419250f8e8b553805 Mon Sep 17 00:00:00 2001 From: Herb Sutter Date: Thu, 31 Oct 2024 16:50:49 -1000 Subject: [PATCH] Added support for "guard" local object naming convention For a local variable whose name starts with "guard" we do not automatically move from last use. This gives language meaning to a naming convention of "guard" as a name prefix for guard-like stack objects, such as local `std::scoped_lock` objects, whose destructors are always the object's real last use. --- docs/cpp2/functions.md | 4 +++- regression-tests/test-results/version | 2 +- source/build.info | 2 +- source/to_cpp1.h | 1 + 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/cpp2/functions.md b/docs/cpp2/functions.md index f4a648d64..2b116582d 100644 --- a/docs/cpp2/functions.md +++ b/docs/cpp2/functions.md @@ -388,10 +388,12 @@ In a function body, a **definite last use** of a local name is a single use of t For each definite last use: -- If the name is a local object or a `copy` or `move` parameter, we know the object will not be used again before being destroyed, and so the object is automatically treated as an rvalue (move candidate). If the expression that contains the last use is able to move from the rvalue, the move will happen automatically. +- If the name is a `copy` or `move` parameter or is a local object whose name does not start with `guard`, we know the object will not be used again before being destroyed, and so the object is automatically treated as an rvalue (move candidate). If the expression that contains the last use is able to move from the rvalue, the move will happen automatically. - If the name is a `forward` parameter, the object is automatically forwarded to preserve its constness and value category (`std::forward`-ed). +> Note: This gives language meaning to a naming convention of `guard` as a name prefix for "guard" stack objects, such as local `std::scoped_lock` objects, whose destructors are always the object's real last use. + For example: ``` cpp title="Definite last uses" linenums="1" hl_lines="13 16 19 21" diff --git a/regression-tests/test-results/version b/regression-tests/test-results/version index 419132af7..e01639c74 100644 --- a/regression-tests/test-results/version +++ b/regression-tests/test-results/version @@ -1,4 +1,4 @@ -cppfront compiler v0.8.0 Build 9A10:1540 +cppfront compiler v0.8.0 Build 9A31:1626 SPDX-License-Identifier Apache-2.0 WITH LLVM-exception Copyright (c) 2022-2024 Herb Sutter diff --git a/source/build.info b/source/build.info index 71b5052e3..37a3f9449 100644 --- a/source/build.info +++ b/source/build.info @@ -1 +1 @@ -"9A10:1540" \ No newline at end of file +"9A31:1626" \ No newline at end of file diff --git a/source/to_cpp1.h b/source/to_cpp1.h index 73b904ecb..16a698400 100644 --- a/source/to_cpp1.h +++ b/source/to_cpp1.h @@ -1764,6 +1764,7 @@ class cppfront // Otherwise we'll use cpp2::move bool add_move = !add_forward + && !n.identifier->as_string_view().starts_with("guard") && ( !last_use || last_use->safe_to_move