Skip to content

Commit 93aed56

Browse files
authored
Merge pull request #25458 from DougGregor/property-wrapper-wrapped-value-rename
2 parents 0c20230 + 99b40ba commit 93aed56

File tree

7 files changed

+153
-109
lines changed

7 files changed

+153
-109
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4387,7 +4387,8 @@ ERROR(property_wrapper_ambiguous_value_property, none,
43874387
"named %1", (Type, Identifier))
43884388
ERROR(property_wrapper_wrong_initial_value_init, none,
43894389
"'init(initialValue:)' parameter type (%0) must be the same as its "
4390-
"'value' property type (%1) or an @autoclosure thereof", (Type, Type))
4390+
"'wrappedValue' property type (%1) or an @autoclosure thereof",
4391+
(Type, Type))
43914392
ERROR(property_wrapper_failable_init, none,
43924393
"%0 cannot be failable", (DeclName))
43934394
ERROR(property_wrapper_ambiguous_initial_value_init, none,
@@ -4440,7 +4441,7 @@ NOTE(property_wrapper_direct_init,none,
44404441
"'(...') on the attribute", ())
44414442

44424443
ERROR(property_wrapper_incompatible_property, none,
4443-
"property type %0 does not match that of the 'value' property of "
4444+
"property type %0 does not match that of the 'wrappedValue' property of "
44444445
"its wrapper type %1", (Type, Type))
44454446

44464447
ERROR(property_wrapper_type_access,none,
@@ -4460,6 +4461,9 @@ ERROR(property_wrapper_type_not_usable_from_inline,none,
44604461
WARNING(property_wrapper_delegateValue,none,
44614462
"property wrapper's `delegateValue` property should be renamed to "
44624463
"'wrapperValue'; use of 'delegateValue' is deprecated", ())
4464+
WARNING(property_wrapper_value,none,
4465+
"property wrapper's `value` property should be renamed to "
4466+
"'wrappedValue'; use of 'value' is deprecated", ())
44634467

44644468
//------------------------------------------------------------------------------
44654469
// MARK: function builder diagnostics

include/swift/AST/KnownIdentifiers.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ IDENTIFIER(WinSDK)
121121
IDENTIFIER(with)
122122
IDENTIFIER(withArguments)
123123
IDENTIFIER(withKeywordArguments)
124+
IDENTIFIER(wrappedValue)
124125
IDENTIFIER(wrapperValue)
125126

126127
// Kinds of layout constraints

lib/Sema/TypeCheckPropertyWrapper.cpp

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ using namespace swift;
2828
/// Find the named property in a property wrapper to which access will
2929
/// be delegated.
3030
static VarDecl *findValueProperty(ASTContext &ctx, NominalTypeDecl *nominal,
31-
Identifier name, bool allowMissing) {
31+
Identifier name, bool allowMissing,
32+
bool *diagnosed = nullptr) {
3233
SmallVector<VarDecl *, 2> vars;
3334
{
3435
SmallVector<ValueDecl *, 2> decls;
@@ -49,6 +50,8 @@ static VarDecl *findValueProperty(ASTContext &ctx, NominalTypeDecl *nominal,
4950
if (!allowMissing) {
5051
nominal->diagnose(diag::property_wrapper_no_value_property,
5152
nominal->getDeclaredType(), name);
53+
if (diagnosed)
54+
*diagnosed = true;
5255
}
5356
return nullptr;
5457

@@ -62,6 +65,8 @@ static VarDecl *findValueProperty(ASTContext &ctx, NominalTypeDecl *nominal,
6265
var->diagnose(diag::kind_declname_declared_here,
6366
var->getDescriptiveKind(), var->getFullName());
6467
}
68+
if (diagnosed)
69+
*diagnosed = true;
6570
return nullptr;
6671
}
6772

@@ -72,6 +77,8 @@ static VarDecl *findValueProperty(ASTContext &ctx, NominalTypeDecl *nominal,
7277
var->getFormalAccess(), var->getDescriptiveKind(),
7378
var->getFullName(), nominal->getDeclaredType(),
7479
nominal->getFormalAccess());
80+
if (diagnosed)
81+
*diagnosed = true;
7582
return nullptr;
7683
}
7784

@@ -230,13 +237,32 @@ PropertyWrapperTypeInfoRequest::evaluate(
230237
return PropertyWrapperTypeInfo();
231238
}
232239

233-
// Look for a non-static property named "value" in the property wrapper
234-
// type.
240+
// Look for a non-static property named "wrappedValue" in the property
241+
// wrapper type.
235242
ASTContext &ctx = nominal->getASTContext();
243+
bool diagnosed = false;
236244
auto valueVar =
237-
findValueProperty(ctx, nominal, ctx.Id_value, /*allowMissing=*/false);
238-
if (!valueVar)
239-
return PropertyWrapperTypeInfo();
245+
findValueProperty(ctx, nominal, ctx.Id_wrappedValue,
246+
/*allowMissing=*/true, &diagnosed);
247+
if (!valueVar) {
248+
if (!diagnosed) {
249+
// Look for a non-static property named "value". This is the old name,
250+
// but accept it with a warning.
251+
valueVar = findValueProperty(ctx, nominal, ctx.Id_value,
252+
/*allowMissing=*/true, &diagnosed);
253+
}
254+
255+
if (!valueVar) {
256+
if (!diagnosed) {
257+
valueVar = findValueProperty(ctx, nominal, ctx.Id_wrappedValue,
258+
/*allowMissing=*/false);
259+
}
260+
261+
return PropertyWrapperTypeInfo();
262+
}
263+
264+
valueVar->diagnose(diag::property_wrapper_value);
265+
}
240266

241267
PropertyWrapperTypeInfo result;
242268
result.valueVar = valueVar;

test/SILOptimizer/di_property_wrappers_errors.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
// RUN: %target-swift-frontend -emit-sil -verify %s
22
@propertyWrapper
33
final class ClassWrapper<T> {
4-
var value: T {
4+
var wrappedValue: T {
55
didSet {
6-
print(" .. set \(value)")
6+
print(" .. set \(wrappedValue)")
77
}
88
}
99

1010
init(initialValue: T) {
1111
print(" .. init \(initialValue)")
12-
self.value = initialValue
12+
self.wrappedValue = initialValue
1313
}
1414

1515
deinit {
16-
print(" .. deinit \(value)")
16+
print(" .. deinit \(wrappedValue)")
1717
}
1818
}
1919

test/decl/protocol/special/coding/property_wrappers_codable.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
@propertyWrapper
44
struct Wrapper<T: Codable> {
5-
var value: T
5+
var wrappedValue: T
66
}
77

88
@propertyWrapper
99
struct WrapperWithInitialValue<T: Codable> {
10-
var value: T
10+
var wrappedValue: T
1111

1212
init(initialValue: T) {
13-
self.value = initialValue
13+
self.wrappedValue = initialValue
1414
}
1515
}
1616

test/decl/var/property_wrapper_aliases.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
@propertyDelegate // expected-warning{{'@propertyDelegate' has been renamed to '@propertyWrapper'}}{{2-18=propertyWrapper}}
66
struct Delegate<T> {
7-
var value: T
7+
var wrappedValue: T
88

99
var delegateValue: Wrapper<T> { // expected-warning{{property wrapper's `delegateValue` property should be renamed to 'wrapperValue'; use of 'delegateValue' is deprecated}}{{7-20=wrapperValue}}
10-
return Wrapper(value: value)
10+
return Wrapper(wrappedValue: wrappedValue)
1111
}
1212
}
1313

@@ -21,5 +21,18 @@ struct TestDelegateValue {
2121

2222
@_propertyWrapper // expected-warning{{'@_propertyWrapper' has been renamed to '@propertyWrapper'}}{{2-18=propertyWrapper}}
2323
struct Wrapper<T> {
24-
var value: T
24+
var wrappedValue: T
25+
}
26+
27+
@propertyWrapper
28+
struct OldValue<T> {
29+
var value: T // expected-warning{{property wrapper's `value` property should be renamed to 'wrappedValue'; use of 'value' is deprecated}}
30+
}
31+
32+
struct TestOldValue {
33+
@OldValue var x: String
34+
35+
func f() -> String {
36+
return x
37+
}
2538
}

0 commit comments

Comments
 (0)