diff --git a/compiler/src/dotty/tools/dotc/transform/localopt/FormatChecker.scala b/compiler/src/dotty/tools/dotc/transform/localopt/FormatChecker.scala index b0ce1d6614fd..f152ac9a6876 100644 --- a/compiler/src/dotty/tools/dotc/transform/localopt/FormatChecker.scala +++ b/compiler/src/dotty/tools/dotc/transform/localopt/FormatChecker.scala @@ -228,8 +228,20 @@ class TypedFormatChecker(partsElems: List[Tree], parts: List[String], args: List case _ => true def lintToString(arg: Type): Unit = - if ctx.settings.Whas.toStringInterpolated && kind == StringXn && !(arg.widen =:= defn.StringType) && !arg.isPrimitiveValueType - then warningAt(CC)("interpolation uses toString") + def checkIsStringify(tp: Type): Boolean = tp.widen match + case OrType(tp1, tp2) => + checkIsStringify(tp1) || checkIsStringify(tp2) + case tp => + !(tp =:= defn.StringType) + && { + tp =:= defn.UnitType + && { warningAt(CC)("interpolated Unit value"); true } + || + !tp.isPrimitiveValueType + && { warningAt(CC)("interpolation uses toString"); true } + } + if ctx.settings.Whas.toStringInterpolated && kind == StringXn then + checkIsStringify(arg): Unit // what arg type if any does the conversion accept def acceptableVariants: List[Type] = diff --git a/compiler/src/dotty/tools/dotc/transform/localopt/StringInterpolatorOpt.scala b/compiler/src/dotty/tools/dotc/transform/localopt/StringInterpolatorOpt.scala index 1afcfbac6206..0bb1df9a21e1 100644 --- a/compiler/src/dotty/tools/dotc/transform/localopt/StringInterpolatorOpt.scala +++ b/compiler/src/dotty/tools/dotc/transform/localopt/StringInterpolatorOpt.scala @@ -109,9 +109,20 @@ class StringInterpolatorOpt extends MiniPhase: result end mkConcat def lintToString(t: Tree): Unit = - val arg: Type = t.tpe - if ctx.settings.Whas.toStringInterpolated && !(arg.widen =:= defn.StringType) && !arg.isPrimitiveValueType - then report.warning("interpolation uses toString", t.srcPos) + def checkIsStringify(tp: Type): Boolean = tp.widen match + case OrType(tp1, tp2) => + checkIsStringify(tp1) || checkIsStringify(tp2) + case tp => + !(tp =:= defn.StringType) + && { + tp =:= defn.UnitType + && { report.warning("interpolated Unit value", t.srcPos); true } + || + !tp.isPrimitiveValueType + && { report.warning("interpolation uses toString", t.srcPos); true } + } + if ctx.settings.Whas.toStringInterpolated then + checkIsStringify(t.tpe): Unit val sym = tree.symbol // Test names first to avoid loading scala.StringContext if not used, and common names first val isInterpolatedMethod =