-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Show the raw argument type in reveal_type
#19400
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
Conversation
| let revealed_type = overload | ||
| .arguments_for_parameter(call_arguments, 0) | ||
| .fold(UnionBuilder::new(db), |builder, (_, ty)| builder.add(ty)) | ||
| .build(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the meat of the change; the rest is just refactoring to enable this + cleanup.
We need the union builder because technically multiple arguments could be matched to the first parameter, though that would require a custom typeshed definition of reveal_type that uses a variadic parameter:
def reveal_type(*args) -> Any: ...
|
sharkdp
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much!
I was thinking about how to write a test for this. One could probably create an unsimplified int | Literal[1] union type via UnionType::new(db, […]) (which we would ideally make private if we could, but for this test, it would be helpfully available).
But then how do we feed that type into reveal_type? We could expose some unsafe functionality via ty_extensions maybe, but that seems like a very bad idea.
All that is to say: I'm fine with there not being a test. It will help everyone who's working on the {Union/Intersection}Builder in the future.
|
I can try writing a Rust test instead of an mdtest |
I was thinking about Rust tests as well. But I assume it's not trivial to invoke the whole call inference machinery without having an AST. And if you construct an AST by parsing some example, then it's unclear how to inject that "wrong" type? Maybe you figure something out, it's certainly not impossible. But I'm not sure if it's worth spending a lot of time on. That's what I wanted to say originally. |
This PR is changes how
reveal_typedetermines what type to reveal, in a way that should be a no-op to most callers.Previously, we would reveal the type of the first parameter, after all of the call binding machinery had done its work. This includes inferring the specialization of a generic function, and then applying that specialization to all parameter and argument types, which is relevant since the typeshed definition of
reveal_typeis generic:Normally this does not matter, since we infer
_T = [arg type]and apply that to the parameter type, yielding[arg type]. But applying that specialization also simplifies the argument type, which makesreveal_typeless useful as a debugging aid when we want to see the actual, raw, unsimplified argument type.With this patch, we now grab the original unmodified argument type and reveal that instead.
In addition to making the debugging aid example work, this also makes our
reveal_typeimplementation more robust to custom typeshed definitions, such as(That custom definition is probably not what anyone would want, since you wouldn't be able to depend on the return type being equivalent to the argument type, but still)