Skip to content

Commit 33043bd

Browse files
committed
Fix #6336: Error on implicit conversions to AnyVal or AnyRef
1 parent f548a18 commit 33043bd

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3140,7 +3140,9 @@ class Typer extends Namer
31403140
if (isFullyDefined(wtp, force = ForceDegree.all) &&
31413141
ctx.typerState.constraint.ne(prevConstraint)) readapt(tree)
31423142
else err.typeMismatch(tree, pt, failure)
3143-
if (ctx.mode.is(Mode.ImplicitsEnabled) && tree.typeOpt.isValueType)
3143+
if ctx.mode.is(Mode.ImplicitsEnabled) && tree.typeOpt.isValueType then
3144+
if pt.isRef(defn.AnyValClass) || pt.isRef(defn.ObjectClass) then
3145+
ctx.error(em"the result of an implicit conversion must be more specific than $pt", tree.sourcePos)
31443146
inferView(tree, pt) match {
31453147
case SearchSuccess(found: ExtMethodApply, _, _) =>
31463148
found // nothing to check or adapt for extension method applications

tests/neg/i6336.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object Test {
2+
val a: AnyVal = "foo" // error
3+
val b: AnyRef = 1 // error
4+
}

0 commit comments

Comments
 (0)