Skip to content

Disable property wrapper composition. #26287

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
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -4476,6 +4476,9 @@ ERROR(property_wrapper_attribute_not_on_property, none,
NOTE(property_wrapper_declared_here,none,
"property wrapper type %0 declared here", (DeclName))

ERROR(property_wrapper_composition_not_implemented, none,
"multiple property wrappers are not supported", ())

ERROR(property_wrapper_local,none,
"property wrappers are not yet supported on local properties", ())
ERROR(property_wrapper_top_level,none,
Expand Down
9 changes: 8 additions & 1 deletion lib/Sema/TypeCheckPropertyWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,17 @@ AttachedPropertyWrappersRequest::evaluate(Evaluator &evaluator,
ctx.Diags.diagnose(attr->getLocation(), diag::property_wrapper_computed);
continue;
}

result.push_back(mutableAttr);
}

// TODO: Property wrapper compositions do not yet compose correctly
// in all situtaions.
if (result.size() > 1) {
ctx.Diags.diagnose(result[result.size() - 2]->getLocation(),
diag::property_wrapper_composition_not_implemented);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: If you do this after the std::reverse below, you can avoid the weird "second-to-last" computation above, which confused me until I read the comment below.


// Attributes are stored in reverse order in the AST, but we want them in
// source order so that the outermost property wrapper comes first.
std::reverse(result.begin(), result.end());
Expand Down
28 changes: 15 additions & 13 deletions test/SILGen/property_wrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -308,31 +308,33 @@ struct WrapperC<Value> {
var wrappedValue: Value?
}

/* TODO: Reenable composed property wrappers
struct CompositionMembers {
// CompositionMembers.p1.getter
// CHECK-LABEL: sil hidden [ossa] @$s17property_wrappers18CompositionMembersV2p1SiSgvg : $@convention(method) (@guaranteed CompositionMembers) -> Optional<Int>
// CHECK: bb0([[SELF:%.*]] : @guaranteed $CompositionMembers):
// CHECK: [[P1:%.*]] = struct_extract [[SELF]] : $CompositionMembers, #CompositionMembers._p1
// CHECK: [[P1_VALUE:%.*]] = struct_extract [[P1]] : $WrapperA<WrapperB<WrapperC<Int>>>, #WrapperA.wrappedValue
// CHECK: [[P1_VALUE2:%.*]] = struct_extract [[P1_VALUE]] : $WrapperB<WrapperC<Int>>, #WrapperB.wrappedValue
// CHECK: [[P1_VALUE3:%.*]] = struct_extract [[P1_VALUE2]] : $WrapperC<Int>, #WrapperC.wrappedValue
// CHECK: return [[P1_VALUE3]] : $Optional<Int>
// C/HECK-LABEL: sil hidden [ossa] @$s17property_wrappers18CompositionMembersV2p1SiSgvg : $@convention(method) (@guaranteed CompositionMembers) -> Optional<Int>
// C/HECK: bb0([[SELF:%.*]] : @guaranteed $CompositionMembers):
// C/HECK: [[P1:%.*]] = struct_extract [[SELF]] : $CompositionMembers, #CompositionMembers._p1
// C/HECK: [[P1_VALUE:%.*]] = struct_extract [[P1]] : $WrapperA<WrapperB<WrapperC<Int>>>, #WrapperA.wrappedValue
// C/HECK: [[P1_VALUE2:%.*]] = struct_extract [[P1_VALUE]] : $WrapperB<WrapperC<Int>>, #WrapperB.wrappedValue
// C/HECK: [[P1_VALUE3:%.*]] = struct_extract [[P1_VALUE2]] : $WrapperC<Int>, #WrapperC.wrappedValue
// C/HECK: return [[P1_VALUE3]] : $Optional<Int>
@WrapperA @WrapperB @WrapperC var p1: Int?
@WrapperA @WrapperB @WrapperC var p2 = "Hello"

// variable initialization expression of CompositionMembers.$p2
// CHECK-LABEL: sil hidden [transparent] [ossa] @$s17property_wrappers18CompositionMembersV3_p233_{{.*}}8WrapperAVyAA0N1BVyAA0N1CVySSGGGvpfi : $@convention(thin) () -> @owned Optional<String> {
// CHECK: %0 = string_literal utf8 "Hello"
// C/HECK-LABEL: sil hidden [transparent] [ossa] @$s17property_wrappers18CompositionMembersV3_p233_{{.*}}8WrapperAVyAA0N1BVyAA0N1CVySSGGGvpfi : $@convention(thin) () -> @owned Optional<String> {
// C/HECK: %0 = string_literal utf8 "Hello"

// CHECK-LABEL: sil hidden [ossa] @$s17property_wrappers18CompositionMembersV2p12p2ACSiSg_SSSgtcfC : $@convention(method) (Optional<Int>, @owned Optional<String>, @thin CompositionMembers.Type) -> @owned CompositionMembers
// CHECK: function_ref @$s17property_wrappers8WrapperCV12wrappedValueACyxGxSg_tcfC
// CHECK: function_ref @$s17property_wrappers8WrapperBV12wrappedValueACyxGx_tcfC
// CHECK: function_ref @$s17property_wrappers8WrapperAV12wrappedValueACyxGx_tcfC
// C/HECK-LABEL: sil hidden [ossa] @$s17property_wrappers18CompositionMembersV2p12p2ACSiSg_SSSgtcfC : $@convention(method) (Optional<Int>, @owned Optional<String>, @thin CompositionMembers.Type) -> @owned CompositionMembers
// C/HECK: function_ref @$s17property_wrappers8WrapperCV12wrappedValueACyxGxSg_tcfC
// C/HECK: function_ref @$s17property_wrappers8WrapperBV12wrappedValueACyxGx_tcfC
// C/HECK: function_ref @$s17property_wrappers8WrapperAV12wrappedValueACyxGx_tcfC
}

func testComposition() {
_ = CompositionMembers(p1: nil)
}
*/

// Observers with non-default mutatingness.
@propertyWrapper
Expand Down
12 changes: 6 additions & 6 deletions test/decl/var/property_wrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ struct BadCombinations {

struct MultipleWrappers {
@Wrapper(stored: 17)
@WrapperWithInitialValue // expected-error{{extra argument 'wrappedValue' in call}}
@WrapperWithInitialValue // expected-error{{extra argument 'wrappedValue' in call}} expected-error{{multiple property wrappers are not supported}}
var x: Int = 17

@WrapperWithInitialValue // expected-error 2{{property wrapper can only apply to a single variable}}
Expand Down Expand Up @@ -959,11 +959,11 @@ struct WrapperE<Value> {
}

struct TestComposition {
@WrapperA @WrapperB @WrapperC var p1: Int?
@WrapperA @WrapperB @WrapperC var p2 = "Hello"
@WrapperD<WrapperE, Int, String> @WrapperE var p3: Int?
@WrapperD<WrapperC, Int, String> @WrapperC var p4: Int?
@WrapperD<WrapperC, Int, String> @WrapperE var p5: Int // expected-error{{property type 'Int' does not match that of the 'wrappedValue' property of its wrapper type 'WrapperD<WrapperC, Int, String>'}}
@WrapperA @WrapperB @WrapperC var p1: Int? // expected-error{{multiple property wrappers are not supported}}
@WrapperA @WrapperB @WrapperC var p2 = "Hello" // expected-error{{multiple property wrappers are not supported}}
@WrapperD<WrapperE, Int, String> @WrapperE var p3: Int? // expected-error{{multiple property wrappers are not supported}}
@WrapperD<WrapperC, Int, String> @WrapperC var p4: Int? // expected-error{{multiple property wrappers are not supported}}
@WrapperD<WrapperC, Int, String> @WrapperE var p5: Int // expected-error{{property type 'Int' does not match that of the 'wrappedValue' property of its wrapper type 'WrapperD<WrapperC, Int, String>'}} // expected-error{{multiple property wrappers are not supported}}

func triggerErrors(d: Double) {
p1 = d // expected-error{{cannot assign value of type 'Double' to type 'Int?'}}
Expand Down