Skip to content

Sema: Fix 'super' calls from methods returning 'Self' [3.1] #7914

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2506,8 +2506,13 @@ namespace {
if (!selfDecl->hasType())
return ErrorType::get(tc.Context);

Type declaredType = selfDecl->getType()->getRValueInstanceType();
Type superclassTy = declaredType->getSuperclass(&tc);
// If the method returns 'Self', the type of 'self' is a
// DynamicSelfType. Unwrap it before getting the superclass.
auto selfTy = selfDecl->getType()->getRValueInstanceType();
if (auto *dynamicSelfTy = selfTy->getAs<DynamicSelfType>())
selfTy = dynamicSelfTy->getSelfType();

auto superclassTy = selfTy->getSuperclass(&tc);

if (selfDecl->getType()->is<MetatypeType>())
superclassTy = MetatypeType::get(superclassTy);
Expand Down
55 changes: 55 additions & 0 deletions test/SILGen/dynamic_self.swift
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,61 @@ func partialApplySelfReturn(c: Factory, t: Factory.Type) {
_ = Factory.staticNewInstance
}

// Super call to a method returning Self
class Base {
required init() {}

func returnsSelf() -> Self {
return self
}

static func returnsSelfStatic() -> Self {
return self.init()
}
}

class Derived : Base {
// CHECK-LABEL: sil hidden @_TFC12dynamic_self7Derived9superCallfT_T_ : $@convention(method) (@guaranteed Derived) -> ()
// CHECK: [[SELF:%.*]] = copy_value %0
// CHECK: [[SUPER:%.*]] = upcast [[SELF]] : $Derived to $Base
// CHECK: [[METHOD:%.*]] = function_ref @_TFC12dynamic_self4Base11returnsSelffT_DS0_
// CHECK: apply [[METHOD]]([[SUPER]])
// CHECK: return
func superCall() {
super.returnsSelf()
}

// CHECK-LABEL: sil hidden @_TZFC12dynamic_self7Derived15superCallStaticfT_T_ : $@convention(method) (@thick Derived.Type) -> ()
// CHECK: [[SUPER:%.*]] = upcast %0 : $@thick Derived.Type to $@thick Base.Type
// CHECK: [[METHOD:%.*]] = function_ref @_TZFC12dynamic_self4Base17returnsSelfStaticfT_DS0_
// CHECK: apply [[METHOD]]([[SUPER]])
// CHECK: return
static func superCallStatic() {
super.returnsSelfStatic()
}

// CHECK-LABEL: sil hidden @_TFC12dynamic_self7Derived32superCallFromMethodReturningSelffT_DS0_ : $@convention(method) (@guaranteed Derived) -> @owned Derived
// CHECK: [[SELF:%.*]] = copy_value %0
// CHECK: [[SUPER:%.*]] = upcast [[SELF]] : $Derived to $Base
// CHECK: [[METHOD:%.*]] = function_ref @_TFC12dynamic_self4Base11returnsSelffT_DS0_
// CHECK: apply [[METHOD]]([[SUPER]])
// CHECK: return
func superCallFromMethodReturningSelf() -> Self {
super.returnsSelf()
return self
}

// CHECK-LABEL: sil hidden @_TZFC12dynamic_self7Derived38superCallFromMethodReturningSelfStaticfT_DS0_ : $@convention(method) (@thick Derived.Type) -> @owned Derived
// CHECK: [[SUPER:%.*]] = upcast %0 : $@thick Derived.Type to $@thick Base.Type
// CHECK: [[METHOD:%.*]] = function_ref @_TZFC12dynamic_self4Base17returnsSelfStaticfT_DS0_
// CHECK: apply [[METHOD]]([[SUPER]])
// CHECK: return
static func superCallFromMethodReturningSelfStatic() -> Self {
super.returnsSelfStatic()
return self.init()
}
}

// CHECK-LABEL: sil_witness_table hidden X: P module dynamic_self {
// CHECK: method #P.f!1: @_TTWC12dynamic_self1XS_1PS_FS1_1f

Expand Down