Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/util/SourceFile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,7 @@ object SourceFile {
* It relies on SourceFile#virtual implementation to create the virtual file.
*/
def virtual(uri: URI, content: String): SourceFile =
val path = Paths.get(uri).toString
SourceFile.virtual(path, content)
SourceFile(new VirtualFile(Paths.get(uri), content.getBytes(StandardCharsets.UTF_8)), content.toCharArray)

/** Returns the relative path of `source` within the `reference` path
*
Expand Down
19 changes: 18 additions & 1 deletion compiler/src/dotty/tools/io/VirtualFile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,32 @@ class VirtualFile(val name: String, override val path: String) extends AbstractF
this.content = content
}

/**
* Initializes this instance with the specified path
* and a name taken from the last path element.
*
* @param path the path of the virtual file to be created
* @param content the initial contents of the virtual file
* @return the created virtual file
*/
def this(path: JPath, content: Array[Byte]) = {
this(path.toString.replace(java.io.File.separatorChar, '/'), content)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this(path.toString.replace(java.io.File.separatorChar, '/'), content)
this(jpath.getFileName().toString(), path)
this.content = content

wouldn't make sense? I think / is only used to get the last element

this.jpath_ = path
}

private var content = Array.emptyByteArray

private var jpath_ : JPath = null

def absolute: AbstractFile = this

/** Returns null. */
def jpath: JPath = null
def jpath: JPath = jpath_

override def sizeOption: Option[Int] = Some(content.length)

override def exists: Boolean = true

def input : InputStream = new ByteArrayInputStream(content)

override def output: OutputStream = {
Expand Down
20 changes: 20 additions & 0 deletions presentation-compiler-testcases/src/tests/macros/metals7460.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package tests.macros

import scala.quoted.*

object Macros7460 {

transparent inline def foo: String =
${ fooImpl }

private def fooImpl(using Quotes): Expr[String] =
Expr("foo...")

transparent inline def bar: String =
${ barImpl }

private def barImpl(using Quotes): Expr[String] =
quotes.reflect.Position.ofMacroExpansion.sourceFile.getJPath.get // this line is the culprit
Expr("bar...")

}
Original file line number Diff line number Diff line change
Expand Up @@ -799,3 +799,19 @@ class HoverTermSuite extends BaseHoverSuite:
|""".stripMargin,
"def valueOf($name: String): Foo".hover
)

@Test def `i7460` =
check(
"""|package tests.macros
|def m = Macros7460.foo.sub@@string(2, 4)
|""".stripMargin,
"def substring(x$0: Int, x$1: Int): String".hover
)

@Test def `i7460-2` =
check(
"""|package tests.macros
|def m = Macros7460.bar.sub@@string(2, 4)
|""".stripMargin,
"def substring(x$0: Int, x$1: Int): String".hover
)
Loading