Commit Graph

40 Commits

Author SHA1 Message Date
Paul Berry 0e8a84173e Migration: fix futures using .then rather than as
Previously, if the migration tool encountered a Future expression with
a bad type (e.g. a Future<String?> where a Future<String> was needed),
it would "fix" the problem by introducing a cast.  That is nearly
always the wrong thing to do; what we want to do is null check the
value that the future *completes* with.

This CL changes the migration tool so that it fixes this case by
appending `.then((value) => value!)` to the future expression.

Fixes #45472.

Bug: https://github.com/dart-lang/sdk/issues/45472
Change-Id: I7a35b54f673936e2e4b0f8f3a077ba8bf684b4eb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/193700
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2021-03-31 17:48:19 +00:00
Konstantin Shcheglov 875af7c991 Deprecated TypeProvider.futureType2/listType2/etc
Change-Id: Ia994be1e29b4d96b89d714beafcb9256a0cb4de6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/184641
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
2021-02-12 17:26:07 +00:00
Sam Rawlins 4097d3024a Migrator: Insert required keyword in correct position
Previously, migration might change:

m({@required @deprecated x}) {}

to

m({required @deprecated x}) {}

Which is illegal syntax.

Change-Id: Ib8397e9e22dbb951758a04c085c40905860a7cb3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/170026
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
2020-11-02 23:40:25 +00:00
Paul Berry 65eb643181 Migration: don't produce null!.
Instead, produce `null /*no valid migration*/`.  This should lead to
less user confusion (`null!` seems crazy, since obviously a null
literal will fail a `!` check).  Note that this will be a static
error, which will give the user a better chance of noticing and fixing
the problem.

Fixes #43972.

Bug: https://github.com/dart-lang/sdk/issues/43972
Change-Id: Ibf83b786dc486b14c001c17ca2ba902dafcb8a18
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/169600
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2020-10-29 18:34:36 +00:00
Paul Berry a666ac7ffa Migration: add preview info when changing iterable method calls.
With this change, the functionality of changing iterable method calls
such as `.firstWhere` into extension method calls such as
`.firstWhereOrNull` is working well enough that I think we can switch
it on.

We don't update the pubspec properly yet; that will be addressed in
follow-up CLs.

Change-Id: I758332e9752b12d399ecb5c70cf37bbc8da77d6c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/168988
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
2020-10-25 18:57:35 +00:00
Paul Berry f1843b0abe Add special case migration logic for iterator methods.
This logic allows us to migrate code like this:

    int firstEven(Iterable<int> values)
        => values.firstWhere((i) => i.isEven, orElse: () => null);

Into:

    import 'package:collection/collection.dart' show IterableExtension;

    int? firstEven(Iterable<int> values)
        => values.firstWhereOrNull((i) => i.isEven);

Rather than the default behavior, which would migrate it to:

    int? firsteven(Iterable<int?> values)
        => values.firstWhere((i) => i!.isEven, orElse: () => null);

(The new migration is far superior because it doesn't require the
input iterable to accept null).

This functionality is disabled at the moment, because:
- The changes it makes don't show up properly in the web preview.
- The pubspec is not yet properly updated.

I will address these issues in follow-up CLs and then enable the
feature.

Change-Id: I96f3b12d682c586631920b38406bad6aa3f4789e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/168500
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2020-10-20 17:45:04 +00:00
Konstantin Shcheglov 0cc487a00e Stop using package:analyzer/analyzer.dart in migration test.
R=paulberry@google.com

Change-Id: I47ac999b0912d14f17a16da88a48816f65b4c5f9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/159973
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
2020-08-25 02:20:17 +00:00
Sam Rawlins 562b11a4d8 Migrator: Add types to implicitly-typed field formal parameters
Fixes https://github.com/dart-lang/sdk/issues/38453

Change-Id: I56474071fcc7d408487fc74bd76d7520022f0259
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/148643
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
2020-05-20 22:29:14 +00:00
Sam Rawlins a432ca0f8c Migrator: Improve adding explicit types to parameters and variable declarations
While working on #38453, I encountered these issues.

Change-Id: I6abbfca19d9f6de771181e7f5515681cbbc39a4e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/148080
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
2020-05-14 17:32:01 +00:00
Sam Rawlins 1279bbf0f1 Migrator: Always remove @ from @required
Commenting out the @ (producing `/* @*/ required`) does no one good. It
does not help weak-mode or strong-mode testing.

