Skip to content

Commit f4e4dd1

Browse files
authored
Add error for 'destroy this' in constructor, fixes #754 (#1099)
1 parent 7588a14 commit f4e4dd1

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/validation/WurstValidator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,6 +1863,13 @@ private void visit(ModuleDef moduleDef) {
18631863
}
18641864

18651865
private void visit(ExprDestroy stmtDestroy) {
1866+
if (stmtDestroy.getDestroyedObj() instanceof ExprThis) {
1867+
if (isInConstructor(stmtDestroy)) {
1868+
stmtDestroy.addError("Cannot destroy 'this' in constructor");
1869+
return;
1870+
}
1871+
}
1872+
18661873
WurstType typ = stmtDestroy.getDestroyedObj().attrTyp();
18671874
if (typ instanceof WurstTypeModule) {
18681875

de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/BugTests.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,6 @@ public void minusRewrite() {
14421442
);
14431443
}
14441444

1445-
14461445
@Test
14471446
public void duplicateNameInClassHierachy() {
14481447
testAssertErrorsLines(false, "Variable x in class B hides variable x from superclass A",
@@ -1459,5 +1458,19 @@ public void duplicateNameInClassHierachy() {
14591458
"endpackage");
14601459
}
14611460

1461+
@Test
1462+
public void callingDestroyThisInConstructor() {
1463+
testAssertErrorsLines(false, "Cannot destroy 'this' in constructor",
1464+
"package test",
1465+
"native testSuccess()",
1466+
"class A",
1467+
" construct()",
1468+
" destroy this",
1469+
"init",
1470+
" let b = new A()",
1471+
" if b != null",
1472+
" testSuccess()",
1473+
"endpackage");
1474+
}
14621475

14631476
}

0 commit comments

Comments
 (0)