Skip to content

Commit 9ea521a

Browse files
som-snytttgodzik
authored andcommitted
Lazy val def member is pattern var
1 parent 8900b86 commit 9ea521a

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import config.Feature.{sourceVersion, migrateTo3, enabled}
1414
import config.SourceVersion.*
1515
import collection.mutable.ListBuffer
1616
import reporting.*
17-
import annotation.constructorOnly
1817
import printing.Formatting.hl
1918
import config.Printers
2019

2120
import scala.annotation.internal.sharable
21+
import scala.annotation.{unchecked as _, *}
2222
import dotty.tools.dotc.util.SrcPos
2323

2424
object desugar {
@@ -1255,6 +1255,7 @@ object desugar {
12551255
DefDef(named.name.asTermName, Nil, tpt, selector(n))
12561256
.withMods(mods &~ Lazy)
12571257
.withSpan(named.span)
1258+
.withAttachment(PatternVar, ())
12581259
else
12591260
valDef(
12601261
ValDef(named.name.asTermName, tpt, selector(n))
@@ -1557,6 +1558,7 @@ object desugar {
15571558
mayNeedSetter
15581559
}
15591560

1561+
@unused
15601562
private def derivedDefDef(original: Tree, named: NameTree, tpt: Tree, rhs: Tree, mods: Modifiers)(implicit src: SourceFile) =
15611563
DefDef(named.name.asTermName, Nil, tpt, rhs)
15621564
.withMods(mods)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,11 @@ object CheckUnused:
455455
case tree: Bind =>
456456
if !tree.name.isInstanceOf[DerivedName] && !tree.name.is(WildcardParamName) && !tree.hasAttachment(NoWarn) then
457457
pats.addOne((tree.symbol, tree.namePos))
458-
case tree: ValDef if tree.hasAttachment(PatternVar) =>
459-
if !tree.name.isInstanceOf[DerivedName] then
460-
pats.addOne((tree.symbol, tree.namePos))
461458
case tree: NamedDefTree =>
462-
if (tree.symbol ne NoSymbol)
459+
if tree.hasAttachment(PatternVar) then
460+
if !tree.name.isInstanceOf[DerivedName] then
461+
pats.addOne((tree.symbol, tree.namePos))
462+
else if (tree.symbol ne NoSymbol)
463463
&& !tree.name.isWildcard
464464
&& !tree.hasAttachment(NoWarn)
465465
&& !tree.symbol.is(ModuleVal) // track only the ModuleClass using the object symbol, with correct namePos

tests/warn/i15503d.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,9 @@ object `mutable patvar in for`:
115115
class `unset var requires -Wunused`:
116116
private var i = 0 // no warn as we didn't ask for it
117117
def f = println(i)
118+
119+
class `i22743 lazy vals are defs`:
120+
def f: (Int, String) = (42, "hello, world")
121+
lazy val (i, s) = f // no warn because def is neither local nor private
122+
val (j, t) = f // existing no warn for val with attachment
123+
private lazy val (k, u) = f // warn // warn a warning so nice, they warn it twice

0 commit comments

Comments
 (0)