From 514a86ad1c5361ecdbacc203f2d8da19ffa97f29 Mon Sep 17 00:00:00 2001 From: Frotty Date: Sat, 18 Oct 2025 19:07:21 +0200 Subject: [PATCH 1/2] add error for destroy this in constructor, fixes #754 --- .../validation/WurstValidator.java | 7 +++++ .../tests/wurstscript/tests/BugTests.java | 30 +++++++++++++++++++ 2 files changed, 37 insertions(+) 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 02e8e68dc..ca0495c03 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 @@ -1817,6 +1817,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 e60d417fe..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,5 +1442,35 @@ public void minusRewrite() { ); } + @Test + public void duplicateNameInClassHierachy() { + testAssertErrorsLines(false, "Variable x in class B hides variable x from superclass A", + "package test", + "native testSuccess()", + "class A", + " int x", + "class B extends A", + " int x", + "init", + " let b = new B()", + " if b != null", + " testSuccess()", + "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"); + } } From 319e316ebf43a27235577c29408c5ae4e92aa01f Mon Sep 17 00:00:00 2001 From: Frotty Date: Sat, 18 Oct 2025 19:07:44 +0200 Subject: [PATCH 2/2] Update BugTests.java --- .../java/tests/wurstscript/tests/BugTests.java | 17 ----------------- 1 file changed, 17 deletions(-) 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 86a74a00f..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 @@ -1473,21 +1473,4 @@ public void callingDestroyThisInConstructor() { "endpackage"); } - @Test - public void duplicateNameInClassHierachy() { - testAssertErrorsLines(false, "Variable x in class B hides variable x from superclass A", - "package test", - "native testSuccess()", - "class A", - " int x", - "class B extends A", - " int x", - "init", - " let b = new B()", - " if b != null", - " testSuccess()", - "endpackage"); - } - - }