Skip to content

Don't propagate imports into inlined code #16160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/inlines/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,9 @@ class Inliner(val call: tpd.Tree)(using Context):
// reference to a private method is kept at runtime.
cpy.Select(tree)(qual.asInstance(qual.tpe.widen), name)

case tree: ImportOrExport =>
EmptyTree

case tree => tree
},
oldOwners = inlinedMethod :: Nil,
Expand Down
10 changes: 8 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/CrossVersionChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import util.SrcPos
import config.{ScalaVersion, NoScalaVersion, Feature, ScalaRelease}
import MegaPhase.MiniPhase
import scala.util.{Failure, Success}
import ast.tpd
import ast.{tpd, untpd}

class CrossVersionChecks extends MiniPhase:
import tpd.*
Expand All @@ -21,6 +21,11 @@ class CrossVersionChecks extends MiniPhase:
override def runsAfterGroupsOf: Set[String] = Set(FirstTransform.name)
// We assume all type trees except TypeTree have been eliminated

override def checkPostCondition(tree: Tree)(using Context): Unit = tree match
case tree: Import =>
assert(untpd.languageImport(tree.expr).nonEmpty, i"illegal tree: $tree")
case _ =>

// Note: if a symbol has both @deprecated and @migration annotations and both
// warnings are enabled, only the first one checked here will be emitted.
// I assume that's a consequence of some code trying to avoid noise by suppressing
Expand Down Expand Up @@ -182,7 +187,8 @@ class CrossVersionChecks extends MiniPhase:
case t: RefTree => checkUndesiredProperties(t.symbol, t.srcPos)
case _ =>
}
tree
if untpd.languageImport(tree.expr).isEmpty then EmptyTree
else tree
case _ => tree

end CrossVersionChecks
Expand Down
25 changes: 25 additions & 0 deletions tests/neg/i16156/Defs_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//import quotes.reflect._

trait MyEncoder[T]:
def encode: String
class Context:
inline def summonMyEncoder[T]: String =
${ SummonEncoder.impl[T] }
implicit val encoderInstance: MyEncoder[String] =
new MyEncoder[String] { def encode = "blah" }
end Context

import scala.quoted._

object SummonEncoder:
def impl[T: Type](using Quotes) =
import quotes.reflect._
Expr.summon[MyEncoder[T]] match
case Some(enc) => '{ $enc.encode }
case None => report.throwError("can't do it")

class Repo[T]:
val ctx = new Context
inline def summonEncoder = { import ctx._ // change to: import ctx.{given, _} for the given example
ctx.summonMyEncoder[T]
}
4 changes: 4 additions & 0 deletions tests/neg/i16156/Use_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

object Use:
val repo = new Repo[String]
val v = repo.summonEncoder // error happens here!