[analyzer/cfe] Replace StackListener.importUri with isDartLibrary

The [StackListener.importUri] property is problematic because it assumes
or implies that when a compilation unit is being parsed, the import URI
for the containing library is known. This might not be the case if a part file is read before the main library file.

Currently the [StackListener.importUri] is only used to detect whether
the current file is part of a `dart:` library, so the property is replace with [isDartLibrary] which does just that.

Change-Id: I2d2baf2fe20bb62fd1922864c0e5af95e8fd1ca7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/372084
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
diff --git a/pkg/_fe_analyzer_shared/lib/src/parser/stack_listener.dart b/pkg/_fe_analyzer_shared/lib/src/parser/stack_listener.dart
index d4779b7..815fb23 100644
--- a/pkg/_fe_analyzer_shared/lib/src/parser/stack_listener.dart
+++ b/pkg/_fe_analyzer_shared/lib/src/parser/stack_listener.dart
@@ -130,7 +130,8 @@
   @override
   Uri get uri;
 
-  Uri get importUri;
+  /// Returns `true` if the current file is part of a `dart:` library.
+  bool get isDartLibrary;
 
   void discard(int n) {
     for (int i = 0; i < n; i++) {
@@ -402,8 +403,7 @@
       // Ignored. This error is handled by the BodyBuilder.
       return true;
     } else if (code == codeBuiltInIdentifierInDeclaration) {
-      if (importUri.isScheme("dart")) return true;
-      if (uri.isScheme("org-dartlang-sdk")) return true;
+      if (isDartLibrary) return true;
       return false;
     } else {
       return false;
diff --git a/pkg/analyzer/lib/src/fasta/ast_builder.dart b/pkg/analyzer/lib/src/fasta/ast_builder.dart
index 7399091..f7ee93f 100644
--- a/pkg/analyzer/lib/src/fasta/ast_builder.dart
+++ b/pkg/analyzer/lib/src/fasta/ast_builder.dart
@@ -177,7 +177,8 @@
         uri = uri ?? fileUri;
 
   @override
-  Uri get importUri => uri;
+  bool get isDartLibrary =>
+      uri.isScheme("dart") || uri.isScheme("org-dartlang-sdk");
 
   @override
   void addProblem(Message message, int charOffset, int length,
@@ -5236,8 +5237,7 @@
       return;
     } else if (message.code == codeBuiltInIdentifierInDeclaration) {
       // Allow e.g. 'class Function' in sdk.
-      if (importUri.isScheme("dart")) return;
-      if (uri.isScheme("org-dartlang-sdk")) return;
+      if (isDartLibrary) return;
     }
     debugEvent("Error: ${message.problemMessage}");
     if (message.code.analyzerCodes == null && startToken is ErrorToken) {
diff --git a/pkg/analyzer/tool/summary/mini_ast.dart b/pkg/analyzer/tool/summary/mini_ast.dart
index 5f93c5e..91a2b93 100644
--- a/pkg/analyzer/tool/summary/mini_ast.dart
+++ b/pkg/analyzer/tool/summary/mini_ast.dart
@@ -160,7 +160,7 @@
   final compilationUnit = CompilationUnit();
 
   @override
-  Uri get importUri => throw UnimplementedError();
+  bool get isDartLibrary => throw UnimplementedError();
 
   @override
   Uri get uri => throw UnimplementedError();
diff --git a/pkg/front_end/lib/src/fasta/source/stack_listener_impl.dart b/pkg/front_end/lib/src/fasta/source/stack_listener_impl.dart
index f115852..26dcc57 100644
--- a/pkg/front_end/lib/src/fasta/source/stack_listener_impl.dart
+++ b/pkg/front_end/lib/src/fasta/source/stack_listener_impl.dart
@@ -22,7 +22,9 @@
   LibraryFeatures get libraryFeatures => libraryBuilder.libraryFeatures;
 
   @override
-  Uri get importUri => libraryBuilder.origin.importUri;
+  bool get isDartLibrary =>
+      libraryBuilder.origin.importUri.isScheme("dart") ||
+      uri.isScheme("org-dartlang-sdk");
 
   AsyncMarker asyncMarkerFromTokens(Token? asyncToken, Token? starToken) {
     if (asyncToken == null || identical(asyncToken.stringValue, "sync")) {