Skip to content

Commit 1230deb

Browse files
committed
Respect initialized receivers in constructors
1 parent 66af3ca commit 1230deb

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1780,7 +1780,8 @@ private void checkField(NameRef access) {
17801780
if (!(field.getInitialExpr() instanceof NoExpr)
17811781
|| (isCurrentInstanceAccess(access)
17821782
&& writtenFields.contains(field))
1783-
|| (!(function instanceof ConstructorDef) && hasGuaranteedConstructorAssignment(field))
1783+
|| ((!isCurrentInstanceAccess(access) || !(function instanceof ConstructorDef))
1784+
&& hasGuaranteedConstructorAssignment(field))
17841785
|| (delegatedConstructorCall != null && !access.isSubtreeOf(delegatedConstructorCall)
17851786
&& hasGuaranteedConstructorAssignment(field))
17861787
|| !warned.add(field)) {

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

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

281+
@Test
282+
public void doesNotWarnForInitializedExplicitReceiverInsideConstructor() {
283+
CompilationResult result = test()
284+
.setStopOnFirstError(false)
285+
.executeProg(false)
286+
.lines(
287+
"package Test",
288+
"class Source",
289+
" int value",
290+
" construct()",
291+
" value = 1",
292+
"class Holder",
293+
" construct(Source source)",
294+
" int copy = source.value"
295+
);
296+
297+
assertFalse(result.getGui().getWarningList().stream()
298+
.anyMatch(w -> w.getMessage().contains("no explicit initializer and is not definitely assigned")),
299+
result.getGui().getWarningList().toString());
300+
}
301+
281302
@Test
282303
public void warnsForUninitializedFieldReadFromPackageFunction() {
283304
test()

0 commit comments

Comments
 (0)