Skip to content

Commit

Permalink
Sema: Targeted fix for LLDB REPL issue
Browse files Browse the repository at this point in the history
LLDB changes accessiblity of declarations after type checking,
which is not a good idea because it is likely to break invariants.
Indeed, the validateFixedLayoutAttribute() / hasFixedLayout()
logic was not prepared for this possibility.

This is a targeted fix to address the immediate breakage. A better
fix would be to change LLDB, and also to change Sema to store the
global -enable-resilience flag state in a bit in the serialized
module, instead of sticking it on every declaration.

Fixes <rdar://problem/23545959>.
  • Loading branch information
slavapestov committed Nov 21, 2015
1 parent 88ca1e5 commit 1a640d4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
4 changes: 4 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1857,6 +1857,10 @@ bool NominalTypeDecl::hasFixedLayout() const {
return true;
}

// Objective-C enums always have a fixed layout.
if (isa<EnumDecl>(this) && isObjC())
return true;

// Otherwise, access via indirect "resilient" interfaces.
return false;
}
Expand Down
13 changes: 6 additions & 7 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6957,18 +6957,17 @@ static void validateFixedLayoutAttribute(TypeChecker &TC,
NominalTypeDecl *D) {
DeclAttributes &Attrs = D->getAttrs();

if (Attrs.hasAttribute<FixedLayoutAttr>())
// FIXME: Add a per-module serialized HasFixedLayout flag, instead of
// giving every decl this attribute.

if (Attrs.hasAttribute<FixedLayoutAttr>() ||
TC.Context.LangOpts.EnableResilience)
return;

// Since -enable-resilience should not change how we call into
// existing compiled modules, make all value types @_fixed_layout
// when the frontend is not run with the -enable-resilience flag.
if (!TC.Context.LangOpts.EnableResilience &&
D->getFormalAccess() == Accessibility::Public)
Attrs.add(new (TC.Context) FixedLayoutAttr(/*IsImplicit*/ true));
// @objc enums are always @_fixed_layout.
else if (isa<EnumDecl>(D) && D->isObjC())
Attrs.add(new (TC.Context) FixedLayoutAttr(/*IsImplicit*/ true));
Attrs.add(new (TC.Context) FixedLayoutAttr(/*IsImplicit*/ true));
}

static void validateAttributes(TypeChecker &TC, Decl *D) {
Expand Down
4 changes: 2 additions & 2 deletions test/Serialization/alignment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// RUN: %target-swift-frontend %s -emit-module -parse-as-library -o %t
// RUN: %target-sil-opt -enable-sil-verify-all %t/alignment.swiftmodule -o - | FileCheck %s

//CHECK: @_alignment(16) struct Foo {
@_alignment(16) struct Foo {}
//CHECK: @_alignment(16) @_fixed_layout struct Foo {
@_alignment(16) @_fixed_layout struct Foo {}

func foo(x: Foo) {}

0 comments on commit 1a640d4

Please sign in to comment.