Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit ca3ad26

Browse files
MichaelRFairhurstcommit-bot@chromium.org
authored andcommitted
[nnbd_migration] Report info building exceptions to server adapter
Change-Id: I4e3eef0d3e404bd090f36f8b2516abb97cc6c311 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/137520 Reviewed-by: Paul Berry <paulberry@google.com> Commit-Queue: Mike Fairhurst <mfairhurst@google.com>
1 parent 1cb188e commit ca3ad26

File tree

7 files changed

+79
-35
lines changed

7 files changed

+79
-35
lines changed

pkg/analysis_server/lib/src/edit/fix/non_nullable_fix.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class NonNullableFix extends FixCodeTask {
4646

4747
InstrumentationListener instrumentationListener;
4848

49+
NullabilityMigrationAdapter adapter;
50+
4951
NullabilityMigration migration;
5052

5153
/// If this flag has a value of `false`, then something happened to prevent
@@ -57,7 +59,8 @@ class NonNullableFix extends FixCodeTask {
5759
: includedRoot =
5860
_getIncludedRoot(included, listener.server.resourceProvider) {
5961
instrumentationListener = InstrumentationListener();
60-
migration = NullabilityMigration(NullabilityMigrationAdapter(listener),
62+
adapter = NullabilityMigrationAdapter(listener);
63+
migration = NullabilityMigration(adapter,
6164
permissive: _usePermissiveMode,
6265
instrumentation: instrumentationListener);
6366
}
@@ -76,7 +79,7 @@ class NonNullableFix extends FixCodeTask {
7679
migration.finish();
7780

7881
var state = MigrationState(
79-
migration, includedRoot, listener, instrumentationListener);
82+
migration, includedRoot, listener, instrumentationListener, adapter);
8083
await state.refresh();
8184

8285
server = HttpPreviewServer(state);

pkg/analysis_server/lib/src/edit/nnbd_migration/info_builder.dart

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:collection';
77
import 'package:analysis_server/src/analysis_server.dart';
88
import 'package:analysis_server/src/domains/analysis/navigation_dart.dart';
99
import 'package:analysis_server/src/edit/fix/dartfix_listener.dart';
10+
import 'package:analysis_server/src/edit/fix/non_nullable_fix.dart';
1011
import 'package:analysis_server/src/edit/nnbd_migration/instrumentation_information.dart';
1112
import 'package:analysis_server/src/edit/nnbd_migration/migration_info.dart';
1213
import 'package:analysis_server/src/edit/nnbd_migration/offset_mapper.dart';
@@ -41,6 +42,12 @@ class InfoBuilder {
4142
/// The listener used to gather the changes to be applied.
4243
final DartFixListener listener;
4344

45+
/// The dartfix adapter, which can be used to report exceptions that occur.
46+
final NullabilityMigrationAdapter adapter;
47+
48+
/// The [NullabilityMigration] instance for this migration.
49+
final NullabilityMigration migration;
50+
4451
/// A flag indicating whether types that were not changed (because they should
4552
/// be non-nullable) should be explained.
4653
final bool explainNonNullableTypes;
@@ -51,6 +58,7 @@ class InfoBuilder {
5158

5259
/// Initialize a newly created builder.
5360
InfoBuilder(this.provider, this.includedPath, this.info, this.listener,
61+
this.adapter, this.migration,
5462
// TODO(srawlins): Re-enable once
5563
// https://github.com/dart-lang/sdk/issues/40253 is fixed.
5664
{this.explainNonNullableTypes = false});
@@ -613,25 +621,31 @@ class InfoBuilder {
613621
}
614622
var info = edit.info;
615623
String explanation = info?.description?.appliedMessage;
624+
List<EditDetail> edits =
625+
info != null ? _computeEdits(info, sourceOffset) : [];
626+
List<RegionDetail> details;
616627
try {
617-
List<EditDetail> edits =
618-
info != null ? _computeEdits(info, sourceOffset) : [];
619-
List<RegionDetail> details = _computeDetails(edit);
620-
var lineNumber = lineInfo.getLocation(sourceOffset).lineNumber;
621-
if (explanation != null) {
622-
if (length > 0) {
623-
regions.add(RegionInfo(RegionType.remove, offset, length,
624-
lineNumber, explanation, details,
625-
edits: edits));
626-
} else {
627-
regions.add(RegionInfo(RegionType.add, offset, replacement.length,
628-
lineNumber, explanation, details,
629-
edits: edits));
630-
}
628+
details = _computeDetails(edit);
629+
} catch (e, st) {
630+
// TODO(mfairhurst): get the correct Source, and an AstNode.
631+
if (migration.isPermissive) {
632+
adapter.reportException(result.libraryElement.source, null, e, st);
633+
details = [];
634+
} else {
635+
rethrow;
636+
}
637+
}
638+
var lineNumber = lineInfo.getLocation(sourceOffset).lineNumber;
639+
if (explanation != null) {
640+
if (length > 0) {
641+
regions.add(RegionInfo(RegionType.remove, offset, length,
642+
lineNumber, explanation, details,
643+
edits: edits));
644+
} else {
645+
regions.add(RegionInfo(RegionType.add, offset, replacement.length,
646+
lineNumber, explanation, details,
647+
edits: edits));
631648
}
632-
} catch (e) {
633-
// TODO(mfairhurst): collect and recover instead of rethrowing.
634-
throw '$e $sourceOffset ${result.path}';
635649
}
636650
offset += replacement.length;
637651
}

pkg/analysis_server/lib/src/edit/nnbd_migration/migration_state.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analysis_server/src/edit/fix/dartfix_listener.dart';
6+
import 'package:analysis_server/src/edit/fix/non_nullable_fix.dart';
67
import 'package:analysis_server/src/edit/nnbd_migration/info_builder.dart';
78
import 'package:analysis_server/src/edit/nnbd_migration/instrumentation_listener.dart';
89
import 'package:analysis_server/src/edit/nnbd_migration/migration_info.dart';
@@ -15,6 +16,9 @@ class MigrationState {
1516
/// The migration associated with the state.
1617
final NullabilityMigration migration;
1718

19+
/// The adapter to dartfix for this migration.
20+
final NullabilityMigrationAdapter adapter;
21+
1822
/// The root directory that contains all of the files that were migrated.
1923
final String includedRoot;
2024

@@ -32,13 +36,13 @@ class MigrationState {
3236

3337
/// Initialize a newly created migration state with the given values.
3438
MigrationState(this.migration, this.includedRoot, this.listener,
35-
this.instrumentationListener);
39+
this.instrumentationListener, this.adapter);
3640

3741
/// Refresh the state of the migration after the migration has been updated.
3842
void refresh() async {
3943
OverlayResourceProvider provider = listener.server.resourceProvider;
40-
InfoBuilder infoBuilder = InfoBuilder(
41-
provider, includedRoot, instrumentationListener.data, listener);
44+
InfoBuilder infoBuilder = InfoBuilder(provider, includedRoot,
45+
instrumentationListener.data, listener, adapter, migration);
4246
Set<UnitInfo> unitInfos = await infoBuilder.explainMigration();
4347
var pathContext = provider.pathContext;
4448
migrationInfo = MigrationInfo(

pkg/analysis_server/test/src/edit/nnbd_migration/info_builder_test.dart

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -853,31 +853,28 @@ class C {
853853
edit: edits[0], offset: 42, length: 0, replacement: '@required ');
854854
}
855855

856-
@FailingTest(issue: 'https://dartbug.com/40773')
857856
Future<void> test_insertedRequired_parameter() async {
858857
UnitInfo unit = await buildInfoForSingleTestFile('''
859858
class C {
860-
int level;
859+
int level = 0;
861860
bool f({int lvl}) => lvl >= level;
862861
}
863862
''', migratedContent: '''
864863
class C {
865-
int? level;
866-
bool f({required int lvl}) => lvl >= level!;
864+
int level = 0;
865+
bool f({required int lvl}) => lvl >= level;
867866
}
868867
''');
869868
List<RegionInfo> regions = unit.fixRegions;
870-
expect(regions, hasLength(3));
871-
// regions[0] is the `int? s` fix.
872-
var region = regions[1];
869+
expect(regions, hasLength(1));
870+
var region = regions[0];
873871
var edits = region.edits;
874-
assertRegion(region: region, offset: 34, length: 9, details: [
872+
assertRegion(region: region, offset: 37, length: 9, details: [
875873
'This parameter is non-nullable, so cannot have an implicit default '
876874
"value of 'null'"
877875
]);
878876
assertEdit(
879-
edit: edits[0], offset: 33, length: 0, replacement: '@required ');
880-
// regions[2] is the `level!` fix.
877+
edit: edits[0], offset: 37, length: 0, replacement: '@required ');
881878
}
882879

883880
Future<void> test_insertParens() async {
@@ -1750,6 +1747,26 @@ class C {
17501747
assertDetail(detail: region.details[2], offset: 70, length: 3);
17511748
}
17521749

1750+
@FailingTest(issue: 'https://dartbug.com/40773')
1751+
Future<void> test_uninitializedMember() async {
1752+
UnitInfo unit = await buildInfoForSingleTestFile('''
1753+
class C {
1754+
int level;
1755+
}
1756+
''', migratedContent: '''
1757+
class C {
1758+
int? level;
1759+
}
1760+
''');
1761+
List<RegionInfo> regions = unit.fixRegions;
1762+
expect(regions, hasLength(1));
1763+
expect(regions[0].details, isNotEmpty);
1764+
// disabled so that it won't interfere with @FailingTest annotation.
1765+
//assertRegion(region: regions[0], offset: 15, length: 1, details: [
1766+
// 'This field is not initialized and is therefore made nullable'
1767+
//]);
1768+
}
1769+
17531770
Future<void> test_uninitializedVariable_notLate_uninitializedUse() async {
17541771
UnitInfo unit = await buildInfoForSingleTestFile('''
17551772
void f() {

pkg/analysis_server/test/src/edit/nnbd_migration/nnbd_migration_test_base.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ class NnbdMigrationTestBase extends AbstractAnalysisTest {
7878
// Run the migration engine.
7979
DartFixListener listener = DartFixListener(server);
8080
InstrumentationListener instrumentationListener = InstrumentationListener();
81-
NullabilityMigration migration = NullabilityMigration(
82-
NullabilityMigrationAdapter(listener),
81+
NullabilityMigrationAdapter adapter = NullabilityMigrationAdapter(listener);
82+
NullabilityMigration migration = NullabilityMigration(adapter,
8383
permissive: false,
8484
instrumentation: instrumentationListener,
8585
removeViaComments: removeViaComments);
@@ -101,7 +101,7 @@ class NnbdMigrationTestBase extends AbstractAnalysisTest {
101101
// Build the migration info.
102102
InstrumentationInformation info = instrumentationListener.data;
103103
InfoBuilder builder = InfoBuilder(
104-
resourceProvider, includedRoot, info, listener,
104+
resourceProvider, includedRoot, info, listener, adapter, migration,
105105
explainNonNullableTypes: true);
106106
infos = await builder.explainMigration();
107107
}

pkg/nnbd_migration/lib/nnbd_migration.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ abstract class NullabilityMigration {
143143
NullabilityMigrationInstrumentation instrumentation,
144144
bool removeViaComments}) = NullabilityMigrationImpl;
145145

146+
/// Check if this migration is being run permissively.
147+
bool get isPermissive;
148+
146149
void finalizeInput(ResolvedUnitResult result);
147150

148151
void finish();

pkg/nnbd_migration/lib/src/nullability_migration_impl.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ class NullabilityMigrationImpl implements NullabilityMigration {
7474
_postmortemFileWriter?.graph = _graph;
7575
}
7676

77+
@override
78+
bool get isPermissive => _permissive;
79+
7780
@override
7881
void finalizeInput(ResolvedUnitResult result) {
7982
if (!_propagated) {

0 commit comments

Comments
 (0)