Skip to content

Commit 85f545b

Browse files
committed
Fix flags used to distinguish case accessors
- Fix flag confusion between (case)accessor and (potentially) generated method in classes - Update test suits
1 parent c4d63cc commit 85f545b

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

compiler/src/dotty/tools/dotc/transform/CheckUnused.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ object CheckUnused:
367367
explicitParamInScope += memDef
368368
else if currScopeType.top == ScopeType.Local then
369369
localDefInScope += memDef
370-
else if currScopeType.top == ScopeType.Template && memDef.symbol.is(Private, butNot = SelfName) then
370+
else if memDef.shouldReportPrivateDef then
371371
privateDefInScope += memDef
372372

373373
/** Register pattern variable */
@@ -589,10 +589,13 @@ object CheckUnused:
589589

590590
private def isValidParam(using Context): Boolean =
591591
val sym = memDef.symbol
592-
(sym.is(Param) || sym.isAllOf(PrivateParamAccessor)) &&
592+
(sym.is(Param) || sym.isAllOf(PrivateParamAccessor | Local, butNot = CaseAccessor)) &&
593593
!isSyntheticMainParam(sym) &&
594594
!sym.shouldNotReportParamOwner
595595

596+
private def shouldReportPrivateDef(using Context): Boolean =
597+
currScopeType.top == ScopeType.Template && !memDef.symbol.isConstructor && memDef.symbol.is(Private, butNot = SelfName | Synthetic | CaseAccessor)
598+
596599
extension (imp: tpd.Import)
597600
/** Enum generate an import for its cases (but outside them), which should be ignored */
598601
def isGeneratedByEnum(using Context): Boolean =

tests/neg-custom-args/fatal-warnings/i15503c.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@ class A:
1717

1818
val x = 1 // OK
1919
def y = 2 // OK
20-
def z = g // OK
20+
def z = g // OK
21+
22+
package foo.test.contructors:
23+
case class A private (x:Int) // OK
24+
class B private (val x: Int) // OK
25+
class C private (private val x: Int) // error
26+
class D private (private val x: Int): // OK
27+
def y = x

tests/neg-custom-args/fatal-warnings/i15503i.scala

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,45 @@ package foo.test.companionprivate:
7373

7474
object A:
7575
private def b = c // OK
76-
def c = List(1,2,3) // OK
76+
def c = List(1,2,3) // OK
77+
78+
package foo.test.possibleclasses:
79+
case class AllCaseClass(
80+
k: Int, // OK
81+
private val y: Int // OK /* Kept as it can be taken from pattern */
82+
)(
83+
s: Int, // error /* But not these */
84+
val t: Int, // OK
85+
private val z: Int // error
86+
)
87+
88+
case class AllCaseUsed(
89+
k: Int, // OK
90+
private val y: Int // OK
91+
)(
92+
s: Int, // OK
93+
val t: Int, // OK
94+
private val z: Int // OK
95+
) {
96+
def a = k + y + s + t + z
97+
}
98+
99+
class AllClass(
100+
k: Int, // error
101+
private val y: Int // error
102+
)(
103+
s: Int, // error
104+
val t: Int, // OK
105+
private val z: Int // error
106+
)
107+
108+
class AllUsed(
109+
k: Int, // OK
110+
private val y: Int // OK
111+
)(
112+
s: Int, // OK
113+
val t: Int, // OK
114+
private val z: Int // OK
115+
) {
116+
def a = k + y + s + t + z
117+
}

0 commit comments

Comments
 (0)