-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
[Breaking change request] Make stack traces not be null
.
#40130
Comments
why StackTrace.empty, instead of StackTrace. current? |
Because If someone throws an exception (not If it is an |
cc @Hixie @matanlurey @dgrove for review and approval. |
Seems fine |
lgtm for dart |
I would prefer if we just make StackTrace.current cheap and used that, but I don't feel strongly about it. |
Approved |
Bug: #40130 Change-Id: I13aba0c2a3fa5b1c3d3995f075ffd38f03aca897 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/139880 Reviewed-by: Bob Nystrom <rnystrom@google.com>
This has landed here. |
Summary
We will replace a
null
stack trace supplied by users with a default stack trace.History: dart-lang/language#747
CL: https://dart-review.googlesource.com/c/sdk/+/128660
What is changing:
A lot of async methods currently allow the user to optionally supply a stack trace. Examples include:
Completer.completeError
StreamController.addError
AsyncError
constructorIf the user actually calls these methods with no stack trace, or with an explicit
null
stack trace, then the system will dutifully propagatenull
all the way to the user error handler (likeFuture.catchError
or an async methodtry
/catch
).We change this so that if no stack trace is supplied, the system will instead insert a default stack trace.
That default stack trace is computed as either:
Error
then read its.stackTrace
.null
, then use a default stack trace,StackTrace.empty
.The default stack trace is similar to
StackTrace.fromString("")
. It is a unique object not equal to any other stack trace.Why is this changing?
Because the current behavior means that in null-sound mode all error handling functions need to take
StackTrace?
(a nullable type) as argument, even though almost all stack traces are non-null in practice.That's an unnecessary annoyance when writing error handlers (the experience of migrating the platform libraries had many situations of forgetting the
?
).Providing a nullable value is a tax on users of that value, and in this case there is no benefit from it.
So, we change the behavior in order to be able to have a better and more usable null-sound API.
Example
This code would not be valid NNBD code because it lacks the
?
afterStackTrace
. That's a small annoyance, but an unnecessary one.With this change, the code becomes valid, and the
$stack
won't printnull
.Expected impact
Most likely nothing will break.
If you have code which depends on a stack trace being
null
, then the code will stop working. That is not expected to happen outside of tests.If you have code which assumes that a stack trace is not
null
, which is much more common, your code will now be sound.There will be cases where the default stack trace will be different from the stack trace currently produced. For example, an
await Future.error("no stack trace")
will currently synthesize a stack trace at theawait
expression (which is why we haven't seentry ... catch (error, stack)
have a nullstack
value). It will now get the empty stack trace instead.Again this will likely not matter outside of tests.
The stack trace package may want to adapt to recognize the default empty stack trace.
It is not necessary, the package already works with a trace that has an empty
toString
, and it creates a trace with no frames.The text was updated successfully, but these errors were encountered: