Skip to content

Commit

Permalink
Fix opaque types leaking rhs when inlined and found in type params (a…
Browse files Browse the repository at this point in the history
…nd a related stale symbol issue) (#22655)

This PR fixes the 2 issues found in
#20449, split into 2 commits.

The first commit fixes the stale symbol related issue found if the files
from the issue minimization are compiled together. After suspending and
retrying compilation, the classDefs that are defined directly in
packages previously would sometimes not have companion objects
regenerated, instead relying on the stale symbols from the previous run,
causing them not to to pass the reallyExists check when looking for a
specific ref. Now we make sure to go through lastKnwonDenotation, since
the current one may not exists and may not point us to a Module flag
when checking if to regenerate it.

The second commit fixes the opaque type alias rhs leaking in a macro.
That was caused by building proxies for all parts of the type, including
type arguments to opaque types - from the perspective of a type like
Object[OpaqueType], the opaque type rhs should not be visible.
  • Loading branch information
dwijnand authored Mar 4, 2025
2 parents 63a02dd + 23911df commit 86b59aa
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 2 deletions.
12 changes: 11 additions & 1 deletion compiler/src/dotty/tools/dotc/inlines/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,17 @@ class Inliner(val call: tpd.Tree)(using Context):
* type aliases, add proxy definitions to `opaqueProxies` that expose these aliases.
*/
private def addOpaqueProxies(tp: Type, span: Span, forThisProxy: Boolean)(using Context): Unit =
tp.foreachPart {
val foreachTpPart =
(p: Type => Unit) =>
if forThisProxy then
// Performs operations on all parts of this type, outside of the applied type arguments
new ForeachAccumulator(p, StopAt.None) {
override def apply(x: Unit, tp: Type) = tp match
case AppliedType(tycon, _) => super.apply(x, tycon)
case other => super.apply(x, other)
}.apply((), tp)
else tp.foreachPart(p)
foreachTpPart {
case ref: TermRef =>
for cls <- ref.widen.baseClasses do
if cls.containsOpaques
Expand Down
8 changes: 7 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,13 @@ class Namer { typer: Typer =>
enterSymbol(classConstructorCompanion(classSym.asClass))
else
for moduleSym <- companionVals do
if moduleSym.is(Module) && !moduleSym.isDefinedInCurrentRun then
// by not going through `.lastKnownDenotation` (instead using `.current`),
// we guarantee that the `moduleSym` will be brought forward to the current run,
// rendering `moduleSym.isDefinedInCurrentRun` as always true.
// We want to regenerate the companion instead of bringing it forward,
// as even if we are able to bring forward the object symbol,
// we might not be able to do the same with its stale module class symbol (see `tests/pos/i20449`)
if moduleSym.lastKnownDenotation.is(Module) && !moduleSym.isDefinedInCurrentRun then
val companion =
if needsConstructorProxies(classSym) then
classConstructorCompanion(classSym.asClass)
Expand Down
3 changes: 3 additions & 0 deletions tests/pos-macros/i20449/Macro.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import scala.quoted.*
transparent inline def getTypeInfo[T]() = ${ getTypeInfoImpl[T] }
def getTypeInfoImpl[T: Type](using ctx: Quotes): Expr[Unit] = '{ () }
6 changes: 6 additions & 0 deletions tests/pos-macros/i20449/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

class Wrapper1[A]
val a = {
getTypeInfo[Any]()
val wrapper2 = Wrapper1[Any]()
}
50 changes: 50 additions & 0 deletions tests/run-macros/i20449.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
------ UserName.T - Directly -------
Original: Main_2$package.UserName.T
Dealias: Main_2$package.UserName.T
Dealias dealias: Main_2$package.UserName.T

------ UserName.T - Directly -------
Original: Main_2$package.UserName
Dealias: Main_2$package.UserName.T
Dealias dealias: Main_2$package.UserName.T

------ ForeignWrapper1[UserName.T] -------
Original: Main_2$package.UserName.T
Dealias: Main_2$package.UserName.T
Dealias dealias: Main_2$package.UserName.T

------ ForeignWrapper2[UserName.T] -------
Original: Main_2$package.UserName.T
Dealias: Main_2$package.UserName.T
Dealias dealias: Main_2$package.UserName.T

------ ForeignWrapper1[UserName] -------
Original: Main_2$package.UserName
Dealias: Main_2$package.UserName.T
Dealias dealias: Main_2$package.UserName.T

------ ForeignWrapper2[UserName] -------
Original: Main_2$package.UserName
Dealias: Main_2$package.UserName.T
Dealias dealias: Main_2$package.UserName.T

------ Wrapper1[UserName.T] -------
Original: Main_2$package.UserName.T
Dealias: Main_2$package.UserName.T
Dealias dealias: Main_2$package.UserName.T

------ Wrapper2[UserName.T] -------
Original: Main_2$package.UserName.T
Dealias: Main_2$package.UserName.T
Dealias dealias: Main_2$package.UserName.T

------ Wrapper1[UserName] -------
Original: Main_2$package.UserName
Dealias: Main_2$package.UserName.T
Dealias dealias: Main_2$package.UserName.T

------ Wrapper2[UserName] -------
Original: Main_2$package.UserName
Dealias: Main_2$package.UserName.T
Dealias dealias: Main_2$package.UserName.T

29 changes: 29 additions & 0 deletions tests/run-macros/i20449/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import scala.quoted.*

class ForeignWrapper1[-A] {
inline def getTypeInfo(inline source: String): String =
${ getTypeInfoImpl[A]('source) }
def createWrapper2 = ForeignWrapper2(this)
}

class ForeignWrapper2[-A](val self: ForeignWrapper1[A]) {
inline def getTypeInfo(inline source: String): String =
${getTypeInfoImpl[A]('source)}
}

transparent inline def getTypeInfo[T](inline source: String) =
${ getTypeInfoImpl[T]('source) }

def getTypeInfoImpl[T: Type](source: Expr[String])(using ctx: Quotes) : Expr[String] = {
import ctx.reflect.*

val tpe = TypeRepr.of[T]
val str =
s"""|------ ${source.valueOrAbort} -------
|Original: ${tpe.show}
|Dealias: ${tpe.dealias.show}
|Dealias dealias: ${tpe.dealias.dealias.show}
""".stripMargin

Expr(str)
}
41 changes: 41 additions & 0 deletions tests/run-macros/i20449/Main_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
object UserName {
opaque type T = String

def apply(s: String): T = s
}

type UserName = UserName.T

class Wrapper1[-A] {
inline def getTypeInfo(inline source: String): String =
${ getTypeInfoImpl[A]('source) }
def createWrapper2 = Wrapper2(this)
}

class Wrapper2[-A](val self: Wrapper1[A]) {
inline def getTypeInfo(inline source: String): String =
${getTypeInfoImpl[A]('source)}
}


@main def Test() = {
println(getTypeInfo[UserName.T]("UserName.T - Directly"))
println(getTypeInfo[UserName]("UserName.T - Directly"))

val foreignWrapper = ForeignWrapper1[UserName.T]()
println(foreignWrapper.getTypeInfo("ForeignWrapper1[UserName.T]"))
println(foreignWrapper.createWrapper2.getTypeInfo("ForeignWrapper2[UserName.T]"))

val foreignWrapper2 = ForeignWrapper1[UserName]()
println(foreignWrapper2.getTypeInfo("ForeignWrapper1[UserName]"))
println(foreignWrapper2.createWrapper2.getTypeInfo("ForeignWrapper2[UserName]"))

val wrapper = Wrapper1[UserName.T]()
println(wrapper.getTypeInfo("Wrapper1[UserName.T]"))
println(wrapper.createWrapper2.getTypeInfo("Wrapper2[UserName.T]"))

val wrapper2 = Wrapper1[UserName]()
println(wrapper2.getTypeInfo("Wrapper1[UserName]"))
println(wrapper2.createWrapper2.getTypeInfo("Wrapper2[UserName]"))

}

0 comments on commit 86b59aa

Please sign in to comment.