Skip to content

Commit 15cf794

Browse files
scheglovCommit Bot
authored and
Commit Bot
committed
Check that referenced objects are declared, so not stale.
Change-Id: I2eb64617aaae1da7ac80399621ad227cdd08af97 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/249946 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent ca26ed2 commit 15cf794

File tree

4 files changed

+625
-474
lines changed

4 files changed

+625
-474
lines changed

pkg/analyzer/lib/src/dart/analysis/file_state.dart

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ class FileState {
275275
FileStateKind? _kind;
276276

277277
/// Files that reference this file.
278-
final List<FileState> referencingFiles = [];
278+
final Set<FileState> referencingFiles = {};
279279

280280
List<FileState?> _augmentationFiles = [];
281281
List<FileState?>? _importedFiles;
@@ -1036,6 +1036,8 @@ abstract class FileStateKind {
10361036

10371037
/// Returns the library in which this file should be analyzed.
10381038
LibraryFileStateKind? get library;
1039+
1040+
void dispose() {}
10391041
}
10401042

10411043
enum FileStateRefreshResult {
@@ -1155,6 +1157,8 @@ class FileSystemState {
11551157
for (var reference in file.referencingFiles.toList()) {
11561158
changeFile(reference.path, removedFiles);
11571159
}
1160+
1161+
file._kind?.dispose();
11581162
}
11591163

11601164
/// Collected files that transitively reference a file with the [path].
@@ -1387,9 +1391,7 @@ class FileSystemState {
13871391

13881392
var removedFiles = <FileState>[];
13891393
for (var path in unusedPaths) {
1390-
var file = _pathToFile.remove(path)!;
1391-
_uriToFile.remove(file.uri);
1392-
removedFiles.add(file);
1394+
changeFile(path, removedFiles);
13931395
}
13941396

13951397
return removedFiles;
@@ -1607,6 +1609,11 @@ class LibraryFileStateKind extends LibraryOrAugmentationFileKind {
16071609
return _libraryCycle!;
16081610
}
16091611

1612+
@override
1613+
void dispose() {
1614+
invalidateLibraryCycle();
1615+
}
1616+
16101617
bool hasPart(PartFileStateKind part) {
16111618
return file.partedFiles.contains(part.file);
16121619
}
@@ -1714,7 +1721,7 @@ class PartOfNameFileStateKind extends PartFileStateKind {
17141721
/// first one as if sorted by path.
17151722
@override
17161723
LibraryFileStateKind? get library {
1717-
_discoverLibraries();
1724+
discoverLibraries();
17181725

17191726
LibraryFileStateKind? result;
17201727
for (final library in libraries) {
@@ -1729,7 +1736,8 @@ class PartOfNameFileStateKind extends PartFileStateKind {
17291736
return result;
17301737
}
17311738

1732-
void _discoverLibraries() {
1739+
@visibleForTesting
1740+
void discoverLibraries() {
17331741
if (libraries.isEmpty) {
17341742
var resourceProvider = file._fsState._resourceProvider;
17351743
var pathContext = resourceProvider.pathContext;

pkg/analyzer/test/src/dart/analysis/analyzer_state_printer.dart

Lines changed: 104 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -316,31 +316,79 @@ class AnalyzerStatePrinter {
316316
void _writeFiles(FileSystemTestData testData) {
317317
fileSystemState.pullReferencedFiles();
318318

319-
final fileDataList = <FileTestData>[];
320-
for (final fileData in testData.files.values) {
321-
if (omitSdkFiles && fileData.uri.isScheme('dart')) {
322-
continue;
319+
// Discover libraries for parts.
320+
// This is required for consistency checking.
321+
for (final fileData in testData.files.values.toList()) {
322+
final current = fileSystemState.getExisting(fileData.file);
323+
if (current != null) {
324+
final kind = current.kind;
325+
if (kind is PartOfNameFileStateKind) {
326+
kind.discoverLibraries();
327+
}
328+
}
329+
}
330+
331+
// Ask referenced libraries.
332+
// This is required for consistency checking.
333+
// TODO(scheglov) Remove when we use these for cycles.
334+
for (final fileData in testData.files.values.toList()) {
335+
final current = fileSystemState.getExisting(fileData.file);
336+
if (current != null) {
337+
final kind = current.kind;
338+
if (kind is LibraryOrAugmentationFileKind) {
339+
kind.imports;
340+
kind.exports;
341+
}
323342
}
324-
fileDataList.add(fileData);
325343
}
326-
fileDataList.sortBy((fileData) => fileData.file.path);
344+
345+
// Sort, mostly by path.
346+
// But sort SDK libraries to the end, with `dart:core` first.
347+
final fileDataList = testData.files.values.toList();
348+
fileDataList.sort((first, second) {
349+
final firstPath = first.file.path;
350+
final secondPath = second.file.path;
351+
if (omitSdkFiles) {
352+
final firstUri = first.uri;
353+
final secondUri = second.uri;
354+
final firstIsSdk = firstUri.isScheme('dart');
355+
final secondIsSdk = secondUri.isScheme('dart');
356+
if (firstIsSdk && !secondIsSdk) {
357+
return 1;
358+
} else if (!firstIsSdk && secondIsSdk) {
359+
return -1;
360+
} else if (firstIsSdk && secondIsSdk) {
361+
if ('$firstUri' == 'dart:core') {
362+
return -1;
363+
} else if ('$secondUri' == 'dart:core') {
364+
return 1;
365+
}
366+
}
367+
}
368+
return firstPath.compareTo(secondPath);
369+
});
327370

328371
// Ask ID for every file in the sorted order, so that IDs are nice.
372+
// Register objects that can be referenced.
373+
idProvider.resetRegisteredObject();
329374
for (final fileData in fileDataList) {
330375
final current = fileSystemState.getExisting(fileData.file);
331376
if (current != null) {
332-
idProvider.fileState(current);
377+
idProvider.registerFileState(current);
333378
final kind = current.kind;
334-
idProvider.fileStateKind(kind);
379+
idProvider.registerFileStateKind(kind);
335380
if (kind is LibraryFileStateKind) {
336-
idProvider.libraryCycle(kind.libraryCycle);
381+
idProvider.registerLibraryCycle(kind.libraryCycle);
337382
}
338383
}
339384
}
340385

341386
_writelnWithIndent('files');
342387
_withIndent(() {
343388
for (final fileData in fileDataList) {
389+
if (omitSdkFiles && fileData.uri.isScheme('dart')) {
390+
continue;
391+
}
344392
final file = fileData.file;
345393
_writelnWithIndent(_posixPath(file));
346394
_withIndent(() {
@@ -474,9 +522,10 @@ class AnalyzerStatePrinter {
474522
void _writeReferencingFiles(FileState file) {
475523
final referencingFiles = file.referencingFiles;
476524
if (referencingFiles.isNotEmpty) {
477-
// TODO(scheglov) Print space-separated.
478-
final fileIds =
479-
referencingFiles.map(idProvider.fileState).sorted(compareNatural);
525+
final fileIds = referencingFiles
526+
.map(idProvider.fileState)
527+
.sorted(compareNatural)
528+
.join(' ');
480529
_writelnWithIndent('referencingFiles: $fileIds');
481530
}
482531
}
@@ -511,16 +560,26 @@ class IdProvider {
511560
final Map<String, String> _shortToKey = {};
512561
final Map<String, String> _apiSignature = {};
513562

563+
Set<FileState> _currentFiles = {};
564+
Set<FileStateKind> _currentKinds = {};
565+
Set<LibraryCycle> _currentCycles = {};
566+
514567
String apiSignature(String signature) {
515568
final length = _apiSignature.length;
516569
return _apiSignature[signature] ??= 'apiSignature_$length';
517570
}
518571

519572
String fileState(FileState file) {
573+
if (!_currentFiles.contains(file)) {
574+
throw StateError('$file');
575+
}
520576
return _fileState[file] ??= 'file_${_fileState.length}';
521577
}
522578

523579
String fileStateKind(FileStateKind kind) {
580+
if (!_currentKinds.contains(kind)) {
581+
throw StateError('$kind');
582+
}
524583
return _fileStateKind[kind] ??= () {
525584
if (kind is AugmentationKnownFileStateKind) {
526585
return 'augmentation_${_fileStateKind.length}';
@@ -541,9 +600,42 @@ class IdProvider {
541600
}
542601

543602
String libraryCycle(LibraryCycle cycle) {
603+
if (!_currentCycles.contains(cycle)) {
604+
throw StateError('$cycle');
605+
}
544606
return _libraryCycle[cycle] ??= 'cycle_${_libraryCycle.length}';
545607
}
546608

609+
/// Register that [file] is an object that can be referenced.
610+
void registerFileState(FileState file) {
611+
if (_currentFiles.contains(file)) {
612+
throw StateError('Duplicate: $file');
613+
}
614+
_currentFiles.add(file);
615+
fileState(file);
616+
}
617+
618+
/// Register that [kind] is an object that can be referenced.
619+
void registerFileStateKind(FileStateKind kind) {
620+
if (_currentKinds.contains(kind)) {
621+
throw StateError('Duplicate: $kind');
622+
}
623+
_currentKinds.add(kind);
624+
fileStateKind(kind);
625+
}
626+
627+
/// Register that [cycle] is an object that can be referenced.
628+
void registerLibraryCycle(LibraryCycle cycle) {
629+
_currentCycles.add(cycle);
630+
libraryCycle(cycle);
631+
}
632+
633+
void resetRegisteredObject() {
634+
_currentFiles = {};
635+
_currentKinds = {};
636+
_currentCycles = {};
637+
}
638+
547639
String shortKey(String key) {
548640
var short = _keyToShort[key];
549641
if (short == null) {

0 commit comments

Comments
 (0)