Skip to content

Commit 3a2d11e

Browse files
committed
Generate return-old-value overload of operator++/operator-- only if the type is copyable
1 parent e7f4bea commit 3a2d11e

File tree

5 files changed

+11
-23
lines changed

5 files changed

+11
-23
lines changed

docs/cpp2/common.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ Postfix notation lets the code read fluidly left-to-right, in the same order in
220220

221221
> Note: The `...` pack expansion syntax is also supported. The above `...` and `..=` are the Cpp2 range operators, which overlap in syntax.
222222
223-
> Note: Because `++` and `--` always have in-place update semantics, we never need to remember "use prefix `++`/`--` unless you need a copy of the old value." If you do need a copy of the old value, just take the copy before calling `++`/`--`. When you write a type that overloads `operator++` or `operator--`, cppfront generates both Cpp1 overloads (in-place-update and copy-old-value) for that function to support natural use of the type from Cpp1 code.
223+
> Note: Because `++` and `--` always have in-place update semantics, we never need to remember "use prefix `++`/`--` unless you need a copy of the old value." If you do need a copy of the old value, just take the copy before calling `++`/`--`. When you write a copyable type that overloads `operator++` or `operator--`, cppfront generates also the copy-old-value overload of that function to support natural use of the type from Cpp1 code.
224224
225225

226226
### <a id="binary-operators"></a> Binary operators

regression-tests/test-results/version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
cppfront compiler v0.7.2 Build 9731:1430
2+
cppfront compiler v0.7.2 Build 9731:1814
33
Copyright(c) Herb Sutter All rights reserved
44

55
SPDX-License-Identifier: CC-BY-NC-ND-4.0

source/build.info

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"9731:1430"
1+
"9731:1814"

source/sema.h

+1-19
Original file line numberDiff line numberDiff line change
@@ -2068,13 +2068,7 @@ class sema
20682068

20692069
// An increment/decrement function must have a single 'inout' parameter,
20702070
// and if it's a member flag it if we know the type is not copyable
2071-
if (
2072-
n.my_decl
2073-
&& (
2074-
n.my_decl->has_name("operator++")
2075-
|| n.my_decl->has_name("operator--")
2076-
)
2077-
)
2071+
if (n.is_increment_or_decrement())
20782072
{
20792073
if (
20802074
(*n.parameters).ssize() != 1
@@ -2095,18 +2089,6 @@ class sema
20952089
);
20962090
return false;
20972091
}
2098-
2099-
if (
2100-
n.my_decl->parent_declaration
2101-
&& n.my_decl->parent_declaration->cannot_be_a_copy_constructible_type()
2102-
)
2103-
{
2104-
errors.emplace_back(
2105-
n.position(),
2106-
"a user-defined " + n.my_decl->name()->to_string() + " in type scope must be a member of a copyable type"
2107-
);
2108-
return false;
2109-
}
21102092
}
21112093

21122094
return true;

source/to_cpp1.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -6589,12 +6589,18 @@ class cppfront
65896589
printer.print_cpp2( suffix2, n.position() );
65906590

65916591
// If this is ++ or --, also generate a Cpp1 postfix version of the operator
6592+
// (as long as we don't know for sure this isn't a copyable type)
65926593
if (func->is_increment_or_decrement())
65936594
{
65946595
if (generating_postfix_inc_dec_from) {
65956596
assert (generating_postfix_inc_dec_from == &n);
65966597
}
6597-
else {
6598+
else if (
6599+
!n.parent_declaration
6600+
|| !n.parent_declaration->is_type()
6601+
|| !n.parent_declaration->cannot_be_a_copy_constructible_type()
6602+
)
6603+
{
65986604
need_to_generate_postfix_inc_dec = true;
65996605
}
66006606
}

0 commit comments

Comments
 (0)