remove a special case in sigmatch; distinct pointer types no longer match nil type (#20251)

* remove a special case in sigmatch; distinct pointer types no longer match `nil` type

* add tests

* fixes tests

* Update changelog.md

Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>
This commit is contained in:
ringabout
2022-08-24 01:27:20 +08:00
committed by GitHub
parent 7d7886b729
commit 1182216381
4 changed files with 30 additions and 15 deletions

View File

@@ -29,6 +29,8 @@
- `nimPreviewDotLikeOps` is going to be removed or deprecated.
- `nil` is no longer a valid value for distinct pointer types.
## Standard library additions and changes
[//]: # "Changes:"

View File

@@ -1305,8 +1305,6 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
if sameDistinctTypes(f, a): result = isEqual
#elif f.base.kind == tyAnything: result = isGeneric # issue 4435
elif c.coerceDistincts: result = typeRel(c, f.base, a, flags)
elif a.kind == tyNil and f.base.kind in NilableTypes:
result = f.allowsNil # XXX remove this typing rule, it is not in the spec
elif c.coerceDistincts: result = typeRel(c, f.base, a, flags)
of tySet:
if a.kind == tySet:

View File

@@ -1,29 +1,21 @@
discard """
output: '''
1
0
0
'''
"""
{.experimental: "notnil".}
type
MyPointer = distinct pointer
MyString = distinct string
MyInt = distinct int
proc foo(a: MyPointer) =
proc foo(a: MyPointer): int =
# workaround a Windows 'repr' difference:
echo cast[int](a)
cast[int](a)
foo(cast[MyPointer](1))
foo(cast[MyPointer](nil))
foo(nil)
doAssert foo(cast[MyPointer](1)) == 1
doAssert foo(cast[MyPointer](nil)) == 0
doAssert foo(MyPointer(nil)) == 0
var p: MyPointer
p = cast[MyPointer](1)
p = cast[MyPointer](nil)
p = nil.MyPointer
p = nil
var i: MyInt
i = 1.MyInt

View File

@@ -0,0 +1,23 @@
discard """
cmd: "nim check $file"
action: reject
nimout: '''
tdistinct_nil.nim(23, 4) Error: type mismatch: got <typeof(nil)>
but expected one of:
proc foo(x: DistinctPointer)
first type mismatch at position: 1
required type for x: DistinctPointer
but expression 'nil' is of type: typeof(nil)
expression: foo(nil)
'''
"""
type
DistinctPointer = distinct pointer
proc foo(x: DistinctPointer) =
discard
foo(DistinctPointer(nil))
foo(nil)