[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>
This commit is contained in:
Johnni Winther 2024-06-19 12:47:21 +00:00 committed by Commit Queue
parent 4f8e65318d
commit 7ed610de1d
4 changed files with 10 additions and 8 deletions

View file

@ -130,7 +130,8 @@ abstract class StackListener extends Listener with StackChecker {
@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 @@ abstract class StackListener extends Listener with StackChecker {
// 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;

View file

@ -177,7 +177,8 @@ class AstBuilder extends StackListener {
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 @@ class AstBuilder extends StackListener {
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) {

View file

@ -160,7 +160,7 @@ class MiniAstBuilder extends StackListener {
final compilationUnit = CompilationUnit();
@override
Uri get importUri => throw UnimplementedError();
bool get isDartLibrary => throw UnimplementedError();
@override
Uri get uri => throw UnimplementedError();

View file

@ -22,7 +22,9 @@ abstract class StackListenerImpl extends StackListener {
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")) {