Skip to content

Commit

Permalink
Merge pull request #914 from hylo-lang/coercions
Browse files Browse the repository at this point in the history
Implement implicit coercions for let and inout bindings
  • Loading branch information
kyouko-taiga authored Sep 4, 2023
2 parents a4422be + 7bf524f commit 197c149
Show file tree
Hide file tree
Showing 38 changed files with 872 additions and 341 deletions.
20 changes: 10 additions & 10 deletions Sources/CLI/FullPathInFatalErrors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ func precondition(

/// Just like Swift.preconditionFailure, but includes the full file path in the diagnostic.
func preconditionFailure(
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) -> Never {
Swift.preconditionFailure(message(), file: (file), line: line)
}
Expand All @@ -28,19 +28,19 @@ func fatalError(

/// Just like Swift.assert, but includes the full file path in the diagnostic.
func assert(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) {
Swift.assert(condition(), message(), file: (file), line: line)
}

/// Just like Swift.assertionFailure, but includes the full file path in the diagnostic.
func assertionFailure(
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) {
Swift.assertionFailure(message(), file: (file), line: line)
}
1 change: 0 additions & 1 deletion Sources/CodeGen/FullPathInFatalErrors.swift

This file was deleted.

46 changes: 46 additions & 0 deletions Sources/CodeGen/FullPathInFatalErrors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/// Just like Swift.precondition, but includes the full file path in the diagnostic.
func precondition(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) {
Swift.precondition(condition(), message(), file: (file), line: line)
}

/// Just like Swift.preconditionFailure, but includes the full file path in the diagnostic.
func preconditionFailure(
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) -> Never {
Swift.preconditionFailure(message(), file: (file), line: line)
}

/// Just like Swift.fatalError, but includes the full file path in the diagnostic.
func fatalError(
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) -> Never {
Swift.fatalError(message(), file: (file), line: line)
}

/// Just like Swift.assert, but includes the full file path in the diagnostic.
func assert(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) {
Swift.assert(condition(), message(), file: (file), line: line)
}

/// Just like Swift.assertionFailure, but includes the full file path in the diagnostic.
func assertionFailure(
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) {
Swift.assertionFailure(message(), file: (file), line: line)
}
12 changes: 7 additions & 5 deletions Sources/CodeGen/LLVM/Transpilation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,6 @@ extension LLVM.Module {
case let v as TraitType:
return transpiledTrait(v, usedIn: m, from: ir)

case is IR.Poison:
let t = ir.llvm(c.type.ast, in: &self)
return LLVM.Poison(of: t)

case is IR.VoidConstant:
return LLVM.StructConstant(aggregating: [], in: &self)

Expand Down Expand Up @@ -920,7 +916,13 @@ extension LLVM.Module {
/// Inserts the transpilation of `i` at `insertionPoint`.
func insert(openUnion i: IR.InstructionID) {
let s = m[i] as! OpenUnion
register[.register(i)] = llvm(s.container)
let t = UnionType(m.type(of: s.container).ast)!

let baseType = ir.llvm(unionType: t, in: &self)
let container = llvm(s.container)
let indices = [i32.constant(0), i32.constant(0)]
register[.register(i)] = insertGetElementPointerInBounds(
of: container, typed: baseType, indices: indices, at: insertionPoint)
}

/// Inserts the transpilation of `i` at `insertionPoint`.
Expand Down
12 changes: 12 additions & 0 deletions Sources/Core/AccessEffect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ public enum AccessEffect: UInt8, Codable {
/// Value may be accessed with any of the other effects, depending on the context.
case yielded = 16

/// Creates an instance denoting the access granted to a binding introduced with `i`.
public init(_ i: BindingPattern.Introducer) {
switch i {
case .let:
self = .let
case .inout:
self = .inout
case .sinklet, .var:
self = .sink
}
}

}

extension AccessEffect: Comparable {
Expand Down
1 change: 0 additions & 1 deletion Sources/Core/FullPathInFatalErrors.swift

This file was deleted.

46 changes: 46 additions & 0 deletions Sources/Core/FullPathInFatalErrors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/// Just like Swift.precondition, but includes the full file path in the diagnostic.
func precondition(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) {
Swift.precondition(condition(), message(), file: (file), line: line)
}

/// Just like Swift.preconditionFailure, but includes the full file path in the diagnostic.
func preconditionFailure(
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) -> Never {
Swift.preconditionFailure(message(), file: (file), line: line)
}

/// Just like Swift.fatalError, but includes the full file path in the diagnostic.
func fatalError(
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) -> Never {
Swift.fatalError(message(), file: (file), line: line)
}

/// Just like Swift.assert, but includes the full file path in the diagnostic.
func assert(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) {
Swift.assert(condition(), message(), file: (file), line: line)
}

/// Just like Swift.assertionFailure, but includes the full file path in the diagnostic.
func assertionFailure(
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) {
Swift.assertionFailure(message(), file: (file), line: line)
}
5 changes: 5 additions & 0 deletions Sources/Core/Types/RemoteType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public struct RemoteType: TypeProtocol {
self.flags = bareType.flags.inserting(.hasRemoteType)
}

/// Creates an instance converting `t`.
public init(_ t: ParameterType) {
self.init(t.access, t.bareType)
}

public func transformParts<M>(
mutating m: inout M, _ transformer: (inout M, AnyType) -> TypeTransformAction
) -> Self {
Expand Down
1 change: 0 additions & 1 deletion Sources/Driver/FullPathInFatalErrors.swift

This file was deleted.

46 changes: 46 additions & 0 deletions Sources/Driver/FullPathInFatalErrors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/// Just like Swift.precondition, but includes the full file path in the diagnostic.
func precondition(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) {
Swift.precondition(condition(), message(), file: (file), line: line)
}

/// Just like Swift.preconditionFailure, but includes the full file path in the diagnostic.
func preconditionFailure(
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) -> Never {
Swift.preconditionFailure(message(), file: (file), line: line)
}

/// Just like Swift.fatalError, but includes the full file path in the diagnostic.
func fatalError(
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) -> Never {
Swift.fatalError(message(), file: (file), line: line)
}

/// Just like Swift.assert, but includes the full file path in the diagnostic.
func assert(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) {
Swift.assert(condition(), message(), file: (file), line: line)
}

/// Just like Swift.assertionFailure, but includes the full file path in the diagnostic.
func assertionFailure(
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) {
Swift.assertionFailure(message(), file: (file), line: line)
}
1 change: 0 additions & 1 deletion Sources/FrontEnd/FullPathInFatalErrors.swift

This file was deleted.

46 changes: 46 additions & 0 deletions Sources/FrontEnd/FullPathInFatalErrors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/// Just like Swift.precondition, but includes the full file path in the diagnostic.
func precondition(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) {
Swift.precondition(condition(), message(), file: (file), line: line)
}

/// Just like Swift.preconditionFailure, but includes the full file path in the diagnostic.
func preconditionFailure(
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) -> Never {
Swift.preconditionFailure(message(), file: (file), line: line)
}

/// Just like Swift.fatalError, but includes the full file path in the diagnostic.
func fatalError(
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) -> Never {
Swift.fatalError(message(), file: (file), line: line)
}

/// Just like Swift.assert, but includes the full file path in the diagnostic.
func assert(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) {
Swift.assert(condition(), message(), file: (file), line: line)
}

/// Just like Swift.assertionFailure, but includes the full file path in the diagnostic.
func assertionFailure(
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) {
Swift.assertionFailure(message(), file: (file), line: line)
}
6 changes: 6 additions & 0 deletions Sources/FrontEnd/TypeChecking/TypeChecker+Diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ extension Diagnostic {
.error("ambiguous disjunction", at: site)
}

static func error(
binding a: BindingPattern.Introducer, requiresInitializerAt site: SourceRange
) -> Diagnostic {
.error("declaration of \(a) binding requires an initializer", at: site)
}

static func error(circularRefinementAt site: SourceRange) -> Diagnostic {
.error("circular trait refinement", at: site)
}
Expand Down
9 changes: 8 additions & 1 deletion Sources/FrontEnd/TypeChecking/TypeChecker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3432,7 +3432,14 @@ struct TypeChecker {

guard let i = program[d].initializer else {
assert(program[d].pattern.annotation != nil, "expected type annotation")
return pattern

let a = program[d].pattern.introducer.value
if ((a == .let) || (a == .inout)) && program.isLocal(d) {
report(.error(binding: a, requiresInitializerAt: program[d].pattern.introducer.site))
return .error
} else {
return pattern
}
}

// Note: `i` should not have been assigned a type if before `d` is checked.
Expand Down
1 change: 0 additions & 1 deletion Sources/GenerateHyloFileTests/FullPathInFatalErrors.swift

This file was deleted.

46 changes: 46 additions & 0 deletions Sources/GenerateHyloFileTests/FullPathInFatalErrors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/// Just like Swift.precondition, but includes the full file path in the diagnostic.
func precondition(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) {
Swift.precondition(condition(), message(), file: (file), line: line)
}

/// Just like Swift.preconditionFailure, but includes the full file path in the diagnostic.
func preconditionFailure(
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) -> Never {
Swift.preconditionFailure(message(), file: (file), line: line)
}

/// Just like Swift.fatalError, but includes the full file path in the diagnostic.
func fatalError(
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) -> Never {
Swift.fatalError(message(), file: (file), line: line)
}

/// Just like Swift.assert, but includes the full file path in the diagnostic.
func assert(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) {
Swift.assert(condition(), message(), file: (file), line: line)
}

/// Just like Swift.assertionFailure, but includes the full file path in the diagnostic.
func assertionFailure(
_ message: @autoclosure () -> String = String(),
file: StaticString = #filePath,
line: UInt = #line
) {
Swift.assertionFailure(message(), file: (file), line: line)
}
Loading

0 comments on commit 197c149

Please sign in to comment.