Skip to content

Commit

Permalink
keep given instance summoner type params
Browse files Browse the repository at this point in the history
  • Loading branch information
bishabosha committed Aug 25, 2021
1 parent ec3a21a commit 7a01b0f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class ExtractSemanticDB extends Phase:
|| sym.is(Synthetic)
|| sym.isSetter
|| sym.isOldStyleImplicitConversion(forImplicitClassOnly = true)
|| sym.owner.isGivenInstanceSummoner
|| sym.isTypeParam && sym.owner.isGivenInstanceClass
|| excludeDefOrUse(sym)

private def excludeDefOrUse(sym: Symbol)(using Context): Boolean =
Expand Down
5 changes: 5 additions & 0 deletions compiler/src/dotty/tools/dotc/semanticdb/Tools.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ object Tools:
document.copy(text = text)
end loadTextDocument

def loadTextDocumentUnsafe(scalaAbsolutePath: Path, semanticdbAbsolutePath: Path): TextDocument =
val docs = parseTextDocuments(semanticdbAbsolutePath).documents
assert(docs.length == 1)
docs.head.copy(text = new String(Files.readAllBytes(scalaAbsolutePath), StandardCharsets.UTF_8))

/** Parses SemanticDB text documents from an absolute path to a `*.semanticdb` file. */
private def parseTextDocuments(path: Path): TextDocuments =
val bytes = Files.readAllBytes(path) // NOTE: a semanticdb file is a TextDocuments message, not TextDocument
Expand Down
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/SymUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ object SymUtils:

def isGenericProduct(using Context): Boolean = whyNotGenericProduct.isEmpty

/** Is this the an old style implicit conversion?
/** Is this an old style implicit conversion?
* @param directOnly only consider explicitly written methods
* @param forImplicitClassOnly only consider methods generated from implicit classes
*/
Expand All @@ -100,6 +100,7 @@ object SymUtils:
case _ =>
false

/** Is this the method that summons a structural given instance? */
def isGivenInstanceSummoner(using Context): Boolean =
def isCodefined(info: Type): Boolean = info.stripPoly match
case mt: MethodType =>
Expand All @@ -111,6 +112,9 @@ object SymUtils:
self.isCoDefinedGiven(res.typeSymbol)
self.isAllOf(Given | Method) && isCodefined(self.info)

def isGivenInstanceClass(using Context): Boolean =
self.isClass && self.is(Given)

def useCompanionAsMirror(using Context): Boolean = self.linkedClass.exists && !self.is(Scala2x)

/** Is this a sealed class or trait for which a sum mirror is generated?
Expand Down
20 changes: 20 additions & 0 deletions compiler/test/dotty/tools/dotc/semanticdb/SemanticdbTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ import dotty.tools.dotc.util.SourceFile
@main def updateExpect =
SemanticdbTests().runExpectTest(updateExpectFiles = true)

@main def metac(root: String, source: String) =
val rootSrc = Paths.get(root)
val sourceSrc = Paths.get(source)
val semanticFile = FileSystems.getDefault.getPathMatcher("glob:**.semanticdb")
def inputFiles(): List[Path] =
val ls = Files.walk(rootSrc.resolve("META-INF").resolve("semanticdb"))
val files =
try ls.filter(p => semanticFile.matches(p)).collect(Collectors.toList).asScala
finally ls.close()
require(files.nonEmpty, s"No semanticdb files! $rootSrc")
files.toList
val metacSb: StringBuilder = StringBuilder(5000)
for
semanticdbPath <- inputFiles().sorted
do
val doc = Tools.loadTextDocumentUnsafe(sourceSrc.toAbsolutePath, semanticdbPath)
Tools.metac(doc, Paths.get(doc.uri))(using metacSb)
Files.write(rootSrc.resolve("metac.expect"), metacSb.toString.getBytes(StandardCharsets.UTF_8))


@Category(Array(classOf[BootstrappedOnlyTests]))
class SemanticdbTests:
val javaFile = FileSystems.getDefault.getPathMatcher("glob:**.java")
Expand Down
2 changes: 1 addition & 1 deletion tests/semanticdb/expect/InventedNames.expect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ given X/*->givens::X#*/ with
given (using X/*->givens::X#*/): Y/*->givens::Y#*/ with
def doY/*<-givens::InventedNames$package.given_Y#doY().*/ = "7"

given [T/*<-givens::InventedNames$package.given_Z_T#[T]*/]: Z/*->givens::Z#*/[T/*->givens::InventedNames$package.given_Z_T#[T]*/] with
given [T/*<-givens::InventedNames$package.given_Z_T().[T]*/]: Z/*->givens::Z#*/[T/*->givens::InventedNames$package.given_Z_T#[T]*/] with
def doZ/*<-givens::InventedNames$package.given_Z_T#doZ().*/: List/*->scala::package.List#*/[T/*->givens::InventedNames$package.given_Z_T#[T]*/] = Nil/*->scala::package.Nil.*/


Expand Down
2 changes: 1 addition & 1 deletion tests/semanticdb/metac.expect
Original file line number Diff line number Diff line change
Expand Up @@ -1766,7 +1766,7 @@ Occurrences:
[24:13..24:14): X -> givens/X#
[24:17..24:18): Y -> givens/Y#
[25:6..25:9): doY <- givens/InventedNames$package.given_Y#doY().
[27:7..27:8): T <- givens/InventedNames$package.given_Z_T#[T]
[27:7..27:8): T <- givens/InventedNames$package.given_Z_T().[T]
[27:11..27:12): Z -> givens/Z#
[27:13..27:14): T -> givens/InventedNames$package.given_Z_T#[T]
[28:6..28:9): doZ <- givens/InventedNames$package.given_Z_T#doZ().
Expand Down

0 comments on commit 7a01b0f

Please sign in to comment.