Store library nullability status in LibraryElement; adjust TypeParameterElement.type based on it.

Change-Id: I0a9673b3b2f46bbf5c3aea429e90c6ddafa978e4
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/102224
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
diff --git a/pkg/analyzer/lib/dart/element/element.dart b/pkg/analyzer/lib/dart/element/element.dart
index 7b42b5e..61cc2fc 100644
--- a/pkg/analyzer/lib/dart/element/element.dart
+++ b/pkg/analyzer/lib/dart/element/element.dart
@@ -1299,6 +1299,8 @@
   /// included using the `part` directive.
   List<CompilationUnitElement> get units;
 
+  bool get isNonNullableByDefault;
+
   /// Return a list containing all of the imports that share the given [prefix],
   /// or an empty array if there are no such imports.
   List<ImportElement> getImportsWithPrefix(PrefixElement prefix);
diff --git a/pkg/analyzer/lib/src/dart/element/element.dart b/pkg/analyzer/lib/src/dart/element/element.dart
index aa4e55d..693953a 100644
--- a/pkg/analyzer/lib/src/dart/element/element.dart
+++ b/pkg/analyzer/lib/src/dart/element/element.dart
@@ -6250,6 +6250,9 @@
   /// The context of the defining unit.
   final LinkedUnitContext linkedContext;
 
+  @override
+  final bool isNonNullableByDefault;
+
   /// The compilation unit that defines this library.
   CompilationUnitElement _definingCompilationUnit;
 
@@ -6300,8 +6303,8 @@
 
   /// Initialize a newly created library element in the given [context] to have
   /// the given [name] and [offset].
-  LibraryElementImpl(
-      this.context, this.session, String name, int offset, this.nameLength)
+  LibraryElementImpl(this.context, this.session, String name, int offset,
+      this.nameLength, this.isNonNullableByDefault)
       : resynthesizerContext = null,
         unlinkedDefiningUnit = null,
         linkedContext = null,
@@ -6318,6 +6321,7 @@
       CompilationUnit linkedNode)
       : resynthesizerContext = null,
         unlinkedDefiningUnit = null,
+        isNonNullableByDefault = linkedContext.isNNBD,
         super.forLinkedNode(null, reference, linkedNode) {
     _name = name;
     _nameOffset = offset;
@@ -6329,7 +6333,8 @@
 
   /// Initialize a newly created library element in the given [context] to have
   /// the given [name].
-  LibraryElementImpl.forNode(this.context, this.session, LibraryIdentifier name)
+  LibraryElementImpl.forNode(this.context, this.session, LibraryIdentifier name,
+      this.isNonNullableByDefault)
       : nameLength = name != null ? name.length : 0,
         resynthesizerContext = null,
         unlinkedDefiningUnit = null,
@@ -6346,6 +6351,7 @@
       this.resynthesizerContext,
       this.unlinkedDefiningUnit)
       : linkedContext = null,
+        isNonNullableByDefault = unlinkedDefiningUnit.isNNBD,
         super.forSerialized(null) {
     _name = name;
     _nameOffset = offset;
@@ -9566,7 +9572,10 @@
   }
 
   TypeParameterType get type {
-    return _type ??= new TypeParameterTypeImpl(this);
+    return _type ??= new TypeParameterTypeImpl(this,
+        nullabilitySuffix: library.isNonNullableByDefault
+            ? NullabilitySuffix.none
+            : NullabilitySuffix.star);
   }
 
   void set type(TypeParameterType type) {
diff --git a/pkg/analyzer/lib/src/dart/element/handle.dart b/pkg/analyzer/lib/src/dart/element/handle.dart
index f316eb7..67f745c 100644
--- a/pkg/analyzer/lib/src/dart/element/handle.dart
+++ b/pkg/analyzer/lib/src/dart/element/handle.dart
@@ -894,6 +894,9 @@
   LibraryElement get actualElement => super.actualElement as LibraryElement;
 
   @override
+  bool get isNonNullableByDefault => actualElement.isNonNullableByDefault;
+
+  @override
   CompilationUnitElement get definingCompilationUnit =>
       actualElement.definingCompilationUnit;
 
diff --git a/pkg/analyzer/lib/src/dart/element/wrapped.dart b/pkg/analyzer/lib/src/dart/element/wrapped.dart
index 5a9be7c..ab41831 100644
--- a/pkg/analyzer/lib/src/dart/element/wrapped.dart
+++ b/pkg/analyzer/lib/src/dart/element/wrapped.dart
@@ -426,6 +426,9 @@
   AnalysisContext get context => wrappedLib.context;
 
   @override
+  bool get isNonNullableByDefault => wrappedLib.isNonNullableByDefault;
+
+  @override
   CompilationUnitElement get definingCompilationUnit =>
       wrappedLib.definingCompilationUnit;
 
diff --git a/pkg/analyzer/lib/src/generated/testing/element_factory.dart b/pkg/analyzer/lib/src/generated/testing/element_factory.dart
index 8dfa63d..e54a06c 100644
--- a/pkg/analyzer/lib/src/generated/testing/element_factory.dart
+++ b/pkg/analyzer/lib/src/generated/testing/element_factory.dart
@@ -412,12 +412,12 @@
     return spec;
   }
 
-  static LibraryElementImpl library(
-      AnalysisContext context, String libraryName) {
+  static LibraryElementImpl library(AnalysisContext context, String libraryName,
+      {bool isNonNullableByDefault: true}) {
     String fileName = "/$libraryName.dart";
     CompilationUnitElementImpl unit = compilationUnit(fileName);
-    LibraryElementImpl library = new LibraryElementImpl(
-        context, null, libraryName, 0, libraryName.length);
+    LibraryElementImpl library = new LibraryElementImpl(context, null,
+        libraryName, 0, libraryName.length, isNonNullableByDefault);
     library.definingCompilationUnit = unit;
     return library;
   }
diff --git a/pkg/analyzer/lib/src/generated/testing/test_type_provider.dart b/pkg/analyzer/lib/src/generated/testing/test_type_provider.dart
index da58fd1..82a7821 100644
--- a/pkg/analyzer/lib/src/generated/testing/test_type_provider.dart
+++ b/pkg/analyzer/lib/src/generated/testing/test_type_provider.dart
@@ -23,6 +23,8 @@
  * for the core library.
  */
 class TestTypeProvider extends TypeProviderBase {
+  final bool _isNonNullableByDefault;
+
   /**
    * The type representing the built-in type 'bool'.
    */
@@ -190,7 +192,9 @@
    */
   AnalysisDriver _driver;
 
-  TestTypeProvider([this._context, this._driver]);
+  /// TODO(paulberry): rework API and make _isNonNullableByDefault required.
+  TestTypeProvider(
+      [this._context, this._driver, this._isNonNullableByDefault = false]);
 
   @override
   InterfaceType get boolType {
@@ -471,7 +475,10 @@
       // Create a library element for "dart:core"
       // This enables the "isDartCoreNull" getter.
       var library = new LibraryElementImpl.forNode(
-          _context, null, AstTestFactory.libraryIdentifier2(["dart.core"]));
+          _context,
+          null,
+          AstTestFactory.libraryIdentifier2(["dart.core"]),
+          _isNonNullableByDefault);
       var unit = new CompilationUnitElementImpl();
       library.definingCompilationUnit = unit;
       unit.librarySource = unit.source = new StringSource('', null);
@@ -619,7 +626,10 @@
     }
     CompilationUnitElementImpl asyncUnit = new CompilationUnitElementImpl();
     LibraryElementImpl asyncLibrary = new LibraryElementImpl.forNode(
-        _context, null, AstTestFactory.libraryIdentifier2(["dart.async"]));
+        _context,
+        null,
+        AstTestFactory.libraryIdentifier2(["dart.async"]),
+        _isNonNullableByDefault);
     asyncLibrary.definingCompilationUnit = asyncUnit;
     asyncUnit.librarySource = asyncUnit.source = asyncSource;
 
diff --git a/pkg/analyzer/lib/src/summary/format.dart b/pkg/analyzer/lib/src/summary/format.dart
index 68e2d88..df52af8 100644
--- a/pkg/analyzer/lib/src/summary/format.dart
+++ b/pkg/analyzer/lib/src/summary/format.dart
@@ -15263,6 +15263,7 @@
   LinkedNodeBuilder _node;
   UnlinkedTokensBuilder _tokens;
   String _uriStr;
+  bool _isNNBD;
 
   @override
   List<LinkedNodeBuilder> get genericFunctionTypes =>
@@ -15315,19 +15316,28 @@
     this._uriStr = value;
   }
 
+  @override
+  bool get isNNBD => _isNNBD ??= false;
+
+  set isNNBD(bool value) {
+    this._isNNBD = value;
+  }
+
   LinkedNodeUnitBuilder(
       {List<LinkedNodeBuilder> genericFunctionTypes,
       bool isSynthetic,
       List<int> lineStarts,
       LinkedNodeBuilder node,
       UnlinkedTokensBuilder tokens,
-      String uriStr})
+      String uriStr,
+      bool isNNBD})
       : _genericFunctionTypes = genericFunctionTypes,
         _isSynthetic = isSynthetic,
         _lineStarts = lineStarts,
         _node = node,
         _tokens = tokens,
-        _uriStr = uriStr;
+        _uriStr = uriStr,
+        _isNNBD = isNNBD;
 
   /// Flush [informative] data recursively.
   void flushInformative() {
@@ -15353,6 +15363,7 @@
         x?.collectApiSignature(signature);
       }
     }
+    signature.addBool(this._isNNBD == true);
   }
 
   fb.Offset finish(fb.Builder fbBuilder) {
@@ -15396,6 +15407,9 @@
     if (offset_uriStr != null) {
       fbBuilder.addOffset(0, offset_uriStr);
     }
+    if (_isNNBD == true) {
+      fbBuilder.addBool(6, true);
+    }
     return fbBuilder.endTable();
   }
 }
@@ -15422,6 +15436,7 @@
   idl.LinkedNode _node;
   idl.UnlinkedTokens _tokens;
   String _uriStr;
+  bool _isNNBD;
 
   @override
   List<idl.LinkedNode> get genericFunctionTypes {
@@ -15462,6 +15477,12 @@
     _uriStr ??= const fb.StringReader().vTableGet(_bc, _bcOffset, 0, '');
     return _uriStr;
   }
+
+  @override
+  bool get isNNBD {
+    _isNNBD ??= const fb.BoolReader().vTableGet(_bc, _bcOffset, 6, false);
+    return _isNNBD;
+  }
 }
 
 abstract class _LinkedNodeUnitMixin implements idl.LinkedNodeUnit {
@@ -15476,6 +15497,7 @@
     if (node != null) _result["node"] = node.toJson();
     if (tokens != null) _result["tokens"] = tokens.toJson();
     if (uriStr != '') _result["uriStr"] = uriStr;
+    if (isNNBD != false) _result["isNNBD"] = isNNBD;
     return _result;
   }
 
@@ -15487,6 +15509,7 @@
         "node": node,
         "tokens": tokens,
         "uriStr": uriStr,
+        "isNNBD": isNNBD,
       };
 
   @override
diff --git a/pkg/analyzer/lib/src/summary/format.fbs b/pkg/analyzer/lib/src/summary/format.fbs
index ba6a1ed..b4ede7a 100644
--- a/pkg/analyzer/lib/src/summary/format.fbs
+++ b/pkg/analyzer/lib/src/summary/format.fbs
@@ -2054,6 +2054,8 @@
   tokens:UnlinkedTokens (id: 1);
 
   uriStr:string (id: 0);
+
+  isNNBD:bool (id: 6);
 }
 
 /// Information about a top-level declaration, or a field declaration that
diff --git a/pkg/analyzer/lib/src/summary/idl.dart b/pkg/analyzer/lib/src/summary/idl.dart
index 182acf6..e9e7240 100644
--- a/pkg/analyzer/lib/src/summary/idl.dart
+++ b/pkg/analyzer/lib/src/summary/idl.dart
@@ -2051,6 +2051,9 @@
 
   @Id(0)
   String get uriStr;
+
+  @Id(6)
+  bool get isNNBD;
 }
 
 /// Information about a top-level declaration, or a field declaration that
diff --git a/pkg/analyzer/lib/src/summary/link.dart b/pkg/analyzer/lib/src/summary/link.dart
index 47692db..5bc3580 100644
--- a/pkg/analyzer/lib/src/summary/link.dart
+++ b/pkg/analyzer/lib/src/summary/link.dart
@@ -3605,6 +3605,9 @@
   }
 
   @override
+  bool get isNonNullableByDefault => _unlinkedDefiningUnit.isNNBD;
+
+  @override
   ContextForLink get context => _linker.context;
 
   @override
diff --git a/pkg/analyzer/lib/src/summary/resynthesize.dart b/pkg/analyzer/lib/src/summary/resynthesize.dart
index 840947d..fbe8c41 100644
--- a/pkg/analyzer/lib/src/summary/resynthesize.dart
+++ b/pkg/analyzer/lib/src/summary/resynthesize.dart
@@ -353,7 +353,7 @@
       Source librarySource = _getSource(uri);
       if (serializedLibrary == null) {
         LibraryElementImpl libraryElement =
-            new LibraryElementImpl(context, session, '', -1, 0);
+            new LibraryElementImpl(context, session, '', -1, 0, true);
         libraryElement.isSynthetic = true;
         CompilationUnitElementImpl unitElement =
             new CompilationUnitElementImpl();
diff --git a/pkg/analyzer/lib/src/summary2/link.dart b/pkg/analyzer/lib/src/summary2/link.dart
index f958156..e4f4f7b 100644
--- a/pkg/analyzer/lib/src/summary2/link.dart
+++ b/pkg/analyzer/lib/src/summary2/link.dart
@@ -195,6 +195,7 @@
             uriStr: unitContext.uriStr,
             lineStarts: unit.lineInfo.lineStarts,
             node: unitLinkedNode,
+            isNNBD: unit.featureSet.isEnabled(Feature.non_nullable),
             genericFunctionTypes: writer.genericFunctionTypes,
           ),
         );
diff --git a/pkg/analyzer/lib/src/summary2/linked_unit_context.dart b/pkg/analyzer/lib/src/summary2/linked_unit_context.dart
index 3e8f6d1..552f46c 100644
--- a/pkg/analyzer/lib/src/summary2/linked_unit_context.dart
+++ b/pkg/analyzer/lib/src/summary2/linked_unit_context.dart
@@ -2,6 +2,7 @@
 // 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.
 
+import 'package:analyzer/dart/analysis/features.dart';
 import 'package:analyzer/dart/ast/ast.dart';
 import 'package:analyzer/dart/element/element.dart';
 import 'package:analyzer/dart/element/type.dart';
@@ -78,6 +79,11 @@
   /// Return `true` if this unit is a part of a bundle that is being linked.
   bool get isLinking => bundleContext.isLinking;
 
+  bool get isNNBD {
+    if (data != null) return data.isNNBD;
+    return _unit.featureSet.isEnabled(Feature.non_nullable);
+  }
+
   TypeProvider get typeProvider =>
       bundleContext.elementFactory.analysisContext.typeProvider;
 
diff --git a/pkg/analyzer/test/generated/analysis_context_factory.dart b/pkg/analyzer/test/generated/analysis_context_factory.dart
index e2aef27..b70df9d 100644
--- a/pkg/analyzer/test/generated/analysis_context_factory.dart
+++ b/pkg/analyzer/test/generated/analysis_context_factory.dart
@@ -4,6 +4,7 @@
 
 import 'dart:collection';
 
+import 'package:analyzer/dart/analysis/features.dart';
 import 'package:analyzer/dart/ast/ast.dart';
 import 'package:analyzer/dart/ast/token.dart';
 import 'package:analyzer/dart/element/element.dart';
@@ -47,7 +48,8 @@
       {UriResolver contributedResolver,
       MemoryResourceProvider resourceProvider}) {
     AnalysisContextForTests context = new AnalysisContextForTests();
-    return initContextWithCore(context, contributedResolver, resourceProvider);
+    return initContextWithCore(context, FeatureSet.forTesting(),
+        contributedResolver, resourceProvider);
   }
 
   /**
@@ -60,7 +62,8 @@
       {MemoryResourceProvider resourceProvider}) {
     AnalysisContextForTests context = new AnalysisContextForTests();
     context._internalSetAnalysisOptions(options);
-    return initContextWithCore(context, null, resourceProvider);
+    return initContextWithCore(
+        context, options.contextFeatures, null, resourceProvider);
   }
 
   /**
@@ -73,8 +76,8 @@
       Map<String, String> packages,
       {MemoryResourceProvider resourceProvider}) {
     AnalysisContextForTests context = new AnalysisContextForTests();
-    return initContextWithCore(
-        context, new TestPackageUriResolver(packages), resourceProvider);
+    return initContextWithCore(context, FeatureSet.forTesting(),
+        new TestPackageUriResolver(packages), resourceProvider);
   }
 
   /**
@@ -84,7 +87,7 @@
    * be used when accessing the file system.
    */
   static InternalAnalysisContext initContextWithCore(
-      InternalAnalysisContext context,
+      InternalAnalysisContext context, FeatureSet featureSet,
       [UriResolver contributedResolver,
       MemoryResourceProvider resourceProvider]) {
     DartSdk sdk = new _AnalysisContextFactory_initContextWithCore(
@@ -174,7 +177,10 @@
       proxyTopLevelVariableElt
     ];
     LibraryElementImpl coreLibrary = new LibraryElementImpl.forNode(
-        coreContext, null, AstTestFactory.libraryIdentifier2(["dart", "core"]));
+        coreContext,
+        null,
+        AstTestFactory.libraryIdentifier2(["dart", "core"]),
+        featureSet.isEnabled(Feature.non_nullable));
     coreLibrary.definingCompilationUnit = coreUnit;
     //
     // dart:async
@@ -182,7 +188,8 @@
     LibraryElementImpl asyncLibrary = new LibraryElementImpl.forNode(
         coreContext,
         null,
-        AstTestFactory.libraryIdentifier2(["dart", "async"]));
+        AstTestFactory.libraryIdentifier2(["dart", "async"]),
+        featureSet.isEnabled(Feature.non_nullable));
     CompilationUnitElementImpl asyncUnit = new CompilationUnitElementImpl();
     Source asyncSource = sourceFactory.forUri(DartSdk.DART_ASYNC);
     asyncUnit.librarySource = asyncUnit.source = asyncSource;
@@ -329,8 +336,11 @@
             "document", false, true, htmlDocumentElement.type);
     htmlUnit.topLevelVariables = <TopLevelVariableElement>[document];
     htmlUnit.accessors = <PropertyAccessorElement>[document.getter];
-    LibraryElementImpl htmlLibrary = new LibraryElementImpl.forNode(coreContext,
-        null, AstTestFactory.libraryIdentifier2(["dart", "dom", "html"]));
+    LibraryElementImpl htmlLibrary = new LibraryElementImpl.forNode(
+        coreContext,
+        null,
+        AstTestFactory.libraryIdentifier2(["dart", "dom", "html"]),
+        featureSet.isEnabled(Feature.non_nullable));
     htmlLibrary.definingCompilationUnit = htmlUnit;
     //
     // dart:math
@@ -390,7 +400,10 @@
     ];
     mathUnit.types = <ClassElement>[randomElement];
     LibraryElementImpl mathLibrary = new LibraryElementImpl.forNode(
-        coreContext, null, AstTestFactory.libraryIdentifier2(["dart", "math"]));
+        coreContext,
+        null,
+        AstTestFactory.libraryIdentifier2(["dart", "math"]),
+        featureSet.isEnabled(Feature.non_nullable));
     mathLibrary.definingCompilationUnit = mathUnit;
     //
     // Record the elements.
diff --git a/pkg/analyzer/test/generated/resolver_test.dart b/pkg/analyzer/test/generated/resolver_test.dart
index 3ec7c06..7fa83ed 100644
--- a/pkg/analyzer/test/generated/resolver_test.dart
+++ b/pkg/analyzer/test/generated/resolver_test.dart
@@ -397,10 +397,10 @@
     asyncUnit.source = new TestSource('dart:async');
     asyncUnit.librarySource = asyncUnit.source;
     LibraryElementImpl coreLibrary = new LibraryElementImpl.forNode(
-        null, null, AstTestFactory.libraryIdentifier2(["dart.core"]));
+        null, null, AstTestFactory.libraryIdentifier2(["dart.core"]), true);
     coreLibrary.definingCompilationUnit = coreUnit;
     LibraryElementImpl asyncLibrary = new LibraryElementImpl.forNode(
-        null, null, AstTestFactory.libraryIdentifier2(["dart.async"]));
+        null, null, AstTestFactory.libraryIdentifier2(["dart.async"]), true);
     asyncLibrary.definingCompilationUnit = asyncUnit;
     //
     // Create a type provider and ensure that it can return the expected types.
@@ -1190,10 +1190,10 @@
     asyncUnit.source = new TestSource('dart:async');
     asyncUnit.librarySource = asyncUnit.source;
     LibraryElementImpl coreLibrary = new LibraryElementImpl.forNode(
-        null, null, AstTestFactory.libraryIdentifier2(["dart.core"]));
+        null, null, AstTestFactory.libraryIdentifier2(["dart.core"]), true);
     coreLibrary.definingCompilationUnit = coreUnit;
     LibraryElementImpl asyncLibrary = new LibraryElementImpl.forNode(
-        null, null, AstTestFactory.libraryIdentifier2(["dart.async"]));
+        null, null, AstTestFactory.libraryIdentifier2(["dart.async"]), true);
     asyncLibrary.definingCompilationUnit = asyncUnit;
     //
     // Create a type provider and ensure that it can return the expected types.
@@ -1293,14 +1293,17 @@
     InternalAnalysisContext context = AnalysisContextFactory.contextWithCore(
         resourceProvider: resourceProvider);
     Source librarySource = new FileSource(getFile("/lib.dart"));
-    LibraryElementImpl element = new LibraryElementImpl.forNode(
-        context, null, AstTestFactory.libraryIdentifier2(["lib"]));
-    element.definingCompilationUnit = new CompilationUnitElementImpl();
-    _typeProvider = new TestTypeProvider();
-    libraryScope = new LibraryScope(element);
     // TODO(paulberry): make it possible to override the feature set so we can
     // test NNBD features.
     var featureSet = FeatureSet.forTesting(sdkVersion: '2.2.2');
+    LibraryElementImpl element = new LibraryElementImpl.forNode(
+        context,
+        null,
+        AstTestFactory.libraryIdentifier2(["lib"]),
+        featureSet.isEnabled(Feature.non_nullable));
+    element.definingCompilationUnit = new CompilationUnitElementImpl();
+    _typeProvider = new TestTypeProvider();
+    libraryScope = new LibraryScope(element);
     _visitor = new TypeResolverVisitor(
         element, librarySource, _typeProvider, _listener,
         featureSet: featureSet,
@@ -1335,12 +1338,13 @@
       InternalAnalysisContext context = AnalysisContextFactory.contextWithCore(
           resourceProvider: resourceProvider);
       var source = getFile('/test.dart').createSource();
-      var libraryElement = new LibraryElementImpl.forNode(context, null, null)
-        ..definingCompilationUnit = unitElement;
-      var libraryScope = new LibraryScope(libraryElement);
       // TODO(paulberry): make it possible to override the feature set so we can
       // test NNBD features.
       var featureSet = FeatureSet.forTesting(sdkVersion: '2.2.2');
+      var libraryElement = new LibraryElementImpl.forNode(
+          context, null, null, featureSet.isEnabled(Feature.non_nullable))
+        ..definingCompilationUnit = unitElement;
+      var libraryScope = new LibraryScope(libraryElement);
       var visitor = new TypeResolverVisitor(
           libraryElement, source, _typeProvider, _listener,
           featureSet: featureSet,
@@ -2211,12 +2215,13 @@
       InternalAnalysisContext context = AnalysisContextFactory.contextWithCore(
           resourceProvider: resourceProvider);
       var source = getFile('/test.dart').createSource();
-      var libraryElement = new LibraryElementImpl.forNode(context, null, null)
-        ..definingCompilationUnit = unitElement;
-      libraryScope = new LibraryScope(libraryElement);
       // TODO(paulberry): make it possible to override the feature set so we can
       // test NNBD features.
       var featureSet = FeatureSet.forTesting(sdkVersion: '2.2.2');
+      var libraryElement = new LibraryElementImpl.forNode(
+          context, null, null, featureSet.isEnabled(Feature.non_nullable))
+        ..definingCompilationUnit = unitElement;
+      libraryScope = new LibraryScope(libraryElement);
       visitor = new TypeResolverVisitor(
           libraryElement, source, _typeProvider, _listener,
           featureSet: featureSet,
diff --git a/pkg/analyzer/test/generated/resolver_test_case.dart b/pkg/analyzer/test/generated/resolver_test_case.dart
index 611479c..b0bb6d4 100644
--- a/pkg/analyzer/test/generated/resolver_test_case.dart
+++ b/pkg/analyzer/test/generated/resolver_test_case.dart
@@ -4,6 +4,7 @@
 
 import 'dart:async';
 
+import 'package:analyzer/dart/analysis/features.dart';
 import 'package:analyzer/dart/analysis/results.dart';
 import 'package:analyzer/dart/ast/ast.dart';
 import 'package:analyzer/dart/ast/standard_resolution_map.dart';
@@ -567,10 +568,12 @@
         new CompilationUnitElementImpl();
     compilationUnit.librarySource =
         compilationUnit.source = definingCompilationUnitSource;
+    var featureSet = context.analysisOptions.contextFeatures;
     LibraryElementImpl library = new LibraryElementImpl.forNode(
         context,
         driver?.currentSession,
-        AstTestFactory.libraryIdentifier2([libraryName]));
+        AstTestFactory.libraryIdentifier2([libraryName]),
+        featureSet.isEnabled(Feature.non_nullable));
     library.definingCompilationUnit = compilationUnit;
     library.parts = sourcedCompilationUnits;
     return library;
diff --git a/pkg/analyzer/test/generated/static_type_analyzer_test.dart b/pkg/analyzer/test/generated/static_type_analyzer_test.dart
index 8382b04..e261136 100644
--- a/pkg/analyzer/test/generated/static_type_analyzer_test.dart
+++ b/pkg/analyzer/test/generated/static_type_analyzer_test.dart
@@ -1528,14 +1528,14 @@
         new CompilationUnitElementImpl();
     definingCompilationUnit.librarySource =
         definingCompilationUnit.source = source;
-    LibraryElementImpl definingLibrary =
-        new LibraryElementImpl.forNode(context, null, null);
+    var featureSet = FeatureSet.forTesting();
+    LibraryElementImpl definingLibrary = new LibraryElementImpl.forNode(
+        context, null, null, featureSet.isEnabled(Feature.non_nullable));
     definingLibrary.definingCompilationUnit = definingCompilationUnit;
     _typeProvider = context.typeProvider;
     _visitor = new ResolverVisitor(
         inheritance, definingLibrary, source, _typeProvider, _listener,
-        featureSet: FeatureSet.forTesting(),
-        nameScope: new LibraryScope(definingLibrary));
+        featureSet: featureSet, nameScope: new LibraryScope(definingLibrary));
     return _visitor.typeAnalyzer;
   }
 
