Skip to content

Commit 8ac4a29

Browse files
committed
---
yaml --- r: 349393 b: refs/heads/master-next c: d58a1d5 h: refs/heads/master i: 349391: 13a628a
1 parent 427826d commit 8ac4a29

File tree

10 files changed

+72
-65
lines changed

10 files changed

+72
-65
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
refs/heads/master: 3574c513bbc5578dd9346b4ea9ab5995c5927bb5
3-
refs/heads/master-next: 4801938d366cbc4f1f7bdb63598f619a40c96c4e
3+
refs/heads/master-next: d58a1d5c0d5fcc0710c42d23e5edc09abd626bc6
44
refs/tags/osx-passed: b6b74147ef8a386f532cf9357a1bde006e552c54
55
refs/tags/swift-2.2-SNAPSHOT-2015-12-01-a: 6bb18e013c2284f2b45f5f84f2df2887dc0f7dea
66
refs/tags/swift-2.2-SNAPSHOT-2015-12-01-b: 66d897bfcf64a82cb9a87f5e663d889189d06d07

branches/master-next/include/swift/AST/Type.h

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,8 @@ class Type {
374374
class CanType : public Type {
375375
bool isActuallyCanonicalOrNull() const;
376376

377-
static bool isReferenceTypeImpl(CanType type, bool functionsCount);
377+
static bool isReferenceTypeImpl(CanType type, GenericSignature *sig,
378+
bool functionsCount);
378379
static bool isExistentialTypeImpl(CanType type);
379380
static bool isAnyExistentialTypeImpl(CanType type);
380381
static bool isObjCExistentialTypeImpl(CanType type);
@@ -408,8 +409,26 @@ class CanType : public Type {
408409
// Provide a few optimized accessors that are really type-class queries.
409410

410411
/// Do values of this type have reference semantics?
412+
///
413+
/// This includes isAnyClassReferenceType(), as well as function types.
411414
bool hasReferenceSemantics() const {
412-
return isReferenceTypeImpl(*this, /*functions count*/ true);
415+
return isReferenceTypeImpl(*this,
416+
/*signature*/ nullptr,
417+
/*functions count*/ true);
418+
}
419+
420+
/// Are variables of this type permitted to have
421+
/// ownership attributes?
422+
///
423+
/// This includes:
424+
/// - class types, generic or not
425+
/// - archetypes with class or class protocol bounds
426+
/// - existentials with class or class protocol bounds
427+
/// But not:
428+
/// - function types
429+
bool allowsOwnership(GenericSignature *sig) const {
430+
return isReferenceTypeImpl(*this, sig,
431+
/*functions count*/ false);
413432
}
414433

415434
/// Are values of this type essentially just class references,
@@ -419,10 +438,13 @@ class CanType : public Type {
419438
/// - a class type
420439
/// - a bound generic class type
421440
/// - a class-bounded archetype type
441+
/// - a class-bounded type parameter
422442
/// - a class-bounded existential type
423443
/// - a dynamic Self type
424444
bool isAnyClassReferenceType() const {
425-
return isReferenceTypeImpl(*this, /*functions count*/ false);
445+
return isReferenceTypeImpl(*this,
446+
/*signature*/ nullptr,
447+
/*functions count*/ false);
426448
}
427449

428450
/// Is this type existential?

branches/master-next/include/swift/AST/Types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {
541541

542542
/// allowsOwnership() - Are variables of this type permitted to have
543543
/// ownership attributes?
544-
bool allowsOwnership();
544+
bool allowsOwnership(GenericSignature *sig=nullptr);
545545

546546
/// Determine whether this type involves a type variable.
547547
bool hasTypeVariable() const {

branches/master-next/include/swift/ClangImporter/ClangImporter.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,13 @@ enum class SelectorSplitKind;
7171
/// Clang AST to ClangImporter to import the type into Swift.
7272
class DWARFImporterDelegate {
7373
public:
74-
virtual ~DWARFImporterDelegate() {}
74+
virtual ~DWARFImporterDelegate() = default;
7575
/// Perform a qualified lookup of a Clang type with this name.
7676
/// \param kind Only return results with this type kind.
7777
virtual void lookupValue(StringRef name, llvm::Optional<ClangTypeKind> kind,
7878
SmallVectorImpl<clang::Decl *> &results) {}
79+
/// vtable anchor.
80+
virtual void anchor();
7981
};
8082

8183
/// Class that imports Clang modules into Swift, mapping directly

branches/master-next/lib/AST/Type.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ bool TypeBase::isAnyClassReferenceType() {
161161
return getCanonicalType().isAnyClassReferenceType();
162162
}
163163

164-
bool CanType::isReferenceTypeImpl(CanType type, bool functionsCount) {
164+
bool CanType::isReferenceTypeImpl(CanType type, GenericSignature *sig,
165+
bool functionsCount) {
165166
switch (type->getKind()) {
166167
#define SUGARED_TYPE(id, parent) case TypeKind::id:
167168
#define TYPE(id, parent)
@@ -180,7 +181,7 @@ bool CanType::isReferenceTypeImpl(CanType type, bool functionsCount) {
180181
// For Self types, recur on the underlying type.
181182
case TypeKind::DynamicSelf:
182183
return isReferenceTypeImpl(cast<DynamicSelfType>(type).getSelfType(),
183-
functionsCount);
184+
sig, functionsCount);
184185

185186
// Archetypes and existentials are only class references if class-bounded.
186187
case TypeKind::PrimaryArchetype:
@@ -231,13 +232,14 @@ bool CanType::isReferenceTypeImpl(CanType type, bool functionsCount) {
231232

232233
case TypeKind::GenericTypeParam:
233234
case TypeKind::DependentMember:
234-
llvm_unreachable("Dependent types can't answer reference-semantics query");
235+
assert(sig && "dependent types can't answer reference semantics query");
236+
return sig->requiresClass(type);
235237
}
236238

237239
llvm_unreachable("Unhandled type kind!");
238240
}
239241

240-
/// hasOwnership - Are variables of this type permitted to have
242+
/// Are variables of this type permitted to have
241243
/// ownership attributes?
242244
///
243245
/// This includes:
@@ -246,8 +248,8 @@ bool CanType::isReferenceTypeImpl(CanType type, bool functionsCount) {
246248
/// - existentials with class or class protocol bounds
247249
/// But not:
248250
/// - function types
249-
bool TypeBase::allowsOwnership() {
250-
return getCanonicalType().isAnyClassReferenceType();
251+
bool TypeBase::allowsOwnership(GenericSignature *sig) {
252+
return getCanonicalType().allowsOwnership(sig);
251253
}
252254

253255
ExistentialLayout::ExistentialLayout(ProtocolType *type) {

branches/master-next/lib/ClangImporter/DWARFImporter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
using namespace swift;
1717

18+
void DWARFImporterDelegate::anchor() {}
19+
1820
/// Represents a Clang module that was "imported" from debug info. Since all the
1921
/// loading of types is done on demand, this class is effectively empty.
2022
class DWARFModuleUnit final : public LoadedFile {

branches/master-next/lib/Sema/TypeCheckAttr.cpp

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class AttributeEarlyChecker : public AttributeVisitor<AttributeEarlyChecker> {
133133
IGNORED_ATTR(DisfavoredOverload)
134134
IGNORED_ATTR(FunctionBuilder)
135135
IGNORED_ATTR(ProjectedValueProperty)
136+
IGNORED_ATTR(ReferenceOwnership)
136137
#undef IGNORED_ATTR
137138

138139
void visitAlignmentAttr(AlignmentAttr *attr) {
@@ -175,10 +176,6 @@ class AttributeEarlyChecker : public AttributeVisitor<AttributeEarlyChecker> {
175176
void visitConsumingAttr(ConsumingAttr *attr) { visitMutationAttr(attr); }
176177
void visitDynamicAttr(DynamicAttr *attr);
177178

178-
void visitReferenceOwnershipAttr(ReferenceOwnershipAttr *attr) {
179-
TC.checkReferenceOwnershipAttr(cast<VarDecl>(D), attr);
180-
}
181-
182179
void visitFinalAttr(FinalAttr *attr) {
183180
// Reject combining 'final' with 'open'.
184181
if (auto accessAttr = D->getAttrs().getAttribute<AccessControlAttr>()) {
@@ -2642,26 +2639,13 @@ void TypeChecker::checkDeclAttributes(Decl *D) {
26422639
}
26432640
}
26442641

2645-
void TypeChecker::checkTypeModifyingDeclAttributes(VarDecl *var) {
2646-
if (!var->hasType())
2647-
return;
2648-
2649-
if (auto *attr = var->getAttrs().getAttribute<ReferenceOwnershipAttr>())
2650-
checkReferenceOwnershipAttr(var, attr);
2651-
}
2652-
2653-
void TypeChecker::checkReferenceOwnershipAttr(VarDecl *var,
2642+
Type TypeChecker::checkReferenceOwnershipAttr(VarDecl *var, Type type,
26542643
ReferenceOwnershipAttr *attr) {
2655-
// Don't check ownership attribute if the declaration is already marked invalid.
2656-
if (var->isInvalid())
2657-
return;
2644+
auto *dc = var->getDeclContext();
26582645

2659-
Type type = var->getType();
2660-
Type interfaceType = var->getInterfaceType();
2661-
2662-
// Just stop if we've already processed this declaration.
2663-
if (type->is<ReferenceStorageType>())
2664-
return;
2646+
// Don't check ownership attribute if the type is invalid.
2647+
if (attr->isInvalid() || type->is<ErrorType>())
2648+
return type;
26652649

26662650
auto ownershipKind = attr->get();
26672651

@@ -2713,11 +2697,12 @@ void TypeChecker::checkReferenceOwnershipAttr(VarDecl *var,
27132697
if (!underlyingType)
27142698
underlyingType = type;
27152699

2716-
if (!underlyingType->allowsOwnership()) {
2700+
auto *sig = var->getDeclContext()->getGenericSignatureOfContext();
2701+
if (!underlyingType->allowsOwnership(sig)) {
27172702
auto D = diag::invalid_ownership_type;
27182703

27192704
if (underlyingType->isExistentialType() ||
2720-
underlyingType->is<ArchetypeType>()) {
2705+
underlyingType->isTypeParameter()) {
27212706
// Suggest the possibility of adding a class bound.
27222707
D = diag::invalid_ownership_protocol_type;
27232708
}
@@ -2735,7 +2720,7 @@ void TypeChecker::checkReferenceOwnershipAttr(VarDecl *var,
27352720
attr->setInvalid();
27362721
}
27372722

2738-
auto PDC = dyn_cast<ProtocolDecl>((var->getDeclContext()));
2723+
auto PDC = dyn_cast<ProtocolDecl>(dc);
27392724
if (PDC && !PDC->isObjC()) {
27402725
// Ownership does not make sense in protocols, except for "weak" on
27412726
// properties of Objective-C protocols.
@@ -2748,13 +2733,10 @@ void TypeChecker::checkReferenceOwnershipAttr(VarDecl *var,
27482733
}
27492734

27502735
if (attr->isInvalid())
2751-
return;
2736+
return type;
27522737

27532738
// Change the type to the appropriate reference storage type.
2754-
var->setType(ReferenceStorageType::get(
2755-
type, ownershipKind, Context));
2756-
var->setInterfaceType(ReferenceStorageType::get(
2757-
interfaceType, ownershipKind, Context));
2739+
return ReferenceStorageType::get(type, ownershipKind, Context);
27582740
}
27592741

27602742
Optional<Diag<>>

branches/master-next/lib/Sema/TypeCheckPattern.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,7 @@ bool TypeChecker::typeCheckParameterList(ParameterList *PL,
838838
} else {
839839
param->setInterfaceType(type);
840840
}
841-
842-
checkTypeModifyingDeclAttributes(param);
841+
843842
if (!hadError) {
844843
auto *nestedRepr = typeRepr;
845844

@@ -1125,19 +1124,25 @@ bool TypeChecker::coercePatternToType(Pattern *&P, TypeResolution resolution,
11251124
VarDecl *var = NP->getDecl();
11261125
if (var->isInvalid())
11271126
type = ErrorType::get(Context);
1128-
var->setType(type);
1129-
// FIXME: wtf
1130-
if (type->hasTypeParameter())
1131-
var->setInterfaceType(type);
1132-
else
1133-
var->setInterfaceType(type->mapTypeOutOfContext());
11341127

1135-
checkTypeModifyingDeclAttributes(var);
1136-
if (var->getAttrs().hasAttribute<ReferenceOwnershipAttr>())
1137-
type = var->getType()->getReferenceStorageReferent();
1138-
else if (!var->isInvalid())
1139-
type = var->getType();
1128+
Type interfaceType = type;
1129+
if (interfaceType->hasArchetype())
1130+
interfaceType = interfaceType->mapTypeOutOfContext();
1131+
1132+
// In SIL mode, VarDecls are written as having reference storage types.
1133+
if (type->is<ReferenceStorageType>()) {
1134+
assert(interfaceType->is<ReferenceStorageType>());
1135+
type = type->getReferenceStorageReferent();
1136+
} else {
1137+
if (auto *attr = var->getAttrs().getAttribute<ReferenceOwnershipAttr>())
1138+
interfaceType = checkReferenceOwnershipAttr(var, interfaceType, attr);
1139+
}
1140+
1141+
// Note that the pattern's type does not include the reference storage type.
11401142
P->setType(type);
1143+
var->setInterfaceType(interfaceType);
1144+
var->setType(var->getDeclContext()->mapTypeIntoContext(interfaceType));
1145+
11411146
var->getTypeLoc() = tyLoc;
11421147
var->getTypeLoc().setType(var->getType());
11431148

@@ -1663,8 +1668,6 @@ void TypeChecker::coerceParameterListToType(ParameterList *P, ClosureExpr *CE,
16631668
param->setType(ty);
16641669
param->setInterfaceType(ty->mapTypeOutOfContext());
16651670
}
1666-
1667-
checkTypeModifyingDeclAttributes(param);
16681671
};
16691672

16701673
// Coerce each parameter to the respective type.

branches/master-next/lib/Sema/TypeCheckStorage.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,6 @@ static void validatePatternBindingEntry(TypeChecker &tc,
251251
? Property : GlobalVariable);
252252
}
253253
}
254-
255-
// If we have any type-adjusting attributes, apply them here.
256-
assert(binding->getPattern(entryNumber)->hasType() && "Type missing?");
257-
if (auto var = binding->getSingleVar()) {
258-
tc.checkTypeModifyingDeclAttributes(var);
259-
}
260254
}
261255

262256
/// Validate the entries in the given pattern binding declaration.

branches/master-next/lib/Sema/TypeChecker.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -989,9 +989,9 @@ class TypeChecker final : public LazyResolver {
989989
void checkParameterAttributes(ParameterList *params);
990990
void checkDynamicReplacementAttribute(ValueDecl *D);
991991
static ValueDecl *findReplacedDynamicFunction(const ValueDecl *d);
992-
void checkTypeModifyingDeclAttributes(VarDecl *var);
993992

994-
void checkReferenceOwnershipAttr(VarDecl *D, ReferenceOwnershipAttr *attr);
993+
Type checkReferenceOwnershipAttr(VarDecl *D, Type interfaceType,
994+
ReferenceOwnershipAttr *attr);
995995

996996
/// Check the default arguments that occur within this value decl.
997997
void checkDefaultArguments(ParameterList *params, ValueDecl *VD);

0 commit comments

Comments
 (0)