Skip to content

[SR-985] Nested generic type in function is not allowed #43597

@an0

Description

@an0
mannequin
Previous ID SR-985
Radar None
Original Reporter @an0
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: f3e4c0c6558ec266c5547d7f7e079fa3

Issue Description:

This code runs in playground but repl and swiftc reports error:

type 'Stack' nested in generic function 'foo' is not allowed

func foo() {
    class Stack<Element> {
        init() {
            array = []
        }
        init(e: Element) {
            array = [e]
        }
        init<S: Sequence where S.Iterator.Element == Element>(es: S) {
            array = [Element](es)
        }
        var array: [Element]
        func push(_ e: Element) {
            array.append(e)
        }
        func push<S: Sequence where S.Iterator.Element == Element>(_ es: S) {
            array.append(contentsOf: es)
        }
        func pop() -> Element? {
            return array.popLast()
        }
        var isEmpty: Bool {
            return array.isEmpty
        }
    }
    
    func tailRecursive(_ stack: Stack<Int>) {
        guard let i = stack.pop() else { return }
        print(i)
        stack.push(i + 1)
        tailRecursive(stack)
    }
    
    tailRecursive(Stack(e: 0))
}

foo()

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itself

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions