Skip to content

Global object init checker gives warning when accessing an object before its super constructor finishes#24349

Merged
olhotak merged 3 commits intoscala:mainfrom
dotty-staging:fix-global-init-checker-object-call
Nov 13, 2025
Merged

Global object init checker gives warning when accessing an object before its super constructor finishes#24349
olhotak merged 3 commits intoscala:mainfrom
dotty-staging:fix-global-init-checker-object-call

Conversation

@EnzeXing
Copy link
Copy Markdown
Contributor

@EnzeXing EnzeXing commented Nov 6, 2025

Accessing an object before its super constructor finishes returns null and may lead to exceptions when constructing the object (see #24201). This PR lets global object init checker gives warning for this case

@EnzeXing EnzeXing force-pushed the fix-global-init-checker-object-call branch from 3385505 to 4737d4f Compare November 7, 2025 02:52
@EnzeXing
Copy link
Copy Markdown
Contributor Author

EnzeXing commented Nov 7, 2025

@olhotak @liufengyun

@EnzeXing EnzeXing force-pushed the fix-global-init-checker-object-call branch from 4737d4f to edf760c Compare November 7, 2025 03:09
ObjectRef(classSym)
val obj = ObjectRef(classSym)
obj.setAfterSuperCall()
obj
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about obj.setAfterSuperCall here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is when we don't have source, so we're not checking this object at all. Presumably in that case it should always be OK to access the object, so setAfterSuperCall makes sense to me.

UnknownValue
else if ref.isInstanceOf[ObjectRef] && !ref.asObjectRef.isAfterSuperCall then
report.warning("Calling " + target + " of object " + ref.klass + " before the super constructor of the object finishes! " + Trace.show, Trace.position)
Bottom
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking whether the check should be moved to accessObject?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so too.

@EnzeXing EnzeXing force-pushed the fix-global-init-checker-object-call branch from edf760c to 0cb7497 Compare November 11, 2025 21:35
@EnzeXing EnzeXing changed the title Global object init checker gives warning when calling object methods before super constructor finishes Global object init checker gives warning when accessing an object before its super constructor finishes Nov 11, 2025
@EnzeXing EnzeXing force-pushed the fix-global-init-checker-object-call branch from 0cb7497 to cdcf91a Compare November 12, 2025 02:57
Copy link
Copy Markdown
Contributor

@olhotak olhotak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Copy Markdown
Contributor

@liufengyun liufengyun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you @EnzeXing !

@olhotak olhotak merged commit 8b5d445 into scala:main Nov 13, 2025
53 checks passed
@olhotak olhotak deleted the fix-global-init-checker-object-call branch November 13, 2025 02:30
HarrisL2 pushed a commit to HarrisL2/scala3 that referenced this pull request Nov 18, 2025
…ore its super constructor finishes (scala#24349)

Accessing an object before its super constructor finishes returns `null`
and may lead to exceptions when constructing the object (see scala#24201).
This PR lets global object init checker gives warning for this case

---------

Co-authored-by: EnzeXing <enzexing12@gmail.com>
@WojciechMazur WojciechMazur added this to the 3.8.1 milestone Jan 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants