From 744e34aec07a2bbe982deb32ca6307b54003c1db Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Tue, 15 Sep 2020 04:39:31 +0000 Subject: [PATCH] Support type parameters in ReplaceTopBottomVisitor. See https://github.com/dart-lang/language/pull/1133 Change-Id: I3800b88496a8bf214b4bc366f4cb7ef3e9968f17 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/162784 Reviewed-by: Brian Wilkerson Commit-Queue: Konstantin Shcheglov --- .../element/replace_top_bottom_visitor.dart | 11 ++++++++++ .../dart/element/replace_top_bottom_test.dart | 21 ++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/pkg/analyzer/lib/src/dart/element/replace_top_bottom_visitor.dart b/pkg/analyzer/lib/src/dart/element/replace_top_bottom_visitor.dart index 7e5e45735906..5dd69610023b 100644 --- a/pkg/analyzer/lib/src/dart/element/replace_top_bottom_visitor.dart +++ b/pkg/analyzer/lib/src/dart/element/replace_top_bottom_visitor.dart @@ -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'; @@ -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; diff --git a/pkg/analyzer/test/src/dart/element/replace_top_bottom_test.dart b/pkg/analyzer/test/src/dart/element/replace_top_bottom_test.dart index e3a64cf6a353..4851abd9df9d 100644 --- a/pkg/analyzer/test/src/dart/element/replace_top_bottom_test.dart +++ b/pkg/analyzer/test/src/dart/element/replace_top_bottom_test.dart @@ -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?', ); }