Skip to content

Commit 3768c04

Browse files
committed
Exclude named given from synthetics (when name starts with given_)
1 parent 4ddbd46 commit 3768c04

File tree

4 files changed

+37
-33
lines changed

4 files changed

+37
-33
lines changed

compiler/src/dotty/tools/dotc/semanticdb/ExtractSemanticDB.scala

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,11 @@ class ExtractSemanticDB extends Phase:
168168
// ignore rhs
169169

170170
// `given Int` (syntax sugar of `given given_Int: Int`)
171-
case tree: ValDef if tree.symbol.isInventedGiven =>
172-
synth.tryFindSynthetic(tree).foreach { synth =>
173-
synthetics += synth
174-
}
175-
traverse(tree.tpt)
171+
case tree: ValDef if isInventedGiven(tree) =>
172+
synth.tryFindSynthetic(tree).foreach { synth =>
173+
synthetics += synth
174+
}
175+
traverse(tree.tpt)
176176
case PatternValDef(pat, rhs) =>
177177
traverse(rhs)
178178
PatternValDef.collectPats(pat).foreach(traverse)
@@ -346,24 +346,6 @@ class ExtractSemanticDB extends Phase:
346346
if !sym.is(Package) then
347347
registerSymbol(sym, symkinds)
348348

349-
private def namePresentInSource(sym: Symbol, span: Span, source:SourceFile)(using Context): Boolean =
350-
if !span.exists then false
351-
else
352-
val content = source.content()
353-
val (start, end) =
354-
if content.lift(span.end - 1).exists(_ == '`') then
355-
(span.start + 1, span.end - 1)
356-
else (span.start, span.end)
357-
val nameInSource = content.slice(start, end).mkString
358-
// for secondary constructors `this`
359-
if sym.isConstructor && nameInSource == nme.THISkw.toString then
360-
true
361-
else
362-
val target =
363-
if sym.isPackageObject then sym.owner
364-
else sym
365-
nameInSource == target.name.stripModuleClassSuffix.lastPart.toString
366-
367349
private def spanOfSymbol(sym: Symbol, span: Span, treeSource: SourceFile)(using Context): Span =
368350
val contents = if treeSource.exists then treeSource.content() else Array.empty[Char]
369351
val idx = contents.indexOfSlice(sym.name.show, span.start)

compiler/src/dotty/tools/dotc/semanticdb/Scala3.scala

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ object Scala3:
3636
val (endLine, endCol) = lineCol(span.end)
3737
Some(Range(startLine, startCol, endLine, endCol))
3838

39+
def namePresentInSource(sym: Symbol, span: Span, source:SourceFile)(using Context): Boolean =
40+
if !span.exists then false
41+
else
42+
val content = source.content()
43+
val (start, end) =
44+
if content.lift(span.end - 1).exists(_ == '`') then
45+
(span.start + 1, span.end - 1)
46+
else (span.start, span.end)
47+
val nameInSource = content.slice(start, end).mkString
48+
// for secondary constructors `this`
49+
if sym.isConstructor && nameInSource == nme.THISkw.toString then
50+
true
51+
else
52+
val target =
53+
if sym.isPackageObject then sym.owner
54+
else sym
55+
nameInSource == target.name.stripModuleClassSuffix.lastPart.toString
56+
3957
sealed trait FakeSymbol {
4058
private[Scala3] var sname: Option[String] = None
4159
}
@@ -231,12 +249,6 @@ object Scala3:
231249
def isSyntheticWithIdent(using Context): Boolean =
232250
sym.is(Synthetic) && !sym.isAnonymous && !sym.name.isEmptyNumbered
233251

234-
/** Check if the symbol is invented by Desugar.inventGivenOrExtensionName
235-
* return true if the symbol is defined as `given Int = ...` and name is invented as "given_Int"
236-
*/
237-
def isInventedGiven(using Context): Boolean =
238-
sym.is(Given) && sym.name.startsWith("given_")
239-
240252
/** The semanticdb name of the given symbol */
241253
def symbolName(using builder: SemanticSymbolBuilder)(using Context): String =
242254
builder.symbolName(sym)
@@ -502,4 +514,16 @@ object Scala3:
502514

503515
end IdentifierOrdering
504516

517+
/** Check if the symbol is invented by Desugar.inventGivenOrExtensionName
518+
* return true if the symbol is defined as `given Int = ...` and name is invented as "given_Int"
519+
*/
520+
def isInventedGiven(tree: tpd.Tree)(using Context): Boolean =
521+
tree match
522+
case tree: tpd.ValDef =>
523+
val sym = tree.symbol
524+
sym.is(Given) &&
525+
sym.name.startsWith("given_") &&
526+
!namePresentInSource(sym, tree.nameSpan, tree.source)
527+
case _ => false
528+
505529
end Scala3

compiler/src/dotty/tools/dotc/semanticdb/SyntheticsExtractor.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class SyntheticsExtractor:
1515
extension (synth: s.Synthetic)
1616
def toOpt: Some[s.Synthetic] = Some(synth)
1717

18-
if tree.span.isSynthetic || tree.symbol.isInventedGiven then
18+
if tree.span.isSynthetic || isInventedGiven(tree) then
1919
tree match
2020
case tree: Apply if isForSynthetic(tree) =>
2121
None // not yet supported (for synthetics)

tests/semanticdb/metac.expect

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,7 +1711,7 @@ Text => empty
17111711
Language => Scala
17121712
Symbols => 45 entries
17131713
Occurrences => 61 entries
1714-
Synthetics => 7 entries
1714+
Synthetics => 5 entries
17151715

17161716
Symbols:
17171717
givens/InventedNames$package. => final package object givens extends Object { self: givens.type => +24 decls }
@@ -1826,8 +1826,6 @@ Occurrences:
18261826
Synthetics:
18271827
[14:0..14:20):given String = "str" => given_String
18281828
[15:13..15:16):Int => x$1
1829-
[17:0..17:28):given given_Char: Char = '?' => given_Char
1830-
[18:0..18:32):given `given_Float`: Float = 3.0 => given_Float
18311829
[24:13..24:14):X => x$1
18321830
[34:8..34:20):given_Double => *(intValue)
18331831
[40:8..40:15):given_Y => *(given_X)

0 commit comments

Comments
 (0)