-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Permit -> _
return types for improved diagnostics
#56132
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
Comments
This change would potentially set precedent for treating specific invalid/incomplete code specially in ways that are not reflected in valid code. In other words:
This was originally proposed by @eddyb on Discord. Relevant conversation:
|
As this potentially sets precedent for diagnostic-motivated behaviour changes, it seems prudent to check that there aren't any concerns with doing this. @rfcbot merge |
Team member @varkor has proposed to merge this. The next step is review by the rest of the tagged team members:
Concerns:
Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
Note that the implementation difficulty of this should be negligible, becuase we alrrady have on-demand queries, and we can switch between "check body against signature" and "get signature from body" trivially. |
I support this precedent, and think it obviates things like rust-lang/rfcs#2479 |
Why is |
The difference is that calls to the function would also know about the return type and not produce additional fallout. Basically return type inference like |
@rfcbot concern unneeded An alternative would be to carry optional inferred type information on
or just completely elide the error in line 6 by checking against both the resolved type ( |
I think in the Haskell world for something like this they invent a pretty name (something like Return Type Holes) and write a paper on it. |
@estebank That's not actionable because it breaks with cycles (think recursive function) - and we don't want to add recovery from dependency cycles. The way it works with |
Couldn't this (changing |
@estebank No, that sort of side-effect is incompatible with the incremental dataflow ("query") model. |
Obviously I disagree that it obviates that change; this will still error, not letting you finish That said I fully support this change. |
Is this issue already taken? Please confirm I would like to take this on |
@FrankSD: it's not, but you'll want to wait until the FCP has (started and) finished before working on this, to avoid wasting time if the FCP does not pass. |
I have philosophical disagreements with this idea, as I'd prefer the compiler to not need this (as I see it) kludge and instead provide better diagnostics with more advanced inference, but I understand the pragmatic reasoning behind it. +1 |
It's hard to produce disagnostics if you can't parse the source. |
Anyone still working on this? |
@estebank: you need to resolve your previous concern, for FCP to begin. |
@rfcbot resolve unneeded |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
I've been thinking about this and in the implementation we probably should support underscore with these suggestions anywhere we accept types, not just return type annotations, like function arguments and let bindings. |
Will it automatically recommend using fn qqq() -> _ {
|x:i32|{x+1}
} Will it also work in generators / |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. The RFC will be merged soon. |
@estebank For function arguments you'd need global inference to actually know the type, other than that, the only thing you could reasonably do today is turn the As for @vi Only if we special-case it for closures. |
I sadly don't have a lot of time to spend on this, but I can give some quick pointers here, and can answer questions on IRC/Discord later, if that helps. The goal is to use something like this (which is the type rust/src/librustc_typeck/collect.rs Lines 1226 to 1235 in c3b8ab5
When computing a function signature: rust/src/librustc_typeck/collect.rs Lines 1657 to 1669 in c3b8ab5
But only if the return type in the signature is To avoid a cyclical dependency, this should also check for rust/src/librustc_typeck/check/mod.rs Lines 835 to 836 in c3b8ab5
As for where to get that inferred return type in |
Oh and you'll have to emit an error at one of those points (I'm thinking |
Maybe it would be nice to have some
|
Downgrading this to a warning would require a RFC, AFAIK. |
rustc_typeck: improve diagnostics for -> _ fn return type This should implement IIUC the mentioned issue. ~~I'm not sure if there is a better way than `get_infer_ret_ty` to get/check the return type without code duplication.~~ ~~Also, is this unwrap be okay `ty::Binder::bind(*tables.liberated_fn_sigs().get(hir_id).unwrap())`?~~ r? @eddyb Closes: #56132
Consider (the invalid Rust code containing) a function that returns a value, but has no return type:
Currently, the compiler produces these errors:
In theory, the only problem is the missing return type, which, if provided, would cause the code to compile successfully.
The compiler knows what the return type should be here, and could potentially interpret the rest of the program as if the correct return type were given.
This issue proposes giving special treatment in the compiler for diagnostics only (note that this is not a language change) to the syntax
-> _
for return types. This would report a normal error for a missing return type (no extra code would compile after this change), but would otherwise treat the function as having the missing return type. A machine-applicable suggestion would be provided to replace_
with the correct return type.For example:
The text was updated successfully, but these errors were encountered: