Skip to content

Handle some explicit paths in quotes #8113

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 1 commit 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
9 changes: 7 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/PCPCheckAndHeal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Decorators._
import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.quoted._
import dotty.tools.dotc.core.Mode
import dotty.tools.dotc.core.NameKinds._
import dotty.tools.dotc.core.StagingContext._
import dotty.tools.dotc.core.StdNames._
Expand Down Expand Up @@ -49,7 +50,10 @@ class PCPCheckAndHeal(@constructorOnly ictx: Context) extends TreeMapWithStages(
case annot => transform(annot.tree)(given annotCtx)
}
checkLevel(super.transform(tree))
case _ => checkLevel(super.transform(tree))
case _ =>
val ctx1 = if tree.isType then ctx.addMode(Mode.Type) else ctx
given Context = ctx1
checkLevel(super.transform(tree))
}

/** Transform quoted trees while maintaining phase correctness */
Expand Down Expand Up @@ -100,7 +104,7 @@ class PCPCheckAndHeal(@constructorOnly ictx: Context) extends TreeMapWithStages(
case Some(tpRef) => tpRef
case _ => tree
}
case _: TypeTree | _: AppliedTypeTree | _: Apply | _: TypeApply | _: UnApply | Select(_, OuterSelectName(_, _)) =>
case _: TypeTree | _: AppliedTypeTree | _: Apply | _: UnApply | Select(_, OuterSelectName(_, _)) =>
tree.withType(checkTp(tree.tpe))
Copy link
Contributor

Choose a reason for hiding this comment

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

Why ignore TypeApply?

case _: ValOrDefDef | _: Bind =>
tree.symbol.info = checkTp(tree.symbol.info)
Expand Down Expand Up @@ -179,6 +183,7 @@ class PCPCheckAndHeal(@constructorOnly ictx: Context) extends TreeMapWithStages(
case tp: TypeRef =>
if levelOf(sym).getOrElse(0) < level then tryHeal(sym, tp, pos)
else None
case _: TermRef if ctx.mode.is(Mode.Type) && levelOf(sym).getOrElse(0) >= level => None
Copy link
Contributor

@liufengyun liufengyun Feb 10, 2020

Choose a reason for hiding this comment

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

Will it be an issue, if it's used in pattern matching, e.g. case _: p.T? Or, t.asInstanceOf[p.T] can also be problematic.

case _ =>
levelError(sym, tp, pos, "")
else if (!sym.owner.isStaticOwner) // non-top level class reference that is phase inconsistent
Expand Down
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ class ReifyQuotes extends MacroTransform {
if (isType) ref(defn.Unpickler_unpickleType).appliedToType(originalTp)
else ref(defn.Unpickler_unpickleExpr).appliedToType(originalTp.widen)
val spliceResType =
if (isType) defn.QuotedTypeClass.typeRef.appliedTo(WildcardType)
else defn.FunctionType(1, isContextual = true).appliedTo(defn.QuoteContextClass.typeRef, defn.QuotedExprClass.typeRef.appliedTo(defn.AnyType)) | defn.QuotedTypeClass.typeRef.appliedTo(WildcardType)
defn.FunctionType(1, isContextual = true).appliedTo(defn.QuoteContextClass.typeRef, defn.QuotedExprClass.typeRef.appliedTo(defn.AnyType)) | defn.QuotedTypeClass.typeRef.appliedTo(WildcardType)
val pickledQuoteStrings = liftList(PickledQuotes.pickleQuote(body).map(x => Literal(Constant(x))), defn.StringType)
val splicesList = liftList(splices, defn.FunctionType(1).appliedTo(defn.SeqType.appliedTo(defn.AnyType), spliceResType))
meth.appliedTo(pickledQuoteStrings, splicesList)
Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/internal/quoted/Unpickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ object Unpickler {

type PickledQuote = List[String]
type PickledExprArgs = Seq[Seq[Any] => ((QuoteContext ?=> Expr[Any]) | Type[_])]
type PickledTypeArgs = Seq[Seq[Any] => Type[_]]
type PickledTypeArgs = Seq[Seq[Any] => ((QuoteContext ?=> Expr[Any]) | Type[_])]
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we simplify to only keep PickledArgs, as PickledExprArgs and PickledTypeArgs now have the same definition.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but there are some bootstrapping constraints.


/** Unpickle `repr` which represents a pickled `Expr` tree,
* replacing splice nodes with `args`
Expand Down
21 changes: 21 additions & 0 deletions tests/neg/i8100.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

import scala.quoted._
import scala.quoted.matching._

class M {
type E
}

def f[T: Type](using QuoteContext) =
summonExpr[M] match
case Some('{ $mm : $tt }) =>
'{
val m = $mm
${ val b: m.type =
m // error
???
}
}


def g[T](using Type[T]) = ???
25 changes: 25 additions & 0 deletions tests/pos/i8100.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

import scala.quoted._
import scala.quoted.matching._

class M {
type E
}

def f(using QuoteContext): Expr[Any] =
val mm: Expr[M] = ???
'{
val m: M = $mm
type ME = m.E
${ g[ME](using '[ME]) }
${ g[m.E](using '[ME]) }
${ g[ME](using '[m.E]) }
${ g[m.E](using '[m.E]) }
// ${ g[ME] } // FIXME
// ${ g[m.E] } // FIXME
${ g(using '[ME]) }
${ g(using '[m.E]) }
}


def g[T](using Type[T]) = ???