Skip to content

[5.1] Bring property wrappers up-to-date with the proposal under review #25484

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
29 changes: 21 additions & 8 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5078,26 +5078,39 @@ class VarDecl : public AbstractStorageDecl {
Bits.VarDecl.IsREPLVar = IsREPLVar;
}

/// Retrieve the custom attribute that attaches a property wrapper to this
/// property.
CustomAttr *getAttachedPropertyWrapper() const;
/// Retrieve the custom attributes that attach property wrappers to this
/// property. The returned list contains all of the attached property wrapper attributes in source order,
/// which means the outermost wrapper attribute is provided first.
llvm::TinyPtrVector<CustomAttr *> getAttachedPropertyWrappers() const;

/// Whether this property has any attached property wrappers.
bool hasAttachedPropertyWrapper() const;

/// Whether all of the attached property wrappers have an init(initialValue:) initializer.
bool allAttachedPropertyWrappersHaveInitialValueInit() const;

/// Retrieve the type of the attached property wrapper as a contextual
/// type.
///
/// \param index Which property wrapper type is being computed, where 0
/// indicates the first (outermost) attached property wrapper.
///
/// \returns a NULL type for properties without attached wrappers,
/// an error type when the property wrapper type itself is erroneous,
/// or the wrapper type itself, which may involve unbound generic
/// types.
Type getAttachedPropertyWrapperType() const;
Type getAttachedPropertyWrapperType(unsigned index) const;

/// Retrieve information about the attached property wrapper type.
PropertyWrapperTypeInfo getAttachedPropertyWrapperTypeInfo() const;
///
/// \param i Which attached property wrapper type is being queried, where 0 is the outermost (first)
/// attached property wrapper type.
PropertyWrapperTypeInfo getAttachedPropertyWrapperTypeInfo(unsigned i) const;

/// Retrieve the fully resolved attached property wrapper type.
///
/// This type will be the fully-resolved form of
/// \c getAttachedPropertyWrapperType(), which will not contain any
/// \c getAttachedPropertyWrapperType(0), which will not contain any
/// unbound generic types. It will be the type of the backing property.
Type getPropertyWrapperBackingPropertyType() const;

