Skip to content

Change private local inference to allow inferring private local for private vals constructor parameters #23378

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,9 @@ object SymDenotations {
( !this.is(Local)
|| isAccessPrivilegedThisType(pre)
|| canBeLocal(name, flags)
&& {
!symbol.defTree.hasAttachment(typer.Typer.OriginallyPrivateLocal)
}
&& {
resetFlag(Local)
true
Expand Down Expand Up @@ -2658,11 +2661,9 @@ object SymDenotations {
* This holds for all symbols except
* - constructors, since they can never be referred to as members of their
* own, fully elaborated `this`.
* - parameters and parameter accessors, since their Local status is already
* determined by whether they have a `val` or `var` or not.
*/
def canBeLocal(name: Name, flags: FlagSet)(using Context) =
!name.isConstructorName && !flags.is(Param) && !flags.is(ParamAccessor)
!name.isConstructorName

/** Factory method for SymDenotion creation. All creations
* should be done via this method.
Expand Down
7 changes: 7 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ object Typer {
/** Tree adaptation lost fidelity; this attachment preserves the original tree. */
val AdaptedTree = new Property.StickyKey[tpd.Tree]

/** An attachment on a tree that was originally private local
*/
val OriginallyPrivateLocal = new Property.Key[Unit]

/** An attachment on a Select node with an `apply` field indicating that the `apply`
* was inserted by the Typer.
*/
Expand Down Expand Up @@ -3039,6 +3043,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
report.error(em"Cannot return repeated parameter type ${sym.info.finalResultType}", sym.srcPos)
if !sym.is(Module) && !sym.isConstructor && sym.info.finalResultType.isErasedClass then
sym.setFlag(Erased)
// This is probably too late. Do this in Namer?
if mdef.isInstanceOf[ValDef] && sym.isAllOf(PrivateLocal) then
mdef.putAttachment(OriginallyPrivateLocal, ())
mdef.ensureHasSym(sym)
mdef.setDefTree

Expand Down
4 changes: 0 additions & 4 deletions tests/neg/i22620.scala

This file was deleted.

4 changes: 4 additions & 0 deletions tests/pos/i22620.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import scala.collection.mutable.ArrayBuffer

class PrivateTest[-M](private val v: ArrayBuffer[M])

11 changes: 11 additions & 0 deletions tests/pos/private-local-override.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sealed abstract class Tree[+A](
final val key: A
)
final class RedTree[+A](key: A) extends Tree[A](key)
final class BlackTree[+A](key: A) extends Tree[A](key)
object RedTree {
def unapply[A](t: RedTree[A]) = Some((t.key))
}
object BlackTree {
def unapply[A](t: BlackTree[A]) = Some((t.key))
}
Loading