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-7359] Generic Subscript Parameter Requires Conformance to Hashable, But Then Says It's Redundant #49907
Comments
Oops, we fixed this for functions, but I guess we missed subscripts. Thanks, Ben! cc @DougGregor, @huonw @swift-ci create |
Comment by Ben Stock (JIRA) @belkadan No problem! I have ADD, so seeing that little yellow warning indicator in Xcode’s activity area ❓ has been driving me a little nuts. 😃 |
I tried with
And it seems to compile without a warning or error with a recent development compiler and with Xcode 9.3. Could you provide a little more context for the code? |
Comment by Ben Stock (JIRA) Update So, it looks like the parameter names or argument labels are causing some discrepancy. Weird. I’m not the most knowledgable about the inner workings of the compiler (or any compiler, for that matter), so I think I’ll let you professionals take a stab at it. I’m basically creating a lightweight key-value store. The subscript causing the problems is defined in this store. Here is an overview of the types I’m working with:
If none of that makes sense, here’s a slightly condensed version of the code: public class AnyCacheKey : RawRepresentable, Hashable, Comparable {
public let rawValue: String
// ...
// Hashable and Comparable implementations
// ...
}
public struct Cache {
// This is the concrete key class with the additional static type information.
final public class Key<T> : AnyCacheKey {}
// The underlying dictionary containing the cached values.
private var storage: [AnyCacheKey: Any]
// ...
// Some other code
// ...
// Base getter and setter
public subscript <T>(key: Key<T>) -> T? {
get { return storage[key] as? T }
set { storage[key] = newValue }
}
// The problem child
public subscript <K, V>(key: Key<[K: V]>, _ nestedKey: K) -> V? where K : Hashable {
get {
return self[key]?[nestedKey]
}
set {
var values = storage[key] as? [K: V] ?? [:]
values[nestedKey] = newValue
self[key] = values
}
}
} |
Thanks for the additional info! It's looking like this might have been fixed between Xcode 9.2 and 9.3, which is always nice: theoretically this issue should be fixed with just an update. I tried both my example with argument names and your example after adding the required methods for it to compile (for future reference, it's really helpful if any examples can just be copied-pasted into a file to demonstrate your problem, to make sure we're both looking at exactly the same thing 🙂 ) and there wasn't an error, with or without the
and also after updating your example to compile:
|
That might just be Doug scoping down the "redundant" warning. We should add these as test cases in case we ever decide to ramp it back up. |
I believe this was fixed by 98e88bc |
Environment
OS: macOS 10.12.6 (16G1036)
Language: Swift 4.0
Editor: Xcode 9.2 (9C40b)
Base SDK: macOS 10.13
Swift Compiler Optimization Level: None
Additional Detail from JIRA
md5: adce73713697b986a00300014df32a22
Issue Description:
When implementing a
subscript
with generics like so:The following error occurs:
If one then tries to add
Hashable
conformance in the generic parameter list itself:or in a
where
clause:A warning appears:
In other words, if you add conformance, you get a redundancy warning, and if you don't add conformance, you get an error.
The text was updated successfully, but these errors were encountered: