Skip to content
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

Add an -Yimplicit-as-given flag to easily test changes in the ecosystem #22580

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/ast/untpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {

case class Given()(implicit @constructorOnly src: SourceFile) extends Mod(Flags.Given)

case class GivenFromImplicit()(implicit @constructorOnly src: SourceFile) extends Mod(Flags.Given | Flags.FromImplicit)

case class Erased()(implicit @constructorOnly src: SourceFile) extends Mod(Flags.Erased)

case class Final()(implicit @constructorOnly src: SourceFile) extends Mod(Flags.Final)
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/config/ScalaSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ private sealed trait YSettings:

val Yinstrument: Setting[Boolean] = BooleanSetting(ForkSetting, "Yinstrument", "Add instrumentation code that counts allocations and closure creations.")
val YinstrumentDefs: Setting[Boolean] = BooleanSetting(ForkSetting, "Yinstrument-defs", "Add instrumentation code that counts method calls; needs -Yinstrument to be set, too.")
val YimplicitAsGiven: Setting[Boolean] = BooleanSetting(ForkSetting, "Yimplicit-as-given", "Interpret the compiled implicit keywords as their scala-3 given counterparts.")

// Deprecated: lifted from -Y to -X
@deprecated(message = "Lifted to -X, Scheduled for removal.", since = "3.5.0")
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Flags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,9 @@ object Flags {
/** Symbol is a constructor proxy (either companion, or apply method) */
val (ConstructorProxy @ _, _, _) = newFlags(62, "<constructor proxy>") // (could be merged with Lifted)

/** Changed from implicit with -Yimplicits-as-given flags */
val (FromImplicit @ _, _, _) = newFlags(63, "<from implicit>")

// --------- Combined Flag Sets and Conjunctions ----------------------

/** All possible flags */
Expand Down
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/dotc/core/NamerOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ object NamerOps:
case Nil =>
resultType
case TermSymbols(params) :: paramss1 =>
val (isContextual, isImplicit) =
if params.isEmpty then (false, false)
else (params.head.is(Given), params.head.is(Implicit))
val make = MethodType.companion(isContextual = isContextual, isImplicit = isImplicit)
val (isContextual, isImplicit, fromImplicit) =
if params.isEmpty then (false, false, false)
else (params.head.is(Given), params.head.is(Implicit), params.head.is(FromImplicit))
val make = MethodType.companion(isContextual = isContextual, isImplicit = isImplicit, fromImplicit = fromImplicit)
if isJava then
for param <- params do
if param.info.isDirectRef(defn.ObjectClass) then param.info = defn.AnyType
Expand Down
13 changes: 10 additions & 3 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,8 @@ object Types extends TypeUtils {
/** Is this a Method or PolyType which has implicit or contextual parameters? */
def isImplicitMethod: Boolean = false

def wasImplicitMethod: Boolean = false

/** Is this a Method or PolyType which has contextual parameters as first value parameter list? */
def isContextualMethod: Boolean = false

Expand Down Expand Up @@ -4104,7 +4106,10 @@ object Types extends TypeUtils {
paramInfos.exists(p => p.hasAnnotation(defn.ErasedParamAnnot))

final override def isContextualMethod: Boolean =
companion.eq(ContextualMethodType)
companion.eq(ContextualMethodType) || companion.eq(ContextualFromImplicitMethodType)

final override def wasImplicitMethod: Boolean =
companion.eq(ContextualFromImplicitMethodType)

def erasedParams(using Context): List[Boolean] =
paramInfos.map(p => p.hasAnnotation(defn.ErasedParamAnnot))
Expand Down Expand Up @@ -4209,14 +4214,16 @@ object Types extends TypeUtils {
}

object MethodType extends MethodTypeCompanion("MethodType") {
def companion(isContextual: Boolean = false, isImplicit: Boolean = false): MethodTypeCompanion =
if (isContextual) ContextualMethodType
def companion(isContextual: Boolean = false, isImplicit: Boolean = false, fromImplicit: Boolean = false): MethodTypeCompanion =
if (fromImplicit) ContextualFromImplicitMethodType
else if (isContextual) ContextualMethodType
else if (isImplicit) ImplicitMethodType
else MethodType
}

object ContextualMethodType extends MethodTypeCompanion("ContextualMethodType")
object ImplicitMethodType extends MethodTypeCompanion("ImplicitMethodType")
object ContextualFromImplicitMethodType extends MethodTypeCompanion("ContextualFromImplicitMethodType")

/** A ternary extractor for MethodType */
object MethodTpe {
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3283,6 +3283,7 @@ object Parsers {
private def modOfToken(tok: Int, name: Name): Mod = tok match {
case ABSTRACT => Mod.Abstract()
case FINAL => Mod.Final()
case IMPLICIT if ctx.settings.YimplicitAsGiven.value => Mod.GivenFromImplicit()
case IMPLICIT => Mod.Implicit()
case GIVEN => Mod.Given()
case LAZY => Mod.Lazy()
Expand Down Expand Up @@ -3552,7 +3553,7 @@ object Parsers {

def paramMods() =
if in.token == IMPLICIT then
addParamMod(() => Mod.Implicit())
addParamMod(() => if (ctx.settings.YimplicitAsGiven.value) Mod.GivenFromImplicit() else Mod.Implicit())
else if isIdent(nme.using) then
if initialMods.is(Given) then
syntaxError(em"`using` is already implied here, should not be given explicitly", in.offset)
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4855,6 +4855,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
val isUsingApply = pt.applyKind == ApplyKind.Using
methType.isContextualMethod == isUsingApply
|| methType.isImplicitMethod && isUsingApply // for a transition allow `using` arguments for regular implicit parameters
|| methType.wasImplicitMethod && !isUsingApply

/** Check that `tree == x: pt` is typeable. Used when checking a pattern
* against a selector of type `pt`. This implementation accounts for
Expand Down
22 changes: 22 additions & 0 deletions tests/pos-macros/i22482/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//> using options -Yimplicit-as-given
import scala.quoted._
object Macro:
inline def test[I] = ${testImpl[I]}
def testImpl[I: Type](using Quotes): Expr[Unit] =
import quotes.reflect._

val implicitClause = '{
def clause(implicit num: Int) = ???
}
val expectedImplicitClause =
"""|{
| def clause(using num: scala.Int): scala.Nothing = scala.Predef.???
| ()
|}""".stripMargin

assert(implicitClause.show == expectedImplicitClause)

// test ImplicitClauseInCaseClass
assert(TypeRepr.of[I].typeSymbol.primaryConstructor.paramSymss(1)(0).flags.is(Flags.Given))

'{()}
3 changes: 3 additions & 0 deletions tests/pos-macros/i22482/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//> using options -Yimplicit-as-given
case class ImplicitClauseInCaseClass(dummy: Int)(implicit num: Int)
def Test() = Macro.test[ImplicitClauseInCaseClass]
8 changes: 8 additions & 0 deletions tests/pos/i22482.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//> using options -Yimplicit-as-given

def foo(implicit a: Int): Unit = ???
def bar()(implicit a: Int) : Unit = ???

def main() =
foo(0)
bar()(0)
1 change: 1 addition & 0 deletions tests/run/i22482.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
found if given
15 changes: 15 additions & 0 deletions tests/run/i22482.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//> using options -Yimplicit-as-given

object Lib:
class LibComponent:
def msg = "found if given"
implicit val libComponent: LibComponent = LibComponent()

class UserComponent extends LibComponent:
override def msg = "found if implicit"
implicit val userComponent: UserComponent = UserComponent()

def printComponent(implicit c: LibComponent) = println(c.msg)


@main def Test = Lib.printComponent
Loading