Skip to content

Commit 590cab7

Browse files
#1376: Fix non-constructors with 'out this' (#1377)
* #1376: Fix crash on non-constructor with 'out this' * #1376: Add regression test. * Minor tidying Removed the eliminated assert Re-ran regression tests --------- Co-authored-by: Herb Sutter <[email protected]>
1 parent 252991d commit 590cab7

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
Machine: @polymorphic_base <I:int> type = {
3+
operator%: (out this, _: std::string) = {}
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pure2-bugfix-for-out-this-nonconstructor-error.cpp2...
2+
pure2-bugfix-for-out-this-nonconstructor-error.cpp2(3,16): error: a function with an 'out this' parameter must be a constructor
3+

source/parse.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -4562,9 +4562,10 @@ auto function_type_node::is_constructor() const
45624562
(*parameters).ssize() > 0
45634563
&& (*parameters)[0]->has_name("this")
45644564
&& (*parameters)[0]->direction() == passing_style::out
4565+
&& my_decl
4566+
&& my_decl->has_name("operator=")
45654567
)
45664568
{
4567-
assert(my_decl && my_decl->has_name("operator="));
45684569
return true;
45694570
}
45704571
return false;

source/sema.h

+15
Original file line numberDiff line numberDiff line change
@@ -2086,6 +2086,21 @@ class sema
20862086
}
20872087
}
20882088

2089+
// If the first parameter is 'out this', it must be a constructor.
2090+
if (
2091+
!n.is_constructor()
2092+
&& (*n.parameters).ssize() > 0
2093+
&& (*n.parameters)[0]->has_name("this")
2094+
&& (*n.parameters)[0]->direction() == passing_style::out
2095+
)
2096+
{
2097+
errors.emplace_back(
2098+
n.position(),
2099+
"a function with an 'out this' parameter must be a constructor"
2100+
);
2101+
return false;
2102+
}
2103+
20892104
return true;
20902105
}
20912106

0 commit comments

Comments
 (0)