diff options
author | metagn <metagngn@gmail.com> | 2024-09-23 18:18:22 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-23 18:18:22 +0300 |
commit | b9de2bb4f3864d264c3aeffdc630227448ad8c14 (patch) | |
tree | c05007bc45e2af21c18f64f59dbaa2abd76bc068 | |
parent | 6f6e34ebb037cdc83f8ec24b0422e70fc70d7107 (diff) | |
download | Nim-b9de2bb4f3864d264c3aeffdc630227448ad8c14.tar.gz |
fix `nil` literal giving itself type `untyped`/`typed` [backport] (#24165)
fixes #24164, regression from #20091 The expression `nil` as the default value of template parameter `x: untyped` is typechecked with expected type `untyped` since #20091. The expected type is checked if it matches the `nil` literal with a match better than a subtype match, and the type is set to it if it does. However `untyped` matches with a generic match which is better, so the `nil` literal has type `untyped`. This breaks type matching for the literal. So if the expected type is `untyped` or `typed`, it is now ignored and the `nil` literal just has the `nil` type.
-rw-r--r-- | compiler/semexprs.nim | 2 | ||||
-rw-r--r-- | tests/types/ttopdowninference.nim | 6 |
2 files changed, 7 insertions, 1 deletions
diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 41493b046..f5b32637e 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -3314,7 +3314,7 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}, expectedType: PType of nkNilLit: if result.typ == nil: result.typ = getNilType(c) - if expectedType != nil: + if expectedType != nil and expectedType.kind notin {tyUntyped, tyTyped}: var m = newCandidate(c, result.typ) if typeRel(m, expectedType, result.typ) >= isSubtype: result.typ = expectedType diff --git a/tests/types/ttopdowninference.nim b/tests/types/ttopdowninference.nim index 2a26e0e34..765761e99 100644 --- a/tests/types/ttopdowninference.nim +++ b/tests/types/ttopdowninference.nim @@ -325,3 +325,9 @@ block: # bug #22180 else: (ref A)(nil) doAssert y.isNil + +block: # issue #24164, related regression + proc foo(x: proc ()) = discard + template bar(x: untyped = nil) = + foo(x) + bar() |