Merge pull request #4572 from oderwat/distinct-nil
Allowing `nil` for distinct types where the base type is nilable
This commit is contained in:
commit
df0baefb12
2 changed files with 49 additions and 0 deletions
|
|
@ -900,6 +900,8 @@ proc typeRel(c: var TCandidate, f, aOrig: PType, doBind = true): TTypeRelation =
|
||||||
if sameDistinctTypes(f, a): result = isEqual
|
if sameDistinctTypes(f, a): result = isEqual
|
||||||
elif f.base.kind == tyAnything: result = isGeneric
|
elif f.base.kind == tyAnything: result = isGeneric
|
||||||
elif c.coerceDistincts: result = typeRel(c, f.base, a)
|
elif c.coerceDistincts: result = typeRel(c, f.base, a)
|
||||||
|
elif a.kind == tyNil and f.base.kind in NilableTypes:
|
||||||
|
result = f.allowsNil
|
||||||
elif c.coerceDistincts: result = typeRel(c, f.base, a)
|
elif c.coerceDistincts: result = typeRel(c, f.base, a)
|
||||||
of tySet:
|
of tySet:
|
||||||
if a.kind == tySet:
|
if a.kind == tySet:
|
||||||
|
|
|
||||||
47
tests/distinct/tnil.nim
Normal file
47
tests/distinct/tnil.nim
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
discard """
|
||||||
|
file: "tnil.nim"
|
||||||
|
output: '''0x1
|
||||||
|
|
||||||
|
nil
|
||||||
|
|
||||||
|
nil
|
||||||
|
|
||||||
|
'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
type
|
||||||
|
MyPointer = distinct pointer
|
||||||
|
MyString = distinct string
|
||||||
|
MyStringNotNil = distinct (string not nil)
|
||||||
|
MyInt = distinct int
|
||||||
|
|
||||||
|
proc foo(a: MyPointer) =
|
||||||
|
echo a.repr
|
||||||
|
|
||||||
|
foo(cast[MyPointer](1))
|
||||||
|
foo(cast[MyPointer](nil))
|
||||||
|
foo(nil)
|
||||||
|
|
||||||
|
var p: MyPointer
|
||||||
|
p = cast[MyPointer](1)
|
||||||
|
p = cast[MyPointer](nil)
|
||||||
|
p = nil.MyPointer
|
||||||
|
p = nil
|
||||||
|
|
||||||
|
var c: MyString
|
||||||
|
c = "Test".MyString
|
||||||
|
c = nil.MyString
|
||||||
|
c = nil
|
||||||
|
|
||||||
|
p = nil
|
||||||
|
doAssert(compiles(c = p) == false)
|
||||||
|
|
||||||
|
var n: MyStringNotNil = "Test".MyStringNotNil # Cannot prove warning ...
|
||||||
|
n = "Test".MyStringNotNil
|
||||||
|
doAssert(compiles(n = nil.MyStringNotNil) == false)
|
||||||
|
doAssert(compiles(n = nil.MyStringNotNil) == false)
|
||||||
|
doAssert(compiles(n = nil) == false)
|
||||||
|
|
||||||
|
var i: MyInt
|
||||||
|
i = 1.MyInt
|
||||||
|
doAssert(compiles(i = nil) == false)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue