Skip to content

Commit 4ba00a0

Browse files
committed
AST: Remove FuncDecl::getDynamicSelf() and getDynamicSelfInterface()
1 parent a17eec5 commit 4ba00a0

File tree

6 files changed

+28
-60
lines changed

6 files changed

+28
-60
lines changed

include/swift/AST/Decl.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5043,15 +5043,6 @@ class FuncDecl final : public AbstractFunctionDecl,
50435043
void setDynamicSelf(bool hasDynamicSelf) {
50445044
FuncDeclBits.HasDynamicSelf = hasDynamicSelf;
50455045
}
5046-
5047-
/// Retrieve the dynamic \c Self type for this method, or a null type if
5048-
/// this method does not have a dynamic \c Self return type.
5049-
DynamicSelfType *getDynamicSelf() const;
5050-
5051-
/// Retrieve the dynamic \c Self interface type for this method, or
5052-
/// a null type if this method does not have a dynamic \c Self
5053-
/// return type.
5054-
DynamicSelfType *getDynamicSelfInterface() const;
50555046

50565047
/// Determine whether this method has an archetype \c Self return
50575048
/// type. This is when a method defined in a protocol extension

lib/AST/Decl.cpp

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3959,12 +3959,19 @@ static Type getSelfTypeForContainer(AbstractFunctionDecl *theMethod,
39593959
bool isInitializingCtor,
39603960
bool wantInterfaceType) {
39613961
auto *dc = theMethod->getDeclContext();
3962+
auto &Ctx = dc->getASTContext();
39623963

39633964
// Determine the type of the container.
3964-
Type containerTy = wantInterfaceType ? dc->getDeclaredInterfaceType()
3965+
auto containerTy = wantInterfaceType ? dc->getDeclaredInterfaceType()
39653966
: dc->getDeclaredTypeInContext();
3966-
if (!containerTy)
3967-
return ErrorType::get(dc->getASTContext());
3967+
if (!containerTy || containerTy->hasError())
3968+
return ErrorType::get(Ctx);
3969+
3970+
// Determine the type of 'self' inside the container.
3971+
auto selfTy = wantInterfaceType ? dc->getSelfInterfaceType()
3972+
: dc->getSelfTypeInContext();
3973+
if (!selfTy || selfTy->hasError())
3974+
return ErrorType::get(Ctx);
39683975

39693976
bool isStatic = false;
39703977
bool isMutating = false;
@@ -3977,8 +3984,9 @@ static Type getSelfTypeForContainer(AbstractFunctionDecl *theMethod,
39773984
// The non-interface type of a method that returns DynamicSelf
39783985
// uses DynamicSelf for the type of 'self', which is important
39793986
// when type checking the body of the function.
3980-
if (!wantInterfaceType)
3981-
selfTypeOverride = FD->getDynamicSelf();
3987+
if (!wantInterfaceType && FD->hasDynamicSelf()) {
3988+
selfTy = DynamicSelfType::get(selfTy, Ctx);
3989+
}
39823990
} else if (isa<ConstructorDecl>(theMethod)) {
39833991
if (isInitializingCtor) {
39843992
// initializing constructors of value types always have an implicitly
@@ -3993,37 +4001,19 @@ static Type getSelfTypeForContainer(AbstractFunctionDecl *theMethod,
39934001
isMutating = true;
39944002
}
39954003

3996-
Type selfTy = selfTypeOverride;
3997-
if (!selfTy) {
3998-
// For a protocol, the type of 'self' is the parameter type 'Self', not
3999-
// the protocol itself.
4000-
if (containerTy->is<ProtocolType>()) {
4001-
if (wantInterfaceType)
4002-
selfTy = dc->getSelfInterfaceType();
4003-
else
4004-
selfTy = dc->getSelfTypeInContext();
4005-
} else
4006-
selfTy = containerTy;
4007-
}
4008-
4009-
// If the self type couldn't be computed, or is the result of an
4010-
// upstream error, return an error type.
4011-
if (!selfTy || selfTy->hasError())
4012-
return ErrorType::get(dc->getASTContext());
4013-
40144004
// 'static' functions have 'self' of type metatype<T>.
40154005
if (isStatic)
4016-
return MetatypeType::get(selfTy, dc->getASTContext());
4017-
4006+
return MetatypeType::get(selfTy, Ctx);
4007+
40184008
// Reference types have 'self' of type T.
40194009
if (containerTy->hasReferenceSemantics())
40204010
return selfTy;
4021-
4011+
40224012
// Mutating methods are always passed inout so we can receive the side
40234013
// effect.
40244014
if (isMutating)
40254015
return InOutType::get(selfTy);
4026-
4016+
40274017
// Nonmutating methods on structs and enums pass the receiver by value.
40284018
return selfTy;
40294019
}
@@ -4530,23 +4520,6 @@ void DestructorDecl::setSelfDecl(ParamDecl *selfDecl) {
45304520
}
45314521
}
45324522

4533-
4534-
DynamicSelfType *FuncDecl::getDynamicSelf() const {
4535-
if (!hasDynamicSelf())
4536-
return nullptr;
4537-
4538-
return DynamicSelfType::get(getDeclContext()->getSelfTypeInContext(),
4539-
getASTContext());
4540-
}
4541-
4542-
DynamicSelfType *FuncDecl::getDynamicSelfInterface() const {
4543-
if (!hasDynamicSelf())
4544-
return nullptr;
4545-
4546-
return DynamicSelfType::get(getDeclContext()->getSelfInterfaceType(),
4547-
getASTContext());
4548-
}
4549-
45504523
SourceRange FuncDecl::getSourceRange() const {
45514524
SourceLoc StartLoc = getStartLoc();
45524525
if (StartLoc.isInvalid() ||

lib/ClangImporter/ImportDecl.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3287,10 +3287,9 @@ namespace {
32873287
// in Swift as DynamicSelf, do so.
32883288
if (decl->hasRelatedResultType()) {
32893289
result->setDynamicSelf(true);
3290-
resultTy = result->getDynamicSelfInterface();
3291-
assert(resultTy && "failed to get dynamic self");
3290+
resultTy = DynamicSelfType::get(dc->getSelfInterfaceType(),
3291+
Impl.SwiftContext);
32923292

3293-
Type dynamicSelfTy = result->getDynamicSelfInterface();
32943293
OptionalTypeKind nullability = OTK_ImplicitlyUnwrappedOptional;
32953294
if (auto typeNullability = decl->getReturnType()->getNullability(
32963295
Impl.getClangASTContext())) {
@@ -3299,7 +3298,6 @@ namespace {
32993298
}
33003299
if (nullability != OTK_None && !errorConvention.hasValue()) {
33013300
resultTy = OptionalType::get(nullability, resultTy);
3302-
dynamicSelfTy = OptionalType::get(nullability, dynamicSelfTy);
33033301
}
33043302

33053303
// Update the method type with the new result type.

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,9 @@ static Type getResultType(TypeChecker &TC, FuncDecl *fn, Type resultType) {
466466

467467
// Rewrite dynamic self to the appropriate interface type.
468468
if (resultType->is<DynamicSelfType>()) {
469-
return fn->getDynamicSelfInterface();
469+
return DynamicSelfType::get(
470+
fn->getDeclContext()->getSelfInterfaceType(),
471+
TC.Context);
470472
}
471473

472474
// Weird hacky special case.
@@ -611,6 +613,8 @@ void TypeChecker::configureInterfaceType(AbstractFunctionDecl *func,
611613
auto *dc = ctor->getDeclContext();
612614

613615
funcTy = dc->getSelfInterfaceType();
616+
if (!funcTy)
617+
funcTy = ErrorType::get(Context);
614618

615619
// Adjust result type for failability.
616620
if (ctor->getFailability() != OTK_None)

lib/Sema/TypeCheckType.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,9 @@ resolveTopLevelIdentTypeComponent(TypeChecker &TC, DeclContext *DC,
939939
// The issue is though that ComponentIdentTypeRepr only accepts a ValueDecl
940940
// while the 'Self' type is more than just a reference to a TypeDecl.
941941

942-
return func->getDynamicSelf();
942+
return DynamicSelfType::get(
943+
func->getDeclContext()->getSelfTypeInContext(),
944+
TC.Context);
943945
}
944946

945947
// For lookups within the generic signature, look at the generic

validation-test/compiler_crashers/28155-swift-typechecker-validategenericfuncsignature.swift renamed to validation-test/compiler_crashers_fixed/28155-swift-typechecker-validategenericfuncsignature.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -typecheck
8+
// RUN: not %target-swift-frontend %s -typecheck
99
// Crash type: memory error ("Invalid read of size 2")
1010
class A{func b->Self{{{}}class B<n{let a=self

0 commit comments

Comments
 (0)