Skip to content

Commit 5ecf6db

Browse files
scheglovCommit Queue
authored and
Commit Queue
committed
Deprecate 'DartType.isVoid', use 'is VoidType' instead.
Maybe? With an explicit type check we don't try to make an impression that anything more happens here, e.g. for type parameters. Similarly, for #36697 we would need to introduce `InvalidType`, which is not `DynamicType`. And so, `DartType.isDynamic` should not return `true` for `InvalidType`, it is not a property that various types may have, and should be replaced with explicit `is DynamicType`. We also have `UnknownInferredType`, but it should never leak to clients. Change-Id: I302b06355143d97bb52922c805dbad585a522e0a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/288340 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
1 parent 9698e72 commit 5ecf6db

38 files changed

+86
-73
lines changed

pkg/analysis_server/lib/src/services/completion/dart/completion_manager.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ class DartCompletionRequest {
365365
);
366366

367367
var opType = OpType.forCompletion(target, offset);
368-
if (contextType != null && contextType.isVoid) {
368+
if (contextType is VoidType) {
369369
opType.includeVoidReturnSuggestions = true;
370370
}
371371

pkg/analysis_server/lib/src/services/completion/dart/local_library_contributor.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class LibraryElementSuggestionBuilder extends GeneralizingElementVisitor<void> {
9494
return;
9595
}
9696
var returnType = element.returnType;
97-
if (returnType.isVoid) {
97+
if (returnType is VoidType) {
9898
if (opType.includeVoidReturnSuggestions) {
9999
builder.suggestTopLevelFunction(element, kind: kind, prefix: prefix);
100100
}

pkg/analysis_server/lib/src/services/completion/dart/local_reference_contributor.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class LocalReferenceContributor extends DartCompletionContributor {
129129
for (var method in type.methods) {
130130
if (!method.isStatic) {
131131
if (_visibilityTracker._isVisible(method.declaration)) {
132-
if (!method.returnType.isVoid) {
132+
if (method.returnType is! VoidType) {
133133
if (opType.includeReturnValueSuggestions) {
134134
memberBuilder.addSuggestionForMethod(
135135
method: method,

pkg/analysis_server/lib/src/services/completion/dart/utilities.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ CompletionDefaultArgumentList computeCompletionDefaultArgumentList(
102102
// todo (pq): consider refactoring to share common logic w/
103103
// ArgListContributor.buildClosureSuggestions
104104
final returnType = parameterType.returnType;
105-
if (returnType.isVoid) {
105+
if (returnType is VoidType) {
106106
blockBuffer.write('{');
107107
rangeStart = sb.length + blockBuffer.length;
108108
blockBuffer.write(' }');

pkg/analysis_server/lib/src/services/correction/dart/add_async.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class AddAsync extends CorrectionProducer {
7777
/// Return `true` if the [type] is `Future<void>`.
7878
bool _isFutureVoid(DartType type) {
7979
if (type is InterfaceType && type.isDartAsyncFuture) {
80-
return type.typeArguments[0].isVoid;
80+
return type.typeArguments[0] is VoidType;
8181
}
8282
return false;
8383
}

pkg/analysis_server/lib/src/services/correction/dart/assign_to_local_variable.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:analysis_server/src/services/correction/dart/abstract_producer.d
88
import 'package:analysis_server/src/services/correction/name_suggestion.dart';
99
import 'package:analysis_server/src/utilities/extensions/ast.dart';
1010
import 'package:analyzer/dart/ast/ast.dart';
11+
import 'package:analyzer/dart/element/type.dart';
1112
import 'package:analyzer/src/dart/ast/extensions.dart';
1213
import 'package:analyzer/src/dart/ast/utilities.dart';
1314
import 'package:analyzer_plugin/utilities/assist/assist.dart';
@@ -50,7 +51,7 @@ class AssignToLocalVariable extends CorrectionProducer {
5051
var offset = expression.offset;
5152
// prepare expression type
5253
var type = expression.typeOrThrow;
53-
if (type.isVoid) {
54+
if (type is VoidType) {
5455
return;
5556
}
5657
// prepare excluded names

pkg/analysis_server/lib/src/services/correction/dart/convert_into_block_body.dart

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:analysis_server/src/services/correction/assist.dart';
66
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
77
import 'package:analyzer/dart/ast/ast.dart';
88
import 'package:analyzer/dart/element/element.dart';
9+
import 'package:analyzer/dart/element/type.dart';
910
import 'package:analyzer/src/dart/ast/extensions.dart';
1011
import 'package:analyzer_plugin/utilities/assist/assist.dart';
1112
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
@@ -64,7 +65,7 @@ class ConvertIntoBlockBody extends CorrectionProducer {
6465
var lines = ['// TODO: implement ${functionElement.displayName}'];
6566

6667
var returnValueType = functionElement.returnType;
67-
if (!returnValueType.isVoid) {
68+
if (returnValueType is! VoidType) {
6869
lines.add('throw UnimplementedError();');
6970
}
7071
return lines;
@@ -85,9 +86,9 @@ class ConvertIntoBlockBody extends CorrectionProducer {
8586
var returnValueType = returnValue.typeOrThrow;
8687
var returnValueCode = utils.getNodeText(returnValue);
8788
var returnCode = '';
88-
if (!returnValueType.isVoid &&
89+
if (returnValueType is! VoidType &&
8990
!returnValueType.isBottom &&
90-
!functionElement.returnType.isVoid) {
91+
functionElement.returnType is! VoidType) {
9192
returnCode = 'return ';
9293
}
9394
returnCode += '$returnValueCode;';

pkg/analysis_server/lib/src/services/refactoring/legacy/convert_method_to_getter.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:analysis_server/src/services/search/search_engine.dart';
1111
import 'package:analyzer/dart/analysis/session.dart';
1212
import 'package:analyzer/dart/ast/ast.dart';
1313
import 'package:analyzer/dart/element/element.dart';
14+
import 'package:analyzer/dart/element/type.dart';
1415
import 'package:analyzer/src/dart/analysis/session_helper.dart';
1516
import 'package:analyzer/src/dart/ast/utilities.dart';
1617
import 'package:analyzer_plugin/utilities/range_factory.dart';
@@ -88,7 +89,7 @@ class ConvertMethodToGetterRefactoringImpl extends RefactoringImpl
8889
'Only class methods or top-level functions can be converted to getters.');
8990
}
9091
// returns a value
91-
if (element.returnType.isVoid) {
92+
if (element.returnType is VoidType) {
9293
return RefactoringStatus.fatal(
9394
'Cannot convert ${element.kind.displayName} returning void.');
9495
}

pkg/analysis_server/lib/src/services/refactoring/legacy/extract_local.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import 'package:analyzer/dart/ast/ast.dart';
2020
import 'package:analyzer/dart/ast/token.dart';
2121
import 'package:analyzer/dart/ast/visitor.dart';
2222
import 'package:analyzer/dart/element/element.dart';
23+
import 'package:analyzer/dart/element/type.dart';
2324
import 'package:analyzer/src/dart/ast/utilities.dart';
2425
import 'package:analyzer/src/generated/java_core.dart';
2526
import 'package:analyzer/src/generated/source.dart';
@@ -314,7 +315,7 @@ class ExtractLocalRefactoringImpl extends RefactoringImpl
314315
if (node is MethodInvocation) {
315316
var invocation = node;
316317
var element = invocation.methodName.staticElement;
317-
if (element is ExecutableElement && element.returnType.isVoid) {
318+
if (element is ExecutableElement && element.returnType is VoidType) {
318319
if (singleExpression == null) {
319320
return RefactoringStatus.fatal(
320321
'Cannot extract the void expression.',

pkg/analysis_server/tool/code_completion/implicit_type_declarations.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class ImpliedTypeCollector extends RecursiveAstVisitor<void> {
104104
void handleVariableDeclaration(VariableDeclaration node, DartType? dartType) {
105105
// If some untyped variable declaration
106106
if (node.equals != null && dartType == null ||
107-
(dartType != null && (dartType.isDynamic || dartType.isVoid))) {
107+
(dartType != null && (dartType.isDynamic || dartType is VoidType))) {
108108
// And if we can determine the type on the RHS of the variable declaration
109109
var rhsType = node.initializer?.staticType;
110110
if (rhsType != null && !rhsType.isDynamic) {

pkg/analyzer/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 5.8.0-dev
2+
* Deprecated `DartType.isVoid`, use `is VoidType` instead.
3+
14
## 5.7.1
25
* Require SDK `>=2.19.0 <3.0.0` to use `PathNotFoundException` from `dart:io`.
36

pkg/analyzer/lib/dart/element/type.dart

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ abstract class DartType {
122122
bool get isDynamic;
123123

124124
/// Return `true` if this type represents the type 'void'.
125+
@Deprecated('Use `is VoidType` instead')
125126
bool get isVoid;
126127

127128
/// Return the name of this type, or `null` if the type does not have a name,

pkg/analyzer/lib/src/dart/element/type.dart

+1
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,7 @@ class VoidTypeImpl extends TypeImpl implements VoidType {
14621462
@override
14631463
int get hashCode => 2;
14641464

1465+
@Deprecated('Use `is VoidType` instead')
14651466
@override
14661467
bool get isVoid => true;
14671468

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ class TypeSystemImpl implements TypeSystem {
11001100

11011101
@override
11021102
bool isNonNullable(DartType type) {
1103-
if (type.isDynamic || type.isVoid || type.isDartCoreNull) {
1103+
if (type.isDynamic || type is VoidType || type.isDartCoreNull) {
11041104
return false;
11051105
} else if (type is TypeParameterTypeImpl && type.promotedBound != null) {
11061106
return isNonNullable(type.promotedBound!);
@@ -1141,7 +1141,7 @@ class TypeSystemImpl implements TypeSystem {
11411141

11421142
@override
11431143
bool isNullable(DartType type) {
1144-
if (type.isDynamic || type.isVoid || type.isDartCoreNull) {
1144+
if (type.isDynamic || type is VoidType || type.isDartCoreNull) {
11451145
return true;
11461146
} else if (type is TypeParameterTypeImpl && type.promotedBound != null) {
11471147
return isNullable(type.promotedBound!);
@@ -1183,7 +1183,7 @@ class TypeSystemImpl implements TypeSystem {
11831183

11841184
@override
11851185
bool isStrictlyNonNullable(DartType type) {
1186-
if (type.isDynamic || type.isVoid || type.isDartCoreNull) {
1186+
if (type.isDynamic || type is VoidType || type.isDartCoreNull) {
11871187
return false;
11881188
} else if (type.nullabilitySuffix != NullabilitySuffix.none) {
11891189
return false;

pkg/analyzer/lib/src/dart/resolver/assignment_expression_resolver.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class AssignmentExpressionResolver {
119119
DartType rightType, {
120120
required Map<DartType, NonPromotionReason> Function()? whyNotPromoted,
121121
}) {
122-
if (!writeType.isVoid && _checkForUseOfVoidResult(right)) {
122+
if (writeType is! VoidType && _checkForUseOfVoidResult(right)) {
123123
return;
124124
}
125125

@@ -207,7 +207,7 @@ class AssignmentExpressionResolver {
207207
// Values of the type void cannot be used.
208208
// Example: `y += 0`, is not allowed.
209209
if (operatorType != TokenType.EQ) {
210-
if (leftType.isVoid) {
210+
if (leftType is VoidType) {
211211
_errorReporter.reportErrorForToken(
212212
CompileTimeErrorCode.USE_OF_VOID_RESULT,
213213
operator,

pkg/analyzer/lib/src/dart/resolver/body_inference_context.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ class BodyInferenceContext {
127127
// If `R` is `void`, or the function literal is marked `async` and `R` is
128128
// `FutureOr<void>`, let `S` be `void`.
129129
if (_typeSystem.isNonNullableByDefault) {
130-
if (R.isVoid ||
130+
if (R is VoidType ||
131131
isAsynchronous &&
132132
R is InterfaceType &&
133133
R.isDartAsyncFutureOr &&
134-
R.typeArguments[0].isVoid) {
134+
R.typeArguments[0] is VoidType) {
135135
return VoidTypeImpl.instance;
136136
}
137137
}

pkg/analyzer/lib/src/dart/resolver/extension_member_resolver.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ class ExtensionMemberResolver {
222222
substitution,
223223
);
224224

225-
if (receiverType.isVoid) {
225+
if (receiverType is VoidType) {
226226
_errorReporter.reportErrorForNode(
227227
CompileTimeErrorCode.USE_OF_VOID_RESULT, receiverExpression);
228228
} else if (!_typeSystem.isAssignableTo(receiverType, extendedType)) {

pkg/analyzer/lib/src/dart/resolver/legacy_type_asserter.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class LegacyTypeAsserter extends GeneralizingAstVisitor<void> {
127127
return;
128128
}
129129

130-
if (type.isDynamic || type.isVoid) {
130+
if (type.isDynamic || type is VoidType) {
131131
return;
132132
}
133133

pkg/analyzer/lib/src/dart/resolver/property_element_resolver.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class PropertyElementResolver with ScopeHelpers {
7777
var targetType = target.typeOrThrow;
7878
targetType = _typeSystem.resolveToBound(targetType);
7979

80-
if (targetType.isVoid) {
80+
if (targetType is VoidType) {
8181
// TODO(scheglov) Report directly in TypePropertyResolver?
8282
_reportUnresolvedIndex(
8383
node,
@@ -431,7 +431,7 @@ class PropertyElementResolver with ScopeHelpers {
431431
);
432432
}
433433

434-
if (targetType.isVoid) {
434+
if (targetType is VoidType) {
435435
errorReporter.reportErrorForNode(
436436
CompileTimeErrorCode.USE_OF_VOID_RESULT,
437437
propertyName,

pkg/analyzer/lib/src/error/best_practices_verifier.dart

+3-2
Original file line numberDiff line numberDiff line change
@@ -1579,12 +1579,13 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
15791579
if (returnType == null) return;
15801580

15811581
bool isReturnVoid;
1582-
if (returnType.isVoid) {
1582+
if (returnType is VoidType) {
15831583
isReturnVoid = true;
15841584
} else if (returnType is ParameterizedType &&
15851585
(returnType.isDartAsyncFuture || returnType.isDartAsyncFutureOr)) {
15861586
var typeArguments = returnType.typeArguments;
1587-
isReturnVoid = typeArguments.length == 1 && typeArguments.first.isVoid;
1587+
isReturnVoid =
1588+
typeArguments.length == 1 && typeArguments.first is VoidType;
15881589
} else {
15891590
isReturnVoid = false;
15901591
}

pkg/analyzer/lib/src/error/literal_element_verifier.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class LiteralElementVerifier {
6868
void _verifyElement(CollectionElement? element) {
6969
if (element is Expression) {
7070
if (forList || forSet) {
71-
if (!elementType!.isVoid &&
71+
if (elementType is! VoidType &&
7272
_errorVerifier.checkForUseOfVoidResult(element)) {
7373
return;
7474
}
@@ -103,14 +103,14 @@ class LiteralElementVerifier {
103103
/// Verify that the [entry]'s key and value are assignable to [mapKeyType]
104104
/// and [mapValueType].
105105
void _verifyMapLiteralEntry(MapLiteralEntry entry) {
106-
var mapKeyType = this.mapKeyType;
107-
if (!mapKeyType!.isVoid &&
106+
var mapKeyType = this.mapKeyType!;
107+
if (mapKeyType is! VoidType &&
108108
_errorVerifier.checkForUseOfVoidResult(entry.key)) {
109109
return;
110110
}
111111

112-
var mapValueType = this.mapValueType;
113-
if (!mapValueType!.isVoid &&
112+
var mapValueType = this.mapValueType!;
113+
if (mapValueType is! VoidType &&
114114
_errorVerifier.checkForUseOfVoidResult(entry.value)) {
115115
return;
116116
}

0 commit comments

Comments
 (0)