diff --git a/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/validation/WurstValidator.java b/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/validation/WurstValidator.java index 28ad77798..380a7cb06 100644 --- a/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/validation/WurstValidator.java +++ b/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/validation/WurstValidator.java @@ -1863,6 +1863,13 @@ private void visit(ModuleDef moduleDef) { } private void visit(ExprDestroy stmtDestroy) { + if (stmtDestroy.getDestroyedObj() instanceof ExprThis) { + if (isInConstructor(stmtDestroy)) { + stmtDestroy.addError("Cannot destroy 'this' in constructor"); + return; + } + } + WurstType typ = stmtDestroy.getDestroyedObj().attrTyp(); if (typ instanceof WurstTypeModule) { diff --git a/de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/BugTests.java b/de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/BugTests.java index 864fedda7..af0ec3d4e 100644 --- a/de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/BugTests.java +++ b/de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/BugTests.java @@ -1442,7 +1442,6 @@ public void minusRewrite() { ); } - @Test public void duplicateNameInClassHierachy() { testAssertErrorsLines(false, "Variable x in class B hides variable x from superclass A", @@ -1459,5 +1458,19 @@ public void duplicateNameInClassHierachy() { "endpackage"); } + @Test + public void callingDestroyThisInConstructor() { + testAssertErrorsLines(false, "Cannot destroy 'this' in constructor", + "package test", + "native testSuccess()", + "class A", + " construct()", + " destroy this", + "init", + " let b = new A()", + " if b != null", + " testSuccess()", + "endpackage"); + } }