Skip to content

[5.9][Sema/SILGen] InitAccessors: A few improvements and bug fixes #67365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7109,6 +7109,7 @@ struct Parser::ParsedAccessors {

/// Find the first accessor that can be used to perform mutation.
AccessorDecl *findFirstMutator() const {
if (Init) return Init;
if (Set) return Set;
if (Modify) return Modify;
if (MutableAddress) return MutableAddress;
Expand Down
17 changes: 11 additions & 6 deletions lib/SIL/IR/SILFunctionType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2381,22 +2381,27 @@ static CanSILFunctionType getSILFunctionTypeForInitAccessor(
// Drop `self` parameter.
inputs.pop_back();

auto getLoweredTypeOfProperty = [&](VarDecl *property) {
auto type = property->getInterfaceType();
AbstractionPattern pattern(genericSig, type->getCanonicalType());
auto loweredTy = TC.getLoweredType(pattern, type, context);
return loweredTy.getASTType();
};

// accessed properties appear as `inout` parameters because they could be
// read from and modified.
for (auto *property : accessor->getAccessedProperties()) {
inputs.push_back(
SILParameterInfo(property->getInterfaceType()->getCanonicalType(),
ParameterConvention::Indirect_Inout));
inputs.push_back(SILParameterInfo(getLoweredTypeOfProperty(property),
ParameterConvention::Indirect_Inout));
}

SmallVector<SILResultInfo, 8> results;

// initialized properties appear as `@out` results because they are
// initialized by the accessor.
for (auto *property : accessor->getInitializedProperties()) {
results.push_back(
SILResultInfo(property->getInterfaceType()->getCanonicalType(),
ResultConvention::Indirect));
results.push_back(SILResultInfo(getLoweredTypeOfProperty(property),
ResultConvention::Indirect));
}

auto calleeConvention = ParameterConvention::Direct_Unowned;
Expand Down
4 changes: 2 additions & 2 deletions lib/SILGen/SILGenConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1584,8 +1584,8 @@ void SILGenFunction::emitInitAccessor(AccessorDecl *accessor) {
bool markUninitialized = false) {
auto *arg = ParamDecl::createImplicit(
getASTContext(), property->getBaseIdentifier(),
property->getBaseIdentifier(), type.getASTType()->mapTypeOutOfContext(),
accessor, ParamSpecifier::InOut);
property->getBaseIdentifier(), property->getInterfaceType(), accessor,
ParamSpecifier::InOut);

RegularLocation loc(property);
loc.markAutoGenerated();
Expand Down
2 changes: 1 addition & 1 deletion lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2954,7 +2954,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
}

case DAK_StorageRestrictions: {
auto abbrCode = S.DeclTypeAbbrCodes[AccessesDeclAttrLayout::Code];
auto abbrCode = S.DeclTypeAbbrCodes[StorageRestrictionsDeclAttrLayout::Code];
auto attr = cast<StorageRestrictionsAttr>(DA);

SmallVector<IdentifierID, 4> properties;
Expand Down
24 changes: 24 additions & 0 deletions test/SILOptimizer/init_accessors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,30 @@ func test_local_with_memberwise() {
_ = TestMemberwiseGeneric(a: 1, pair: ("a", [0]))
}

// CHECK-LABEL: sil private [ossa] @$s14init_accessors023test_type_lowering_for_A9_accessoryyF4TestL_V2fnADyxq_Gq_xc_tcfC : $@convention(method) <T, U> (@owned @callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <T, U>, @thin Test<T, U>.Type) -> @owned Test<T, U>
// CHECK: {{.*}} = function_ref @$s14init_accessors023test_type_lowering_for_A9_accessoryyF4TestL_V2fnyq_xcvi : $@convention(thin) <τ_0_0, τ_0_1> (@owned @callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <τ_0_0, τ_0_1>) -> @out Optional<@callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <τ_0_0, τ_0_1>>
func test_type_lowering_for_init_accessor() {
struct Test<T, U> {
var _fn: ((T) -> U)? = nil

// CHECK-LABEL: sil private [ossa] @$s14init_accessors023test_type_lowering_for_A9_accessoryyF4TestL_V2fnyq_xcvi : $@convention(thin) <T, U> (@owned @callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <T, U>) -> @out Optional<@callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <T, U>>
var fn: (T) -> U {
@storageRestrictions(initializes: _fn)
init { _fn = newValue }
get { _fn! }
set { _fn = newValue }
}

init(fn: @escaping (T) -> U) {
self.fn = fn
}
}

_ = Test<Int, () -> Void> { _ in
return {}
} // Ok
}

func test_assignments() {
struct Test {
var _a: Int
Expand Down
66 changes: 66 additions & 0 deletions test/Serialization/init_accessors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// RUN: %empty-directory(%t)
// RUN: %empty-directory(%t/src)
// RUN: %empty-directory(%t/sdk)
// RUN: split-file %s %t/src

// REQUIRES: asserts

// RUN: %target-swift-frontend -emit-module %t/src/PublicModule.swift \
// RUN: -module-name PublicModule -swift-version 5 \
// RUN: -emit-module-path %t/sdk/PublicModule.swiftmodule \
// RUN: -enable-experimental-feature InitAccessors

// RUN: %target-swift-frontend -typecheck %t/src/Client.swift \
// RUN: -enable-experimental-feature InitAccessors \
// RUN: -module-name Client -I %t/sdk

//--- PublicModule.swift
public struct Test<T> {
private var _x: T

public var x: T {
@storageRestrictions(initializes: _x)
init {
_x = newValue
}

get { _x }
set { _x = newValue }
}

public init(data: T) {
self.x = data
}
}

public struct TestMulti<T, U> {
private var _a: T
private var _c: U

public var b: Int

public var data: (T, U) {
@storageRestrictions(initializes: _a, _c, accesses: b)
init {
_a = newValue.0
_c = newValue.1
b = 0
}

get { (_a, _c) }
}

public init(data: (T, U), b: Int) {
self.b = b
self.data = data
}
}

//--- Client.swift
import PublicModule

let test1 = Test<[Int]>(data: [1, 2, 3])
_ = test1.x

let test2 = TestMulti(data: ("Question", 42), b: -1)
_ = print("\(test2.data), \(test2.b)")
23 changes: 23 additions & 0 deletions test/decl/var/init_accessors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,16 @@ func test_invalid_refs_in_init_attrs() {
// expected-error@-2 {{find type 'b' in scope}}
// expected-error@-3 {{init accessor cannot refer to property 'c'; init accessors can refer only to stored properties}}
init(initialValue) {}

get { 0 }
}

var y: String {
@storageRestrictions(initializes: test)
// expected-error@-1 {{ambiguous reference to member 'test'}}
init(initialValue) {}

get { "" }
}

func test(_: Int) {} // expected-note {{'test' declared here}}
Expand All @@ -110,6 +114,8 @@ func test_assignment_to_let_properties() {
self.x = initialValue // Ok
self.y = 42 // expected-error {{cannot assign to property: 'y' is a 'let' constant}}
}

get { x }
}

var point: (Int, Int) {
Expand Down Expand Up @@ -157,6 +163,8 @@ func test_duplicate_and_computed_lazy_properties() {
// expected-error@-2 {{init accessor cannot refer to property 'b'; init accessors can refer only to stored properties}}
// expected-error@-3 {{init accessor cannot refer to property 'c'; init accessors can refer only to stored properties}}
init(initialValue) {}

get { _a }
}

var b: Int {
Expand Down Expand Up @@ -475,11 +483,15 @@ func test_default_arguments_are_analyzed() {
struct Test {
var pair: (Int, Int) = (0, 1) { // Ok
init {}

get { (0, 1) }
}

var other: (Int, String) = ("", 42) {
// expected-error@-1 {{cannot convert value of type '(String, Int)' to specified type '(Int, String)'}}
init(initialValue) {}

get { (0, "") }
}

var otherPair = (0, 1) {
Expand Down Expand Up @@ -558,36 +570,47 @@ func test_invalid_storage_restrictions() {
@storageRestrictions()
// expected-error@-1 {{missing label in @storageRestrictions attribute}}
init {}

get { _a }
}

var b: Int {
@storageRestrictions(initializes:)
// expected-error@-1 {{expected property name in @storageRestrictions list}}
init {}

get { _b }
}

var c: Int {
@storageRestrictions(initializes: a, initializes: b)
// expected-error@-1 {{duplicate label 'initializes' in @storageRestrictions attribute}}
init {}

get { 0 }
}

var d: Int {
@storageRestrictions(accesses: a, accesses: c)
// expected-error@-1 {{duplicate label 'accesses' in @storageRestrictions attribute}}
init {}

get { 0 }
}

var e: Int {
@storageRestrictions(initialize: a, b, accesses: c, d)
// expected-error@-1 {{unexpected label 'initialize' in @storageRestrictions attribute}}
init {}

get { 0 }
}

var f: Int {
@storageRestrictions(initializes: _a, accesses: _b, _a)
// expected-error@-1 {{property '_a' cannot be both initialized and accessed}}
init {}
// expected-error@-1 {{variable with an init accessor must also have a getter}}
}

var g: Int {
Expand Down