Skip to content

Commit 514a86a

Browse files
committed
add error for destroy this in constructor, fixes #754
1 parent fdcd03d commit 514a86a

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
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
@@ -1817,6 +1817,13 @@ private void visit(ModuleDef moduleDef) {
18171817
}
18181818

18191819
private void visit(ExprDestroy stmtDestroy) {
1820+
if (stmtDestroy.getDestroyedObj() instanceof ExprThis) {
1821+
if (isInConstructor(stmtDestroy)) {
1822+
stmtDestroy.addError("Cannot destroy 'this' in constructor");
1823+
return;
1824+
}
1825+
}
1826+
18201827
WurstType typ = stmtDestroy.getDestroyedObj().attrTyp();
18211828
if (typ instanceof WurstTypeModule) {
18221829

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,5 +1442,35 @@ public void minusRewrite() {
14421442
);
14431443
}
14441444

1445+
@Test
1446+
public void duplicateNameInClassHierachy() {
1447+
testAssertErrorsLines(false, "Variable x in class B hides variable x from superclass A",
1448+
"package test",
1449+
"native testSuccess()",
1450+
"class A",
1451+
" int x",
1452+
"class B extends A",
1453+
" int x",
1454+
"init",
1455+
" let b = new B()",
1456+
" if b != null",
1457+
" testSuccess()",
1458+
"endpackage");
1459+
}
1460+
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+
}
14451475

14461476
}

0 commit comments

Comments
 (0)