Skip to content

Fix a bug with class vs. protocol @_borrowed mismatches #20426

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 1 commit into from
Nov 8, 2018
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
7 changes: 3 additions & 4 deletions lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3064,10 +3064,9 @@ class Verifier : public ASTWalker {
abort();
}

if (AD->getAccessorKind() != AccessorKind::Read &&
AD->getAccessorKind() != AccessorKind::Modify) {
Out << "hasForcedStaticDispatch() set on accessor other than "
"read or modify\n";
if (AD->getStorage()->requiresOpaqueAccessor(AD->getAccessorKind())) {
Out << "hasForcedStaticDispatch() set on accessor that's opaque "
"for its storage\n";
abort();
}
}
Expand Down
29 changes: 16 additions & 13 deletions lib/Sema/CodeSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ static AccessorDecl *createGetterPrototype(TypeChecker &TC,
if (storage->isStatic())
getter->setStatic();

if (!storage->requiresOpaqueAccessor(AccessorKind::Get))
getter->setForcedStaticDispatch(true);

// Always add the getter to the context immediately after the storage.
addMemberToContextIfNeeded(getter, storage->getDeclContext(), storage);

Expand Down Expand Up @@ -298,6 +301,9 @@ static AccessorDecl *createSetterPrototype(TypeChecker &TC,
if (isStatic)
setter->setStatic();

// All mutable storage requires a setter.
assert(storage->requiresOpaqueAccessor(AccessorKind::Set));

// Always add the setter to the context immediately after the getter.
if (!getter) getter = storage->getGetter();
if (!getter) getter = storage->getReadCoroutine();
Expand All @@ -307,14 +313,6 @@ static AccessorDecl *createSetterPrototype(TypeChecker &TC,
return setter;
}

// True if the storage is dynamic or imported from Objective-C. In these cases,
// we need to emit static coroutine accessors that dynamically dispatch
// to 'get' and 'set', rather than the normal dynamically dispatched
// opaque accessors that peer dispatch to 'get' and 'set'.
static bool needsDynamicCoroutineAccessors(AbstractStorageDecl *storage) {
return storage->isObjCDynamic() || storage->hasClangNode();
}

/// Mark the accessor as transparent if we can.
///
/// If the storage is inside a fixed-layout nominal type, we can mark the
Expand Down Expand Up @@ -418,11 +416,11 @@ createCoroutineAccessorPrototype(TypeChecker &TC,
if (storage->isFinal())
makeFinal(ctx, accessor);

// If the storage is dynamic or ObjC-native, we can't add a dynamically-
// dispatched method entry for the accessor, so force it to be
// statically dispatched. ("final" would be inappropriate because the
// property can still be overridden.)
if (needsDynamicCoroutineAccessors(storage))
// If the storage does not provide this accessor as an opaque accessor,
// we can't add a dynamically-dispatched method entry for the accessor,
// so force it to be statically dispatched. ("final" would be inappropriate
// because the property can still be overridden.)
if (!storage->requiresOpaqueAccessor(kind))
accessor->setForcedStaticDispatch(true);

// Make sure the coroutine is available enough to access
Expand Down Expand Up @@ -2093,6 +2091,11 @@ void swift::maybeAddAccessorsToStorage(TypeChecker &TC,
}

static void synthesizeGetterBody(TypeChecker &TC, AccessorDecl *getter) {
if (getter->hasForcedStaticDispatch()) {
synthesizeTrivialGetterBody(TC, getter, TargetImpl::Ordinary);
return;
}

switch (getter->getStorage()->getReadImpl()) {
case ReadImplKind::Stored:
synthesizeTrivialGetterBody(TC, getter);
Expand Down
14 changes: 4 additions & 10 deletions lib/Sema/TypeCheckDeclOverride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1776,21 +1776,15 @@ OverriddenDeclsRequest::evaluate(Evaluator &evaluator, ValueDecl *decl) const {
auto baseAccessor = baseASD->getAccessor(kind);
if (!baseAccessor) continue;

switch (kind) {
case AccessorKind::Read:
if (baseASD->getReadCoroutine()->hasForcedStaticDispatch())
continue;
LLVM_FALLTHROUGH;
if (baseAccessor->hasForcedStaticDispatch())
continue;

switch (kind) {
case AccessorKind::Get:
case AccessorKind::Read:
break;

case AccessorKind::Modify:
if (baseASD->getModifyCoroutine()->hasForcedStaticDispatch())
continue;

LLVM_FALLTHROUGH;

case AccessorKind::Set:
// For setter accessors, we need the base's setter to be
// accessible from the overriding context, or it's not an override.
Expand Down
32 changes: 32 additions & 0 deletions test/SILGen/read_accessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,35 @@ public struct TestBorrowedProperty {
@_borrowed
public var borrowedString = ""
}

protocol ReadableTitle {
@_borrowed
var title: String { get }
}
class OverridableGetter : ReadableTitle {
var title: String = ""
}
// The concrete read accessor is generated on-demand and does a class dispatch to the getter.
// CHECK-LABEL: sil shared @$s13read_accessor17OverridableGetterC5titleSSvr
// CHECK: class_method %0 : $OverridableGetter, #OverridableGetter.title!getter.1
// CHECK-LABEL: // end sil function '$s13read_accessor17OverridableGetterC5titleSSvr'
// The read witness thunk does a direct call to the concrete read accessor.
// CHECK-LABEL: sil private [transparent] [thunk] @$s13read_accessor17OverridableGetterCAA13ReadableTitleA2aDP5titleSSvrTW
// CHECK: function_ref @$s13read_accessor17OverridableGetterC5titleSSvr
// CHECK-LABEL: // end sil function '$s13read_accessor17OverridableGetterCAA13ReadableTitleA2aDP5titleSSvrTW'

protocol GettableTitle {
var title: String { get }
}
class OverridableReader : GettableTitle {
@_borrowed
var title: String = ""
}
// The concrete getter is generated on-demand and does a class dispatch to the read accessor.
// CHECK-LABEL: sil shared @$s13read_accessor17OverridableReaderC5titleSSvg
// CHECK: class_method %0 : $OverridableReader, #OverridableReader.title!read.1
// CHECK-LABEL: // end sil function '$s13read_accessor17OverridableReaderC5titleSSvg'
// The getter witness thunk does a direct call to the concrete getter.
// CHECK-LABEL: sil private [transparent] [thunk] @$s13read_accessor17OverridableReaderCAA13GettableTitleA2aDP5titleSSvgTW
// CHECK: function_ref @$s13read_accessor17OverridableReaderC5titleSSvg
// CHECK-LABEL: // end sil function '$s13read_accessor17OverridableReaderCAA13GettableTitleA2aDP5titleSSvgTW'