Skip to content

Commit 3a9fbd0

Browse files
committed
Exclude named given from synthetics (when name starts with given_)
1 parent 28c3846 commit 3a9fbd0

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
@@ -171,11 +171,11 @@ class ExtractSemanticDB extends Phase:
171171
// ignore rhs
172172

173173
// `given Int` (syntax sugar of `given given_Int: Int`)
174-
case tree: ValDef if tree.symbol.isInventedGiven =>
175-
synth.tryFindSynthetic(tree).foreach { synth =>
176-
synthetics += synth
177-
}
178-
traverse(tree.tpt)
174+
case tree: ValDef if isInventedGiven(tree) =>
175+
synth.tryFindSynthetic(tree).foreach { synth =>
176+
synthetics += synth
177+
}
178+
traverse(tree.tpt)
179179
case PatternValDef(pat, rhs) =>
180180
traverse(rhs)
181181
PatternValDef.collectPats(pat).foreach(traverse)
@@ -349,24 +349,6 @@ class ExtractSemanticDB extends Phase:
349349
if !sym.is(Package) then
350350
registerSymbol(sym, symkinds)
351351

352-
private def namePresentInSource(sym: Symbol, span: Span, source:SourceFile)(using Context): Boolean =
353-
if !span.exists then false
354-
else
355-
val content = source.content()
356-
val (start, end) =
357-
if content.lift(span.end - 1).exists(_ == '`') then
358-
(span.start + 1, span.end - 1)
359-
else (span.start, span.end)
360-
val nameInSource = content.slice(start, end).mkString
361-
// for secondary constructors `this`
362-
if sym.isConstructor && nameInSource == nme.THISkw.toString then
363-
true
364-
else
365-
val target =
366-
if sym.isPackageObject then sym.owner
367-
else sym
368-
nameInSource == target.name.stripModuleClassSuffix.lastPart.toString
369-
370352
private def spanOfSymbol(sym: Symbol, span: Span, treeSource: SourceFile)(using Context): Span =
371353
val contents = if treeSource.exists then treeSource.content() else Array.empty[Char]
372354
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)