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:
parent
7d7886b729
commit
1182216381
4 changed files with 30 additions and 15 deletions
|
|
@ -29,6 +29,8 @@
|
||||||
|
|
||||||
- `nimPreviewDotLikeOps` is going to be removed or deprecated.
|
- `nimPreviewDotLikeOps` is going to be removed or deprecated.
|
||||||
|
|
||||||
|
- `nil` is no longer a valid value for distinct pointer types.
|
||||||
|
|
||||||
## Standard library additions and changes
|
## Standard library additions and changes
|
||||||
|
|
||||||
[//]: # "Changes:"
|
[//]: # "Changes:"
|
||||||
|
|
|
||||||
|
|
@ -1305,8 +1305,6 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
|
||||||
if sameDistinctTypes(f, a): result = isEqual
|
if sameDistinctTypes(f, a): result = isEqual
|
||||||
#elif f.base.kind == tyAnything: result = isGeneric # issue 4435
|
#elif f.base.kind == tyAnything: result = isGeneric # issue 4435
|
||||||
elif c.coerceDistincts: result = typeRel(c, f.base, a, flags)
|
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)
|
elif c.coerceDistincts: result = typeRel(c, f.base, a, flags)
|
||||||
of tySet:
|
of tySet:
|
||||||
if a.kind == tySet:
|
if a.kind == tySet:
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,21 @@
|
||||||
discard """
|
|
||||||
output: '''
|
|
||||||
1
|
|
||||||
0
|
|
||||||
0
|
|
||||||
'''
|
|
||||||
"""
|
|
||||||
{.experimental: "notnil".}
|
{.experimental: "notnil".}
|
||||||
type
|
type
|
||||||
MyPointer = distinct pointer
|
MyPointer = distinct pointer
|
||||||
MyString = distinct string
|
MyString = distinct string
|
||||||
MyInt = distinct int
|
MyInt = distinct int
|
||||||
|
|
||||||
proc foo(a: MyPointer) =
|
proc foo(a: MyPointer): int =
|
||||||
# workaround a Windows 'repr' difference:
|
# workaround a Windows 'repr' difference:
|
||||||
echo cast[int](a)
|
cast[int](a)
|
||||||
|
|
||||||
foo(cast[MyPointer](1))
|
doAssert foo(cast[MyPointer](1)) == 1
|
||||||
foo(cast[MyPointer](nil))
|
doAssert foo(cast[MyPointer](nil)) == 0
|
||||||
foo(nil)
|
doAssert foo(MyPointer(nil)) == 0
|
||||||
|
|
||||||
var p: MyPointer
|
var p: MyPointer
|
||||||
p = cast[MyPointer](1)
|
p = cast[MyPointer](1)
|
||||||
p = cast[MyPointer](nil)
|
p = cast[MyPointer](nil)
|
||||||
p = nil.MyPointer
|
p = nil.MyPointer
|
||||||
p = nil
|
|
||||||
|
|
||||||
var i: MyInt
|
var i: MyInt
|
||||||
i = 1.MyInt
|
i = 1.MyInt
|
||||||
|
|
|
||||||
23
tests/errmsgs/tdistinct_nil.nim
Normal file
23
tests/errmsgs/tdistinct_nil.nim
Normal 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)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue