Skip to content

Commit ff4134e

Browse files
committed
Disallow declarations of parents after functions.
1 parent 5d3e324 commit ff4134e

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
crash_m0b: type = {
2+
}
3+
4+
crash_m0c: type = {
5+
name: i32;
6+
get_name: (this) -> i32 = { return name; }
7+
this: crash_m0b;
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pure2-bugfix-for-functions-before-superclasses.cpp2...
2+
pure2-bugfix-for-functions-before-superclasses.cpp2(7,3): error: a type cannot declare a parent after defining a function
3+

source/sema.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,6 +2037,7 @@ class sema
20372037
if (n.is_type()) {
20382038
auto compound_stmt = n.initializer->get_if<compound_statement_node>();
20392039
assert (compound_stmt);
2040+
bool seen_function = false;
20402041
for (auto& stmt : compound_stmt->statements) {
20412042
if (
20422043
!stmt->is_declaration()
@@ -2049,6 +2050,24 @@ class sema
20492050
);
20502051
return false;
20512052
}
2053+
auto stmt_decl = stmt->get_if<declaration_node>();
2054+
// If this is a declaration, check if it's a function
2055+
if (stmt_decl && stmt_decl->is_function())
2056+
seen_function = true;
2057+
2058+
// If this is called 'this', then make sure we haven't seen any functions
2059+
if (
2060+
stmt_decl
2061+
&& stmt_decl->has_name("this")
2062+
&& seen_function
2063+
)
2064+
{
2065+
handle_error(
2066+
stmt->position(),
2067+
"a type cannot declare a parent after defining a function"
2068+
);
2069+
return false;
2070+
}
20522071
}
20532072
}
20542073

0 commit comments

Comments
 (0)