Skip to content

Commit

Permalink
Fix flags used to distinguish case accessors
Browse files Browse the repository at this point in the history
- Fix flag confusion between (case)accessor and (potentially) generated
  method in classes
- Update test suits. Add test from scala#16675
  • Loading branch information
PaulCoral authored and little-inferno committed Jan 25, 2023
1 parent eacb5f9 commit 7a66a33
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
7 changes: 5 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ object CheckUnused:
explicitParamInScope += memDef
else if currScopeType.top == ScopeType.Local then
localDefInScope += memDef
else if currScopeType.top == ScopeType.Template && memDef.symbol.is(Private, butNot = SelfName) then
else if memDef.shouldReportPrivateDef then
privateDefInScope += memDef

/** Register pattern variable */
Expand Down Expand Up @@ -589,10 +589,13 @@ object CheckUnused:

private def isValidParam(using Context): Boolean =
val sym = memDef.symbol
(sym.is(Param) || sym.isAllOf(PrivateParamAccessor)) &&
(sym.is(Param) || sym.isAllOf(PrivateParamAccessor | Local, butNot = CaseAccessor)) &&
!isSyntheticMainParam(sym) &&
!sym.shouldNotReportParamOwner

private def shouldReportPrivateDef(using Context): Boolean =
currScopeType.top == ScopeType.Template && !memDef.symbol.isConstructor && memDef.symbol.is(Private, butNot = SelfName | Synthetic | CaseAccessor)

extension (imp: tpd.Import)
/** Enum generate an import for its cases (but outside them), which should be ignored */
def isGeneratedByEnum(using Context): Boolean =
Expand Down
9 changes: 8 additions & 1 deletion tests/neg-custom-args/fatal-warnings/i15503c.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ class A:

val x = 1 // OK
def y = 2 // OK
def z = g // OK
def z = g // OK

package foo.test.contructors:
case class A private (x:Int) // OK
class B private (val x: Int) // OK
class C private (private val x: Int) // error
class D private (private val x: Int): // OK
def y = x
49 changes: 48 additions & 1 deletion tests/neg-custom-args/fatal-warnings/i15503i.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,51 @@ package foo.test.companionprivate:

object A:
private def b = c // OK
def c = List(1,2,3) // OK
def c = List(1,2,3) // OK

package foo.test.possibleclasses:
case class AllCaseClass(
k: Int, // OK
private val y: Int // OK /* Kept as it can be taken from pattern */
)(
s: Int, // error /* But not these */
val t: Int, // OK
private val z: Int // error
)

case class AllCaseUsed(
k: Int, // OK
private val y: Int // OK
)(
s: Int, // OK
val t: Int, // OK
private val z: Int // OK
) {
def a = k + y + s + t + z
}

class AllClass(
k: Int, // error
private val y: Int // error
)(
s: Int, // error
val t: Int, // OK
private val z: Int // error
)

class AllUsed(
k: Int, // OK
private val y: Int // OK
)(
s: Int, // OK
val t: Int, // OK
private val z: Int // OK
) {
def a = k + y + s + t + z
}

package foo.test.from.i16675:
case class PositiveNumber private (i: Int) // OK
object PositiveNumber:
def make(i: Int): Option[PositiveNumber] = //OK
Option.when(i >= 0)(PositiveNumber(i)) // OK

0 comments on commit 7a66a33

Please sign in to comment.