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

Offer additional note for why method with Self reference cannot be used on any SomeType #76320

Open
ktoso opened this issue Sep 6, 2024 · 9 comments
Labels
feature A feature request or implementation good first issue Good for newcomers type checker Area → compiler: Semantic analysis

Comments

@ktoso
Copy link
Contributor

ktoso commented Sep 6, 2024

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

@ktoso ktoso added feature A feature request or implementation triage needed This issue needs more specific labels type checker Area → compiler: Semantic analysis and removed triage needed This issue needs more specific labels labels Sep 6, 2024
@amritpan
Copy link
Member

amritpan commented Sep 8, 2024

@GunjanRawat26 This could be a good intro to diagnostics

@ktoso
Copy link
Contributor Author

ktoso commented Sep 10, 2024

Linking with radar rdar://135445764

Contributions welcome here, this could be an simple first issue maybe?

@ktoso ktoso added the good first issue Good for newcomers label Sep 10, 2024
@flash1729
Copy link

hey @ktoso , i would like to work on this as my first issue

from what i have understood is we need to give a more refined compiler message for this error and for it

  1. identify where in the compiler this error message is generated
  2. change the message there to be more specific

it would be great if you could guide me further on anything that i am missing

@flash1729
Copy link

Current progress :
I found the code related to error message in DiagnosticsSema.def

ERROR(could_not_use_member_on_existential,none,
      "member %1 cannot be used on value of type %0; consider using a"
      " generic constraint instead",
      (Type, DeclNameRef))

// error: member 'take' cannot be used on value of type 'any Alpha'; consider using a generic constraint instead

This is where changes will be made to show different error message

Questions

  1. Are there any related places where this code is rewritten or referenced so changes will be made there as well ?
  2. this is my first contribution so i dont know how to set up environment to test our changes so for that i am reading GettingStarted.md. I am a bit unsure about exactly what needs to be done rn.
  3. I am understanding what error is exactly. also are there any specific guidelines for wording error message ?

@Lenhart94
Copy link

protocol Base {
associatedtype SomeID: Hashable
}

protocol Alpha: Base {
func take(id: Self.SomeID)
}

struct Use<A: Alpha> where A.SomeID == Int {
let alpha: A

func test() {
    alpha.take(id: 12)
}

}

@jameesbrown
Copy link

jameesbrown commented Sep 23, 2024

@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:

  1. 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/swift or search manually using CMD+F in Xcode.

  2. For initial debugging, you can pass the -typecheck -verify arguments, and then locate the relevant test cases. A good way to find related tests for this error would be to run grep -rl "cannot be used on value of type" path/to/swift-project/swift/test. Additionally, you may find the documentation here helpful.

  3. 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.

@ktoso
Copy link
Contributor Author

ktoso commented Sep 23, 2024

That's good hints, thanks @jameesbrown

@flash1729
Copy link

flash1729 commented Sep 24, 2024

@jameesbrown Thanks for hints. I will look into them...
For now what i did was i built swift in my device for first time and then after making changes to DiagnosticsSema.def i rebuilt and tried to compile a test file using the modified compiler and as expected i got the Modified Error message which is sort of like this

member %1 cannot be used on value of type %0; the method requires knowledge of the concrete type for 'Self.SomeID'. Consider using a generic constraint instead

I faced issues in determining which schema to use (i used diagnostics_database)
and later couldn't completely get the hang of which builds to do to ensure complete build is good after changes so if you can suggest some additional reading related to this then it would be great...

Also unknowingly i used two build methods (ninja (for complete build) and xcode(after minor changes)) which led to some errors cause xcode build couldn't find some files in expected location of llvm clone.

Now i will go through suggested sources and instead of modifying error message add a related note accordingly...

@jameesbrown
Copy link

@flash1729

I faced issues in determining which schema to use (i used diagnostics_database)
and later couldn't completely get the hang of which builds to do to ensure complete build is good after changes so if you can suggest some additional reading related to this then it would be great...

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 all build/Xcode-*/swift-macosx-*/bin and will not work. If this is the same case for you, then you can select: Other... and choose the executable via finder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A feature request or implementation good first issue Good for newcomers type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

5 participants