Skip to content

Commit ddde3d8

Browse files
committed
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.
1 parent 1032a5b commit ddde3d8

File tree

4 files changed

+6
-3
lines changed

4 files changed

+6
-3
lines changed

docs/cpp2/functions.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,12 @@ In a function body, a **definite last use** of a local name is a single use of t
388388

389389
For each definite last use:
390390

391-
- 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.
391+
- 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.
392392

393393
- If the name is a `forward` parameter, the object is automatically forwarded to preserve its constness and value category (`std::forward`-ed).
394394

395+
> 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.
396+
395397
For example:
396398

397399
``` cpp title="Definite last uses" linenums="1" hl_lines="13 16 19 21"

regression-tests/test-results/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

2-
cppfront compiler v0.8.0 Build 9A10:1540
2+
cppfront compiler v0.8.0 Build 9A31:1626
33
SPDX-License-Identifier Apache-2.0 WITH LLVM-exception
44
Copyright (c) 2022-2024 Herb Sutter

source/build.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"9A10:1540"
1+
"9A31:1626"

source/to_cpp1.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,6 +1764,7 @@ class cppfront
17641764
// Otherwise we'll use cpp2::move
17651765
bool add_move =
17661766
!add_forward
1767+
&& !n.identifier->as_string_view().starts_with("guard")
17671768
&& (
17681769
!last_use
17691770
|| last_use->safe_to_move

0 commit comments

Comments
 (0)