Skip to content

Commit a7dd188

Browse files
authored
Make "cannot 'destroy this' in constructor" error a warning for now (#1105)
To prevent breaking changes without prior announcement
1 parent cfb1c5d commit a7dd188

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,8 +1850,7 @@ private void visit(ModuleDef moduleDef) {
18501850
private void visit(ExprDestroy stmtDestroy) {
18511851
if (stmtDestroy.getDestroyedObj() instanceof ExprThis) {
18521852
if (isInConstructor(stmtDestroy)) {
1853-
stmtDestroy.addError("Cannot destroy 'this' in constructor");
1854-
return;
1853+
stmtDestroy.addWarning("Should not destroy 'this' in constructor, because 'new' would return an invalid object.\nMove destruction logic into a separate function outside the constructor.\nThis will be an error in the future.");
18551854
}
18561855
}
18571856

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1462,7 +1462,7 @@ public void duplicateNameInClassHierachy() {
14621462

14631463
@Test
14641464
public void callingDestroyThisInConstructor() {
1465-
testAssertErrorsLines(false, "Cannot destroy 'this' in constructor",
1465+
testAssertWarningsLines(false, "Should not destroy 'this' in constructor",
14661466
"package test",
14671467
"native testSuccess()",
14681468
"class A",

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class TestConfig {
6363
private boolean executeTests;
6464
private boolean executeProgOnlyAfterTransforms;
6565
private String expectedError;
66+
private String expectedWarning;
67+
6668
private final List<File> inputFiles = new ArrayList<>();
6769
private final List<CU> additionalCompilationUnits = new ArrayList<>();
6870
private boolean stopOnFirstError = true;
@@ -106,6 +108,11 @@ TestConfig executeProg(boolean b) {
106108
return this;
107109
}
108110

111+
TestConfig expectWarning(String expectedWarning) {
112+
this.expectedWarning = expectedWarning;
113+
return this;
114+
}
115+
109116
public TestConfig executeProgOnlyAfterTransforms() {
110117
this.executeProgOnlyAfterTransforms = true;
111118
return this;
@@ -149,6 +156,18 @@ CompilationResult run() {
149156
}
150157
}
151158
}
159+
if (expectedWarning != null) {
160+
List<CompileError> warnings = res.getGui().getWarningList();
161+
if (warnings.isEmpty()) {
162+
fail("No warnings were discovered");
163+
} else if (warnings.stream()
164+
.noneMatch(w -> w.getMessage().toLowerCase().contains(expectedWarning.toLowerCase()))) {
165+
for (CompileError w : warnings) {
166+
System.err.println("Unexpected warning:" + w);
167+
}
168+
throw new RuntimeException("Unexpected warning", warnings.get(0));
169+
}
170+
}
152171
return res;
153172
} catch (CompileError e) {
154173
if (expectedError != null) {
@@ -355,6 +374,23 @@ void testAssertErrors(String name, boolean executeProg, String prog, String erro
355374
test().executeProg(executeProg).expectError(errorMessage).lines(prog);
356375
}
357376

377+
public void testAssertWarningsLines(boolean executeProg, String warningMessage, String... input) {
378+
test()
379+
.setStopOnFirstError(false)
380+
.executeProg(executeProg)
381+
.expectWarning(warningMessage)
382+
.lines(input);
383+
}
384+
385+
public void testAssertWarningsLinesWithStdLib(boolean executeProg, String warningMessage, String... input) {
386+
test()
387+
.withStdLib()
388+
.setStopOnFirstError(false)
389+
.executeProg(executeProg)
390+
.expectWarning(warningMessage)
391+
.lines(input);
392+
}
393+
358394
protected WurstModel testScript(String name, boolean executeProg, String prog) {
359395
return test().executeProg(executeProg).lines(prog).getModel();
360396
}

0 commit comments

Comments
 (0)