-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
[generic] T:seq wrong type inference: can't instantiate proc fun1[T1:seq, T2:seq](a1:T1, a2:T2)
with fun1(@[1], @[1.0])
#8435
Comments
proc fun1[T1:seq, T2:seq](a1:T1, a2:T2) is desugared as proc fun1[T; T1:seq[T], T2:seq[T]](a1:T1, a2:T2) and due to the bind-once nature of generic parameters you can't have proc fun1[U,V; T1:seq[U], T2:seq[V]](a1:T1, a2:T2) I'm not completely sure this is a bug. |
I think that it's more logical to desugar |
indeed, that should be the way desugar works. If I wanted the same type I would write: |
The problem is slightly more complex, the seq*{.magic: "Seq".}[T] ## Generic type to construct sequences. and the generic parameters The first idea that comes to mind is to extend diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim
index 05a9d9882..199177748 100644
--- a/compiler/semtypes.nim
+++ b/compiler/semtypes.nim
@@ -980,11 +980,38 @@ proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
result = addImplicitGeneric(copyType(paramType, getCurrOwner(c), false))
of tyGenericParam:
- markUsed(c.config, info, paramType.sym, c.graph.usageSym)
- styleCheckUse(info, paramType.sym)
- if tfWildcard in paramType.flags:
- paramType.flags.excl tfWildcard
- paramType.sym.kind = skType
+ proc copyTree(t: PType, keepId: bool = false): PType =
+ if t == nil: return
+ result = copyType(t, t.owner, keepId)
+ for i in 0..result.sons.high:
+ result.sons[i] = copyTree(result.sons[i], keepId)
+
+ proc inGen(sym: PIdent): bool =
+ for i in countup(0, genericParams.len - 1):
+ if genericParams.sons[i].sym.name.id == sym.id: return true
+ return false
+
+ proc foo(t: PType, closure: RootRef): PType =
+ result = t
+ if t.kind == tyGenericParam and t.sym != nil and
+ not inGen(t.sym.name):
+ result = addImplicitGenericImpl(cast[PContext](closure),
+ copyType(t, t.owner, false), nil)
+
+ result = copyTree(paramType, true)
+ if result.sons.len > 0:
+ result.sons[^1] = mutateType(result.sons[^1], foo, cast[RootRef](c))
+
+ # if mdbg and genericParams != nil:
+ # echo "Generics"
+ # for p in genericParams: debug p
+ # echo "--------"
+
+ markUsed(c.config, info, result.sym, c.graph.usageSym)
+ styleCheckUse(info, result.sym)
+ if tfWildcard in result.flags:
+ result.flags.excl tfWildcard
+ result.sym.kind = skType
else: discard
diff --git a/compiler/types.nim b/compiler/types.nim
index 4180d34a7..89bedeff3 100644
--- a/compiler/types.nim
+++ b/compiler/types.nim
@@ -27,7 +27,7 @@ proc base*(t: PType): PType =
# ------------------- type iterator: ----------------------------------------
type
TTypeIter* = proc (t: PType, closure: RootRef): bool {.nimcall.} # true if iteration should stop
- TTypeMutator* = proc (t: PType, closure: RootRef): PType {.nimcall.} # copy t and mutate it
+ TTypeMutator* = proc (t: PType, closure: RootRef): PType # copy t and mutate it
TTypePredicate* = proc (t: PType): bool {.nimcall.}
proc iterOverType*(t: PType, iter: TTypeIter, closure: RootRef): bool |
@LemonBoy why not submit a PR with this? (easier for reviewers to test it out) if not I may submit one (and attribute you of course) to move forward with fixing this |
I'm waiting on some feedback about the underlying idea both from the user standpoint and the PL design one. |
Well, the rationale why In any case, this is a tricky language design issue with subtle tradeoffs, but we've already settled on the current rules quite a while ago and there should be a lot of code out there depending on the rules. The type Vector[T; N] = object # details omitted for clarity
proc `*`(v: Vector, s: float): Vector = ... Relevant section from the manual: |
The text was updated successfully, but these errors were encountered: