You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[SR-5858] Incorrectly implementing LocalizedError protocols does not result in an error at compile time but an incorrect error localized description
#4069
Closed
swift-ci opened this issue
Sep 8, 2017
· 4 comments
Stable Xcode 8.3
Latests Xcode 9 beta 6
Swift Dev. 4.0 (Sep 5, 2017) Platform: Linux (x86_64)
Additional Detail from JIRA
Votes
0
Component/s
Compiler, Foundation
Labels
Bug, TypeChecker
Assignee
None
Priority
Medium
md5: b7f836ae6e1516ca27059acef1da0130
relates to:
SR-522 Protocol funcs cannot have covariant returns
Issue Description:
As in the summary, incorrectly implementing LocalizedError protocol leads to an incorrect localizedDescription string. Instead of an incorrect "The operation couldn’t be completed" when I call localizedDescription I would expect the compiler to complain because my implementation is incorrect.
See below two implementations:
importFoundation// This is the bad error.enumBadError: LocalizedError {
casemyFailure// Note the return type is not Optional<String> but String (Due to a typo, bad copy-pasting, whatever)varerrorDescription: String {
switchself {
case .myFailure: return"BadErrorMyFailure"
}
}
}
// This is the good errorenumGoodError: LocalizedError {
casemyFailurevarerrorDescription: String? {
switchself {
case .myFailure: return"GoodErrorMyFailure"
}
}
}
// No compiler error nothing. This compiles without problem in Swift3 and latest 4 too print(BadError.myFailure.localizedDescription)
// "The operation couldn’t be completed. (__lldb_expr_32.BadError error 0.)"// I expect a error at compile time since `errorDescription:String? { get }` is expected but I am writing `errorDescription:String { get }` (without the question mark)print(GoodError.myFailure.localizedDescription)
// "GoodErrorMyFailure"
This has been addressed by the “near-miss” diagnostics from Swift 4.2. If you separate out your protocol conformances into separate extensions, which we generally consider good style, you will get a warning:
enum BadError {
case myFailure
}
extension BadError: LocalizedError {
// Note the return type is not Optional<String> but String (Due to a typo, bad copy-pasting, whatever)
var errorDescription: String { // warning: property 'errorDescription' nearly matches defaulted requirement 'errorDescription' of protocol 'LocalizedError'
switch self {
case .myFailure: return "BadErrorMyFailure"
}
}
}
Your original example, however, will not produce the warning. We initially did produce a warning for your example (in the earlier versions), but it was too noisy. So, we produce the warning when the extension declares the conformance and has a near-meatch with that protocol.
Attachment: Download
Environment
Reproducable everywhere I have tried:
Stable Xcode 8.3
Latests Xcode 9 beta 6
Swift Dev. 4.0 (Sep 5, 2017) Platform: Linux (x86_64)
Additional Detail from JIRA
md5: b7f836ae6e1516ca27059acef1da0130
relates to:
Issue Description:
As in the summary, incorrectly implementing LocalizedError protocol leads to an incorrect localizedDescription string. Instead of an incorrect "The operation couldn’t be completed" when I call localizedDescription I would expect the compiler to complain because my implementation is incorrect.
See below two implementations:
Same example in swift sandbox
The text was updated successfully, but these errors were encountered: