-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Global object init checker gives warning when accessing an object before its super constructor finishes #24349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
olhotak
merged 3 commits into
scala:main
from
dotty-staging:fix-global-init-checker-object-call
Nov 13, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| class C(i: Int = 42, j: Int = 27) | ||
|
|
||
| object X extends C(j = X.foo()): // warn | ||
| def foo() = 5 | ||
|
|
||
| @main def test = println: | ||
| X |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| class C(j: Int) | ||
|
|
||
| object A: | ||
| def foo = X.k // warn | ||
|
|
||
| object X extends C(A.foo): | ||
| def k = 5 | ||
|
|
||
| @main def test = println: | ||
| X |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| class C(i: Int = 42, j: Int = 27) { | ||
| val f = X.foo() // warn | ||
| } | ||
|
|
||
| object X extends C(j = 5): | ||
| def foo() = 5 | ||
|
|
||
| @main def test = println: | ||
| X |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| -- Warning: tests/init-global/warn/global-cycle10.scala:13:14 ---------------------------------------------------------- | ||
| 13 | def foo() = new Inner // warn | ||
| | ^^^^^^^^^ | ||
| | Accessing module class O$ before the super constructor of the object finishes! Calling trace: | ||
| | ├── object O extends Base { // error [ global-cycle10.scala:7 ] | ||
| | │ ^ | ||
| | ├── abstract class Base { [ global-cycle10.scala:1 ] | ||
| | │ ^ | ||
| | ├── foo() [ global-cycle10.scala:4 ] | ||
| | │ ^^^^^ | ||
| | └── def foo() = new Inner // warn [ global-cycle10.scala:13 ] | ||
| | ^^^^^^^^^ | ||
| -- Warning: tests/init-global/warn/global-cycle10.scala:10:12 ---------------------------------------------------------- | ||
| 10 | println(msg) // warn | ||
| | ^^^ | ||
| | Accessing module class O$ before the super constructor of the object finishes! Calling trace: | ||
| | ├── object O extends Base { // error [ global-cycle10.scala:7 ] | ||
| | │ ^ | ||
| | ├── abstract class Base { [ global-cycle10.scala:1 ] | ||
| | │ ^ | ||
| | ├── foo() [ global-cycle10.scala:4 ] | ||
| | │ ^^^^^ | ||
| | ├── def foo() = new Inner // warn [ global-cycle10.scala:13 ] | ||
| | │ ^^^^^^^^^ | ||
| | ├── class Inner { [ global-cycle10.scala:9 ] | ||
| | │ ^ | ||
| | └── println(msg) // warn [ global-cycle10.scala:10 ] | ||
| | ^^^ |
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| class Delegate { | ||
| def foo() = f | ||
| val f: O.type = O | ||
| val f: O.type = O // warn | ||
| } | ||
|
|
||
| object O extends Delegate { | ||
|
|
||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| sealed abstract class OrderType(val reverse: OrderType) | ||
| case object Buy extends OrderType(Sell) // warn | ||
| case object Sell extends OrderType(Buy) | ||
| case object Sell extends OrderType(Buy) // warn |
Oops, something went wrong.
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.
There was a problem hiding this comment.
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.setAfterSuperCallhere.There was a problem hiding this comment.
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
setAfterSuperCallmakes sense to me.