Skip to content
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

[Property Wrappers] Refactor property wrappers so the synthesized backing init is only type checked once #30807

Merged
merged 9 commits into from
Apr 14, 2020

Conversation

hborla
Copy link
Member

@hborla hborla commented Apr 4, 2020

Previously, if a property wrapper had an initial wrapped value expression:

@Lazy var i = 17

Then PropertyWrapperBackingPropertyInfoRequest would synthesize and type-check the backing property initializer twice - once with the wrapped value expression, and again with an opaque value placeholder.

Instead, we can have CSApply inject the opaque value placeholder around the wrappedValue argument in coerceCallArguments.

Resolves: rdar://problem/59685601
Resolves: rdar://problem/56163425
Resolves: SR-11594

@hborla hborla requested a review from DougGregor April 4, 2020 00:55
@hborla
Copy link
Member Author

hborla commented Apr 4, 2020

@swift-ci please smoke test

@hborla
Copy link
Member Author

hborla commented Apr 4, 2020

@swift-ci please test source compatibility

@@ -298,6 +298,7 @@ namespace {
ConstraintSystem &cs;
DeclContext *dc;
Solution &solution;
Optional<SolutionApplicationTarget> target;
Copy link
Member Author

@hborla hborla Apr 4, 2020

Choose a reason for hiding this comment

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

This shouldn't be Optional, but there's a Solution::coerceToType that creates an ExprRewriter just to call ExprRewriter:: coerceToType ...

Copy link
Member

Choose a reason for hiding this comment

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

We could probably refactor away Solution::coerceToType later. It looks unnecessary.

@hborla hborla changed the title [Property Wrappers] Refactor property wrappers so the synthesized backing init is only Typechecker once [Property Wrappers] Refactor property wrappers so the synthesized backing init is only type checked once Apr 4, 2020
include/swift/AST/PropertyWrappers.h Outdated Show resolved Hide resolved
lib/AST/Decl.cpp Outdated Show resolved Hide resolved
lib/Sema/CSApply.cpp Outdated Show resolved Hide resolved
@hborla hborla force-pushed the property-wrapper-refactoring branch from ec54de5 to 37e3950 Compare April 4, 2020 17:15
@hborla
Copy link
Member Author

hborla commented Apr 4, 2020

Looks like MovieSwiftUI exposed some bugs. I'll investigate!

wrapper original wrapped value expression inside of CSApply.

This prevents type checking the synthesized backing storage initializer
twice - once with the original expression and again with the placeholder.
…s that

involve inout closure parameters and were previously type checked twice.
initializer for property wrappers that are not memberwise initialized but
have an explicit original wrapped value.
@hborla hborla force-pushed the property-wrapper-refactoring branch from 37e3950 to 5f00a1b Compare April 6, 2020 02:03
@hborla
Copy link
Member Author

hborla commented Apr 6, 2020

@swift-ci please smoke test

@hborla
Copy link
Member Author

hborla commented Apr 6, 2020

@swift-ci please test source compatibility

@hborla hborla requested a review from slavapestov April 6, 2020 21:31
@@ -298,6 +298,7 @@ namespace {
ConstraintSystem &cs;
DeclContext *dc;
Solution &solution;
Optional<SolutionApplicationTarget> target;
Copy link
Member

Choose a reason for hiding this comment

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

We could probably refactor away Solution::coerceToType later. It looks unnecessary.

@@ -4021,11 +4021,14 @@ class DynamicTypeExpr : public Expr {
/// subexpressions of that AST node.
class OpaqueValueExpr : public Expr {
SourceRange Range;
Expr *UnderlyingValue;
Copy link
Member

Choose a reason for hiding this comment

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

OpaqueValueExpr is a general-purpose construct where the "underlying value" comes from something elsewhere in the AST, above the OpaqueValueExpr. Is there some other place we can put the original initializer value that doesn't require adding this field to OpaqueValueExpr? One option: create a new Expr node ImplicitlyWrappedPropertyValueExpr that stores the OpaqueValueExpr and its underlying value. This will make findWrappedValuePlaceholder even simpler and avoid altering the design of OpaqueValueExpr.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I think having a dedicated Expr node for the wrapped value is the right thing to do. I was also worried about something else starting to use the underlyingValue field that I added here and breaking my assumptions in this PR that it's only used for property wrappers. Thank you!

wrapped value placeholder in an init(wrappedValue:) call that was previously
injected as an OpaqueValueExpr. This commit also restores the old design of
OpaqueValueExpr.
@hborla
Copy link
Member Author

hborla commented Apr 9, 2020

@swift-ci please smoke test

@hborla
Copy link
Member Author

hborla commented Apr 9, 2020

@swift-ci please test source compatibility

…ng a

property wrapper backing init if the property type doesn't match the
wrapped value type.
@hborla
Copy link
Member Author

hborla commented Apr 10, 2020

@swift-ci please smoke test

@hborla
Copy link
Member Author

hborla commented Apr 10, 2020

@swift-ci please test source compatibility

@hborla
Copy link
Member Author

hborla commented Apr 10, 2020

@swift-ci please smoke test Linux platform

1 similar comment
@hborla
Copy link
Member Author

hborla commented Apr 10, 2020

@swift-ci please smoke test Linux platform

@hborla hborla requested a review from DougGregor April 13, 2020 17:06
@hborla
Copy link
Member Author

hborla commented Apr 14, 2020

@swift-ci please smoke test

@hborla hborla merged commit 66e8572 into swiftlang:master Apr 14, 2020
@hborla hborla deleted the property-wrapper-refactoring branch April 14, 2020 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants