Check protected parent constructor accesses only occurs in child constructors#25511
Merged
Conversation
jmesyou
force-pushed
the
github/issue/25542
branch
from
March 13, 2026 02:20
13ab597 to
47017d3
Compare
…tructors Currently, protected parent class constructor calls that occur outside of their subclass constructors are not properly checked. So it possible to call a parent constructor in any method of subclass. This change checks that such accesses only occur inside subclass constructors. This fixes scala#25442.
jmesyou
force-pushed
the
github/issue/25542
branch
from
March 13, 2026 02:37
53d5cfe to
1a3f693
Compare
bracevac
approved these changes
Mar 13, 2026
lrytz
reviewed
Mar 13, 2026
| pre.termSymbol.isPackageObject && accessWithin(pre.termSymbol.owner) | ||
| else | ||
| else { | ||
| val isConstructorAccessOK = isConstructor && ctx.owner.isConstructor |
Member
There was a problem hiding this comment.
The following should also be rejected; is it?
class I protected (x: Int) {
protected def f = x
}
class M protected () extends I(42) {
def this(x: Int) = { this(); new I(x) } // error
}
Member
There was a problem hiding this comment.
Right. It should've been ctx.owner.isPrimaryConstructor then.
Member
There was a problem hiding this comment.
Playing with this some more, I think even checking for isPrimaryConstructor is not granular enough:
class A protected (x: Int)
class B protected (a: A) extends A(a.hashCode)
class C extends B(new A(1)) // should be error, but ok currently
tgodzik
pushed a commit
to scala/scala3-lts
that referenced
this pull request
Mar 31, 2026
…tructors (scala#25511) Fixes scala#25442. Currently, protected parent class constructor calls that occur outside of their subclass constructors are not properly checked. So it possible to call a parent constructor in any method of subclass. This change checks that such accesses only occur inside subclass constructors. No LLM-based tools were used. negative test case from issue included. Co-authored-by: James You <jyou@protonmail.com> [Cherry-picked 230925d]
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.
Fixes #25442.
Currently, protected parent class constructor calls that occur outside of their subclass constructors are not properly checked. So it possible to call a parent constructor in any method of subclass. This change checks that such accesses only occur inside subclass constructors.
No LLM-based tools were used.
negative test case from issue included.