Skip to content

Commit d2fd229

Browse files
committed
Exclude named given from synthetics (when name starts with given_)
1 parent d1a0eac commit d2fd229

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)
@@ -334,24 +334,6 @@ class ExtractSemanticDB extends Phase:
334334
if !sym.is(Package) then
335335
registerSymbol(sym, symkinds)
336336

337-
private def namePresentInSource(sym: Symbol, span: Span, source:SourceFile)(using Context): Boolean =
338-
if !span.exists then false
339-
else
340-
val content = source.content()
341-
val (start, end) =
342-
if content.lift(span.end - 1).exists(_ == '`') then
343-
(span.start + 1, span.end - 1)
344-
else (span.start, span.end)
345-
val nameInSource = content.slice(start, end).mkString
346-
// for secondary constructors `this`
347-
if sym.isConstructor && nameInSource == nme.THISkw.toString then
348-
true
349-
else
350-
val target =
351-
if sym.isPackageObject then sym.owner
352-
else sym
353-
nameInSource == target.name.stripModuleClassSuffix.lastPart.toString
354-
355337
private def spanOfSymbol(sym: Symbol, span: Span, treeSource: SourceFile)(using Context): Span =
356338
val contents = if treeSource.exists then treeSource.content() else Array.empty[Char]
357339
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
}
@@ -230,12 +248,6 @@ object Scala3:
230248
def isSyntheticWithIdent(using Context): Boolean =
231249
sym.is(Synthetic) && !sym.isAnonymous && !sym.name.isEmptyNumbered
232250

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

499511
end IdentifierOrdering
500512

513+
/** Check if the symbol is invented by Desugar.inventGivenOrExtensionName
514+
* return true if the symbol is defined as `given Int = ...` and name is invented as "given_Int"
515+
*/
516+
def isInventedGiven(tree: tpd.Tree)(using Context): Boolean =
517+
tree match
518+
case tree: tpd.ValDef =>
519+
val sym = tree.symbol
520+
sym.is(Given) &&
521+
sym.name.startsWith("given_") &&
522+
!namePresentInSource(sym, tree.nameSpan, tree.source)
523+
case _ => false
524+
501525
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
@@ -1705,7 +1705,7 @@ Text => empty
17051705
Language => Scala
17061706
Symbols => 45 entries
17071707
Occurrences => 61 entries
1708-
Synthetics => 7 entries
1708+
Synthetics => 5 entries
17091709

17101710
Symbols:
17111711
givens/InventedNames$package. => final package object givens extends Object { self: givens.type => +24 decls }
@@ -1820,8 +1820,6 @@ Occurrences:
18201820
Synthetics:
18211821
[14:0..14:20):given String = "str" => given_String
18221822
[15:13..15:16):Int => x$1
1823-
[17:0..17:28):given given_Char: Char = '?' => given_Char
1824-
[18:0..18:32):given `given_Float`: Float = 3.0 => given_Float
18251823
[24:13..24:14):X => x$1
18261824
[34:8..34:20):given_Double => *(intValue)
18271825
[40:8..40:15):given_Y => *(given_X)

0 commit comments

Comments
 (0)