Skip to content

Commit d7ea6c0

Browse files
JohelEGPhsutter
andauthored
fix(to_cpp1): don't emit [[nodiscard]] for pre-increment/decrement (#883)
* fix(to_cpp1): don't emit `[[nodiscard]]` for pre-increment/decrement * Rename the new function, and move part of the test to a sema enforcement * Tweak assert/check order --------- Co-authored-by: Herb Sutter <[email protected]>
1 parent c74a0f2 commit d7ea6c0

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

source/parse.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,6 +2229,9 @@ struct function_type_node
22292229
auto is_comparison() const
22302230
-> bool;
22312231

2232+
auto is_increment_or_decrement() const
2233+
-> bool;
2234+
22322235
auto is_compound_assignment() const
22332236
-> bool;
22342237

@@ -3197,6 +3200,16 @@ struct declaration_node
31973200
return false;
31983201
}
31993202

3203+
auto is_increment_or_decrement() const
3204+
-> bool
3205+
{
3206+
if (auto func = std::get_if<a_function>(&type)) {
3207+
return (*func)->is_increment_or_decrement();
3208+
}
3209+
// else
3210+
return false;
3211+
}
3212+
32003213
auto is_compound_assignment() const
32013214
-> bool
32023215
{
@@ -3709,6 +3722,20 @@ auto function_type_node::is_comparison() const
37093722
}
37103723

37113724

3725+
auto function_type_node::is_increment_or_decrement() const
3726+
-> bool
3727+
{
3728+
if (
3729+
my_decl->has_name("operator++")
3730+
|| my_decl->has_name("operator--")
3731+
)
3732+
{
3733+
return true;
3734+
}
3735+
return false;
3736+
}
3737+
3738+
37123739
auto function_type_node::is_compound_assignment() const
37133740
-> bool
37143741
{

source/sema.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,6 +1523,34 @@ class sema
15231523
}
15241524

15251525

1526+
auto check(function_type_node const& n)
1527+
-> bool
1528+
{
1529+
// An increment/decrement function must have a single parameter that is 'inout this'
1530+
if (
1531+
(
1532+
n.my_decl->has_name("operator++")
1533+
|| n.my_decl->has_name("operator--")
1534+
)
1535+
&&
1536+
(
1537+
(*n.parameters).ssize() != 1
1538+
|| !(*n.parameters)[0]->has_name("this")
1539+
|| (*n.parameters)[0]->direction() != passing_style::inout
1540+
)
1541+
)
1542+
{
1543+
errors.emplace_back(
1544+
n.position(),
1545+
"a user-defined " + n.my_decl->name()->to_string() + " must have a single 'inout this' parameter"
1546+
);
1547+
return false;
1548+
}
1549+
1550+
return true;
1551+
}
1552+
1553+
15261554
auto check(statement_node const& n)
15271555
-> bool
15281556
{

source/to_cpp1.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4596,6 +4596,10 @@ class cppfront
45964596
{ STACKINSTR
45974597
assert(n.parameters);
45984598

4599+
if (!sema.check(n)) {
4600+
return;
4601+
}
4602+
45994603
if (
46004604
is_main
46014605
&& n.parameters->parameters.size() > 0
@@ -6093,6 +6097,7 @@ class cppfront
60936097
func->has_non_void_return_type()
60946098
&& !func->is_assignment()
60956099
&& !func->is_compound_assignment()
6100+
&& !func->is_increment_or_decrement()
60966101
&& (
60976102
printer.get_phase() == printer.phase1_type_defs_func_decls
60986103
|| n.has_initializer() // so we're printing it in phase 2

0 commit comments

Comments
 (0)