Files
swift-mirror/test/SILOptimizer/consume_operator_nonlocalmemory.swift
Michael Gottesman 361562aef5 [consume-operator] Emit a better error message when failing to consume globals or escaping captures.
Specifically, we previously emitted a "compiler doesn't understand error", so we
were always emitting an error appropriately. This just gives a better error
message saying instead that the compiler did understand what happened and that
one cannot apply consume to globals or escaping captures.

https://github.com/apple/swift/issues/67755
rdar://112561671
2023-08-20 18:59:37 -07:00

52 lines
1.6 KiB
Swift

// RUN: %target-swift-frontend -verify %s -emit-sil -o /dev/null
// This file tests that we emit good errors for global lets/vars,
////////////////////////
// MARK: Declarations //
////////////////////////
class Klass {
var s1 = "123"
let s2 = "123"
}
struct LoadableStruct { var k = Klass() }
struct AddressOnlyStruct<T> { var t: T? = nil }
////////////////////////
// MARK: Global Tests //
////////////////////////
let globalLoadableLet = LoadableStruct()
let _ = consume globalLoadableLet // expected-error {{'consume' cannot be applied to globals}}
let globalAddressOnlyLet = AddressOnlyStruct<Any>()
let _ = consume globalAddressOnlyLet // expected-error {{'consume' cannot be applied to globals}}
var globalLoadableVar = LoadableStruct()
let _ = consume globalLoadableVar // expected-error {{'consume' cannot be applied to globals}}
var globalAddressOnlyVar = AddressOnlyStruct<Any>()
let _ = consume globalAddressOnlyVar // expected-error {{'consume' cannot be applied to globals}}
////////////////////////////
// MARK: Mutable Captures //
////////////////////////////
func accessMutableCapture() -> (() -> ()) {
func useKlassInOut(_ x: inout Klass) {}
func useAddressOnlyStructInOut<T>(_ x: inout AddressOnlyStruct<T>) {}
var x = Klass()
var x2 = AddressOnlyStruct<Any>()
var f: () -> () = {}
f = {
useKlassInOut(&x)
useAddressOnlyStructInOut(&x2)
}
let _ = consume x // expected-error {{'consume' cannot be applied to escaping captures}}
let _ = consume x2 // expected-error {{'consume' cannot be applied to escaping captures}}
return f
}