Global object init checker gives warning when accessing an object before its super constructor finishes#24349
Merged
olhotak merged 3 commits intoscala:mainfrom Nov 13, 2025
Conversation
3385505 to
4737d4f
Compare
Contributor
Author
4737d4f to
edf760c
Compare
liufengyun
reviewed
Nov 8, 2025
| ObjectRef(classSym) | ||
| val obj = ObjectRef(classSym) | ||
| obj.setAfterSuperCall() | ||
| obj |
Contributor
There was a problem hiding this comment.
I'm not sure about obj.setAfterSuperCall here.
Contributor
There was a problem hiding this comment.
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 |
Contributor
There was a problem hiding this comment.
I was thinking whether the check should be moved to accessObject?
edf760c to
0cb7497
Compare
0cb7497 to
cdcf91a
Compare
liufengyun
approved these changes
Nov 12, 2025
Contributor
liufengyun
left a comment
There was a problem hiding this comment.
LGTM, thank you @EnzeXing !
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Accessing an object before its super constructor finishes returns
nulland may lead to exceptions when constructing the object (see #24201). This PR lets global object init checker gives warning for this case