Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1850,8 +1850,7 @@ 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;
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.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ public void duplicateNameInClassHierachy() {

@Test
public void callingDestroyThisInConstructor() {
testAssertErrorsLines(false, "Cannot destroy 'this' in constructor",
testAssertWarningsLines(false, "Should not destroy 'this' in constructor",
"package test",
"native testSuccess()",
"class A",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class TestConfig {
private boolean executeTests;
private boolean executeProgOnlyAfterTransforms;
private String expectedError;
private String expectedWarning;

private final List<File> inputFiles = new ArrayList<>();
private final List<CU> additionalCompilationUnits = new ArrayList<>();
private boolean stopOnFirstError = true;
Expand Down Expand Up @@ -106,6 +108,11 @@ TestConfig executeProg(boolean b) {
return this;
}

TestConfig expectWarning(String expectedWarning) {
this.expectedWarning = expectedWarning;
return this;
}

public TestConfig executeProgOnlyAfterTransforms() {
this.executeProgOnlyAfterTransforms = true;
return this;
Expand Down Expand Up @@ -149,6 +156,18 @@ CompilationResult run() {
}
}
}
if (expectedWarning != null) {
List<CompileError> warnings = res.getGui().getWarningList();
if (warnings.isEmpty()) {
fail("No warnings were discovered");
} else if (warnings.stream()
.noneMatch(w -> w.getMessage().toLowerCase().contains(expectedWarning.toLowerCase()))) {
for (CompileError w : warnings) {
System.err.println("Unexpected warning:" + w);
}
throw new RuntimeException("Unexpected warning", warnings.get(0));
}
}
return res;
} catch (CompileError e) {
if (expectedError != null) {
Expand Down Expand Up @@ -355,6 +374,23 @@ void testAssertErrors(String name, boolean executeProg, String prog, String erro
test().executeProg(executeProg).expectError(errorMessage).lines(prog);
}

public void testAssertWarningsLines(boolean executeProg, String warningMessage, String... input) {
test()
.setStopOnFirstError(false)
.executeProg(executeProg)
.expectWarning(warningMessage)
.lines(input);
}

public void testAssertWarningsLinesWithStdLib(boolean executeProg, String warningMessage, String... input) {
test()
.withStdLib()
.setStopOnFirstError(false)
.executeProg(executeProg)
.expectWarning(warningMessage)
.lines(input);
}

protected WurstModel testScript(String name, boolean executeProg, String prog) {
return test().executeProg(executeProg).lines(prog).getModel();
}
Expand Down
Loading