From 8ba915e4498be3d17af894f5fb46c7b621ef5abb Mon Sep 17 00:00:00 2001 From: cooldome Date: Mon, 13 Apr 2020 13:17:22 +0100 Subject: [PATCH] error msg for #13864 (#13962) Co-authored-by: cooldome --- compiler/semexprs.nim | 5 +++++ tests/arc/tref_cast_error.nim | 15 +++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 tests/arc/tref_cast_error.nim diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 5c00ce1f8a..3d41b592c4 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -189,6 +189,11 @@ proc isCastable(conf: ConfigRef; dst, src: PType): bool = return false if skipTypes(src, abstractInst-{tyTypeDesc}).kind == tyTypeDesc: return false + if conf.selectedGC in {gcArc, gcOrc}: + let d = skipTypes(dst, abstractInst) + let s = skipTypes(src, abstractInst) + if d.kind == tyRef and s.kind == tyRef and s[0].isFinal != d[0].isFinal: + return false var dstSize, srcSize: BiggestInt dstSize = computeSize(conf, dst) diff --git a/tests/arc/tref_cast_error.nim b/tests/arc/tref_cast_error.nim new file mode 100644 index 0000000000..b0d2faf771 --- /dev/null +++ b/tests/arc/tref_cast_error.nim @@ -0,0 +1,15 @@ +discard """ + cmd: "nim c --gc:arc $file" + errormsg: "expression cannot be cast to ref RootObj" + joinable: false +""" + +type Variant* = object + refval: ref RootObj + +proc newVariant*[T](val: T): Variant = + let pt = T.new() + pt[] = val + result = Variant(refval: cast[ref RootObj](pt)) + +var v = newVariant(@[1, 2, 3])