Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 0e5d18f

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Ensure that nullability promotion of the Null type yields Never.
Change-Id: I2232f80bbf5c8a9e51f305a603c06ca2b3ad8aea Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/103573 Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 5278d40 commit 0e5d18f

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

pkg/analyzer/lib/src/generated/type_system.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,7 +2107,10 @@ abstract class TypeSystem implements public.TypeSystem {
21072107
return null;
21082108
}
21092109

2110+
/// Returns a non-nullable version of [type]. This is equivalent to the
2111+
/// operation `NonNull` defined in the spec.
21102112
DartType promoteToNonNull(TypeImpl type) {
2113+
if (type.isDartCoreNull) return typeProvider.bottomType;
21112114
// TODO(mfairhurst): handle type parameter types
21122115
return type.withNullability(NullabilitySuffix.none);
21132116
}

pkg/analyzer/test/src/dart/resolution/non_nullable_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,18 @@ m<T extends Function>() {
207207
assertType(findNode.methodInvocation('first').methodName, 'Function?');
208208
}
209209

210+
test_null_assertion_operator_changes_null_to_never() async {
211+
addTestFile('''
212+
main() {
213+
Null x = null;
214+
x!;
215+
}
216+
''');
217+
await resolveTestFile();
218+
assertNoTestErrors();
219+
assertType(findNode.postfix('x!'), 'Never');
220+
}
221+
210222
test_null_assertion_operator_removes_nullability() async {
211223
addTestFile('''
212224
main() {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
// SharedOptions=--enable-experiment=non-nullable
6+
7+
// Test that the trailing "!" properly promotes the type `Null` to `Never`, by
8+
// verifying that it's statically ok to pass it to a function expecting a
9+
// non-null parameter.
10+
import 'package:expect/expect.dart';
11+
12+
void f(int i) {}
13+
14+
void g(Null n) {
15+
// Statically ok because `Never <: int`. Throws at runtime.
16+
f(n!);
17+
}
18+
19+
main() {
20+
Expect.throws(() => g(null));
21+
}

0 commit comments

Comments
 (0)