diff options
-rw-r--r-- | compiler/semexprs.nim | 5 | ||||
-rw-r--r-- | tests/arc/tref_cast_error.nim | 15 |
2 files changed, 20 insertions, 0 deletions
diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 5c00ce1f8..3d41b592c 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 000000000..b0d2faf77 --- /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]) |