From 2a2474d395afb58ef71897530ff334f4725514b4 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Sun, 11 Aug 2024 16:12:00 +0800 Subject: [PATCH] fixes #23902; Compiler infers sink in return type from auto (#23904) fixes #23902 --- compiler/semexprs.nim | 6 +++--- tests/destructor/tsink.nim | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 54a8440351..c1e74b4575 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -2003,7 +2003,7 @@ proc semAsgn(c: PContext, n: PNode; mode=asgnNormal): PNode = if rhsTyp.kind in tyUserTypeClasses and rhsTyp.isResolvedUserTypeClass: rhsTyp = rhsTyp.last if lhs.sym.typ.kind == tyAnything: - rhsTyp = rhsTyp.skipIntLit(c.idgen) + rhsTyp = rhsTyp.skipTypes({tySink}).skipIntLit(c.idgen) if cmpTypes(c, lhs.typ, rhsTyp) in {isGeneric, isEqual}: internalAssert c.config, c.p.resultSym != nil # Make sure the type is valid for the result variable @@ -2825,7 +2825,7 @@ proc semTupleFieldsConstr(c: PContext, n: PNode, flags: TExprFlags; expectedType n[i][1].typ = errorType(c) var f = newSymS(skField, n[i][0], c) - f.typ = skipIntLit(n[i][1].typ, c.idgen) + f.typ = skipIntLit(n[i][1].typ.skipTypes({tySink}), c.idgen) f.position = i rawAddSon(typ, f.typ) typ.n.add newSymNode(f) @@ -2851,7 +2851,7 @@ proc semTuplePositionsConstr(c: PContext, n: PNode, flags: TExprFlags; expectedT # `const foo = [(1, {}), (2, {false})]`, # `const foo = if true: (0, nil) else: (1, new(int))` n[i] = fitNode(c, expectedElemType, n[i], n[i].info) - addSonSkipIntLit(typ, n[i].typ, c.idgen) + addSonSkipIntLit(typ, n[i].typ.skipTypes({tySink}), c.idgen) result.typ = typ include semobjconstr diff --git a/tests/destructor/tsink.nim b/tests/destructor/tsink.nim index 82cbdfbe5d..e214e4cca3 100644 --- a/tests/destructor/tsink.nim +++ b/tests/destructor/tsink.nim @@ -14,3 +14,11 @@ proc foo = # bug #23359 doAssert bar.value == 42 foo() + +block: # bug #23902 + proc foo(a: sink string): auto = (a, a) + + proc bar(a: sink int): auto = return a + + proc foo(a: sink string) = + var x = (a, a)