Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reject or types matching multiple types for int literals #24012

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,16 @@ proc handleRange(c: PContext, f, a: PType, min, max: TTypeKind): TTypeRelation =
let k = ab.kind
let nf = c.config.normalizeKind(f.kind)
let na = c.config.normalizeKind(k)
if k == f.kind: result = isSubrange
elif k == tyInt and f.kind in {tyRange, tyInt..tyInt64,
tyUInt..tyUInt64} and
if k == f.kind:
# `a` is a range type matching its base type
# see very bottom for range types matching different types
if isIntLit(ab):
# range type can only give isFromIntLit for base type
result = isFromIntLit
else:
result = isSubrange
elif a.kind == tyInt and f.kind in {tyRange, tyInt..tyInt64,
tyUInt..tyUInt64} and
isIntLit(ab) and getInt(ab.n) >= firstOrd(nil, f) and
getInt(ab.n) <= lastOrd(nil, f):
# passing 'nil' to firstOrd/lastOrd here as type checking rules should
Expand Down Expand Up @@ -1700,14 +1707,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)>]#
Loading