Skip to content

Commit 66af3ca

Browse files
committed
Complete receiver-aware field read diagnostics
1 parent e1d95d9 commit 66af3ca

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,8 +1760,7 @@ private void checkUninitializedVars(FunctionLike f) {
17601760
* cheap, local check: it does not attempt interprocedural or path-sensitive reasoning.
17611761
*/
17621762
private void checkPotentiallyUninitializedClassFields(FunctionLike function) {
1763-
ClassOrModule owner = function.attrNearestClassOrModule();
1764-
if (owner == null || function instanceof OnDestroyDef) {
1763+
if (function instanceof OnDestroyDef) {
17651764
return;
17661765
}
17671766

@@ -1779,7 +1778,7 @@ private void checkField(NameRef access) {
17791778
return;
17801779
}
17811780
if (!(field.getInitialExpr() instanceof NoExpr)
1782-
|| (!isInNestedClosure(access) && isCurrentInstanceAccess(access)
1781+
|| (isCurrentInstanceAccess(access)
17831782
&& writtenFields.contains(field))
17841783
|| (!(function instanceof ConstructorDef) && hasGuaranteedConstructorAssignment(field))
17851784
|| (delegatedConstructorCall != null && !access.isSubtreeOf(delegatedConstructorCall)

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,43 @@ public void doesNotWarnForExplicitReceiverWithGuaranteedConstructorAssignment()
278278
result.getGui().getWarningList().toString());
279279
}
280280

281+
@Test
282+
public void warnsForUninitializedFieldReadFromPackageFunction() {
283+
test()
284+
.setStopOnFirstError(false)
285+
.executeProg(false)
286+
.expectWarning("no explicit initializer and is not definitely assigned")
287+
.lines(
288+
"package Test",
289+
"class Counter",
290+
" int value",
291+
"function read(Counter counter) returns int",
292+
" return counter.value"
293+
);
294+
}
295+
296+
@Test
297+
public void doesNotWarnForClosureReadAfterPriorAssignment() {
298+
CompilationResult result = test()
299+
.setStopOnFirstError(false)
300+
.executeProg(false)
301+
.lines(
302+
"package Test",
303+
"interface Reader",
304+
" function read() returns int",
305+
"function consume(Reader reader)",
306+
"class Counter",
307+
" int value",
308+
" construct()",
309+
" value = 1",
310+
" consume(() -> value)"
311+
);
312+
313+
assertFalse(result.getGui().getWarningList().stream()
314+
.anyMatch(w -> w.getMessage().contains("no explicit initializer and is not definitely assigned")),
315+
result.getGui().getWarningList().toString());
316+
}
317+
281318
@Test
282319
public void classes1() throws IOException {
283320
testAssertOkFile(new File(TEST_DIR + "Classes_1.wurst"), true);

0 commit comments

Comments
 (0)