fixes #23902; Compiler infers sink in return type from auto (#23904)

fixes #23902
This commit is contained in:
ringabout
2024-08-11 16:12:00 +08:00
committed by GitHub
parent d164f87fbc
commit 2a2474d395
2 changed files with 11 additions and 3 deletions

View File

@@ -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

View File

@@ -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)