@@ -3825,16 +3825,29 @@ private Type invokedClosureFnTypeAt(InvokedClosureExpr ce, TypePath path) {
38253825 _, path , result )
38263826}
38273827
3828+ /**
3829+ * Gets the root type of a closure.
3830+ *
3831+ * We model closures as `dyn Fn` trait object types. A closure might implement
3832+ * only `Fn`, `FnMut`, or `FnOnce`. But since `Fn` is a subtrait of the others,
3833+ * giving closures the type `dyn Fn` works well in practice—even if not entirely
3834+ * accurate.
3835+ */
3836+ private DynTraitType closureRootType ( ) {
3837+ result = TDynTraitType ( any ( FnTrait t ) ) // always exists because of the mention in `builtins/mentions.rs`
3838+ }
3839+
38283840/** Gets the path to a closure's return type. */
38293841private TypePath closureReturnPath ( ) {
3830- result = TypePath:: singleton ( getDynTraitTypeParameter ( any ( FnOnceTrait t ) .getOutputType ( ) ) )
3842+ result =
3843+ TypePath:: singleton ( TDynTraitTypeParameter ( any ( FnTrait t ) , any ( FnOnceTrait t ) .getOutputType ( ) ) )
38313844}
38323845
38333846/** Gets the path to a closure with arity `arity`s `index`th parameter type. */
38343847pragma [ nomagic]
38353848private TypePath closureParameterPath ( int arity , int index ) {
38363849 result =
3837- TypePath:: cons ( TDynTraitTypeParameter ( _, any ( FnOnceTrait t ) .getTypeParam ( ) ) ,
3850+ TypePath:: cons ( TDynTraitTypeParameter ( _, any ( FnTrait t ) .getTypeParam ( ) ) ,
38383851 TypePath:: singleton ( getTupleTypeParameter ( arity , index ) ) )
38393852}
38403853
@@ -3872,9 +3885,7 @@ private Type inferDynamicCallExprType(Expr n, TypePath path) {
38723885 or
38733886 // _If_ the invoked expression has the type of a closure, then we propagate
38743887 // the surrounding types into the closure.
3875- exists ( int arity , TypePath path0 |
3876- ce .getTypeAt ( TypePath:: nil ( ) ) .( DynTraitType ) .getTrait ( ) instanceof FnOnceTrait
3877- |
3888+ exists ( int arity , TypePath path0 | ce .getTypeAt ( TypePath:: nil ( ) ) = closureRootType ( ) |
38783889 // Propagate the type of arguments to the parameter types of closure
38793890 exists ( int index , ArgList args |
38803891 n = ce and
@@ -3898,10 +3909,10 @@ private Type inferClosureExprType(AstNode n, TypePath path) {
38983909 exists ( ClosureExpr ce |
38993910 n = ce and
39003911 path .isEmpty ( ) and
3901- result = TDynTraitType ( any ( FnOnceTrait t ) ) // always exists because of the mention in `builtins/mentions.rs`
3912+ result = closureRootType ( )
39023913 or
39033914 n = ce and
3904- path = TypePath:: singleton ( TDynTraitTypeParameter ( _, any ( FnOnceTrait t ) .getTypeParam ( ) ) ) and
3915+ path = TypePath:: singleton ( TDynTraitTypeParameter ( _, any ( FnTrait t ) .getTypeParam ( ) ) ) and
39053916 result .( TupleType ) .getArity ( ) = ce .getNumberOfParams ( )
39063917 or
39073918 // Propagate return type annotation to body
0 commit comments