From 5e706b13c024d3dd1bfe1ca8ed24a7adcfe78af1 Mon Sep 17 00:00:00 2001 From: Alistair Bell Date: Tue, 25 Mar 2025 19:41:52 -0400 Subject: [PATCH 1/2] #1376: Fix crash on non-constructor with 'out this' --- source/parse.h | 4 +++- source/sema.h | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/source/parse.h b/source/parse.h index 9c80447bf4..01e309abcc 100644 --- a/source/parse.h +++ b/source/parse.h @@ -4502,9 +4502,11 @@ 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=")); + //assert(my_decl && my_decl->has_name("operator=")); return true; } return false; diff --git a/source/sema.h b/source/sema.h index 424261a69b..9891913c1f 100644 --- a/source/sema.h +++ b/source/sema.h @@ -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; } From 935f6583fe289d04fceabe1801c6570f70853189 Mon Sep 17 00:00:00 2001 From: Alistair Bell Date: Tue, 25 Mar 2025 20:02:07 -0400 Subject: [PATCH 2/2] #1376: Add regression test. --- .../pure2-bugfix-for-out-this-nonconstructor.cpp2 | 4 ++++ .../pure2-bugfix-for-out-this-nonconstructor.cpp2.output | 3 +++ 2 files changed, 7 insertions(+) create mode 100644 regression-tests/pure2-bugfix-for-out-this-nonconstructor.cpp2 create mode 100644 regression-tests/test-results/pure2-bugfix-for-out-this-nonconstructor.cpp2.output diff --git a/regression-tests/pure2-bugfix-for-out-this-nonconstructor.cpp2 b/regression-tests/pure2-bugfix-for-out-this-nonconstructor.cpp2 new file mode 100644 index 0000000000..6831e3675d --- /dev/null +++ b/regression-tests/pure2-bugfix-for-out-this-nonconstructor.cpp2 @@ -0,0 +1,4 @@ + +Machine: @polymorphic_base type = { + operator%: (out this, _: std::string) = {} +} diff --git a/regression-tests/test-results/pure2-bugfix-for-out-this-nonconstructor.cpp2.output b/regression-tests/test-results/pure2-bugfix-for-out-this-nonconstructor.cpp2.output new file mode 100644 index 0000000000..b64fc2a6b9 --- /dev/null +++ b/regression-tests/test-results/pure2-bugfix-for-out-this-nonconstructor.cpp2.output @@ -0,0 +1,3 @@ +pure2-bugfix-for-out-this-nonconstructor.cpp2... +pure2-bugfix-for-out-this-nonconstructor.cpp2(3,16): error: a function with an 'out this' parameter must be a constructor +