diff options
-rw-r--r-- | changelog.md | 2 | ||||
-rw-r--r-- | compiler/sigmatch.nim | 2 | ||||
-rw-r--r-- | tests/distinct/tnil.nim | 18 | ||||
-rw-r--r-- | tests/errmsgs/tdistinct_nil.nim | 23 |
4 files changed, 30 insertions, 15 deletions
diff --git a/changelog.md b/changelog.md index d574d1074..5049fe204 100644 --- a/changelog.md +++ b/changelog.md @@ -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:" diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index f195c4e45..760ee697d 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -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: diff --git a/tests/distinct/tnil.nim b/tests/distinct/tnil.nim index 5bdb97f37..b54c07432 100644 --- a/tests/distinct/tnil.nim +++ b/tests/distinct/tnil.nim @@ -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 diff --git a/tests/errmsgs/tdistinct_nil.nim b/tests/errmsgs/tdistinct_nil.nim new file mode 100644 index 000000000..a2b403293 --- /dev/null +++ b/tests/errmsgs/tdistinct_nil.nim @@ -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) |