Skip to content

[Serialization] Mark decls that can never be cross-referenced #17223

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
Jun 18, 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
4 changes: 4 additions & 0 deletions include/swift/AST/Attr.def
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@ SIMPLE_DECL_ATTR(_frozen, Frozen,
OnEnum |
UserInaccessible,
76)
SIMPLE_DECL_ATTR(_forbidSerializingReference, ForbidSerializingReference,
OnAnyDecl |
LongAttribute | RejectByParser | UserInaccessible | NotSerialized,
77)

#undef TYPE_ATTR
#undef DECL_ATTR_ALIAS
Expand Down
2 changes: 2 additions & 0 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4623,6 +4623,8 @@ namespace {
"This Objective-C class has only been forward-declared; "
"import its owning module to use it");
result->getAttrs().add(attr);
result->getAttrs().add(
new (Impl.SwiftContext) ForbidSerializingReferenceAttr(true));
return result;
}

Expand Down
2 changes: 2 additions & 0 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class AttributeEarlyChecker : public AttributeVisitor<AttributeEarlyChecker> {
IGNORED_ATTR(Effects)
IGNORED_ATTR(Exported)
IGNORED_ATTR(FixedLayout)
IGNORED_ATTR(ForbidSerializingReference)
IGNORED_ATTR(Frozen)
IGNORED_ATTR(Implements)
IGNORED_ATTR(ImplicitlyUnwrappedOptional)
Expand Down Expand Up @@ -805,6 +806,7 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
IGNORED_ATTR(Dynamic)
IGNORED_ATTR(Effects)
IGNORED_ATTR(Exported)
IGNORED_ATTR(ForbidSerializingReference)
IGNORED_ATTR(GKInspectable)
IGNORED_ATTR(IBDesignable)
IGNORED_ATTR(IBInspectable)
Expand Down
1 change: 1 addition & 0 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5777,6 +5777,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
UNINTERESTING_ATTR(DynamicMemberLookup)
UNINTERESTING_ATTR(SILGenName)
UNINTERESTING_ATTR(Exported)
UNINTERESTING_ATTR(ForbidSerializingReference)
UNINTERESTING_ATTR(GKInspectable)
UNINTERESTING_ATTR(IBAction)
UNINTERESTING_ATTR(IBDesignable)
Expand Down
4 changes: 4 additions & 0 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,10 @@ static void filterValues(Type expectedTy, ModuleDecl *expectedModule,
return true;
if (value->isStatic() != isStatic)
return true;

if (value->getAttrs().hasAttribute<ForbidSerializingReferenceAttr>())
return true;

// FIXME: Should be able to move a value from an extension in a derived
// module to the original definition in a base module.
if (expectedModule && !value->hasClangNode() &&
Expand Down
4 changes: 4 additions & 0 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,10 @@ DeclID Serializer::addDeclRef(const Decl *D, bool forceSerialization,
isa<PrecedenceGroupDecl>(D)) &&
"cannot cross-reference this decl");

assert((!isDeclXRef(D) ||
!D->getAttrs().hasAttribute<ForbidSerializingReferenceAttr>()) &&
"cannot cross-reference this decl");

assert((allowTypeAliasXRef || !isa<TypeAliasDecl>(D) ||
D->getModuleContext() == M) &&
"cannot cross-reference typealiases directly (use the NameAliasType)");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@class Confusing;

@protocol Confusing
@end
4 changes: 4 additions & 0 deletions test/ClangImporter/Inputs/custom-modules/module.map
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,8 @@ module Warnings9 { header "Warnings9.h" }
module ConditionallyFoo {
header "ConditionallyFoo.h"
config_macros [exhaustive] WANT_FOO
}

module ForwardDeclarationsHelper {
header "ForwardDeclarationsHelper.h"
}
9 changes: 9 additions & 0 deletions test/ClangImporter/objc_forward_declarations.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -o %t -I %S/Inputs/custom-modules -enable-objc-interop %s
// RUN: %target-swift-ide-test -print-module -module-to-print objc_forward_declarations -I %t -I %S/Inputs/custom-modules -enable-objc-interop -enable-objc-forward-declarations -source-filename x | %FileCheck %s

// CHECK: class Innocuous : Confusing {

import ForwardDeclarationsHelper

public class Innocuous: Confusing {}