Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
008ffa4
Infer generic bindings
Jul 22, 2023
bc08ff2
Simple test
Jul 22, 2023
257fd33
Add t
Jul 22, 2023
0c6d8bb
Allow it to work for templates too
Jul 22, 2023
d5cbc7e
Fix some builds by putting bindings in a template
Jul 22, 2023
f2ed402
Fix builtins
Jul 23, 2023
cf36fa8
Slightly more exotic seq test
Jul 23, 2023
a235f14
Test value-based generics using array
Jul 23, 2023
e331214
Pass expectedType into buildBindings
Jul 23, 2023
851fbcf
Put buildBindings into a proc
Jul 23, 2023
04f7e08
Manual entry
Jul 23, 2023
d607d26
Remove leftover `
Jul 23, 2023
f915036
Improve language used in the manual
Jul 24, 2023
165673b
Experimental flag and fix basic constructors
Jul 24, 2023
9e42bbc
Tiny commend cleanup
Jul 24, 2023
e66af09
Move to experimental manual
Jul 24, 2023
444c169
Use 'kind' so tuples continue to fail like before
Jul 24, 2023
b554162
Explicitly disallow tuples
Jul 24, 2023
1cad79f
Table test and document tuples
Jul 24, 2023
189c9e1
Test type reduction
Jul 24, 2023
6d85aeb
Disable inferGenericTypes check for CI tests
Jul 24, 2023
b006c4e
Remove tuple info in manual
Jul 24, 2023
347eb20
Always reduce types. Testing CI
Jul 26, 2023
2a8353c
Fixes
Jul 26, 2023
dccee38
Ignore tyGenericInst
Jul 26, 2023
be98387
Prevent binding already bound generic params
Jul 29, 2023
fe2b42e
tyUncheckedArray
Jul 29, 2023
524a289
Few more types
Jul 29, 2023
d926772
Update manual and check for flag again
Jul 29, 2023
fbde75b
Update tests/generics/treturn_inference.nim
Araq Aug 2, 2023
970fafc
var candidate, remove flag check again for CI
Aug 2, 2023
18465fa
Enable check once more
Aug 2, 2023
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
Prev Previous commit
Next Next commit
Test type reduction
  • Loading branch information
SirOlaf committed Jul 24, 2023
commit 189c9e1011d1dcea61b975fa4161a62d330130c7
32 changes: 32 additions & 0 deletions compiler/semcall.nim
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,38 @@ proc inheritBindings(c: PContext, x: TCandidate, expectedType: PType): TIdTable
if result.idTableGet(y[0].typ) == nil:
# pass it along directly
result.idTablePut(y[0].typ, expectedType)
elif resNode.kind in {nkTupleConstr, nkTupleTy}:
var
flatSyms: seq[PSym]
flatBinds: seq[PType]
var typeStack = @[(resNode.typ, expectedType)]
# TODO: Use this type reduction for the entire inheritance structure instead of just tuples
# Probably put into a proc
block outer:
while typeStack.len() > 0:
let (t, u) = typeStack.pop()
case t.kind
of tyTuple, tyGenericInvocation:
# nested, add all the types to stack
for i in countdown(t.sons.len() - 1, 0):
if i >= t.sons.len() or i >= u.sons.len():
break outer
typeStack.add((t.sons[i], u.sons[i]))
of tyGenericParam:
# fully reduced generic param
if t.sym notin flatSyms:
flatSyms.add(t.sym)
flatBinds.add(u)
of tyGenericBody:
discard # TODO: Combine into else later?
else:
# TODO: Remove/replace
doAssert false # for debugging
for i in 0 ..< flatSyms.len():
result.idTablePut(flatSyms[i].typ, flatBinds[i])
else:
# TODO: Remove/replace
doAssert false # for debugging

proc semResolvedCall(c: PContext, x: TCandidate,
n: PNode, flags: TExprFlags;
Expand Down
13 changes: 6 additions & 7 deletions tests/generics/treturn_inference.nim
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,23 @@ block:
var x: array[2, int] = giveArray()
doAssert x == [0, 1]

# Tuples are quite tough and don't work
#[
# tuples
block:
proc giveTuple[T, Z]: (T, Z, T) = discard
let x: (int, float, int) = giveTuple()
doAssert x is (int, float)
doAssert x[0] == 0 and x[1] == 0.0
doAssert x is (int, float, int)
doAssert x == (0, 0.0, 0)

proc giveNamedTuple[T, Z]: tuple[a: T, b: Z] = discard
let y: tuple[a: int, b: float] = giveTuple()
let y: tuple[a: int, b: float] = giveNamedTuple()
doAssert y is (int, float)
doAssert y is tuple[a: int, b: float]
doAssert y.a == 0 and y.b == 0.0
doAssert y == (0, 0.0)

proc giveNestedTuple[T, Z]: ((T, Z), Z) = discard
let z: ((int, float), float) = giveNestedTuple()
]#
doAssert z is ((int, float), float)
doAssert z == ((0, 0.0), 0.0)


# basic constructors
Expand Down