Fixes https://github.com/dart-lang/sdk/issues/41181

Change-Id: I9de2775e2a17bacdd30cbfb34dc095cf2b5c603f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/147040
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
2020-05-07 14:47:07 +00:00
Paul Berry 11e5a05a70 Migration: when removing dead code, remove unnecessay ??=s
Fixes #38676.

Change-Id: Icc24c6f070d7f3e8a74fc9494aef3e4c2583fac4
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/146320
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
2020-05-05 02:18:13 +00:00
Paul Berry b71d09542c Migration: extend compound assignment diagnostics to ++ and --.
Bug: https://github.com/dart-lang/sdk/issues/38676
Change-Id: Iab5097359d8de7aa9411f51ee0ec6212c3adb678
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/145920
Reviewed-by: Samuel Rawlins <srawlins@google.com>
2020-05-04 23:02:17 +00:00
Paul Berry bf56b2ca2c Migration: produce diagnostic information for assignments.
We produce a diagnostic under the following conditions:

- For compound assignments, if the type read from the LHS is nullable
  (this is illegal after NNBD, and requires user intervention to fix).

- For compound assignments, if the type returned from the combiner is
  not assignable to the LHS (this was required prior to NNBD, but it
  is a stricter condition after migration, both because the LHS might
  have a non-nullable type, and because implicit downcasts are not
  allowed).

- For null-aware assignments, if the type read from the LHS is
  non-nullable (this indicates that once strong mode is enabled, the
  assignment will be dead code).

Bug: https://github.com/dart-lang/sdk/issues/38676
Change-Id: Icb242ba36437e38364ada069880831eb05e3a513
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/145664
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
2020-05-01 22:54:25 +00:00
Paul Berry d52dc104d4 Migration: instead of removing weak-only code, warn about it.
Previously we considered such code "dead", but it's not really dead
until Dart 3.0.  Removing it changes weak mode semantics, and we don't
want to do that automatically.

Fixes #41231

Change-Id: I6a88f016b4f3fcba160dcb738558867219715cbb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/143941
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2020-04-17 17:27:59 +00:00
Paul Berry 1d3105e0b3 Migration: prioritize changes differently if they're due to hints.
Change-Id: I4dfdd0287e488dfac21923c71741d2218a2c070c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/143334
Reviewed-by: Samuel Rawlins <srawlins@google.com>
2020-04-14 23:33:46 +00:00
Mike Fairhurst 2eaa5bcec6 [nnbd_migration] Handle removing language comments after other comments
Change-Id: Iecee603fbef5ff2d0f822728b9ae0d83967afc21
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/140770
Commit-Queue: Mike Fairhurst <mfairhurst@google.com>
Reviewed-by: Janice Collins <jcollins@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
2020-03-26 03:47:57 +00:00
Paul Berry bdf8e7b27e Migration: handle introduction of both ! and as on a single expression.
Fixes #40016.

Bug: https://github.com/dart-lang/sdk/issues/40016
Change-Id: I54047d843d5d1670876a5827276899b1a56dcc65
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/140905
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2020-03-25 19:38:34 +00:00
Paul Berry 244b4247e9 Migration: better description text for edges.
Change-Id: I58651c927a741721283c3c4c951673a86aa7a1b3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/140883
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
2020-03-25 19:38:34 +00:00
Mike Fairhurst 799560bff7 [nnbd_migration] Remove language version comments during migrate
Bug: 41095
Change-Id: I3262d6da485d78fee1e2dfe4ac22692c5e4b6daf
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/140369
Reviewed-by: Janice Collins <jcollins@google.com>
Commit-Queue: Mike Fairhurst <mfairhurst@google.com>
2020-03-24 16:44:05 +00:00
Paul Berry f1e98b84f9 Migration: use prefixes when introducing "as" expressions
Change-Id: I0f90dc4707cae35b04df7388a687895b80996fa2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/140364
Reviewed-by: Samuel Rawlins <srawlins@google.com>
2020-03-20 23:50:50 +00:00
Paul Berry 41b67a9b52 Migration: add grayed-out spaces to indicate types not being made nullable.
Change-Id: I4fa076693c25cf9466d33332030df3983043ffd1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/139940
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
2020-03-18 21:18:22 +00:00
Paul Berry 2ec127a305 Migration: give nullability nodes human-readable names.
Change-Id: I86b510c6cc9afacb7d720af214c8f4e5a311c9ff
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/138885
Reviewed-by: Samuel Rawlins <srawlins@google.com>
2020-03-11 15:20:59 +00:00
Paul Berry fa38644450 Migration: handle dead code removal in awkward places.
Usually when migration removes dead code it can just delete it
(e.g. because it's removing a statement from a block and blocks can
contain any number of statements).  However, in certain contexts dead
code must be replaced with something else, such as an empty statement.

Change-Id: I65faf017897215d750395dc78a2bf8a157d8dce0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/136441
Reviewed-by: Janice Collins <jcollins@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2020-02-19 17:39:44 +00:00
Mike Fairhurst 0216ac8558 [nnbd_migration] Fix crash aggregating fixes on cascade property target
The target of a property access can be null if it is a cascade. If that
access is then modified, fix aggregator gets a NPE. This issue is not
true for index & method calls, same fix applies.

Change-Id: I98eac3c8f7eba5300b30ff366ee227430726440c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/135963
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Mike Fairhurst <mfairhurst@google.com>
2020-02-14 21:45:47 +00:00
Paul Berry a017944035 Migration: fix hard crash in trial_migration when migrating package:async.
There were two problems:

1. An info object isn't being properly passed to
   NodeChangeForExpression, causing an assertion failure during the
   FixAggregator stage.  I've modified the assertion check so that it
   happens earlier, during the FixBuilder, so that the exception is
   caught by permissive mode logic and doesn't cause a hard crash.
   I'll work on fixing the assertion in a follow-up CL.

2. The hack described in https://github.com/dart-lang/sdk/issues/40536
   was causing a bogus assertion failure in
   _PassThroughBuilderImpl._checkParenLogic.  I've weakened the
   assertion slightly so that for now, the hack won't cause trouble.
   See the issue for more information about what's happening--this
   should be cleaned up eventually but it's probably not high
   priority.

Bug: https://github.com/dart-lang/sdk/issues/40533
Change-Id: Iee832fbd07e54b02d2c52f79a097ff9d973cda9a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/134980
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2020-02-10 20:19:54 +00:00
Konstantin Shcheglov 05dcf371e7 Remove 'withNullability' from TypeImpl.toString()
Bug: https://github.com/dart-lang/sdk/issues/40460
Change-Id: Icd967df0252a9fd7ff9941ca7158b7df0190c961
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/134384
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
2020-02-04 21:19:06 +00:00
Paul Berry 0ca9e8522e Migration: add explicit types to variable declarations when needed.
Partly addresses https://github.com/dart-lang/sdk/issues/39404.

Change-Id: Id8139b18bf8339df0eead48cc8648cb5f030acf0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/133741
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2020-01-29 18:30:46 +00:00
Paul Berry 208d8c2e52 Migration: Rework FixAggregator to reduce order dependencies.
Previously, FixAggregator provided one NodeChange-derived class for
each possible kind of fix; if multiple fixes had to be made to a
single AST node, its client (FixBuilder) was required to nest them in
the appropriate order.  FixBuilder accomplished this by carefully
controlling the order in which it decided on fixes.  Unfortunately,
this careful control of order is becoming burdensome for FixBuilder to
maintain.

With this change, the class hierarchy of NodeChange mirrors the class
hierarchy of AstNode, with different fields for each possible kind of
fix.  This means that FixBuilder can decide on fixes in whatever order
is convenient.

Change-Id: I1c79cfc37b6289051b3927c67867b77a1dbb5ebf
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/133541
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
2020-01-29 18:30:46 +00:00
Paul Berry 2edd8d17c0 Migration: replace ?. with . where appropriate.
Change-Id: Id32f60caec0c25099704652c03fafc8b184bf4d8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/133241
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2020-01-24 19:58:43 +00:00
Paul Berry 48aa6470ca Migration: plumb more fix reasons through FixBuilder to instrumentation.
Now there are only two circumstances in which an AtomicEdit won't have
information about the reason for the change:

- The AtomicEdit object was associated with adding or removing parens,
  in which case we don't compute information about the reason for the
  change because it's not straightforward to compute, and it should be
  clear to the user anyway.

- Information about the reason for the change hasn't been plumbed
  through properly (this should be addressed in follow-up CLs).

Since we are rapidly approaching a situation where nearly all
AtomicEdits will have reason information, the separation between
AtomicEdit and AtomicEditWithInfo now seems silly, so I just moved the
`info` object into AtomicEdit and removed AtomicEditWithInfo entirely.

Change-Id: I61afd9cd58b71d4695685e65d0142a2693b8e6b2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/132451
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2020-01-21 15:58:02 +00:00
Paul Berry 31bfacfda4 Migration: change API to make use of FixBuilder's "changes" data structure.
The new FixBuilder component of the migration engine communicates the
changes it produces using a "changes" data structure of type Map<int,
List<AtomicEdit>>, and is not compatible with the old
SingleNullabilityFix class (which made the incorrect assumption that
each SourceEdit was associated with a single fix).  This CL changes
the migration engine's instrumentation API to use this data structure,
and changes its listener API to avoid reference to the
SingleNullabilityFix class.  This allows SingleNullabilityFix to be
deleted.

This required changing AtomicEditWithReason so that it can hold
multiple fix reasons, and a NullabilityFixDescription.  It does so
using a new object, AtomicEditInfo, and AtomicEditWithReason is
renamed to AtomicEditWithInfo.

In addition, the old FixInfo class is removed, since it carries the
same information as the new AtomicEditWithInfo class.

This required deleting the "incremental workflow" functionality from
UnitRenderer.  This functionality was disabled, and I believe it
wouldn't have worked anyway (since it, too, made the incorrect
assumption that each SourceEdit was associated with a single fix).

Change-Id: Id965cfd803b408c66f15436017b87fc3e46521c6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/132306
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2020-01-19 16:50:01 +00:00
Paul Berry 5dff804997 Migration: handle code changes within a non-dead branch of an if.
The FixBuilder implemenation is now at feature parity with the
non-FixBuilder implementation when testing with api_test.dart, except
for one test of extension functionality (which doesn't seem worth
fixing right now, since extensions are still so rarely used).

Change-Id: I89df7ec388824fa9f4181706bfadbaeb232d2b5f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/132169
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
2020-01-17 20:47:16 +00:00
Paul Berry f656cad5f4 Migration: add support for converting @required to required.
Support only exists in the FixBuilder.

Change-Id: I921bd40058fb3b10321fbccf8eae61e02473ff4e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/132220
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
2020-01-17 20:47:16 +00:00
Paul Berry a57350be7b Migration: Add logic to FixAggregator to eliminate dead "if" branches.
Change-Id: Icfe62535f588c691086835ae22c80b49191be514
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/131581
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
2020-01-14 20:41:23 +00:00
Paul Berry 1eb5f10c27 Migration: add the ability to create EditPlans that remove source code.
This will allow for dead code elimination.

Change-Id: I128dddcfe5e83dde351512ebffb14ea7088c8b9e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/131540
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
2020-01-14 20:41:23 +00:00
Paul Berry 6bee2660eb Migration: Remove _incorporateParent method.
We no longer need this--all of its functionality is now implemented in
EditPlanner.passThrough.

Change-Id: I54c1b22fed99443b8ca3844bea1903c48a5f36eb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/131460
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
2020-01-14 20:41:23 +00:00
Brian Wilkerson c8f2683ebc Add a reason to the MakeNullable change and plumb it through to the AtomicEdit
Change-Id: I5a0a258b07e12fa9a43efc219c1af0c021380716
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/130939
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
2020-01-13 21:20:58 +00:00
Paul Berry 1317212b22 Migration: teach FixBuilder to add the required keyword when necessary.
Change-Id: Ifb20e60f1ea8578bcdcfe503582902734d56b881
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/129326
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
2019-12-28 23:50:56 +00:00
Paul Berry 20711a29ef Fix return types in fix_aggregator_test.dart.
The test methods in this test file were declared to return `void`, but
a more accurate return type is actually `Future<void>`.

Change-Id: I31e3ab7e2ad9cb81b07a9f8c7cec84fad9f99f80
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/129325
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2019-12-26 15:35:59 +00:00
Paul Berry 8ce84595c5 Introduce a FixAggregator.
FixAggregator will form the last stage of the migration engine, taking
the output of FixBuilder (which is a map from AST nodes to individual
changes) and feeding it into the EditPlan infrastructure to produce
the complete set of edits for any given file.

In a follow-up CL, I will modify FixBuilder to make use of the
NodeChange classes defined here.

Change-Id: I73fd67d7e989194049d91f978a7d9134814d3265
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/129304
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
2019-12-22 15:16:16 +00:00