Skip to content

Analyzer should provide execution details if evaluation of a constant throws an exception #36526

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

Closed
stereotype441 opened this issue Apr 8, 2019 · 2 comments
Labels
analyzer-constants legacy-area-analyzer Use area-devexp instead. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@stereotype441
Copy link
Member

Consider the following code, adapted from the repro for #36511:

class A {
  const A(int i)
  : assert(i == 1); // (2)
}
class B extends A {
  const B(int i) : super(i);
}
main() {
  print(const B(2)); // (1)
}

Analyzing this code produces the following error message at line (1):

error • Evaluation of this constant expression throws an exception at /usr/local/google/home/paulberry/tmp/test.dart:9:9 • const_eval_throws_exception

Although this is technically correct, it's far too little information to allow the user to figure out where the problem is. In particular, there's absolutely no information to lead the user to the site of the assertion failure at (2), or to aid in understanding how (2) was reached from (1). This would be even more of a problem if the definitions of classes A and B were more distant, or if the constant evaluation were more complex.

By contrast, the VM's error message contains a full stack trace, and it points both to locations (1) and (2):

Unhandled exception:
'file:///.../test.dart': Failed assertion: line 3 pos 12: 'i == 1': is not true.
#0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:40:39)
#1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)
#2      new A (file:///usr/local/google/home/paulberry/tmp/test.dart:3:12)
#3      new B (file:///usr/local/google/home/paulberry/tmp/test.dart:6:20)
#4      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:300:19)
#5      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:171:12)
'file:///usr/local/google/home/paulberry/tmp/test.dart': error: line 9 pos 15: error evaluating constant constructor
  print(const B(2)); // (1)
              ^

We really should improve the analyzer's error reporting here.

@stereotype441 stereotype441 added legacy-area-analyzer Use area-devexp instead. analyzer-constants P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug labels Apr 8, 2019
@lukehutch
Copy link

This is also an issue in VS Code.

Example:

image

The actual exceptions are given in the PROBLEMS pane of VS code, but not in the popup:

image

The Evaluation of this constant expression throws an exception error doesn't give enough info. The actual errors are found by the compiler, but they are apparently suppressed by this error.

@bwilkerson
Copy link
Member

VS Code gets its information from the analysis server (the LSP language server for Dart).

The good news is that work is currently underway to allow the analysis server to produce similar information.

@kallentu

copybara-service bot pushed a commit that referenced this issue Aug 4, 2023
If a condition is valid, but fails in an assert, make sure we report
that and a pseudo trace with context messages for the user to link
back to the problem.

This CL also starts building the stack trace when we call a super constructor for more detailed errors.

Bug: #36526
Change-Id: Ib9bc9841256644d2380d3f806e3bc3c9d84b37cf
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/316625
Reviewed-by: Konstantin Shcheglov <[email protected]>
Commit-Queue: Kallen Tu <[email protected]>
@kallentu kallentu closed this as completed Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-constants legacy-area-analyzer Use area-devexp instead. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants