Skip to content

Commit

Permalink
bugfix: emit symbols for vals in constructors (scalameta#5358)
Browse files Browse the repository at this point in the history
* bugfix: emit symbols for vals in constructors
  • Loading branch information
kasiaMarek authored Jun 23, 2023
1 parent 49a056a commit 4aaa18f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,20 @@ class ScalaToplevelMtags(
currentOwner,
false,
false,
isClassConstructor = true,
isCaseClassConstructor = true
)
)
def newExpectClassTemplate: Some[ExpectTemplate] =
Some(
ExpectTemplate(
indent,
currentOwner,
false,
false,
isClassConstructor = true
)
)
def newExpectPkgTemplate: Some[ExpectTemplate] =
Some(ExpectTemplate(indent, currentOwner, true, false))
def newExpectExtensionTemplate(owner: String): Some[ExpectTemplate] =
Expand Down Expand Up @@ -183,7 +194,7 @@ class ScalaToplevelMtags(
val template = expectTemplate match {
case Some(expect) if expect.isCaseClassConstructor =>
newExpectCaseClassTemplate
case _ => newExpectTemplate
case _ => newExpectClassTemplate
}
loop(indent, isAfterNewline = false, currRegion, template)
// also covers extension methods because of `def` inside
Expand Down Expand Up @@ -336,13 +347,16 @@ class ScalaToplevelMtags(
case LPAREN =>
expectTemplate match {
case Some(expect)
if expect.isCaseClassConstructor && includeInnerClasses => {
if expect.isClassConstructor && includeInnerClasses => {
scanner.nextToken()
loop(
indent,
isAfterNewline = false,
expect.startInParenRegion(currRegion),
None
expect.startInParenRegion(
currRegion,
expect.isCaseClassConstructor
),
expectTemplate
)
}
case _ => {
Expand All @@ -351,7 +365,11 @@ class ScalaToplevelMtags(
loop(indent, isAfterNewline = false, currRegion, expectTemplate)
}
}
case RPAREN if currRegion.changeCaseClassState(true).emitIdentifier =>
case RPAREN if (currRegion match {
case _: Region.InParenCaseClass => true
case _: Region.InParenClass => true
case _ => false
}) =>
scanner.nextToken()
loop(
indent,
Expand Down Expand Up @@ -457,14 +475,10 @@ class ScalaToplevelMtags(
currentOwner = owner
maybeName.foreach { name =>
kind match {
case CLASS =>
case CLASS | ENUM =>
tpe(name.name, name.pos, Kind.CLASS, 0)
case TRAIT =>
tpe(name.name, name.pos, Kind.TRAIT, 0)
case ENUM =>
withOwner(owner) {
tpe(name.name, name.pos, Kind.CLASS, 0)
}
case OBJECT =>
if (isPackageObject) {
currentOwner = symbol(Scala.Descriptor.Package(name.name))
Expand Down Expand Up @@ -736,7 +750,8 @@ object ScalaToplevelMtags {
isPackageBody: Boolean,
isExtension: Boolean = false,
ignoreBody: Boolean = false,
isCaseClassConstructor: Boolean = false
isCaseClassConstructor: Boolean = false,
isClassConstructor: Boolean = false
) {

/**
Expand All @@ -752,8 +767,9 @@ object ScalaToplevelMtags {
def startInBraceRegion(prev: Region, extension: Boolean = false): Region =
new Region.InBrace(owner, adjustRegion(prev), extension)

def startInParenRegion(prev: Region): Region =
Region.InParen(owner, adjustRegion(prev), true)
def startInParenRegion(prev: Region, isCaseClass: Boolean): Region =
if (isCaseClass) Region.InParenCaseClass(owner, adjustRegion(prev), true)
else Region.InParenClass(owner, adjustRegion(prev))

def startIndentedRegion(prev: Region, extension: Boolean = false): Region =
new Region.Indented(owner, indent, adjustRegion(prev), extension)
Expand Down Expand Up @@ -836,7 +852,16 @@ object ScalaToplevelMtags {
Indented(owner, exitIndent, prev, extension, termOwner)
}

final case class InParen(
final case class InParenClass(
owner: String,
prev: Region
) extends Region {
def acceptMembers: Boolean = false
override val produceSourceToplevel: Boolean = false
override val emitIdentifier: Boolean = false
}

final case class InParenCaseClass(
owner: String,
prev: Region,
override val emitIdentifier: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ object ImplicitClasses/*example.ImplicitClasses.*/ {
implicit class Xtension/*example.ImplicitClasses.Xtension#*/(number: Int) {
def increment/*example.ImplicitClasses.Xtension#increment().*/: Int = number + 1
}
implicit class XtensionAnyVal/*example.ImplicitClasses.XtensionAnyVal#*/(private val number: Int) extends AnyVal {
implicit class XtensionAnyVal/*example.ImplicitClasses.XtensionAnyVal#*/(private val number/*example.ImplicitClasses.XtensionAnyVal#number.*/: Int) extends AnyVal {
def double/*example.ImplicitClasses.XtensionAnyVal#double().*/: Int = number * 2
}
}
15 changes: 10 additions & 5 deletions tests/unit/src/test/scala/tests/ScalaToplevelSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,24 @@ class ScalaToplevelSuite extends BaseSuite {
| def foo: Int
| class Z
|
|class B(val v: String):
|class B(val v: String, g: String):
| trait X
| def foo: Int
|
|trait C
|trait C(p: String, val i: Int)
|
|enum D:
| case Da, Db""".stripMargin,
|enum D(val i : Int):
| def getI = i
| case Da extends D(1)
| case Db extends D(2)
|""".stripMargin,
List(
"_empty_/A.", "_empty_/A.foo().", "_empty_/A.Z#", "_empty_/B#",
"_empty_/B#X#", "_empty_/B#foo().", "_empty_/C#", "_empty_/D#",
"_empty_/B#X#", "_empty_/B#foo().", "_empty_/B#v.", "_empty_/C#",
"_empty_/C#i.", "_empty_/D#", "_empty_/D#getI().", "_empty_/D#i.",
),
all = true,
dialect = dialects.Scala3,
)

check(
Expand Down

0 comments on commit 4aaa18f

Please sign in to comment.