Skip to content

Commit

Permalink
Added support for "guard" local object naming convention
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hsutter committed Nov 1, 2024
1 parent 1032a5b commit ddde3d8
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 3 deletions.
4 changes: 3 additions & 1 deletion docs/cpp2/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/test-results/version
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion source/build.info
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"9A10:1540"
"9A31:1626"
1 change: 1 addition & 0 deletions source/to_cpp1.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ddde3d8

Please sign in to comment.