Skip to content

Commit

Permalink
Support type parameters in ReplaceTopBottomVisitor.
Browse files Browse the repository at this point in the history
See dart-lang/language#1133

Change-Id: I3800b88496a8bf214b4bc366f4cb7ef3e9968f17
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/162784
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
  • Loading branch information
scheglov authored and [email protected] committed Sep 15, 2020
1 parent c20d17b commit 744e34a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
11 changes: 11 additions & 0 deletions pkg/analyzer/lib/src/dart/element/replace_top_bottom_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/element/replacement_visitor.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer/src/dart/element/type_system.dart';
import 'package:meta/meta.dart';

Expand Down Expand Up @@ -53,6 +54,16 @@ class ReplaceTopBottomVisitor extends ReplacementVisitor {
return _isCovariant ? null : _topType;
}

@override
DartType visitTypeParameterType(TypeParameterType type) {
if (!_isCovariant && _typeSystem.isNonNullableByDefault) {
if (_typeSystem.isSubtypeOf2(type, NeverTypeImpl.instance)) {
return _typeSystem.objectQuestion;
}
}
return null;
}

@override
DartType visitVoidType(VoidType type) {
return _isCovariant ? _bottomType : null;
Expand Down
21 changes: 16 additions & 5 deletions pkg/analyzer/test/src/dart/element/replace_top_bottom_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,22 @@ class ReplaceTopBottomNullSafetyTest extends AbstractTypeSystemNullSafetyTest {
// Not contravariant.
_check(neverNone, 'Never');

_check(
functionTypeNone(returnType: intNone, parameters: [
requiredParameter(type: neverNone),
]),
'int Function(Object?)',
void checkContravariant(DartType type, String expectedStr) {
_check(
functionTypeNone(returnType: intNone, parameters: [
requiredParameter(type: type),
]),
'int Function($expectedStr)',
);
}

checkContravariant(neverNone, 'Object?');

checkContravariant(
typeParameterTypeNone(
typeParameter('T', bound: neverNone),
),
'Object?',
);
}

Expand Down

0 comments on commit 744e34a

Please sign in to comment.