Skip to content

Check protected parent constructor accesses only occurs in child constructors#25511

Merged
bracevac merged 1 commit into
scala:mainfrom
jmesyou:github/issue/25542
Mar 13, 2026
Merged

Check protected parent constructor accesses only occurs in child constructors#25511
bracevac merged 1 commit into
scala:mainfrom
jmesyou:github/issue/25542

Conversation

@jmesyou

@jmesyou jmesyou commented Mar 12, 2026

Copy link
Copy Markdown
Contributor

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.

@jmesyou
jmesyou force-pushed the github/issue/25542 branch from 13ab597 to 47017d3 Compare March 13, 2026 02:20
…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
jmesyou force-pushed the github/issue/25542 branch from 53d5cfe to 1a3f693 Compare March 13, 2026 02:37
@bracevac
bracevac merged commit 230925d into scala:main Mar 13, 2026
64 checks passed
pre.termSymbol.isPackageObject && accessWithin(pre.termSymbol.owner)
else
else {
val isConstructorAccessOK = isConstructor && ctx.owner.isConstructor

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Right. It should've been ctx.owner.isPrimaryConstructor then.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@jmesyou would you like to take a look?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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]
@WojciechMazur WojciechMazur added this to the 3.8.4 milestone Mar 31, 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.

Compiler incorrectly permits access to protected constructor

5 participants