-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Fix Codable vtable layout #16057
Merged
slavapestov
merged 2 commits into
swiftlang:master
from
slavapestov:codable-vtable-layout
Apr 20, 2018
Merged
Fix Codable vtable layout #16057
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// RUN: %empty-directory(%t) | ||
// RUN: cp %s %t/main.swift | ||
// RUN: %target-build-swift %t/main.swift %S/Inputs/CodableMultifileOther.swift -module-name main -o %t/main | ||
// RUN: %target-run %t/main | ||
// REQUIRES: executable_test | ||
|
||
// FIXME: This test could run on Linux too, if we could either use | ||
// corelibs-foundation, or implement a mock Encoder for testing. | ||
|
||
// REQUIRES: objc_interop | ||
|
||
import StdlibUnittest | ||
import Foundation | ||
|
||
var CodableMultifileTestSuite = TestSuite("CodableMultifile") | ||
|
||
// The first test doesn't synthesize encode(to:) at all. | ||
CodableMultifileTestSuite.test("1") { | ||
let derived = DerivedFirst() | ||
|
||
// Make sure the vtable offset matches between this translation unit and | ||
// the other file, which requires us to force the encode(to:) member when | ||
// type checking this translation unit. | ||
expectEqual(false, derived.derivedMember) | ||
} | ||
|
||
// The second test synthesizes init(from:) before encode(to:). | ||
|
||
// We define a wrapper so that the virtual method BaseSecond.encode(to:) method | ||
// is called from outside its module. If we use the conformance of BaseSecond | ||
// to Codable, we don't expose the bug because the virtual method call is | ||
// encapsulated in the conformance, which is emitted in the same translation unit | ||
// as BaseSecond. | ||
struct WrapperSecond : Encodable { | ||
let base: BaseSecond | ||
|
||
func encode(to encoder: Encoder) throws { | ||
try base.encode(to: encoder) | ||
} | ||
} | ||
|
||
CodableMultifileTestSuite.test("2") { | ||
// Make sure we synthesize the init(from:) member before encode(to:) in | ||
// this translation unit. | ||
_ = BaseSecond.init(from:) | ||
|
||
let base = WrapperSecond(base: BaseSecond()) | ||
let encoder = JSONEncoder() | ||
|
||
expectEqual( | ||
"{\"baseMember\":2}", | ||
String(data: try! encoder.encode(base), encoding: .utf8)!) | ||
} | ||
|
||
// The third test synthesizes encode(to:) before init(from:). | ||
|
||
// See above. | ||
struct WrapperThird : Encodable { | ||
let base: BaseThird | ||
|
||
func encode(to encoder: Encoder) throws { | ||
try base.encode(to: encoder) | ||
} | ||
} | ||
|
||
CodableMultifileTestSuite.test("3") { | ||
// Make sure we synthesize the encode(to:) member before init(from:) in | ||
// this translation unit. | ||
_ = BaseThird.encode(to:) | ||
|
||
let base = WrapperThird(base: BaseThird()) | ||
let encoder = JSONEncoder() | ||
|
||
expectEqual( | ||
"{\"baseMember\":3}", | ||
String(data: try! encoder.encode(base), encoding: .utf8)!) | ||
} | ||
|
||
runAllTests() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class BaseFirst: Codable { | ||
var baseMember = 1 | ||
|
||
init() {} | ||
} | ||
|
||
class DerivedFirst: BaseFirst { | ||
var derivedMember = false | ||
} | ||
|
||
class BaseSecond: Codable { | ||
var baseMember = 2 | ||
|
||
init() {} | ||
} | ||
|
||
class BaseThird: Codable { | ||
var baseMember = 3 | ||
|
||
init() {} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if some of the less-capable C++ compilers we build with will handle this
decltype
idiom. I guess we'll find out