Expand All @@ -5111,8 +5124,8 @@ class VarDecl : public AbstractStorageDecl {
///
/// The backing storage property will be a stored property of the
/// wrapper's type. This will be equivalent to
/// \c getAttachedPropertyWrapperType() when it is fully-specified;
/// if \c getAttachedPropertyWrapperType() involves an unbound
/// \c getAttachedPropertyWrapperType(0) when it is fully-specified;
/// if \c getAttachedPropertyWrapperType(0) involves an unbound
/// generic type, the backing storage property will be the appropriate
/// bound generic version.
VarDecl *getPropertyWrapperBackingProperty() const;
Expand Down
19 changes: 6 additions & 13 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -4370,7 +4370,8 @@ ERROR(property_wrapper_ambiguous_value_property, none,
"named %1", (Type, Identifier))
ERROR(property_wrapper_wrong_initial_value_init, none,
"'init(initialValue:)' parameter type (%0) must be the same as its "
"'value' property type (%1) or an @autoclosure thereof", (Type, Type))
"'wrappedValue' property type (%1) or an @autoclosure thereof",
(Type, Type))
ERROR(property_wrapper_failable_init, none,
"%0 cannot be failable", (DeclName))
ERROR(property_wrapper_ambiguous_initial_value_init, none,
Expand All @@ -4389,11 +4390,6 @@ ERROR(property_wrapper_attribute_not_on_property, none,
NOTE(property_wrapper_declared_here,none,
"property wrapper type %0 declared here", (DeclName))

ERROR(property_wrapper_multiple,none,
"only one property wrapper can be attached to a given property", ())
NOTE(previous_property_wrapper_here,none,
"previous property wrapper specified here", ())

ERROR(property_wrapper_local,none,
"property wrappers are not yet supported on local properties", ())
ERROR(property_wrapper_let, none,
Expand All @@ -4417,18 +4413,12 @@ ERROR(property_with_wrapper_overrides,none,
"property %0 with attached wrapper cannot override another property",
(DeclName))

ERROR(property_wrapper_and_normal_init,none,
"property %0 with attached wrapper cannot initialize both the "
"wrapper type and the property", (DeclName))
ERROR(property_wrapper_init_without_initial_value, none,
"initializing property %0 with wrapper %1 that lacks "
"an 'init(initialValue:)' initializer", (DeclName, Type))
NOTE(property_wrapper_direct_init,none,
"initialize the property wrapper type directly with "
"'(...') on the attribute", ())

ERROR(property_wrapper_incompatible_property, none,
"property type %0 does not match that of the 'value' property of "
"property type %0 does not match that of the 'wrappedValue' property of "
"its wrapper type %1", (Type, Type))

ERROR(property_wrapper_type_access,none,
Expand All @@ -4448,6 +4438,9 @@ ERROR(property_wrapper_type_not_usable_from_inline,none,
WARNING(property_wrapper_delegateValue,none,
"property wrapper's `delegateValue` property should be renamed to "
"'wrapperValue'; use of 'delegateValue' is deprecated", ())
WARNING(property_wrapper_value,none,
"property wrapper's `value` property should be renamed to "
"'wrappedValue'; use of 'value' is deprecated", ())

//------------------------------------------------------------------------------
// MARK: function builder diagnostics
Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/KnownIdentifiers.def
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ IDENTIFIER_WITH_NAME(value_, "_value")
IDENTIFIER(with)
IDENTIFIER(withArguments)
IDENTIFIER(withKeywordArguments)
IDENTIFIER(wrappedValue)
IDENTIFIER(wrapperValue)

// Kinds of layout constraints
Expand Down
12 changes: 6 additions & 6 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,10 @@ class PropertyWrapperTypeInfoRequest

/// Request the nominal type declaration to which the given custom attribute
/// refers.
class AttachedPropertyWrapperRequest :
public SimpleRequest<AttachedPropertyWrapperRequest,
class AttachedPropertyWrappersRequest :
public SimpleRequest<AttachedPropertyWrappersRequest,
CacheKind::Cached,
CustomAttr *,
llvm::TinyPtrVector<CustomAttr *>,
VarDecl *> {
public:
using SimpleRequest::SimpleRequest;
Expand All @@ -432,7 +432,7 @@ class AttachedPropertyWrapperRequest :
friend SimpleRequest;

// Evaluation.
llvm::Expected<CustomAttr *>
llvm::Expected<llvm::TinyPtrVector<CustomAttr *>>
evaluate(Evaluator &evaluator, VarDecl *) const;

public:
Expand All @@ -450,7 +450,7 @@ class AttachedPropertyWrapperTypeRequest :
public SimpleRequest<AttachedPropertyWrapperTypeRequest,
CacheKind::Cached,
Type,
VarDecl *> {
VarDecl *, unsigned> {
public:
using SimpleRequest::SimpleRequest;

Expand All @@ -459,7 +459,7 @@ class AttachedPropertyWrapperTypeRequest :

// Evaluation.
llvm::Expected<Type>
evaluate(Evaluator &evaluator, VarDecl *var) const;
evaluate(Evaluator &evaluator, VarDecl *var, unsigned i) const;

public:
// Caching
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ SWIFT_TYPEID(USRGenerationRequest)
SWIFT_TYPEID(DefaultTypeRequest)
SWIFT_TYPEID(MangleLocalTypeDeclRequest)
SWIFT_TYPEID(PropertyWrapperTypeInfoRequest)
SWIFT_TYPEID(AttachedPropertyWrapperRequest)
SWIFT_TYPEID(AttachedPropertyWrappersRequest)
SWIFT_TYPEID(AttachedPropertyWrapperTypeRequest)
SWIFT_TYPEID(PropertyWrapperBackingPropertyTypeRequest)
SWIFT_TYPEID(PropertyWrapperBackingPropertyInfoRequest)
Expand Down
21 changes: 21 additions & 0 deletions include/swift/Basic/AnyValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "swift/Basic/SimpleDisplay.h"
#include "swift/Basic/TypeID.h"
#include "llvm/ADT/PointerUnion.h" // to define hash_value
#include "llvm/ADT/TinyPtrVector.h"

namespace llvm {
// FIXME: Belongs in LLVM itself
Expand Down Expand Up @@ -146,6 +147,26 @@ class AnyValue {

} // end namespace swift

namespace llvm {
template<typename T>
bool operator==(const TinyPtrVector<T> &lhs, const TinyPtrVector<T> &rhs) {
if (lhs.size() != rhs.size())
return false;

for (unsigned i = 0, n = lhs.size(); i != n; ++i) {
if (lhs[i] != rhs[i])
return false;
}

return true;
}

template<typename T>
bool operator!=(const TinyPtrVector<T> &lhs, const TinyPtrVector<T> &rhs) {
return !(lhs == rhs);
}
} // end namespace llvm

#endif //


15 changes: 15 additions & 0 deletions include/swift/Basic/SimpleDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef SWIFT_BASIC_SIMPLE_DISPLAY_H
#define SWIFT_BASIC_SIMPLE_DISPLAY_H

#include "llvm/ADT/TinyPtrVector.h"
#include "llvm/Support/raw_ostream.h"
#include <tuple>
#include <type_traits>
Expand Down Expand Up @@ -92,6 +93,20 @@ namespace swift {
const std::tuple<Types...> &value) {
simple_display_tuple<0>(out, value);
}

template<typename T>
void simple_display(llvm::raw_ostream &out,
const llvm::TinyPtrVector<T> &vector) {
out << "{";
bool first = true;
for (const T &value : vector) {
if (first) first = false;
else out << ", ";

simple_display(out, value);
}
out << "}";
}
}

#endif // SWIFT_BASIC_SIMPLE_DISPLAY_H
Loading