Skip to content

Commit

Permalink
reject or types matching multiple types for int literals
Browse files Browse the repository at this point in the history
fixes #4858
  • Loading branch information
metagn committed Aug 25, 2024
1 parent 4ef06a5 commit 03f16e4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1698,14 +1698,18 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
result = isNone
let oldInheritancePenalty = c.inheritancePenalty
var minInheritance = maxInheritancePenalty
var intLitConvCount = 0
for branch in f.kids:
c.inheritancePenalty = -1
let x = typeRel(c, branch, aOrig, flags)
if x == isFromIntLit:
inc intLitConvCount
if x >= result:
if c.inheritancePenalty > -1:
minInheritance = min(minInheritance, c.inheritancePenalty)
result = x
if result >= isIntConv:
if result >= isIntConv and not
(result == isFromIntLit and intLitConvCount > 1):
if minInheritance < maxInheritancePenalty:
c.inheritancePenalty = oldInheritancePenalty + minInheritance
if result > isGeneric: result = isGeneric
Expand Down
8 changes: 8 additions & 0 deletions tests/overload/tambiguousintlit.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
block: # issue #4858
type
SomeType = object
field1: uint
proc namedProc(an: var SomeType, b: SomeUnsignedInt) = discard
var t = SomeType()
namedProc(t, 0) #[tt.Error
^ type mismatch: got <SomeType, int literal(0)>]#

0 comments on commit 03f16e4

Please sign in to comment.