Skip to content

[Property wrappers] Reject opaque result types when there is no initializer #32251

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
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
25 changes: 15 additions & 10 deletions lib/Sema/TypeCheckStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2683,16 +2683,21 @@ PropertyWrapperBackingPropertyInfoRequest::evaluate(Evaluator &evaluator,
pbd->setInit(0, initializer);
pbd->setInitializerChecked(0);
wrappedValue = findWrappedValuePlaceholder(initializer);
} else if (!parentPBD->isInitialized(patternNumber) &&
wrapperInfo.defaultInit) {
// FIXME: Record this expression somewhere so that DI can perform the
// initialization itself.
auto typeExpr = TypeExpr::createImplicit(storageType, ctx);
Expr *initializer = CallExpr::createImplicit(ctx, typeExpr, {}, { });
typeCheckSynthesizedWrapperInitializer(pbd, backingVar, parentPBD,
initializer);
pbd->setInit(0, initializer);
pbd->setInitializerChecked(0);
} else {
if (!parentPBD->isInitialized(patternNumber) && wrapperInfo.defaultInit) {
// FIXME: Record this expression somewhere so that DI can perform the
// initialization itself.
auto typeExpr = TypeExpr::createImplicit(storageType, ctx);
Expr *initializer = CallExpr::createImplicit(ctx, typeExpr, {}, { });
typeCheckSynthesizedWrapperInitializer(pbd, backingVar, parentPBD,
initializer);
pbd->setInit(0, initializer);
pbd->setInitializerChecked(0);
}

if (var->getOpaqueResultTypeDecl()) {
var->diagnose(diag::opaque_type_var_no_underlying_type);
}
}

// If there is a projection property (projectedValue) in the wrapper,
Expand Down
2 changes: 1 addition & 1 deletion test/decl/var/property_wrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1972,4 +1972,4 @@ public struct NonVisibleImplicitInit {
public var wrappedValue: Bool {
return false
}
}
}
27 changes: 27 additions & 0 deletions test/decl/var/property_wrappers_opaque.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: %target-typecheck-verify-swift -swift-version 5 -disable-availability-checking

protocol P { }

@propertyWrapper
struct WrapperWithDefaultInit<T> {
private var stored: T?

var wrappedValue: T {
get { stored! }
set { stored = newValue }
}

init() {
self.stored = nil
}
}

// FB7699647 - crash with opaque result type and property wrappers.
struct FB7699647 {
@WrapperWithDefaultInit var property: some P // expected-error{{property declares an opaque return type, but cannot infer the underlying type from its initializer expression}}
@WrapperWithDefaultInit() var otherProperty: some P // expected-error{{property declares an opaque return type, but cannot infer the underlying type from its initializer expression}}
}

struct FB7699647b {
@WrapperWithDefaultInit var property: some P // expected-error{{property declares an opaque return type, but cannot infer the underlying type from its initializer expression}}
}