-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Codable synthesis: excluded vars shouldn’t need explicit value assignments #11854
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
itaiferber
merged 1 commit into
swiftlang:master
from
itaiferber:codable-implicit-nil-values
Sep 11, 2017
Merged
Changes from all commits
Commits
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 hidden or 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
64 changes: 64 additions & 0 deletions
64
test/decl/protocol/special/coding/class_codable_excluded_optional_properties.swift
This file contains hidden or 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,64 @@ | ||
// RUN: %target-typecheck-verify-swift | ||
|
||
class ClassWithNonExcludedLetMembers : Codable { // expected-error {{class 'ClassWithNonExcludedLetMembers' has no initializers}} | ||
// expected-error@-1 {{type 'ClassWithNonExcludedLetMembers' does not conform to protocol 'Decodable'}} | ||
// expected-error@-2 {{type 'ClassWithNonExcludedLetMembers' does not conform to protocol 'Encodable'}} | ||
|
||
// Optional `let`s do not get an implicit nil assignment. | ||
let p1: String? // expected-note {{stored property 'p1' without initial value prevents synthesized initializers}} | ||
let p2: String! // expected-note {{stored property 'p2' without initial value prevents synthesized initializers}} | ||
|
||
// AnyHashable does not conform to Codable. | ||
let p3: AnyHashable? // expected-note {{stored property 'p3' without initial value prevents synthesized initializers}} | ||
// expected-note@-1 {{cannot automatically synthesize 'Decodable' because 'AnyHashable?' does not conform to 'Decodable'}} | ||
// expected-note@-2 {{cannot automatically synthesize 'Encodable' because 'AnyHashable?' does not conform to 'Encodable'}} | ||
let p4: AnyHashable? // expected-note {{stored property 'p4' without initial value prevents synthesized initializers}} | ||
// expected-note@-1 {{cannot automatically synthesize 'Decodable' because 'AnyHashable?' does not conform to 'Decodable'}} | ||
// expected-note@-2 {{cannot automatically synthesize 'Encodable' because 'AnyHashable?' does not conform to 'Encodable'}} | ||
} | ||
|
||
class ClassWithExcludedLetMembers : Codable { // expected-error {{class 'ClassWithExcludedLetMembers' has no initializers}} | ||
// expected-error@-1 {{type 'ClassWithExcludedLetMembers' does not conform to protocol 'Decodable'}} | ||
|
||
// Optional `let`s do not get an implicit nil assignment. | ||
let p1: String? // expected-note {{stored property 'p1' without initial value prevents synthesized initializers}} | ||
let p2: String! // expected-note {{stored property 'p2' without initial value prevents synthesized initializers}} | ||
|
||
// AnyHashable does not conform to Codable. | ||
let p3: AnyHashable? // expected-note {{stored property 'p3' without initial value prevents synthesized initializers}} | ||
// expected-note@-1 {{cannot automatically synthesize 'Decodable' because 'p3' does not have a matching CodingKey and does not have a default value}} | ||
let p4: AnyHashable? // expected-note {{stored property 'p4' without initial value prevents synthesized initializers}} | ||
// expected-note@-1 {{cannot automatically synthesize 'Decodable' because 'p4' does not have a matching CodingKey and does not have a default value}} | ||
|
||
// Explicitly exclude non-Codable properties. | ||
enum CodingKeys : String, CodingKey { | ||
case p1, p2 | ||
} | ||
} | ||
|
||
class ClassWithNonExcludedVarMembers : Codable { // expected-error {{type 'ClassWithNonExcludedVarMembers' does not conform to protocol 'Decodable'}} | ||
// expected-error@-1 {{type 'ClassWithNonExcludedVarMembers' does not conform to protocol 'Encodable'}} | ||
|
||
var p1: String? | ||
var p2: String! | ||
|
||
// AnyHashable does not conform to Codable. | ||
var p3: AnyHashable? // expected-note {{cannot automatically synthesize 'Decodable' because 'AnyHashable?' does not conform to 'Decodable'}} | ||
// expected-note@-1 {{cannot automatically synthesize 'Encodable' because 'AnyHashable?' does not conform to 'Encodable'}} | ||
var p4: AnyHashable! // expected-note {{cannot automatically synthesize 'Decodable' because 'AnyHashable!' does not conform to 'Decodable'}} | ||
// expected-note@-1 {{cannot automatically synthesize 'Encodable' because 'AnyHashable!' does not conform to 'Encodable'}} | ||
} | ||
|
||
class ClassWithExcludedVarMembers : Codable { | ||
var p1: String? | ||
var p2: String! | ||
|
||
// AnyHashable does not conform to Codable. | ||
var p3: AnyHashable? | ||
var p4: AnyHashable! | ||
|
||
// Explicitly exclude non-Codable properties. | ||
enum CodingKeys : String, CodingKey { | ||
case p1, p2 | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
test/decl/protocol/special/coding/struct_codable_excluded_optional_properties.swift
This file contains hidden or 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,69 @@ | ||
// RUN: %target-typecheck-verify-swift | ||
|
||
struct StructWithNonExcludedLetMembers : Codable { // expected-error {{type 'StructWithNonExcludedLetMembers' does not conform to protocol 'Decodable'}} | ||
// expected-error@-1 {{type 'StructWithNonExcludedLetMembers' does not conform to protocol 'Encodable'}} | ||
|
||
// Suppress the memberwise initializer. Optional `let`s do not get an implicit nil assignment. | ||
init() {} | ||
|
||
let p1: String? | ||
let p2: String! | ||
|
||
// AnyHashable does not conform to Codable. | ||
let p3: AnyHashable? // expected-note {{cannot automatically synthesize 'Decodable' because 'AnyHashable?' does not conform to 'Decodable'}} | ||
// expected-note@-1 {{cannot automatically synthesize 'Encodable' because 'AnyHashable?' does not conform to 'Encodable'}} | ||
let p4: AnyHashable! // expected-note {{cannot automatically synthesize 'Decodable' because 'AnyHashable!' does not conform to 'Decodable'}} | ||
// expected-note@-1 {{cannot automatically synthesize 'Encodable' because 'AnyHashable!' does not conform to 'Encodable'}} | ||
} | ||
|
||
struct StructWithExcludedLetMembers : Codable { // expected-error {{type 'StructWithExcludedLetMembers' does not conform to protocol 'Decodable'}} | ||
|
||
// Suppress the memberwise initializer. Optional `let`s do not get an implicit nil assignment. | ||
init() {} | ||
|
||
let p1: String? | ||
let p2: String! | ||
|
||
// AnyHashable does not conform to Codable. | ||
let p3: AnyHashable? // expected-note {{cannot automatically synthesize 'Decodable' because 'p3' does not have a matching CodingKey and does not have a default value}} | ||
let p4: AnyHashable! // expected-note {{cannot automatically synthesize 'Decodable' because 'p4' does not have a matching CodingKey and does not have a default value}} | ||
|
||
// Explicitly exclude non-Codable properties. | ||
enum CodingKeys : String, CodingKey { | ||
case p1, p2 | ||
} | ||
} | ||
|
||
struct StructWithNonExcludedVarMembers : Codable { // expected-error {{type 'StructWithNonExcludedVarMembers' does not conform to protocol 'Decodable'}} | ||
// expected-error@-1 {{type 'StructWithNonExcludedVarMembers' does not conform to protocol 'Encodable'}} | ||
|
||
// Suppress the memberwise initializer. | ||
init() {} | ||
|
||
var p1: String? | ||
var p2: String! | ||
|
||
// AnyHashable does not conform to Codable. | ||
var p3: AnyHashable? // expected-note {{cannot automatically synthesize 'Decodable' because 'AnyHashable?' does not conform to 'Decodable'}} | ||
// expected-note@-1 {{cannot automatically synthesize 'Encodable' because 'AnyHashable?' does not conform to 'Encodable'}} | ||
var p4: AnyHashable! // expected-note {{cannot automatically synthesize 'Decodable' because 'AnyHashable!' does not conform to 'Decodable'}} | ||
// expected-note@-1 {{cannot automatically synthesize 'Encodable' because 'AnyHashable!' does not conform to 'Encodable'}} | ||
} | ||
|
||
struct StructWithExcludedVarMembers : Codable { | ||
|
||
// Suppress the memberwise initializer. | ||
init() {} | ||
|
||
var p1: String? | ||
var p2: String! | ||
|
||
// AnyHashable does not conform to Codable. | ||
var p3: AnyHashable? | ||
var p4: AnyHashable! | ||
|
||
// Explicitly exclude non-Codable properties. | ||
enum CodingKeys : String, CodingKey { | ||
case p1, p2 | ||
} | ||
} |
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.
We have
TypeBase::getAnyOptionalObjectType
for this, but really I'm leery of putting this check in more than one place. Is there any way to reuse the logic inTypeChecker::addImplicitConstructors
?Uh oh!
There was an error while loading. Please reload this page.
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.
@jrose-apple That's likely a smarter solution. Are you interested in copying the logic into here, or factoring it out for reuse (e.g. adding
isDefaultInitializable
or similar toPatternBindingDecl
)?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 suspect it belongs on VarDecl rather than PatternBindingDecl, but the latter sounds good to me. Even if it's mostly just "is optional and is not immutable".
Uh oh!
There was an error while loading. Please reload this page.
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.
@jrose-apple I'll adapt this, then. I mention
PatternBindingDecl
only because the logic inTypeChecker::addImplicitConstructors
revolves around that:(
isDefaultInitializable
takes aPatternBindingDecl
)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.
Hmm, though I guess the implementation we really care about takes a
TypeRepr *
: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.
Ah, right. The existing logic is set up the way it is mostly because we don't have a way to get directly from a VarDecl to an initializer, but I guess it can stay that way.
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.
You also care about the
isNeverDefaultInitializable
bit, so just looking at the TypeRepr isn't really sufficient.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.
Ah, yes. How much of this do we care to expose, then? Should this just be exposed on
PatternBindingDecl
, or is it helpful anywhere else to expose the intermediate layers too (onTypeRepr
andPattern
)?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 think just using PatternBindingDecl is fine. We don't currently default-initialize anything but variables.