Skip to content

Commit 1d03e05

Browse files
authored
Merge pull request swiftlang#6034 from slavapestov/small-cleanups
Small cleanups
2 parents a184057 + 1930a8a commit 1d03e05

File tree

7 files changed

+14
-49
lines changed

7 files changed

+14
-49
lines changed

include/swift/AST/Decl.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,10 +2434,6 @@ class GenericTypeParamDecl : public AbstractTypeParamDecl {
24342434
SourceLoc getStartLoc() const { return getNameLoc(); }
24352435
SourceRange getSourceRange() const;
24362436

2437-
/// Determine whether this is the implicit 'Self' type parameter of
2438-
/// a protocol.
2439-
bool isProtocolSelf() const;
2440-
24412437
static bool classof(const Decl *D) {
24422438
return D->getKind() == DeclKind::GenericTypeParam;
24432439
}

include/swift/AST/DeclContext.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,6 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
265265
/// If this DeclContext is a protocol extension, return the extended protocol.
266266
ProtocolDecl *getAsProtocolExtensionContext() const;
267267

268-
/// \brief Retrieve the generic parameter 'Self' from a protocol or
269-
/// protocol extension.
270-
///
271-
/// Only valid if \c getAsProtocolOrProtocolExtensionContext().
272-
GenericTypeParamDecl *getProtocolSelf() const;
273-
274268
/// \brief Retrieve the generic parameter 'Self' from a protocol or
275269
/// protocol extension.
276270
///

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,9 +533,8 @@ void ASTPrinter::printTextImpl(StringRef Text) {
533533

534534
void ASTPrinter::printTypeRef(Type T, const TypeDecl *RefTo, Identifier Name) {
535535
PrintNameContext Context = PrintNameContext::Normal;
536-
if (auto GP = dyn_cast<GenericTypeParamDecl>(RefTo)) {
537-
if (GP->isProtocolSelf())
538-
Context = PrintNameContext::GenericParameter;
536+
if (isa<GenericTypeParamDecl>(RefTo)) {
537+
Context = PrintNameContext::GenericParameter;
539538
} else if (T && T->is<DynamicSelfType>()) {
540539
assert(T->castTo<DynamicSelfType>()->getSelfType()->getAnyNominal() &&
541540
"protocol Self handled as GenericTypeParamDecl");
@@ -3951,7 +3950,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
39513950
if (Name.empty())
39523951
Printer << "<anonymous>";
39533952
else {
3954-
if (T->getDecl() && T->getDecl()->isProtocolSelf()) {
3953+
if (T->getDecl() &&
3954+
T->getDecl()->getDeclContext()->getAsProtocolOrProtocolExtensionContext()) {
39553955
Printer.printTypeRef(T, T->getDecl(), Name);
39563956
return;
39573957
}

lib/AST/Decl.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2218,14 +2218,6 @@ SourceRange GenericTypeParamDecl::getSourceRange() const {
22182218
return SourceRange(getNameLoc(), endLoc);
22192219
}
22202220

2221-
bool GenericTypeParamDecl::isProtocolSelf() const {
2222-
if (!isImplicit()) return false;
2223-
auto dc = getDeclContext();
2224-
if (!dc->getAsProtocolOrProtocolExtensionContext()) return false;
2225-
return dc->getProtocolSelf() == this;
2226-
}
2227-
2228-
22292221
AssociatedTypeDecl::AssociatedTypeDecl(DeclContext *dc, SourceLoc keywordLoc,
22302222
Identifier name, SourceLoc nameLoc,
22312223
TypeLoc defaultDefinition)

lib/AST/DeclContext.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ ProtocolDecl *DeclContext::getAsProtocolExtensionContext() const {
106106
getAsGenericTypeOrGenericTypeExtensionContext());
107107
}
108108

109-
GenericTypeParamDecl *DeclContext::getProtocolSelf() const {
109+
GenericTypeParamType *DeclContext::getProtocolSelfType() const {
110110
auto *proto = getAsProtocolOrProtocolExtensionContext();
111111
assert(proto && "not a protocol");
112112

@@ -118,14 +118,8 @@ GenericTypeParamDecl *DeclContext::getProtocolSelf() const {
118118
if (!isInnermostContextGeneric())
119119
return nullptr;
120120

121-
return getGenericParamsOfContext()->getParams().front();
122-
}
123-
124-
GenericTypeParamType *DeclContext::getProtocolSelfType() const {
125-
auto *param = getProtocolSelf();
126-
if (!param)
127-
return nullptr;
128-
return param->getDeclaredInterfaceType()
121+
return getGenericParamsOfContext()->getParams().front()
122+
->getDeclaredInterfaceType()
129123
->castTo<GenericTypeParamType>();
130124
}
131125

lib/Sema/TypeCheckDecl.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -273,18 +273,13 @@ void TypeChecker::checkInheritanceClause(Decl *decl,
273273
} else if (auto ext = dyn_cast<ExtensionDecl>(decl)) {
274274
DC = ext;
275275
options |= TR_GenericSignature | TR_InheritanceClause;
276-
} else if (auto GP = dyn_cast<GenericTypeParamDecl>(decl)) {
276+
} else if (isa<GenericTypeParamDecl>(decl)) {
277277
// For generic parameters, we want name lookup to look at just the
278278
// signature of the enclosing entity.
279279
DC = decl->getDeclContext();
280280
if (auto nominal = dyn_cast<NominalTypeDecl>(DC)) {
281281
DC = nominal;
282282
options |= TR_GenericSignature;
283-
// When looking up protocol 'Self' accessibility checks are disabled to
284-
// head off spurious unavailable diagnostics.
285-
if (isa<ProtocolDecl>(DC) && GP->isProtocolSelf()) {
286-
options |= TR_AllowUnavailable;
287-
}
288283
} else if (auto ext = dyn_cast<ExtensionDecl>(DC)) {
289284
DC = ext;
290285
options |= TR_GenericSignature;
@@ -1123,13 +1118,6 @@ void swift::configureImplicitSelf(TypeChecker &tc,
11231118
AbstractFunctionDecl *func) {
11241119
auto selfDecl = func->getImplicitSelfDecl();
11251120

1126-
// Validate the context.
1127-
if (auto nominal = dyn_cast<NominalTypeDecl>(func->getDeclContext())) {
1128-
tc.validateDecl(nominal);
1129-
} else {
1130-
tc.validateExtension(cast<ExtensionDecl>(func->getDeclContext()));
1131-
}
1132-
11331121
// Compute the type of self.
11341122
Type selfTy = func->computeSelfType();
11351123
assert(selfDecl && selfTy && "Not a method");

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ static Type getResultType(TypeChecker &TC, FuncDecl *fn, Type resultType) {
488488

489489
/// Determine whether the given type is \c Self, an associated type of \c Self,
490490
/// or a concrete type.
491-
static bool isSelfDerivedOrConcrete(Type type) {
491+
static bool isSelfDerivedOrConcrete(Type protoSelf, Type type) {
492492
// Check for a concrete type.
493493
if (!type->hasTypeParameter())
494494
return true;
@@ -498,8 +498,8 @@ static bool isSelfDerivedOrConcrete(Type type) {
498498
type = depMem->getBase();
499499
}
500500

501-
if (auto gp = type->getAs<GenericTypeParamType>())
502-
return gp->getDepth() == 0 && gp->getIndex() == 0;
501+
if (type->is<GenericTypeParamType>())
502+
return type->isEqual(protoSelf);
503503

504504
return false;
505505
}
@@ -544,11 +544,12 @@ TypeChecker::validateGenericFuncSignature(AbstractFunctionDecl *func) {
544544
if (!invalid && func->getGenericParams() &&
545545
isa<ProtocolDecl>(func->getDeclContext())) {
546546
auto proto = cast<ProtocolDecl>(func->getDeclContext());
547+
auto protoSelf = proto->getSelfInterfaceType();
547548
for (auto req : sig->getRequirements()) {
548549
// If one of the types in the requirement is dependent on a non-Self
549550
// type parameter, this requirement is okay.
550-
if (!isSelfDerivedOrConcrete(req.getFirstType()) ||
551-
!isSelfDerivedOrConcrete(req.getSecondType()))
551+
if (!isSelfDerivedOrConcrete(protoSelf, req.getFirstType()) ||
552+
!isSelfDerivedOrConcrete(protoSelf, req.getSecondType()))
552553
continue;
553554

554555
// The conformance of 'Self' to the protocol is okay.

0 commit comments

Comments
 (0)