Skip to content

Commit 3c3dc09

Browse files
Test that ! on void is a compile time error
Change-Id: Ia2a43dca23df6d332bd5baa63af7cb68c94ac612 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/108762 Auto-Submit: Mike Fairhurst <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Mike Fairhurst <[email protected]>
1 parent 7139a2a commit 3c3dc09

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,8 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
11461146
_checkForAssignmentToFinal(node.operand);
11471147
_checkForIntNotAssignable(node.operand);
11481148
_checkForNullableDereference(node.operand);
1149+
} else {
1150+
_checkForUseOfVoidResult(node);
11491151
}
11501152
super.visitPostfixExpression(node);
11511153
}

pkg/analyzer/test/src/diagnostics/use_of_void_result_test.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:analyzer/src/dart/analysis/experiments.dart';
6+
import 'package:analyzer/src/error/codes.dart';
7+
import 'package:analyzer/src/generated/engine.dart';
58
import 'package:test_reflective_loader/test_reflective_loader.dart';
69

10+
import '../../generated/test_support.dart';
711
import '../dart/resolution/driver_resolution.dart';
812

913
main() {
1014
defineReflectiveSuite(() {
1115
defineReflectiveTests(UseOfVoidResultTest);
16+
defineReflectiveTests(UseOfVoidResultTest_NonNullable);
1217
});
1318
}
1419

@@ -34,3 +39,28 @@ g() {
3439
''');
3540
}
3641
}
42+
43+
@reflectiveTest
44+
class UseOfVoidResultTest_NonNullable extends DriverResolutionTest {
45+
@override
46+
AnalysisOptionsImpl get analysisOptions =>
47+
AnalysisOptionsImpl()..enabledExperiments = [EnableString.non_nullable];
48+
49+
test_bang_nonVoid() async {
50+
await assertNoErrorsInCode(r'''
51+
int? f() => 1;
52+
g() {
53+
f()!;
54+
}
55+
''');
56+
}
57+
58+
test_bang_void() async {
59+
await assertErrorsInCode(r'''
60+
void f() => 1;
61+
g() {
62+
f()!;
63+
}
64+
''', [ExpectedError(StaticWarningCode.USE_OF_VOID_RESULT, 23, 4)]);
65+
}
66+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
void main() {
7+
void x;
8+
x!; //# 00: compile-time error
9+
}

0 commit comments

Comments
 (0)