Skip to content

Commit af0e3ad

Browse files
rochalatgodzik
authored andcommitted
Add enum type param support in sourceSymbol
[Cherry-picked 62e3af5]
1 parent 19a0db6 commit af0e3ad

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

compiler/src/dotty/tools/dotc/core/Symbols.scala

+14-2
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,25 @@ object Symbols extends SymUtils {
323323
targets.match
324324
case (tp: NamedType) :: _ => tp.symbol.sourceSymbol
325325
case _ => this
326-
else if (denot.is(Synthetic)) {
326+
else if denot.is(Synthetic) then
327327
val linked = denot.linkedClass
328328
if (linked.exists && !linked.is(Synthetic))
329329
linked
330330
else
331331
denot.owner.sourceSymbol
332-
}
332+
else if (
333+
denot.is(TypeParam) &&
334+
denot.maybeOwner.maybeOwner.isAllOf(EnumCase) &&
335+
denot.maybeOwner.isPrimaryConstructor
336+
) then
337+
val enclosingEnumCase = denot.maybeOwner.maybeOwner
338+
val caseTypeParam = enclosingEnumCase.typeParams.find(_.name == denot.name)
339+
if caseTypeParam.exists(_.is(Synthetic)) then
340+
val enumClass = enclosingEnumCase.info.firstParent.typeSymbol
341+
val sourceTypeParam = enumClass.typeParams.find(_.name == denot.name)
342+
sourceTypeParam.getOrElse(this)
343+
else
344+
caseTypeParam.getOrElse(this)
333345
else if (denot.isPrimaryConstructor)
334346
denot.owner.sourceSymbol
335347
else this

compiler/src/dotty/tools/dotc/interactive/Interactive.scala

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ object Interactive {
144144

145145
( sym == tree.symbol
146146
|| sym.exists && sym == tree.symbol.sourceSymbol
147+
|| sym.exists && sym.sourceSymbol == tree.symbol
147148
|| !include.isEmpty && sym.name == tree.symbol.name && sym.maybeOwner != tree.symbol.maybeOwner
148149
&& ( include.isOverridden && overrides(sym, tree.symbol)
149150
|| include.isOverriding && overrides(tree.symbol, sym)

language-server/test/dotty/tools/languageserver/DefinitionTest.scala

+34
Original file line numberDiff line numberDiff line change
@@ -378,4 +378,38 @@ class DefinitionTest {
378378
.definition(m3 to m4, Nil)
379379
.definition(m5 to m6, Nil)
380380
.definition(m7 to m8, Nil)
381+
382+
@Test def typeParam: Unit = {
383+
code"""|class Foo[${m1}T${m2}]:
384+
| def test: ${m3}T${m4}"""
385+
.definition(m3 to m4, List(m1 to m2))
386+
}
387+
388+
@Test def enumTypeParam: Unit = {
389+
code"""|enum Test[${m1}T${m2}]:
390+
| case EnumCase(value: ${m3}T${m4})"""
391+
.definition(m3 to m4, List(m1 to m2))
392+
}
393+
394+
@Test def extMethodTypeParam: Unit = {
395+
code"""extension [${m1}T${m2}](string: String) def xxxx(y: ${m3}T${m4}) = ???"""
396+
.definition(m3 to m4, List(m1 to m2))
397+
}
398+
399+
@Test def typeParamCovariant: Unit = {
400+
code"""|class Foo[+${m1}T${m2}]:
401+
| def test: ${m3}T${m4}"""
402+
.definition(m3 to m4, List(m1 to m2))
403+
}
404+
405+
@Test def enumTypeParamCovariant: Unit = {
406+
code"""|enum Test[+${m1}T${m2}]:
407+
| case EnumCase(value: ${m3}T${m4})"""
408+
.definition(m3 to m4, List(m1 to m2))
409+
}
410+
411+
@Test def extMethodTypeParamCovariant: Unit = {
412+
code"""extension [+${m1}T${m2}](string: String) def xxxx(y: ${m3}T${m4}) = ???"""
413+
.definition(m3 to m4, List(m1 to m2))
414+
}
381415
}

presentation-compiler/test/dotty/tools/pc/tests/definition/PcDefinitionSuite.scala

+27
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,33 @@ class PcDefinitionSuite extends BasePcDefinitionSuite:
478478
|""".stripMargin
479479
)
480480

481+
@Test def `enum-class-type-param` =
482+
check(
483+
"""|
484+
|enum Options[<<AA>>]:
485+
| case Some(x: A@@A)
486+
| case None extends Options[Nothing]
487+
|""".stripMargin
488+
)
489+
490+
@Test def `enum-class-type-param-covariant` =
491+
check(
492+
"""|
493+
|enum Options[+<<AA>>]:
494+
| case Some(x: A@@A)
495+
| case None extends Options[Nothing]
496+
|""".stripMargin
497+
)
498+
499+
@Test def `enum-class-type-param-duplicate` =
500+
check(
501+
"""|
502+
|enum Testing[AA]:
503+
| case Some[<<AA>>](x: A@@A) extends Testing[AA]
504+
| case None extends Testing[Nothing]
505+
|""".stripMargin
506+
)
507+
481508
@Test def `derives-def` =
482509
check(
483510
"""|

0 commit comments

Comments
 (0)