-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Rust: Improve type inference for closures and function traits #21170
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
base: main
Are you sure you want to change the base?
Changes from all commits
196f6e1
dabc5d5
9ab29f9
78b88d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * Added type inference support for the `FnMut(..) -> ..` and `Fn(..) -> ..` traits. They now work in type parameter bounds and are implemented by closures. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3825,16 +3825,29 @@ private Type invokedClosureFnTypeAt(InvokedClosureExpr ce, TypePath path) { | |||||||||||||||||||||||
| _, path, result) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * Gets the root type of a closure. | ||||||||||||||||||||||||
| * | ||||||||||||||||||||||||
| * We model closures as `dyn Fn` trait object types. A closure might implement | ||||||||||||||||||||||||
| * only `Fn`, `FnMut`, or `FnOnce`. But since `Fn` is a subtrait of the others, | ||||||||||||||||||||||||
| * giving closures the type `dyn Fn` works well in practice—even if not entirely | ||||||||||||||||||||||||
| * accurate. | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| private DynTraitType closureRootType() { | ||||||||||||||||||||||||
| result = TDynTraitType(any(FnTrait t)) // always exists because of the mention in `builtins/mentions.rs` | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** Gets the path to a closure's return type. */ | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
| /** Gets the path to a closure's return type. */ | |
| /** | |
| * Gets the path to a closure's return type. | |
| * | |
| * Closures are modeled as `dyn Fn` trait object types (see `closureRootType`), | |
| * so the trait parameter here is an `FnTrait`. However, we obtain the return | |
| * type via the `Output` associated type of `FnOnceTrait`. This works because | |
| * `Fn` is a subtrait of `FnOnce` in Rust and inherits its `Output` associated | |
| * type, so using `FnOnceTrait` to access `Output` is valid even when the | |
| * underlying trait object is `FnTrait`. | |
| */ |
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.
Hm, I think this is pretty clear if one understands how TDynTraitTypeParameter works.
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.
The
FnOnceTraitclass still has its owngetOutputType()method whileAnyFnTraitdoesn't. Consider documenting whyAnyFnTraitdoesn't includegetOutputType()when all three function traits have anOutputassociated type. This would clarify that onlyFnOncedirectly definesOutput, whileFnandFnMutinherit it.