Skip to content

Commit

Permalink
Merge pull request #1015 from hylo-lang/custom-move
Browse files Browse the repository at this point in the history
Implement IR support for custom conformances to 'Movable'
  • Loading branch information
kyouko-taiga authored Sep 17, 2023
2 parents 6652164 + f0299fd commit 784a6be
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
35 changes: 19 additions & 16 deletions Sources/IR/Module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,24 @@ public struct Module {
functions[identity] = function
}

/// Returns the identity of the IR function corresponding to `i`.
mutating func demandDeclaration(lowering i: Core.Conformance.Implementation) -> Function.ID {
switch i {
case .concrete(let d):
return demandDeclaration(lowering: d)!
case .synthetic(let d):
return demandDeclaration(lowering: d)
}
}

/// Returns the identity of the IR function corresponding to `d`, or `nil` if `d` can't be
/// lowered to an IR function.
mutating func demandDeclaration(lowering d: AnyDeclID) -> Function.ID? {
switch d.kind {
case FunctionDecl.self:
return demandDeclaration(lowering: FunctionDecl.ID(d)!)
case MethodImpl.self:
return demandDeclaration(lowering: MethodImpl.ID(d)!)
case InitializerDecl.self:
return demandDeclaration(lowering: InitializerDecl.ID(d)!)
case SubscriptImpl.self:
Expand Down Expand Up @@ -393,31 +405,22 @@ public struct Module {

/// Returns the identity of the IR function implementing the deinitializer defined in
/// conformance `c`.
mutating func demandDeinitDeclaration(from c: Core.Conformance) -> Function.ID {
mutating func demandDeinitDeclaration(
from c: Core.Conformance
) -> Function.ID {
let d = program.ast.deinitRequirement()
switch c.implementations[d]! {
case .concrete(let s):
return demandDeclaration(lowering: FunctionDecl.ID(s)!)
case .synthetic(let s):
return demandDeclaration(lowering: s)
}
return demandDeclaration(lowering: c.implementations[d]!)
}

/// Returns the identity of the IR function implementing the `k` variant move-operator defined in
/// conformance `c`.
/// Returns the identity of the IR function implementing the `k` variant move-operation defined
/// in conformance `c`.
///
/// - Requires: `k` is either `.set` or `.inout`
mutating func demandMoveOperatorDeclaration(
_ k: AccessEffect, from c: Core.Conformance
) -> Function.ID {
let d = program.ast.moveRequirement(k)
switch c.implementations[d]! {
case .concrete:
UNIMPLEMENTED()

case .synthetic(let s):
return demandDeclaration(lowering: s)
}
return demandDeclaration(lowering: c.implementations[d]!)
}

/// Returns the lowered declarations of `d`'s parameters.
Expand Down
32 changes: 32 additions & 0 deletions Tests/EndToEndTests/TestCases/CustomMove.hylo
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//- compileAndRun expecting: success

type A: Deinitializable {
var witness: Int
public var x: Int
public init(x: sink Int) {
&self.x = x
&self.witness = 0
}
}

conformance A: Movable {
public fun take_value(from source: sink Self) -> {self: Self, Void} {
set {
&self.x = source.x
&self.witness = 0
}
inout {
&self.x = source.x
&self.witness += 1
}
}
}

public fun main() {
var s = A(x: 1)
&s = A(x: 2)
&s = A(x: 2)

precondition(s.x == 2)
precondition(s.witness == 2)
}

0 comments on commit 784a6be

Please sign in to comment.