hylo-lang / hylo

The Hylo programming language
https://www.hylo-lang.org
Apache License 2.0
1.23k stars 56 forks source link

Crash in TypeChecker.swift #1492

Open tcbrindle opened 4 months ago

tcbrindle commented 4 months ago

Attempting to compile the following Hylo source:

trait BidirectionalCollection: Collection {
    fun position(before: Position) -> Position
}

public conformance Array: BidirectionalCollection {
    public fun position(before pos: Int) -> Int
    {
        return pos - 1
    }
}

type ReversedCollection<Base: BidirectionalCollection & Movable & Deinitializable>:
    BidirectionalCollection, Movable, Deinitializable
{
    var base: Base

    public memberwise init

    public typealias Element = Base.Element
    public typealias Position = Base.Position

    public fun start_position() -> Position { return base.end_position() }

    public fun end_position() -> Position { return base.start_position() }

    public fun position(after pos: Position) -> Position { return base.position(before: pos) }

    public fun position(before pos: Position) -> Position { return base.position(after: pos) }

    public subscript(_ pos: Position): Element { yield base[position(before: pos)] }
}

public extension BidirectionalCollection where Self: Movable & Deinitializable {
    public fun reversed() sink -> ReversedCollection<Self>
    {
        return ReversedCollection<Self>(base: self)
    }
}

public fun main()
{
    var array = Array<Int>();
    array.append(0)
    array.append(1)
    array.append(2)

    for let i in array.reversed() { print(i) }
}

results in an assertion failure:

FrontEnd/TypeChecker.swift:2125: Fatal error: Unexpectedly found nil while unwrapping an Optional value
Illegal instruction: 4
tcbrindle commented 4 months ago

Removing the extension and instead using ReversedCollection directly in the for loop, i.e. changing the last line to

for let i in ReversedCollection(base: array) { print(i) }

results in a similar crash in a different source file:

begin depolymorphization pass.
IR/Module+Depolymorphize.swift:292: Fatal error: Unexpectedly found nil while unwrapping an Optional value
Illegal instruction: 4
kyouko-taiga commented 4 months ago

I think the first issue (the crash in the type checker) is due to the current algorithm for constructing generic environment, which is being replaced by a more principled approach in #1482.

The second issue is caused by a bug during monomorphization that I should investigate.

kyouko-taiga commented 2 months ago

The first issue has been fixed. The second issue is caused by the for loop and I suspect it is related to https://github.com/hylo-lang/hylo/issues/1510.