Skip to content
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

[SR-13118] Incorrect diagnostic when checking protocol conformance for IUO of array #55564

Closed
typesanitizer opened this issue Jun 29, 2020 · 3 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@typesanitizer
Copy link

Previous ID SR-13118
Radar None
Original Reporter @typesanitizer
Type Bug
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee @slavapestov
Priority Medium

md5: bed0ffeb25a388d30c6a04741f0aa9f6

Issue Description:

Consider the following code

struct S<T>: Codable {
  var s: [T]!
} 

This returns the following diagnostic for Xcode 12 beta 1:

<unknown>:0: error: referencing instance method 'decodeIfPresent(_:forKey:)' on 'Array' requires that 'T' conform to 'Decodable'
Swift.Array:1:11: note: requirement from conditional conformance of '[T]' to 'Decodable'
extension Array : Decodable where Element : Decodable {
          ^
<unknown>:0: error: referencing instance method 'encodeIfPresent(_:forKey:)' on 'Array' requires that 'T' conform to 'Encodable'
Swift.Array:1:11: note: requirement from conditional conformance of '[T]' to 'Encodable'
extension Array : Encodable where Element : Encodable {
          ^

The diagnostics are showing a faulty source location, indicating that we have a type error an after synthesizing the decls. Instead, we should be erroring earlier like we do for [T]?.

struct S<T>: Codable {
   var s: [T]?
} 

This generates the appropriate diagnostics.

tmp.swift:1:8: error: type 'S' does not conform to protocol 'Decodable'
struct S<T>: Codable {
       ^
Swift.Decodable:2:5: note: protocol requires initializer 'init(from:)' with type 'Decodable'
    init(from decoder: Decoder) throws
    ^
tmp.swift:2:9: note: cannot automatically synthesize 'Decodable' because '[T]?' does not conform to 'Decodable'
    var x: [T]?
        ^
tmp.swift:1:8: error: type 'S' does not conform to protocol 'Encodable'
struct S<T>: Codable {
       ^
Swift.Encodable:2:10: note: protocol requires function 'encode(to:)' with type 'Encodable'
    func encode(to encoder: Encoder) throws
         ^
tmp.swift:2:9: note: cannot automatically synthesize 'Encodable' because '[T]?' does not conform to 'Encodable'
    var x: [T]?
        ^ 

At the moment, the example with [T]! crashes with master, which is tracked in SR-13117.

@typesanitizer
Copy link
Author

@swift-ci create

@CodaFi
Copy link
Member

CodaFi commented Jun 30, 2020

Looks like Slava got this one with SR-13117

/Users/rwidmann/test.swift:1:8: error: type 'S' does not conform to protocol 'Decodable'
struct S<T>: Codable {
       ^
Swift.Decodable:2:5: note: protocol requires initializer 'init(from:)' with type 'Decodable'
    init(from decoder: Decoder) throws
    ^
/Users/rwidmann/test.swift:2:7: note: cannot automatically synthesize 'Decodable' because 'T?' does not conform to 'Decodable'
  var s: T!
      ^
/Users/rwidmann/test.swift:1:8: error: type 'S' does not conform to protocol 'Encodable'
struct S<T>: Codable {
       ^
Swift.Encodable:2:10: note: protocol requires function 'encode(to:)' with type 'Encodable'
    func encode(to encoder: Encoder) throws
         ^
/Users/rwidmann/test.swift:2:7: note: cannot automatically synthesize 'Encodable' because 'T?' does not conform to 'Encodable'
  var s: T!
      ^

@swift-ci
Copy link
Collaborator

Comment by Mustafa YALCIN (JIRA)

3

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

3 participants