Skip to content

Commit

Permalink
Fix #16822
Browse files Browse the repository at this point in the history
- Ignore synthetic local private
- Update test suit
  • Loading branch information
PaulCoral committed Feb 15, 2023
1 parent 006e2e4 commit 4a7fd65
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
19 changes: 10 additions & 9 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class CheckUnused extends MiniPhase:
ctx

override def prepareForIdent(tree: tpd.Ident)(using Context): Context =
if tree.symbol.exists then
if tree.symbol.exists then
_key.unusedDataApply(_.registerUsed(tree.symbol, Some(tree.name)))
else if tree.hasType then
_key.unusedDataApply(_.registerUsed(tree.tpe.classSymbol, Some(tree.name)))
Expand All @@ -103,7 +103,7 @@ class CheckUnused extends MiniPhase:
override def prepareForValDef(tree: tpd.ValDef)(using Context): Context =
_key.unusedDataApply{ud =>
// do not register the ValDef generated for `object`
if !tree.symbol.is(Module) then
if !tree.symbol.is(Module) then
ud.registerDef(tree)
ud.addIgnoredUsage(tree.symbol)
}
Expand Down Expand Up @@ -335,7 +335,7 @@ object CheckUnused:
* The optional name will be used to target the right import
* as the same element can be imported with different renaming
*/
def registerUsed(sym: Symbol, name: Option[Name])(using Context): Unit =
def registerUsed(sym: Symbol, name: Option[Name])(using Context): Unit =
if !isConstructorOfSynth(sym) && !doNotRegister(sym) then
if sym.isConstructor && sym.exists then
registerUsed(sym.owner, None) // constructor are "implicitly" imported with the class
Expand Down Expand Up @@ -371,7 +371,7 @@ object CheckUnused:
implicitParamInScope += memDef
else
explicitParamInScope += memDef
else if currScopeType.top == ScopeType.Local then
else if currScopeType.top == ScopeType.Local then
localDefInScope += memDef
else if memDef.shouldReportPrivateDef then
privateDefInScope += memDef
Expand Down Expand Up @@ -578,10 +578,10 @@ object CheckUnused:
else
false

private def usedDefContains(using Context): Boolean =
private def usedDefContains(using Context): Boolean =
sym.everySymbol.exists(usedDef.apply)

private def everySymbol(using Context): List[Symbol] =
private def everySymbol(using Context): List[Symbol] =
List(sym, sym.companionClass, sym.companionModule, sym.moduleClass).filter(_.exists)

end extension
Expand Down Expand Up @@ -614,10 +614,11 @@ object CheckUnused:
private def isValidParam(using Context): Boolean =
val sym = memDef.symbol
(sym.is(Param) || sym.isAllOf(PrivateParamAccessor | Local, butNot = CaseAccessor)) &&
!isSyntheticMainParam(sym) &&
!sym.shouldNotReportParamOwner
!isSyntheticMainParam(sym) &&
!sym.shouldNotReportParamOwner &&
(if sym.exists then !sym.owner.isAllOf(Synthetic | PrivateLocal) else true)

private def shouldReportPrivateDef(using Context): Boolean =
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)
Expand Down
23 changes: 17 additions & 6 deletions tests/neg-custom-args/fatal-warnings/i15503i.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ package foo.test.companionprivate:
package foo.test.i16678:
def foo(func: Int => String, value: Int): String = func(value) // OK

def run =
def run =
println(foo(number => number.toString, value = 5)) // OK
println(foo(number => "<number>", value = 5)) // error
println(foo(func = number => "<number>", value = 5)) // error
println(foo(func = number => number.toString, value = 5)) // OK
println(foo(func = _.toString, value = 5)) // OK

package foo.test.possibleclasses:
case class AllCaseClass(
k: Int, // OK
Expand All @@ -93,7 +93,7 @@ package foo.test.possibleclasses:
s: Int, // error /* But not these */
val t: Int, // OK
private val z: Int // error
)
)

case class AllCaseUsed(
k: Int, // OK
Expand All @@ -113,7 +113,7 @@ package foo.test.possibleclasses:
s: Int, // error
val t: Int, // OK
private val z: Int // error
)
)

class AllUsed(
k: Int, // OK
Expand All @@ -124,10 +124,21 @@ package foo.test.possibleclasses:
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
def make(i: Int): Option[PositiveNumber] = //OK
Option.when(i >= 0)(PositiveNumber(i)) // OK

package foo.test.i16822:
enum ExampleEnum {
case Build(context: String) // OK
case List // OK
}

def demo = {
val x = ExampleEnum.List // OK
println(x) // OK
}

0 comments on commit 4a7fd65

Please sign in to comment.