From db355b96548811002191424717aefe460573a569 Mon Sep 17 00:00:00 2001 From: Ryan McConnell Date: Sun, 25 May 2025 15:52:53 -0400 Subject: [PATCH 1/4] init --- compiler/ast.nim | 1 + compiler/semtypes.nim | 10 ++++++++-- compiler/sigmatch.nim | 21 ++++++++++++++++++--- lib/pure/net.nim | 2 +- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/compiler/ast.nim b/compiler/ast.nim index 13f7890bcd2d8..0dd27254c65d4 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -399,6 +399,7 @@ type tfIsOutParam tfSendable tfImplicitStatic + tfWeakened # for tyDistinct means "weak distinct" TTypeFlags* = set[TTypeFlag] diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index b3839ef6333e7..4549bd8da5628 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -332,7 +332,7 @@ proc addSonSkipIntLitChecked(c: PContext; father, son: PType; it: PNode, id: IdG propagateToOwner(father, s) proc semDistinct(c: PContext, n: PNode, prev: PType): PType = - if n.len == 0: return newConstraint(c, tyDistinct) + if n.kind != nkDistinctTy or n.len == 0: return newConstraint(c, tyDistinct) if prevIsKind(prev, tyDistinct): # the symbol already has a distinct type (likely resem), don't create a new type return skipGenericPrev(prev) @@ -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: @@ -2339,7 +2344,8 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType = of nkPtrTy: result = semAnyRef(c, n, tyPtr, prev) of nkVarTy: result = semVarOutType(c, n, prev, {}) of nkOutTy: result = semVarOutType(c, n, prev, {tfIsOutParam}) - of nkDistinctTy: result = semDistinct(c, n, prev) + of nkDistinctTy: + result = semDistinct(c, n, prev) of nkStaticTy: result = semStaticType(c, n[0], prev) of nkProcTy, nkIteratorTy: if n.len == 0 or n[0].kind == nkEmpty: diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index e486f3a47f2c0..220ca76f21434 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -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 @@ -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 @@ -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 @@ -1552,11 +1562,16 @@ proc typeRel(c: var TCandidate, f, aOrig: PType, result = isSubtype of tyDistinct: a = a.skipTypes({tyOwned, tyGenericInst, tyRange}) + let coerceDistincts = c.coerceDistincts or tfWeakened in f.flags if a.kind == tyDistinct: 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 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: diff --git a/lib/pure/net.nim b/lib/pure/net.nim index df125e1733ccd..325ed824cbf7b 100644 --- a/lib/pure/net.nim +++ b/lib/pure/net.nim @@ -1374,7 +1374,7 @@ proc uniRecv(socket: Socket, buffer: pointer, size, flags: cint): int = proc readIntoBuf(socket: Socket, flags: int32): int = result = 0 - result = uniRecv(socket, addr(socket.buffer), socket.buffer.high, flags) + result = uniRecv(socket, addr(socket.buffer), socket.buffer.high.cint, flags) if result < 0: # Save it in case it gets reset (the Nim codegen occasionally may call # Win API functions which reset it). From a75e47cd91545d793e1143d049fde49f47a7bbdf Mon Sep 17 00:00:00 2001 From: Ryan McConnell Date: Sun, 25 May 2025 17:23:43 -0400 Subject: [PATCH 2/4] meh --- compiler/sigmatch.nim | 4 ++-- tests/distinct/tweakened.nim | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 tests/distinct/tweakened.nim diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index 220ca76f21434..06dd1a3761959 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -1561,10 +1561,10 @@ 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 coerceDistincts: inc c.inheritancePenalty diff --git a/tests/distinct/tweakened.nim b/tests/distinct/tweakened.nim new file mode 100644 index 0000000000000..d818d09651eb1 --- /dev/null +++ b/tests/distinct/tweakened.nim @@ -0,0 +1,2 @@ +let bar = @[1.cdouble] +let foo: seq[float] = bar From 3acb38e43f78b981394f88478342eca979cc192a Mon Sep 17 00:00:00 2001 From: Ryan McConnell Date: Sun, 25 May 2025 20:39:49 -0400 Subject: [PATCH 3/4] fix subscript magic --- compiler/semexprs.nim | 7 ++++--- lib/pure/net.nim | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 5e2a5d0a72289..0bd2d27f95aa0 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -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 diff --git a/lib/pure/net.nim b/lib/pure/net.nim index 325ed824cbf7b..df125e1733ccd 100644 --- a/lib/pure/net.nim +++ b/lib/pure/net.nim @@ -1374,7 +1374,7 @@ proc uniRecv(socket: Socket, buffer: pointer, size, flags: cint): int = proc readIntoBuf(socket: Socket, flags: int32): int = result = 0 - result = uniRecv(socket, addr(socket.buffer), socket.buffer.high.cint, flags) + result = uniRecv(socket, addr(socket.buffer), socket.buffer.high, flags) if result < 0: # Save it in case it gets reset (the Nim codegen occasionally may call # Win API functions which reset it). From c020bcbb6de4dd27499e9ed41338b27f5d502dd0 Mon Sep 17 00:00:00 2001 From: Ryan McConnell Date: Sun, 25 May 2025 23:09:04 -0400 Subject: [PATCH 4/4] delete --- compiler/semtypes.nim | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index 4549bd8da5628..5e3cc0eace30c 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -332,7 +332,7 @@ proc addSonSkipIntLitChecked(c: PContext; father, son: PType; it: PNode, id: IdG propagateToOwner(father, s) proc semDistinct(c: PContext, n: PNode, prev: PType): PType = - if n.kind != nkDistinctTy or n.len == 0: return newConstraint(c, tyDistinct) + if n.len == 0: return newConstraint(c, tyDistinct) if prevIsKind(prev, tyDistinct): # the symbol already has a distinct type (likely resem), don't create a new type return skipGenericPrev(prev) @@ -2344,8 +2344,7 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType = of nkPtrTy: result = semAnyRef(c, n, tyPtr, prev) of nkVarTy: result = semVarOutType(c, n, prev, {}) of nkOutTy: result = semVarOutType(c, n, prev, {tfIsOutParam}) - of nkDistinctTy: - result = semDistinct(c, n, prev) + of nkDistinctTy: result = semDistinct(c, n, prev) of nkStaticTy: result = semStaticType(c, n[0], prev) of nkProcTy, nkIteratorTy: if n.len == 0 or n[0].kind == nkEmpty: