swift: Offer additional note for why method with Self reference cannot be used on `any SomeType`
Motivation
The following snippet;
protocol Base {
associatedtype SomeID: Hashable
}
protocol Alpha: Base {
func take(id: Self.SomeID)
}
struct Use {
let alpha: any Alpha
func test() {
alpha.take(id: 12) // error: Member 'take' cannot be used on value of type 'any Alpha'; consider using a generic constraint instead
}
}
is correct in rejecting the line, since we need Self.SomeID to be well typed, but we dont know what its type is via the any.
It’d be helpful if we can point out that “why” the method can’t be used via a note, in a method with many parameters one can sometimes miss the Self reference.
Proposed solution
Adding a note on the offending parameter would be good
Alternatives considered
No response
Additional information
No response
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 18 (6 by maintainers)
@flash1729 I know you asked the original developer for help, and I’m sure they’ll provide guidance when they’re available. In the meantime, I’m very new myself, but I’ll try my best to give some direction.
Based on the Proposed Solution in the issue submission, it seems like adding a note instead of modifying the existing error would be a good approach. Here’s what I would try:
To locate all references to could_not_use_member_on_existential, you can use
grep -r "could_not_use_member_on_existential" path/to/swift-project/swiftor search manually using CMD+F in Xcode.For initial debugging, you can pass the
-typecheck -verifyarguments, and then locate the relevant test cases. A good way to find related tests for this error would be to rungrep -rl "cannot be used on value of type" path/to/swift-project/swift/test. Additionally, you may find the documentation here helpful.You might also want to review this document: Existential Member Access Limitations. Looking at how other notes are worded across the codebase could give you insights into how to phrase yours.
I hope this helps! Let me know if I’m overstepping or if anything needs more explanation.
Hey @slavapestov could you please help us with this? Thank you 🙏
Thanks for picking this up! Maybe @slavapestov would be better suited to answer these questions though?
@SwiftyJoeyy please do look into this issue. I wont be able to open a pr anytime very soon so its best if you can do so 👍
Yes. Due to some other commitments I was unable to submit a final pr. I’ll look into it and submit it 👍🏻
@flash1729
You may have already looked at it, but https://github.com/swiftlang/swift/blob/main/docs/HowToGuides/GettingStarted.md#using-ninja-with-xcode is the best.
Also one thing to be careful about is when selecting the executable for your scheme (Edit Scheme…>Run), make sure you are selecting one in
build/Ninja-*/swift-macosx-*/bin, the ones that appear in the dropdown (for me at least) are allbuild/Xcode-*/swift-macosx-*/binand will not work. If this is the same case for you, then you can select: Other… and choose the executable via finder.