Skip to content

Commit ea4e1e6

Browse files
committed
Fixed unintentional safe value error
1 parent 90cacad commit ea4e1e6

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

compiler/src/dotty/tools/dotc/transform/init/Objects.scala

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,10 +1181,37 @@ class Objects(using Context @constructorOnly):
11811181

11821182
case v @ SafeValue(_) =>
11831183
if v.typeSymbol != defn.NullClass then
1184-
// selection on Null is sensible on AST level; no warning for it
1184+
// call on Null is sensible on AST level but not in practice
11851185
report.warning("[Internal error] Unexpected selection on safe value " + v.show + ", field = " + field.show + ". " + Trace.show, Trace.position)
11861186
end if
11871187
Bottom
1188+
// case v @ SafeValue(_) =>
1189+
// if v.typeSymbol == defn.NullClass then
1190+
// // call on Null is sensible on AST level but not in practice
1191+
// Bottom
1192+
// else if field.is(Flags.Method) then
1193+
// // Assume such method is pure. Check return type, only try to analyze body if return type is not safe
1194+
// val target = resolve(v.typeSymbol.asClass, field)
1195+
// val targetInfo = target.info
1196+
1197+
// val returnType = targetInfo match {
1198+
// case tpe: MethodicType => tpe.resType
1199+
// case _ =>
1200+
// report.warning("[Internal error] Unexpected selection on safe value " + v.show + ", field = " + field.show + " with type " + targetInfo.show + ". " + Trace.show, Trace.position)
1201+
// Bottom
1202+
// }
1203+
1204+
// val typeSymbol = SafeValue.getSafeTypeSymbol(returnType)
1205+
// if typeSymbol.isDefined then
1206+
// // since method is pure and return type is safe, no need to analyze method body
1207+
// SafeValue(typeSymbol.get)
1208+
// else if !target.hasSource then
1209+
// UnknownValue
1210+
// else
1211+
// call(v, target, args = Nil, receiver = receiver, superType = NoType, needResolve = false)
1212+
// else
1213+
// report.warning("[Internal error] Unexpected selection of a non-method on safe value " + v.show + ", field = " + field.show + ". " + Trace.show, Trace.position)
1214+
// Bottom
11881215

11891216
case Package(packageModuleClass) =>
11901217
if field.isStaticObject then
@@ -1580,7 +1607,13 @@ class Objects(using Context @constructorOnly):
15801607
val target = expr.tpe.widenSingleton.classSymbol.asClass
15811608
withTrace(trace2) { resolveThis(target, qual.asInstanceOf[ThisValue], klass) }
15821609
case _ =>
1583-
withTrace(trace2) { select(qual, expr.symbol, receiver = qualifier.tpe) }
1610+
qual match {
1611+
// Check if expression is a selection of a method
1612+
case v: SafeValue if expr.symbol.is(Flags.Method) =>
1613+
withTrace(trace2) { call(v, expr.symbol, args = Nil, receiver = qualifier.tpe, superType = NoType) }
1614+
case _ =>
1615+
withTrace(trace2) { select(qual, expr.symbol, receiver = qualifier.tpe) }
1616+
}
15841617

15851618
case _: This =>
15861619
evalType(expr.tpe, thisV, klass)

0 commit comments

Comments
 (0)