Skip to content

Commit d9fa37e

Browse files
johnniwinthercommit-bot@chromium.org
authored andcommitted
[kernel] Use Never as static type for throw/rethrow
Change-Id: Iba61a834827747e656a6dc14d73ee152f66864cd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/127882 Reviewed-by: Dmitry Stefantsov <dmitryas@google.com> Commit-Queue: Johnni Winther <johnniwinther@google.com>
1 parent bd008dd commit d9fa37e

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

pkg/front_end/lib/src/testing/id_extractor.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,18 @@ abstract class DataExtractor<T> extends Visitor with DataRegistry<T> {
484484
super.visitStaticInvocation(node);
485485
}
486486

487+
@override
488+
visitThrow(Throw node) {
489+
computeForNode(node, computeDefaultNodeId(node));
490+
super.visitThrow(node);
491+
}
492+
493+
@override
494+
visitRethrow(Rethrow node) {
495+
computeForNode(node, computeDefaultNodeId(node));
496+
super.visitRethrow(node);
497+
}
498+
487499
@override
488500
visitAsExpression(AsExpression node) {
489501
if (node.isTypeError) {

pkg/front_end/test/spell_checking_list_common.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,6 +2412,7 @@ resynthesize
24122412
retain
24132413
retained
24142414
rethrow
2415+
rethrowing
24152416
retired
24162417
retrieve
24172418
retrieved
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/*cfe|dart2js.library: nnbd=false*/
6+
/*cfe:nnbd.library: nnbd=true*/
7+
8+
throwing() {
9+
/*cfe|dart2js.<bottom>*/
10+
/*cfe:nnbd.Never*/
11+
throw
12+
/*cfe|dart2js.String*/
13+
/*cfe:nnbd.String!*/
14+
'foo';
15+
}
16+
17+
rethrowing() {
18+
try {} catch (_) {
19+
/*cfe|dart2js.<bottom>*/
20+
/*cfe:nnbd.Never*/
21+
rethrow;
22+
}
23+
}

pkg/kernel/lib/ast.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4156,7 +4156,10 @@ class ThisExpression extends Expression {
41564156
}
41574157

41584158
class Rethrow extends Expression {
4159-
DartType getStaticType(StaticTypeContext context) => const BottomType();
4159+
DartType getStaticType(StaticTypeContext context) =>
4160+
context.isNonNullableByDefault
4161+
? const NeverType(Nullability.nonNullable)
4162+
: const BottomType();
41604163

41614164
R accept<R>(ExpressionVisitor<R> v) => v.visitRethrow(this);
41624165
R accept1<R, A>(ExpressionVisitor1<R, A> v, A arg) =>
@@ -4173,7 +4176,10 @@ class Throw extends Expression {
41734176
expression?.parent = this;
41744177
}
41754178

4176-
DartType getStaticType(StaticTypeContext context) => const BottomType();
4179+
DartType getStaticType(StaticTypeContext context) =>
4180+
context.isNonNullableByDefault
4181+
? const NeverType(Nullability.nonNullable)
4182+
: const BottomType();
41774183

41784184
R accept<R>(ExpressionVisitor<R> v) => v.visitThrow(this);
41794185
R accept1<R, A>(ExpressionVisitor1<R, A> v, A arg) => v.visitThrow(this, arg);

pkg/kernel/lib/type_environment.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,10 @@ class StaticTypeContext {
714714
///
715715
/// For opt out libraries this is [Nullability.legacy].
716716
Nullability get nullable => _library.nullable;
717+
718+
/// Return `true` if the current library is opted in to non-nullable by
719+
/// default.
720+
bool get isNonNullableByDefault => _library.isNonNullableByDefault;
717721
}
718722

719723
/// Implementation of [StaticTypeContext] that update its state when entering
@@ -792,6 +796,9 @@ class _FlatStatefulStaticTypeContext extends StatefulStaticTypeContext {
792796
@override
793797
Nullability get nullable => _library?.nullable;
794798

799+
@override
800+
bool get isNonNullableByDefault => _library.isNonNullableByDefault;
801+
795802
/// Updates the [nonNullable] and [thisType] to match static type context for
796803
/// the member [node].
797804
///
@@ -876,6 +883,9 @@ class _StackedStatefulStaticTypeContext extends StatefulStaticTypeContext {
876883
@override
877884
Nullability get nullable => _library?.nullable;
878885

886+
@override
887+
bool get isNonNullableByDefault => _library?.isNonNullableByDefault;
888+
879889
/// Updates the [library] and [thisType] to match static type context for
880890
/// the member [node].
881891
///

0 commit comments

Comments
 (0)