Skip to content

Commit 223b2de

Browse files
committed
Infer Local for val constructor params
1 parent fbbae09 commit 223b2de

File tree

8 files changed

+17
-11
lines changed

8 files changed

+17
-11
lines changed

compiler/src/dotty/tools/dotc/core/Flags.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,8 @@ object Flags {
404404
* an existentially bound symbol (Scala 2.x only) */
405405
val (Scala2SpecialFlags @ _, Scala2SuperAccessor @ _, Scala2Existential @ _) = newFlags(55, "<existential>")
406406

407-
/** Children were queried on this class */
408-
val (_, _, ChildrenQueried @ _) = newFlags(56, "<children-queried>")
407+
/** A class parameter that is not a `val` / Children were queried on this class */
408+
val (_, NotAField @ _, ChildrenQueried @ _) = newFlags(56, "<not-a-field>", "<children-queried>")
409409

410410
/** A module variable (Scala 2.x only) / a capture-checked class
411411
* (Scala2ModuleVar is re-used as a flag for private parameter accessors in Recheck)

compiler/src/dotty/tools/dotc/core/SymDenotations.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2694,7 +2694,7 @@ object SymDenotations {
26942694
* determined by whether they have a `val` or `var` or not.
26952695
*/
26962696
def canBeLocal(name: Name, flags: FlagSet)(using Context) =
2697-
!name.isConstructorName && !flags.is(Param) && !flags.is(ParamAccessor)
2697+
!name.isConstructorName && !flags.is(NotAField)
26982698

26992699
/** Factory method for SymDenotion creation. All creations
27002700
* should be done via this method.

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3679,7 +3679,7 @@ object Parsers {
36793679
if (!(mods.flags &~ (ParamAccessor | Inline | Erased | impliedMods.flags)).isEmpty)
36803680
syntaxError(em"`val` or `var` expected")
36813681
if firstClause && paramOwner == ParamOwner.CaseClass then mods
3682-
else mods | PrivateLocal
3682+
else mods | PrivateLocal | NotAField
36833683
else {
36843684
if (isIdent(nme.inline) && in.isSoftModifierInParamModifierPosition)
36853685
mods = addModifier(mods)

tests/neg/i22620.scala

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/pos/i22620.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import scala.collection.mutable.ArrayBuffer
2+
3+
class PrivateTest[-M](private val v: ArrayBuffer[M])

tests/pos/i22620b.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
sealed abstract class Tree[+A](
2+
final val key: A
3+
)
4+
final class RedTree[+A](key: A) extends Tree[A](key)
5+
object RedTree {
6+
def unapply[A](t: RedTree[A]) = Some((t.key))
7+
}

tests/semanticdb/metac.expect

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ classes/C6#`<init>`().(x) => param x: Int
626626
classes/C6#copy$default$1(). => method copy$default$1 => Int @uncheckedVariance
627627
classes/C6#copy(). => method copy (param x: Int): C6
628628
classes/C6#copy().(x) => param x: Int
629-
classes/C6#x. => private val method x Int
629+
classes/C6#x. => private[this] val method x Int
630630
classes/C6. => final object C6 extends Object { self: C6.type => +4 decls }
631631
classes/C6.apply(). => method apply (param x: Int): C6
632632
classes/C6.apply().(x) => param x: Int
@@ -1957,7 +1957,7 @@ example/ImplicitConversion.newAny2stringadd#`+`(). => method + (param other: Str
19571957
example/ImplicitConversion.newAny2stringadd#`+`().(other) => param other: String
19581958
example/ImplicitConversion.newAny2stringadd#`<init>`(). => primary ctor <init> [typeparam A ](param self: A): newAny2stringadd[A]
19591959
example/ImplicitConversion.newAny2stringadd#`<init>`().(self) => param self: A
1960-
example/ImplicitConversion.newAny2stringadd#self. => private val method self A
1960+
example/ImplicitConversion.newAny2stringadd#self. => private[this] val method self A
19611961
example/ImplicitConversion.newAny2stringadd(). => final implicit method newAny2stringadd [typeparam A ](param self: A): newAny2stringadd[A]
19621962
example/ImplicitConversion.newAny2stringadd().(self) => param self: A
19631963
example/ImplicitConversion.newAny2stringadd().[A] => typeparam A

tests/warn/i15503c.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ package foo.test.constructors:
3636
case class A private (x: Int) // OK
3737
class B private (val x: Int) // OK
3838
object B { def default = B(42) }
39-
class C private (private val xy: Int) // warn
39+
class C private (private val xy: Int)
4040
object C { def default = C(42) }
4141
class D private (private val x: Int): // OK
4242
def y = x

0 commit comments

Comments
 (0)