-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Analyzer should detect if improperly typed handler passed to Future.catchError. #35825
Comments
My inclination is that this should be a hint and agree it would be high value. 👍 |
Is there any update on this? Thanks! |
Thanks for the nudge @zanderso. @bwilkerson: any objection to treating this as a hint? |
With null safety, the priority on this is raised. See #44386. I'd like to implement it as a Hint. I think there would be zero false positives.
|
If this can be extended to other Most of the remaining error handlers are |
Sorry, I missed seeing this earlier. I agree that this should be a hint. |
+1 |
Reporting on invalid returns is not as cut and dry as I had thought 😛 . At runtime, the VM and dart2js do not use the static type of any return values; only runtime type. For example: void main() async {
var x = await Future<int>.error(7).catchError((_) {
num y = 1;
return y;
});
print(x);
} In a null-safe program, with Reporting I'm not sure how I would allow |
Correct. We don't check that the function argument is a If you check the actual return statement types, I think it would be fine to warn in the example above. |
WDYT, @bwilkerson , should this still be a Hint, or a Lint? I think we can perfectly detect when the type of the expression in a return statement is not kosher, but we cannot detect perfectly what will be a runtime error. But this is not unlike some other static analyses. For example |
My understanding is that false positives will occur only in cases where the future on which My intuition is that such cases, and hence false positives, will be rare, but we might want to gather some data to verify that. |
#44480 is a semi-blocking issue. I see code in Flutter engine where a I could perhaps make an initial landing of some checks which do not check return statements (though I think that is by far more useful than checking arrow function literals or function-typed expressions), or some checks which do not fire for |
An upcoming feature to the Dart analyzer [0] will report Future.catchError [1] `onError` handlers which return values of invalid type. Currently this is not reported because the `onError` handler function type cannot be accurately expressed. An `onError` handler for `Future<T>.catchError` must be either `FutureOr<T> Function(dynamic)` or `FutureOr<T> Function(dynamic, StackTrace)`. In either case, the return type of the function is `FutureOr<T>`. This CL corrects `onError` handler(s) to return a value of a valid type (typically `null`). [0]: dart-lang/sdk#35825 [1]: https://api.dart.dev/dev/2.12.0-149.0.dev/dart-async/Future/catchError.html
An upcoming feature to the Dart analyzer [0] will report Future.catchError [1] `onError` handlers which return values of invalid type. Currently this is not reported because the `onError` handler function type cannot be accurately expressed. An `onError` handler for `Future<T>.catchError` must be either `FutureOr<T> Function(dynamic)` or `FutureOr<T> Function(dynamic, StackTrace)`. In either case, the return type of the function is `FutureOr<T>`. This CL corrects `onError` handler(s) to return a value of a valid type. [0]: dart-lang/sdk#35825 [1]: https://api.dart.dev/dev/2.12.0-149.0.dev/dart-async/Future/catchError.html
An upcoming feature to the Dart analyzer [0] will report Future.catchError [1] `onError` handlers which return values of invalid type. Currently this is not reported because the `onError` handler function type cannot be accurately expressed. An `onError` handler for `Future<T>.catchError` must be either `FutureOr<T> Function(dynamic)` or `FutureOr<T> Function(dynamic, StackTrace)`. In either case, the return type of the function is `FutureOr<T>`. This CL corrects `onError` handler(s) to return a value of a valid type. [0]: dart-lang/sdk#35825 [1]: https://api.dart.dev/dev/2.12.0-149.0.dev/dart-async/Future/catchError.html
Bug: #35825 Change-Id: I92dda82b0745e47212ffe83b2b82e1d92809e7c6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/177060 Reviewed-by: Devon Carew <devoncarew@google.com> Commit-Queue: Samuel Rawlins <srawlins@google.com>
The type of the `onError` parameter of Future<T>.catchError is just Function, but the function can either have signature `FutureOr<T> Function(dynamic)` or `FutureOr<T> Function(dynamic, StackTrace)`. This change adds checks for return statements in a function literal passed to `onError`, and the return type of a function-typed expression passed to `onError`. We still need to check parameter types. #35825 Change-Id: I3a1d1c444e298c5816fcd1d4bc537f7b87fa3da1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/176221 Commit-Queue: Samuel Rawlins <srawlins@google.com> Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This reverts commit d5ee021. Reason for revert: Breaks google3: b/178222419 Original change's description: > Analyzer: Report on return types of Future.catchError function > > The type of the `onError` parameter of Future<T>.catchError is just Function, > but the function can either have signature `FutureOr<T> Function(dynamic)` or > `FutureOr<T> Function(dynamic, StackTrace)`. This change adds checks for return > statements in a function literal passed to `onError`, and the return type of a > function-typed expression passed to `onError`. > > We still need to check parameter types. > > #35825 > > Change-Id: I3a1d1c444e298c5816fcd1d4bc537f7b87fa3da1 > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/176221 > Commit-Queue: Samuel Rawlins <srawlins@google.com> > Reviewed-by: Konstantin Shcheglov <scheglov@google.com> > Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> TBR=scheglov@google.com,brianwilkerson@google.com,srawlins@google.com # Not skipping CQ checks because original CL landed > 1 day ago. Change-Id: I8ebbaac0a4b44293576809baa2dc2c3fdcf35379 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/180822 Commit-Queue: David Morgan <davidmorgan@google.com> Reviewed-by: David Morgan <davidmorgan@google.com>
The type of the `onError` parameter of Future<T>.catchError is just Function, but the function can either have signature `FutureOr<T> Function(dynamic)` or `FutureOr<T> Function(dynamic, StackTrace)`. This change adds checks for return statements in a function literal passed to `onError`, and the return type of a function-typed expression passed to `onError`. We still need to check parameter types. #35825 Change-Id: I806d9d30e595fb9d63ae669f3c30c428b60fdf4a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/180880 Commit-Queue: Samuel Rawlins <srawlins@google.com> Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
I think the primary function-typed APIs from dart:async on which we could also report diagnostics include:
|
Bug: #35825 Change-Id: I43060e9b0b0a764f14be041c5cbdf1d884d92326 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/181740 Commit-Queue: Samuel Rawlins <srawlins@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
An upcoming feature to the Dart analyzer [0] will report Future.catchError [1] `onError` handlers which return values of invalid type. Currently this is not reported because the `onError` handler function type cannot be accurately expressed. An `onError` handler for `Future<T>.catchError` must be either `FutureOr<T> Function(dynamic)` or `FutureOr<T> Function(dynamic, StackTrace)`. In either case, the return type of the function is `FutureOr<T>`. This CL corrects `onError` handler(s) to return a value of a valid type. [0]: dart-lang/sdk#35825 [1]: https://api.dart.dev/dev/2.12.0-149.0.dev/dart-async/Future/catchError.html
Due to the fact that
Future.catchError
supports two kinds of callbacks, its callback parameter is typedFunction
, which means it gets very limited type checking from the analyzer.Since this is a commonly used core method, it would be nice if the analyzer had some special case logic to type check it. I'm not sure whether it should be a hint or a lint (@bwilkerson, @pq, WDYT?)
Related issues: #35812, #34144, #31939, #35545.
The text was updated successfully, but these errors were encountered: