[dart2js] Migrate visitors.dart and types.dart to nnbd.

Change-Id: Ic9f83d1f1fdae13f71ad04d703d5915148395f2d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/249520
Reviewed-by: Stephen Adams <sra@google.com>
Commit-Queue: Nate Biggs <natebiggs@google.com>
diff --git a/pkg/compiler/lib/src/ir/element_map.dart b/pkg/compiler/lib/src/ir/element_map.dart
index 6b61632..1b4030d 100644
--- a/pkg/compiler/lib/src/ir/element_map.dart
+++ b/pkg/compiler/lib/src/ir/element_map.dart
@@ -8,7 +8,6 @@
 import '../common.dart';
 import '../common/elements.dart';
 import '../elements/entities.dart';
-import '../elements/indexed.dart';
 import '../elements/types.dart';
 import '../ordered_typeset.dart';
 import '../universe/call_structure.dart';
@@ -63,6 +62,6 @@
   DartType substByContext(DartType type, InterfaceType context);
   FunctionType? getCallType(InterfaceType type);
   int getHierarchyDepth(covariant ClassEntity cls);
-  DartType getTypeVariableBound(IndexedTypeVariable typeVariable);
+  DartType getTypeVariableBound(covariant TypeVariableEntity typeVariable);
   List<Variance> getTypeVariableVariances(covariant ClassEntity cls);
 }
diff --git a/pkg/compiler/lib/src/ir/types.dart b/pkg/compiler/lib/src/ir/types.dart
index a14ddbb..c12988a 100644
--- a/pkg/compiler/lib/src/ir/types.dart
+++ b/pkg/compiler/lib/src/ir/types.dart
@@ -2,8 +2,6 @@
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
 
-// @dart = 2.10
-
 import '../common/elements.dart';
 import '../elements/entities.dart';
 import '../elements/types.dart';
@@ -27,7 +25,7 @@
   }
 
   @override
-  InterfaceType getSupertype(ClassEntity cls) {
+  InterfaceType? getSupertype(ClassEntity cls) {
     return elementMap.getSuperType(cls);
   }
 
@@ -42,7 +40,7 @@
   }
 
   @override
-  InterfaceType asInstanceOf(InterfaceType type, ClassEntity cls) {
+  InterfaceType? asInstanceOf(InterfaceType type, ClassEntity cls) {
     return elementMap.asInstanceOf(type, cls);
   }
 
@@ -52,7 +50,7 @@
   }
 
   @override
