Skip to content

try weakened distinct #24967

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

Open
wants to merge 4 commits into
base: devel
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions compiler/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ type
tfIsOutParam
tfSendable
tfImplicitStatic
tfWeakened # for tyDistinct means "weak distinct"

TTypeFlags* = set[TTypeFlag]

Expand Down
7 changes: 4 additions & 3 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1712,10 +1712,11 @@ proc semSubscript(c: PContext, n: PNode, flags: TExprFlags, afterOverloading = f
result = n
result.typ() = elemType(arr)
# Other types have a bit more of leeway
elif n[1].typ.skipTypes(abstractRange-{tyDistinct}).kind in
elif n[1].typ.skipTypes(abstractRange).kind in
{tyInt..tyInt64, tyUInt..tyUInt64}:
result = n
result.typ() = elemType(arr)
if n[1].typ.kind != tyDistinct or tfWeakened in n[1].typ.flags:
result = n
result.typ() = elemType(arr)
of tyTypeDesc:
# The result so far is a tyTypeDesc bound
# a tyGenericBody. The line below will substitute
Expand Down
5 changes: 5 additions & 0 deletions compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2291,6 +2291,11 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
result = s.typ.base
elif prev == nil:
result = s.typ
elif prev.sym != nil and sfImportc in prev.sym.flags:
let tmp = newNode(nkDistinctTy)
tmp.add n
result = semDistinct(c, tmp, prev)
result.flags.incl tfWeakened
else:
let alias = maybeAliasType(c, s.typ, prev)
if alias != nil:
Expand Down
25 changes: 20 additions & 5 deletions compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ proc sumGeneric(t: PType): int =
while true:
case t.kind
of tyAlias, tySink, tyNot: t = t.skipModifier
of tyArray, tyRef, tyPtr, tyDistinct, tyUncheckedArray,
of tyArray, tyRef, tyPtr, tyUncheckedArray,
tyOpenArray, tyVarargs, tySet, tyRange, tySequence,
tyLent, tyOwned, tyVar:
t = t.elementType
Expand Down Expand Up @@ -364,6 +364,9 @@ proc sumGeneric(t: PType): int =
for _, a in t.paramTypes:
result += sumGeneric(a)
break
of tyDistinct:
result += ord(tfWeakened notin t.flags)
t = t.base
else:
if t.isConcept:
result += t.reduceToBase.conceptBody.len
Expand Down Expand Up @@ -1357,6 +1360,13 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
# Foo[templateCall(T)] shouldn't fail early if Foo has a constraint
# and we can't evaluate `templateCall(T)` yet
return isGeneric
of tyDistinct:
if f.kind != tyDistinct:
let coerceDistincts = c.coerceDistincts or tfWeakened in a.flags
if coerceDistincts:
inc c.inheritancePenalty
return typeRel(c, f, a.base, flags)

else: discard

case f.kind
Expand Down Expand Up @@ -1551,12 +1561,17 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
inc c.inheritancePenalty, depth + ord(c.inheritancePenalty < 0)
result = isSubtype
of tyDistinct:
a = a.skipTypes({tyOwned, tyGenericInst, tyRange})
let af = a.skipTypes({tyOwned, tyGenericInst, tyRange})
let coerceDistincts = c.coerceDistincts or tfWeakened in f.flags
if a.kind == tyDistinct:
if sameDistinctTypes(f, a): result = isEqual
if sameDistinctTypes(f, af): result = isEqual
#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 coerceDistincts:
inc c.inheritancePenalty
result = typeRel(c, f.base, a, flags)
elif coerceDistincts:
inc c.inheritancePenalty
result = typeRel(c, f.base, a, flags)
of tySet:
if a.kind == tySet:
if f[0].kind != tyGenericParam and a[0].kind == tyEmpty:
Expand Down
2 changes: 2 additions & 0 deletions tests/distinct/tweakened.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let bar = @[1.cdouble]
let foo: seq[float] = bar
Loading