Skip to content

#1376: Fix non-constructors with 'out this' #1377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

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

3 changes: 2 additions & 1 deletion source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -4502,9 +4502,10 @@ auto function_type_node::is_constructor() const
(*parameters).ssize() > 0
&& (*parameters)[0]->has_name("this")
&& (*parameters)[0]->direction() == passing_style::out
&& my_decl
&& my_decl->has_name("operator=")
)
{
assert(my_decl && my_decl->has_name("operator="));
return true;
}
return false;
Expand Down
15 changes: 15 additions & 0 deletions source/sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -2086,6 +2086,21 @@ class sema
}
}

// If the first parameter is 'out this', it must be a constructor.
if (
!n.is_constructor()
&& (*n.parameters).ssize() > 0
&& (*n.parameters)[0]->has_name("this")
&& (*n.parameters)[0]->direction() == passing_style::out
)
{
errors.emplace_back(
n.position(),
"a function with an 'out this' parameter must be a constructor"
);
return false;
}

return true;
}

Expand Down