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
Changes from 1 commit
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
Prev Previous commit
Handle apply on ContextualMethodTypes created with -Yimplicit-as-give…
…n as if it was implicit
jchyb committed Mar 6, 2025
commit 9a5bd705acd1b41c458774282f8433abbdfbef24
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/ast/untpd.scala
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Flags.scala
Original file line number Diff line number Diff line change
@@ -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 */
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/dotc/core/NamerOps.scala
Original file line number Diff line number Diff line change
@@ -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
13 changes: 10 additions & 3 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
@@ -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

@@ -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))
@@ -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 {
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
@@ -3283,7 +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.Given()
case IMPLICIT if ctx.settings.YimplicitAsGiven.value => Mod.GivenFromImplicit()
case IMPLICIT => Mod.Implicit()
case GIVEN => Mod.Given()
case LAZY => Mod.Lazy()
@@ -3553,7 +3553,7 @@ object Parsers {

def paramMods() =
if in.token == IMPLICIT then
addParamMod(() => if (ctx.settings.YimplicitAsGiven.value) Mod.Given() else 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)
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
@@ -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
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)