-  FunctionType getCallType(InterfaceType type) => elementMap.getCallType(type);
+  FunctionType? getCallType(InterfaceType type) => elementMap.getCallType(type);
 
   @override
   void checkTypeVariableBounds<T>(
@@ -64,7 +62,7 @@
     assert(typeVariables.length == typeArguments.length);
     for (int index = 0; index < typeArguments.length; index++) {
       DartType typeArgument = typeArguments[index];
-      TypeVariableType typeVariable = typeVariables[index];
+      final typeVariable = typeVariables[index] as TypeVariableType;
       DartType bound = subst(typeArguments, typeVariables,
           elementMap.getTypeVariableBound(typeVariable.element));
       checkTypeVariableBound(context, typeArgument, typeVariable, bound);
@@ -96,7 +94,7 @@
 
   @override
   InterfaceType substByContext(InterfaceType type, InterfaceType context) {
-    return elementMap.substByContext(type, context);
+    return elementMap.substByContext(type, context) as InterfaceType;
   }
 
   @override
diff --git a/pkg/compiler/lib/src/ir/visitors.dart b/pkg/compiler/lib/src/ir/visitors.dart
index aea9e7d..c752997 100644
--- a/pkg/compiler/lib/src/ir/visitors.dart
+++ b/pkg/compiler/lib/src/ir/visitors.dart
@@ -2,8 +2,6 @@
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
 
-// @dart = 2.10
-
 import 'package:kernel/ast.dart' as ir;
 
 import '../constants/constant_system.dart' as constant_system;
@@ -14,15 +12,15 @@
 
 /// Visitor that converts string literals and concatenations of string literals
 /// into the string value.
-class Stringifier extends ir.ExpressionVisitor<String> {
+class Stringifier extends ir.ExpressionVisitor<String?> {
   @override
   String visitStringLiteral(ir.StringLiteral node) => node.value;
 
   @override
-  String visitStringConcatenation(ir.StringConcatenation node) {
+  String? visitStringConcatenation(ir.StringConcatenation node) {
     StringBuffer sb = StringBuffer();
     for (ir.Expression expression in node.expressions) {
-      String value = expression.accept(this);
+      String? value = expression.accept(this);
       if (value == null) return null;
       sb.write(value);
     }
@@ -30,7 +28,7 @@
   }
 
   @override
-  String visitConstantExpression(ir.ConstantExpression node) {
+  String? visitConstantExpression(ir.ConstantExpression node) {
     ir.Constant constant = node.constant;
     if (constant is ir.StringConstant) {
       return constant.value;
@@ -39,11 +37,11 @@
   }
 
   @override
-  String defaultExpression(ir.Expression node) => null;
+  String? defaultExpression(ir.Expression node) => null;
 }
 
 /// Visitor that converts kernel dart types into [DartType].
-class DartTypeConverter extends ir.DartTypeVisitor<DartType /*!*/ > {
+class DartTypeConverter extends ir.DartTypeVisitor<DartType> {
   final IrToElementMap elementMap;
   final Map<ir.TypeParameter, DartType> currentFunctionTypeParameters =
       <ir.TypeParameter, DartType>{};
@@ -83,8 +81,6 @@
         throw UnsupportedError(
             'Undetermined nullability on $nullabilitySource');
     }
-    throw UnsupportedError(
-        'Unexpected nullability $nullability on $nullabilitySource');
   }
 
   DartType visitType(ir.DartType type) {
@@ -92,7 +88,8 @@
   }
 
   InterfaceType visitSupertype(ir.Supertype node) =>
-      visitInterfaceType(node.asInterfaceType).withoutNullability;
+      visitInterfaceType(node.asInterfaceType).withoutNullability
+          as InterfaceType;
 
   List<DartType> visitTypes(List<ir.DartType> types) {
     return List.generate(
@@ -101,7 +98,7 @@
 
   @override
   DartType visitTypeParameterType(ir.TypeParameterType node) {
-    DartType typeParameter = currentFunctionTypeParameters[node.parameter];
+    DartType? typeParameter = currentFunctionTypeParameters[node.parameter];
     if (typeParameter != null) {
       return _convertNullability(typeParameter, node);
     }
@@ -118,7 +115,7 @@
   @override
   DartType visitFunctionType(ir.FunctionType node) {
     int index = 0;
-    List<FunctionTypeVariable> typeVariables;
+    List<FunctionTypeVariable>? typeVariables;
     for (ir.TypeParameter typeParameter in node.typeParameters) {
       FunctionTypeVariable typeVariable =
           _dartTypes.functionTypeVariable(index);
@@ -229,7 +226,7 @@
   ConstantValue visitStaticTearOffConstant(ir.StaticTearOffConstant node) {
     ir.Procedure member = node.target;
     FunctionEntity function = elementMap.getMethod(member);
-    DartType type = elementMap.getFunctionType(member.function);
+    final type = elementMap.getFunctionType(member.function);
     return FunctionConstantValue(function, type);
   }
 
@@ -239,7 +236,8 @@
     for (ir.DartType type in node.types) {
       typeArguments.add(elementMap.getDartType(type));
     }
-    FunctionConstantValue function = visitConstant(node.tearOffConstant);
+    final function =
+        visitConstant(node.tearOffConstant) as FunctionConstantValue;
     return InstantiationConstantValue(typeArguments, function);
   }
 
@@ -261,7 +259,7 @@
     for (ir.Constant element in node.entries) {
       elements.add(visitConstant(element));
     }
-    DartType type = elementMap.commonElements
+    final type = elementMap.commonElements
         .listType(elementMap.getDartType(node.typeArgument));
     return constant_system.createList(
         elementMap.commonElements, type, elements);
@@ -273,7 +271,7 @@
     for (ir.Constant element in node.entries) {
       elements.add(visitConstant(element));
     }
-    DartType type = elementMap.commonElements
+    final type = elementMap.commonElements
         .setType(elementMap.getDartType(node.typeArgument));
     return constant_system.createSet(elementMap.commonElements, type, elements);
   }
@@ -286,7 +284,7 @@
       keys.add(visitConstant(element.key));
       values.add(visitConstant(element.value));
     }
-    DartType type = elementMap.commonElements.mapType(
+    final type = elementMap.commonElements.mapType(
         elementMap.getDartType(node.keyType),
         elementMap.getDartType(node.valueType));
     return constant_system.createMap(
diff --git a/pkg/compiler/lib/src/js_model/element_map_impl.dart b/pkg/compiler/lib/src/js_model/element_map_impl.dart
index 149eb78..d23fd743 100644
--- a/pkg/compiler/lib/src/js_model/element_map_impl.dart
+++ b/pkg/compiler/lib/src/js_model/element_map_impl.dart
@@ -1380,7 +1380,7 @@
     return lookup;
   }
 
-  String _getStringArgument(ir.StaticInvocation node, int index) {
+  String /*?*/ _getStringArgument(ir.StaticInvocation node, int index) {
     return node.arguments.positional[index].accept(Stringifier());
   }
 
diff --git a/pkg/compiler/lib/src/kernel/element_map_impl.dart b/pkg/compiler/lib/src/kernel/element_map_impl.dart
index bbf2695..46cbef3 100644
--- a/pkg/compiler/lib/src/kernel/element_map_impl.dart
+++ b/pkg/compiler/lib/src/kernel/element_map_impl.dart
@@ -1013,7 +1013,7 @@
     return lookup;
   }
 
-  String _getStringArgument(ir.StaticInvocation node, int index) {
+  String /*?*/ _getStringArgument(ir.StaticInvocation node, int index) {
     return node.arguments.positional[index].accept(Stringifier());
   }