Skip to content

Commit 40ca110

Browse files
committedMar 10, 2025·
Lazy val def member is pattern var
1 parent 0bbd04f commit 40ca110

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed
 

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ import config.Feature.{sourceVersion, migrateTo3, enabled, betterForsEnabled}
1515
import config.SourceVersion.*
1616
import collection.mutable
1717
import reporting.*
18-
import annotation.constructorOnly
1918
import printing.Formatting.hl
2019
import config.Printers
2120
import parsing.Parsers
2221

23-
import scala.annotation.internal.sharable
24-
import scala.annotation.threadUnsafe
22+
import scala.annotation.{unchecked as _, *}, internal.sharable
2523

2624
object desugar {
2725
import untpd.*
@@ -1546,6 +1544,7 @@ object desugar {
15461544
DefDef(named.name.asTermName, Nil, tpt, selector(n))
15471545
.withMods(mods &~ Lazy)
15481546
.withSpan(named.span)
1547+
.withAttachment(PatternVar, ())
15491548
else
15501549
valDef(
15511550
ValDef(named.name.asTermName, tpt, selector(n))
@@ -1942,6 +1941,7 @@ object desugar {
19421941
mayNeedSetter
19431942
}
19441943

1944+
@unused
19451945
private def derivedDefDef(original: Tree, named: NameTree, tpt: Tree, rhs: Tree, mods: Modifiers)(implicit src: SourceFile) =
19461946
DefDef(named.name.asTermName, Nil, tpt, rhs)
19471947
.withMods(mods)

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,11 @@ object CheckUnused:
464464
case tree: Bind =>
465465
if !tree.name.isInstanceOf[DerivedName] && !tree.name.is(WildcardParamName) && !tree.hasAttachment(NoWarn) then
466466
pats.addOne((tree.symbol, tree.namePos))
467-
case tree: ValDef if tree.hasAttachment(PatternVar) =>
468-
if !tree.name.isInstanceOf[DerivedName] then
469-
pats.addOne((tree.symbol, tree.namePos))
470467
case tree: NamedDefTree =>
471-
if (tree.symbol ne NoSymbol)
468+
if tree.hasAttachment(PatternVar) then
469+
if !tree.name.isInstanceOf[DerivedName] then
470+
pats.addOne((tree.symbol, tree.namePos))
471+
else if (tree.symbol ne NoSymbol)
472472
&& !tree.name.isWildcard
473473
&& !tree.hasAttachment(NoWarn)
474474
&& !tree.symbol.is(ModuleVal) // track only the ModuleClass using the object symbol, with correct namePos

‎tests/warn/i15503d.scala

+6
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)
Please sign in to comment.