-
Notifications
You must be signed in to change notification settings - Fork 12
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
Add an API to resolve the name of a function #68
Comments
@oli-obk would that make sense? |
I dislike it, because it has brought us a lot of trouble, but I also see the need for it. So yea, let's add that. We should document though that it should not (cannot?) be used for walking into anonymous bodies (closures, const blocks, async blocks) and we'll need to figure out how to make |
As a side note, I'm still not okay with limiting the query to functions. Especially, name resolution is quite a general functionality that should be usable for anything. |
We cannot hook into name resolution. Name resolution happens too early for us to do lazily. We can walk definitions where possible, but it won't be surface syntax rules in either case. |
I totally get it. We have similar issues with Kani stubs, where we try to mimic name resolution. @momvart not every definition is supported in StableMIR or MIR, e.g.: modules. That said, we don't need to limit ourselves to functions. We can support other definitions too as long as there's demand for them. |
It makes sense to me that StableMIR tries to keep definitions as strongly-typed and manageable as possible. However, I feel soon or late there will be some use cases for other definitions as well, and introducing a new function for each is not sustainable. Maybe a structure like the following is better, where the definition types are kept as limited as supported and the queries can be written once but in a more general way. enum ItemDef {
Fn(FnDef)
Static(StaticDef)
Const(ConstDef)
// ...
Other, // For unsupported ones.
} |
Well, we do have https://doc.rust-lang.org/nightly/nightly-rustc/stable_mir/crate_def/struct.DefId.html, so we can return that, and then add support for converting it into more concrete |
That makes sense, but apparently returning |
We could potentially change that as long as there's a good reason for that. Question, do you anticipate querying stable mir for something that you don't know the kind and you want to branch accordly? Can you describe your use case a bit more? |
Not really at the moment and I understand your point that we probably always want to find a particular type of item rather than traversing everything. But let's say there are tools that are interested in finding statics and constants. With the current design:
In short, as I see |
One doesn't exclude the other. I propose we create a method that is specific for the type, which is more ergonomic and efficient for the use cases we know exist today. We can add a more generic way later if there's need. We can always refactor the type specific ones later to invoke the generic code. |
I would like to avoid only adding a generic method that forces every known user today to match the result for the type they expect. |
It makes sense. I'm just still not sure if we should add a more generic way backing the type-specific ones now or later when needed. |
We would need to implement a safe translation from |
It would be really nice to be able to get the definition of a function by its name. In some cases, users may query a tool to perform analysis starting from a specific function. In other cases, tools may want to provide further instrumentation or manipulation such as adding extra assertions, or stubbing a method.
Ideally, the API should be something like:
See this discussion here where multiple tool developers describe their current solution:
https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/Resolve.20string.20to.20DefId
The text was updated successfully, but these errors were encountered: