Skip to content

Commit 415cb86

Browse files
committed
Extend convertibleTo to results of function types
1 parent f99621d commit 415cb86

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3787,13 +3787,22 @@ object Types {
37873787
case ExprType(resType) => ExprType(addAnnotation(resType, cls))
37883788
case _ => AnnotatedType(tp, Annotation(cls))
37893789

3790+
def wrapConvertible(tp: Type) =
3791+
AppliedType(defn.ConvertibleToType.typeRef, tp :: Nil)
3792+
37903793
def addConvertibleTo(tp: Type): Type = tp match
3794+
case tp @ AppliedType(tycon, args) if tycon.typeSymbol == defn.RepeatedParamClass =>
3795+
tp.derivedAppliedType(tycon, addConvertibleTo(args.head) :: Nil)
3796+
case tp @ AppliedType(tycon, args) if defn.isFunctionType(tp) =>
3797+
wrapConvertible(tp.derivedAppliedType(tycon, args.init :+ addConvertibleTo(args.last)))
3798+
case tp @ RefinedType(parent, rname, rinfo) if defn.isFunctionType(tp) =>
3799+
wrapConvertible(tp.derivedRefinedType(parent, rname, addConvertibleTo(rinfo)))
3800+
case tp: MethodType =>
3801+
tp.derivedLambdaType(resType = addConvertibleTo(tp.resType))
37913802
case ExprType(resType) =>
37923803
ExprType(addConvertibleTo(resType))
3793-
case tp @ AppliedType(tycon, arg :: Nil) if tycon.typeSymbol == defn.RepeatedParamClass =>
3794-
tp.derivedAppliedType(tycon, addConvertibleTo(arg) :: Nil)
37953804
case _ =>
3796-
AppliedType(defn.ConvertibleToType.typeRef, tp :: Nil)
3805+
wrapConvertible(tp)
37973806

37983807
def paramInfo(param: Symbol) =
37993808
var paramType = param.info.annotatedToRepeated

tests/run-custom-args/fatal-warnings/convertible.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ given Conversion[String, Text] = Text(_)
1111
f("abc", "def", "xyz", "uvw") // ok
1212
f("abc", "def", "xyz", Text("uvw")) // ok
1313

14+
def g(x: convertibleTo () => Text) =
15+
println(x().str)
16+
17+
g(() => "hi")
18+
1419
trait A[X]:
1520
def f(x: X): Unit = ()
1621

0 commit comments

Comments
 (0)