Skip to content

Commit

Permalink
Handle default implicits to context parameters under -3.4-migration
Browse files Browse the repository at this point in the history
Synthesized calls for default implicits needed a using clause when the
method was an implicit method, but had a context bound parameter in
3.4-migration.

Also, we can't rewrite adding a `using` clause if the argument list is
empty, since we are lacking precise position info.

Fixes #19506
  • Loading branch information
odersky authored and Kordyjan committed Jan 24, 2024
1 parent a220621 commit bc20aa6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
toText(tp.argType) ~ " ?=>? " ~ toText(tp.resultType)
case tp @ FunProto(args, resultType) =>
"[applied to ("
~ keywordText("using ").provided(tp.isContextualMethod)
~ keywordText("using ").provided(tp.applyKind == ApplyKind.Using)
~ argsTreeText(args)
~ ") returning "
~ toText(resultType)
Expand Down
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Migrations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,14 @@ trait Migrations:
&& isContextBoundParams
&& pt.applyKind != ApplyKind.Using
then
def rewriteMsg = Message.rewriteNotice("This code", mversion.patchFrom)
def rewriteMsg =
if pt.args.isEmpty then ""
else Message.rewriteNotice("This code", mversion.patchFrom)
report.errorOrMigrationWarning(
em"""Context bounds will map to context parameters.
|A `using` clause is needed to pass explicit arguments to them.$rewriteMsg""",
tree.srcPos, mversion)
if mversion.needsPatch then
if mversion.needsPatch && pt.args.nonEmpty then
patch(Span(pt.args.head.span.start), "using ")
end contextBoundParams

Expand Down
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3906,7 +3906,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
if (arg.tpe.isError) Nil else untpd.NamedArg(pname, untpd.TypedSplice(arg)) :: Nil
}
val app = cpy.Apply(tree)(untpd.TypedSplice(tree), namedArgs)
if (wtp.isContextualMethod) app.setApplyKind(ApplyKind.Using)
val needsUsing = wtp.isContextualMethod || wtp.match
case MethodType(ContextBoundParamName(_) :: _) => sourceVersion.isAtLeast(`3.4`)
case _ => false
if needsUsing then app.setApplyKind(ApplyKind.Using)
typr.println(i"try with default implicit args $app")
typed(app, pt, locked)
else issueErrors()
Expand Down
8 changes: 8 additions & 0 deletions tests/neg/i19506.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//> using options "-source 3.4-migration",

trait Reader[T]
def read[T: Reader](s: String, trace: Boolean = false): T = ???

def Test =
read[Object]("") // error
read[Object]("")() // error

0 comments on commit bc20aa6

Please sign in to comment.