diff options
author | Martin Odersky <odersky@gmail.com> | 2014-03-28 13:00:28 +0100 |
---|---|---|
committer | Dmitry Petrashko <dmitry.petrashko@gmail.com> | 2014-03-31 14:52:08 +0200 |
commit | ad437d8fa9bd4127889569952ae6fdb048d2b1ec (patch) | |
tree | 319a0b0566a6f43733aea0d2d52898d76015382a /src/dotty/tools/dotc/ast/tpd.scala | |
parent | c62d021cdcc0a3dda6daffd74daf8eaf48aeae88 (diff) | |
download | dotty-ad437d8fa9bd4127889569952ae6fdb048d2b1ec.tar.gz dotty-ad437d8fa9bd4127889569952ae6fdb048d2b1ec.tar.bz2 dotty-ad437d8fa9bd4127889569952ae6fdb048d2b1ec.zip |
Make primitive conversions work also for non-numeric types
If the arguments to a primitive conversion are not both numbers, the
conversion will either return the argument itself, or throw a ClassCastException.
Diffstat (limited to 'src/dotty/tools/dotc/ast/tpd.scala')
-rw-r--r-- | src/dotty/tools/dotc/ast/tpd.scala | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/src/dotty/tools/dotc/ast/tpd.scala b/src/dotty/tools/dotc/ast/tpd.scala index 173f81894..3f29fc72f 100644 --- a/src/dotty/tools/dotc/ast/tpd.scala +++ b/src/dotty/tools/dotc/ast/tpd.scala @@ -7,6 +7,7 @@ import util.Positions._, Types._, Contexts._, Constants._, Names._, Flags._ import SymDenotations._, Symbols._, StdNames._, Annotations._, Trees._ import CheckTrees._, Denotations._, Decorators._ import config.Printers._ +import typer.ErrorReporting._ /** Some creators for typed trees */ object tpd extends Trees.Instance[Type] with TypedTreeInfo { @@ -400,11 +401,17 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo { } // convert a numeric with a toXXX method - def numericConversion(tree: Tree, numericCls: Symbol)(implicit ctx: Context): Tree = { + def primitiveConversion(tree: Tree, numericCls: Symbol)(implicit ctx: Context): Tree = { val mname = ("to" + numericCls.name).toTermName val conversion = tree.tpe member mname - assert(conversion.symbol.exists, s"$tree => $numericCls") - ensureApplied(Select(tree, conversion.symbol.termRef)) + if (conversion.symbol.exists) + ensureApplied(Select(tree, conversion.symbol.termRef)) + else if (tree.tpe.widen isRef numericCls) + tree + else { + ctx.warning(i"conversion from ${tree.tpe.widen} to ${numericCls.typeRef} will always fail at runtime.") + Throw(New(defn.ClassCastExceptionClass.typeRef, Nil)) withPos tree.pos + } } def evalOnce(tree: Tree)(within: Tree => Tree)(implicit ctx: Context) = { |