diff --git a/pkg/analyzer/test/src/dart/ast/utilities_test.dart b/pkg/analyzer/test/src/dart/ast/utilities_test.dart
index 31e9a56..5771180 100644
--- a/pkg/analyzer/test/src/dart/ast/utilities_test.dart
+++ b/pkg/analyzer/test/src/dart/ast/utilities_test.dart
@@ -640,7 +640,7 @@
   void test_visitPartDirective() {
     PartDirective fromNode = AstTestFactory.partDirective2("part.dart");
     LibraryElement element = new LibraryElementImpl.forNode(
-        null, null, AstTestFactory.libraryIdentifier2(["lib"]));
+        null, null, AstTestFactory.libraryIdentifier2(["lib"]), true);
     fromNode.element = element;
     PartDirective toNode = AstTestFactory.partDirective2("part.dart");
     ResolutionCopier.copyResolutionData(fromNode, toNode);
@@ -651,7 +651,7 @@
     PartOfDirective fromNode = AstTestFactory.partOfDirective(
         AstTestFactory.libraryIdentifier2(["lib"]));
     LibraryElement element = new LibraryElementImpl.forNode(
-        null, null, AstTestFactory.libraryIdentifier2(["lib"]));
+        null, null, AstTestFactory.libraryIdentifier2(["lib"]), true);
     fromNode.element = element;
     PartOfDirective toNode = AstTestFactory.partOfDirective(
         AstTestFactory.libraryIdentifier2(["lib"]));
diff --git a/pkg/analyzer/test/src/dart/element/element_test.dart b/pkg/analyzer/test/src/dart/element/element_test.dart
index 1abce89..bc41dcb 100644
--- a/pkg/analyzer/test/src/dart/element/element_test.dart
+++ b/pkg/analyzer/test/src/dart/element/element_test.dart
@@ -3559,7 +3559,7 @@
   void test_creation() {
     expect(
         new LibraryElementImpl.forNode(createAnalysisContext(), null,
-            AstTestFactory.libraryIdentifier2(["l"])),
+            AstTestFactory.libraryIdentifier2(["l"]), true),
         isNotNull);
   }
 
@@ -3646,7 +3646,7 @@
   void test_setImports() {
     AnalysisContext context = createAnalysisContext();
     LibraryElementImpl library = new LibraryElementImpl.forNode(
-        context, null, AstTestFactory.libraryIdentifier2(["l1"]));
+        context, null, AstTestFactory.libraryIdentifier2(["l1"]), true);
     List<ImportElementImpl> expectedImports = [
       ElementFactory.importFor(ElementFactory.library(context, "l2"), null),
       ElementFactory.importFor(ElementFactory.library(context, "l3"), null)
diff --git a/pkg/analyzer/test/src/summary/resynthesize_common.dart b/pkg/analyzer/test/src/summary/resynthesize_common.dart
index 85bcfd2..1275558 100644
--- a/pkg/analyzer/test/src/summary/resynthesize_common.dart
+++ b/pkg/analyzer/test/src/summary/resynthesize_common.dart
@@ -7031,6 +7031,26 @@
     }
   }
 
+  test_compilationUnit_nnbd_disabled_via_dart_directive() async {
+    featureSet = enableNnbd;
+    var library = await checkLibrary('''
+// @dart=2.2
+''');
+    expect(library.isNonNullableByDefault, isFalse);
+  }
+
+  test_compilationUnit_nnbd_disabled_via_feature_set() async {
+    featureSet = disableNnbd;
+    var library = await checkLibrary('');
+    expect(library.isNonNullableByDefault, isFalse);
+  }
+
+  test_compilationUnit_nnbd_enabled() async {
+    featureSet = enableNnbd;
+    var library = await checkLibrary('');
+    expect(library.isNonNullableByDefault, isTrue);
+  }
+
   test_inferredType_implicitCreation() async {
     var library = await checkLibrary(r'''
 class A {