Skip to content

Commit 2af5aec

Browse files
Dmitry Stefantsovcommit-bot@chromium.org
authored andcommitted
[cfe] Infer type of x! as Never if x's type is Null
Closes flutter#39822. Bug: http://dartbug.com/39822 Change-Id: I9600efcd4e69e331de5d9fa8f2b676c402df6502 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/128661 Reviewed-by: Johnni Winther <johnniwinther@google.com> Commit-Queue: Dmitry Stefantsov <dmitryas@google.com>
1 parent 3764928 commit 2af5aec

File tree

9 files changed

+67
-7
lines changed

9 files changed

+67
-7
lines changed

pkg/front_end/lib/src/fasta/kernel/inference_visitor.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2370,10 +2370,11 @@ class InferenceVisitor
23702370
// TODO(johnniwinther): Check that the inferred type is potentially
23712371
// nullable.
23722372
inferrer.flowAnalysis.nonNullAssert_end(node.operand);
2373-
// TODO(johnniwinther): Return `NonNull(inferredType)`.
2374-
return new ExpressionInferenceResult(
2375-
operandResult.inferredType.withNullability(Nullability.nonNullable),
2376-
node);
2373+
DartType nonNullableResultType = operandResult.inferredType ==
2374+
inferrer.coreTypes.nullType
2375+
? const NeverType(Nullability.nonNullable)
2376+
: operandResult.inferredType.withNullability(Nullability.nonNullable);
2377+
return new ExpressionInferenceResult(nonNullableResultType, node);
23772378
}
23782379

23792380
ExpressionInferenceResult visitNullAwareMethodInvocation(
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
foo(Null x) {
6+
bar(x!);
7+
}
8+
9+
bar(int y) {}
10+
11+
main() {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
library;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
static method foo(core::Null? x) → dynamic
6+
;
7+
static method bar(core::int y) → dynamic
8+
;
9+
static method main() → dynamic
10+
;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
library;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
static method foo(core::Null? x) → dynamic {
6+
self::bar(x!);
7+
}
8+
static method bar(core::int y) → dynamic {}
9+
static method main() → dynamic {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
library;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
static method foo(core::Null? x) → dynamic {
6+
self::bar(x!);
7+
}
8+
static method bar(core::int y) → dynamic {}
9+
static method main() → dynamic {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
library;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
static method foo(core::Null? x) → dynamic {
6+
self::bar(x!);
7+
}
8+
static method bar(core::int y) → dynamic {}
9+
static method main() → dynamic {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
library;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
static method foo(core::Null? x) → dynamic {
6+
self::bar(x!);
7+
}
8+
static method bar(core::int y) → dynamic {}
9+
static method main() → dynamic {}

pkg/front_end/testcases/text_serialization.status

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,7 @@ nnbd/function_types: TextSerializationFailure
930930
nnbd/inheritance_from_opt_in: TypeCheckError
931931
nnbd/inheritance_from_opt_out: TextSerializationFailure
932932
nnbd/intersection_types: TextSerializationFailure
933+
nnbd/issue39822: TextSerializationFailure
933934
nnbd/issue_39286: TextSerializationFailure
934935
nnbd/issue_39286_2: TextSerializationFailure
935936
nnbd/late: TextSerializationFailure

pkg/kernel/lib/ast.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4013,9 +4013,10 @@ class NullCheck extends Expression {
40134013
}
40144014

40154015
DartType getStaticType(StaticTypeContext context) {
4016-
return operand
4017-
.getStaticType(context)
4018-
.withNullability(Nullability.nonNullable);
4016+
DartType operandType = operand.getStaticType(context);
4017+
return operandType == context.typeEnvironment.nullType
4018+
? const NeverType(Nullability.nonNullable)
4019+
: operandType.withNullability(Nullability.nonNullable);
40194020
}
40204021

40214022
R accept<R>(ExpressionVisitor<R> v) => v.visitNullCheck(this);

0 commit comments

Comments
 (0)