Commit Graph

30 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
Sam Rawlins 360363efc1 Migrator: opt all test files _out_ of null safety.
Well not quite all; migration_cli_test tests are a bit of a different
beast so I am going to tackle them separately. But this fixes over
1000 test failures, and should clean up test output for further
debugging of the flip CL.

Each test file needs to be in a package with a package config file
which spells out that it is _opted out_ of null safety.

Bug: https://github.com/dart-lang/sdk/issues/43883
Change-Id: I583f66119df57031fd80824111923e15e0f91782
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/168900
Reviewed-by: Leaf Petersen <leafp@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
2020-10-23 21:07:04 +00:00
Sam Rawlins 475ffb96c0 Migrator: Use pedantic 1.9.0
Change-Id: Ia53e84ac6b2f3ab0679710c14453fd65c8f9c00d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/163161
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
2020-09-17 20:52:43 +00:00
Sam Rawlins 57ff9b73f9 Migrator: Support nullable function-typed parameters
Feature includes:
* function-typed formal parameters
* function-typed field formal parameters
* overridding parameters
* nullability hints
* adding `required`
* optional parameters

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

Change-Id: I1d9b1a50a9a6a77651fe6179b66c8301c8ab9408
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/146029
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
2020-05-05 20:03:07 +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 a2c9dc18d5 Migration: fix EditPlanner.addCommentPostfix when isInformative = true.
Previously, the spaces inserted by EditPlanner.addCommentPostfix
didn't carry the correct isInformative flag, so they would show up in
final migrated output.

Change-Id: I100ba88040ebff9ae5d406c49b242f6fb17ce7b8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/143887
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2020-04-20 12:43:46 +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 4747672929 Migration: fix preview output when accepting a /*late*/ hint.
Change-Id: I383be37cc86d3c83f5b2b332ae14ce35516f858d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/143563
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
2020-04-17 00:17:15 +00:00
Paul Berry fdc13e3f43 Migration: remove hints when performing migration.
Previously, if we migrated a code snippet like this:

  void f(int/*?*/ i) {
    /*late*/ int j = i/*!*/;
  }

the migration result would leave the hints and add the hinted text,
resulting in:

  void f(int?/*?*/ i) {
    /*late*/ late int j = i!/*!*/;
  }

We now simply remove the `/*` and `*/` surrounding each hint (with
whitespace corrections as needed), to produce:

  void f(int? i) {
    late int j = i!;
  }

Change-Id: Ic4e1216cb4693d73d71c9ab1a37d5cc15b8ef334
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/143460
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
2020-04-17 00:17:15 +00:00
Sam Rawlins e1822e9626 Migrator: Add new fix kinds: replaceVar, addType
Fixes #41410

Change-Id: I127d309e84423c24eb2563063af433187b9bf057
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/143222
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
2020-04-13 20:11:32 +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 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
Paul Berry e989e90346 Migration: avoid ambiguities when adding a cast after a function expression.
When adding a cast to a function expression like `() => null`, we need
to add parentheses, so that we produce `(() => null) as Type`, rather
than `() => null as Type`, which makes the cast appear to apply to the
function body expression.

Change-Id: Ib5ae93df5b840a832c10941e47fa092a4f764eea
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/134044
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
2020-02-03 19:59:38 +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 7467988bbc Migration: simplify edit plans for changing ?. into ..
I realized that I can use the same "pass through" infrastructure for
these kinds of edits.  This should help prepare for a future CL in
which we can aggregate together more complex changes, such as
introducing an "as" cast at the same time as changing `?.` into `.`.

Change-Id: Ie3af07a3563c44d62d4ed34d2a477eef6fae358f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/133540
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 850b89ff94 Migration: create wrapper methods for EditPlanner.surround
Addresses a concern from code review
https://dart-review.googlesource.com/c/sdk/+/129302 that
EditPlanner.surround is difficult to use correctly, by adding wrapper
methods for several common use cases that are easier to use correctly.
Also adapts fix_aggregator.dart to make use of the new methods.

EditPlanner.surround remains in the public API for EditPlanner because
there are still use cases for it that aren't supported by the wrapper
methods.  My hope is that the remaining use cases are either rare
enough or trivial enough that we don't need wrappers for them; if that
proves to be incorrect we can always add more wrapper methods.

Change-Id: Ia1d6cc8b10ffaedcf8803ff7c8f9460ada1d7c4e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/132462
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2020-01-21 15:58:02 +00:00
Paul Berry d9dd257265 Make it easier for tests based on AnalysisContextCollectionImpl to enable NNBD.
This CL allows tests based on AnalysisContextCollectionImpl to enable
NNBD by modifying the analysis options and introduce a different
feature set, rather than having to add virtual filesystem files to
convince the analyzer that NNBD is enabled for the package.  This is
more consistent with the way we enable NNBD for other unit tests that
invoke analysis more directly, and is less sensitive to details of how
the analyzer infers the current language version.

Currently used in the migration engine's EditPlanTest, which in a
subsequent CL will need this functionality to test editing source code
that contains null checks.

Change-Id: I352fd2d7f6ee71d42f02a31714e3395567967240
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/132461
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2020-01-21 15:58:02 +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 d1a7c25b38 Migration: test removal of all list elements when there is a trailing separator
Change-Id: I35e98c967b18b9585f0cd0e08af8957120a27320
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/131710
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
2020-01-15 13:00:53 +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
Paul Berry 2c121e4ecd Migration: modify pass through edit plans to integrate up the tree stepwise.
Consider a generic AST structure like this:

   A
  / \
 B   C
    / \
   D   E

Previously, if we wanted to build a "pass through" edit plan for A,
incorporating inner plans representing changes to be made to B, D, and
F, we would do the following: (1) pass each inner plan through the
_incorporateParent function, which might leave it unchanged or might
produce a plan targeted at the parent node, and then (2) ask each
inner plan for its set of changes, and produce a new plan for A
containing those changes.

The purpose of step (1) was to allow an edit plan to describe changes
that might affect the node above it.  For example, changing an
expression might cause the parentheses surrounding it to become
unnecessary, so those parentheses would be removed (that's the case
that's currently implemented).  Or, removing a list element from a
list might necessitate adjusting the comma before or after it, which
belongs to the parent node (this isn't implemented yet).

This had a design flaw: step (1) might produce overlapping changes.
For instance, calling _incorporateParent on the plans for D and E
might conceivably both plans for C, and those plans might try to make
overlapping (and thus incompatible) changes to node C.

It turns out that this isn't a problem for eliminating unnecessary
parens, because it's impossible for two edit plans to target two
different non-overlapping AST nodes whose parents are both the same
ParenthesizedExpression node (because a ParenthesizedExpression has
only one child).  But it will be a problem for removing elements
lists, statements from blocks, declarations from classes, etc.

The new strategy is that we will bubble inner plans up the tree one
level at a time.  In the example above, we would first combine the
inner plans for D and E into a pass through plan for C.  Then we would
combine the plans for B and C into a plan for A.  This is a bit more
work, but I believe it can be made efficient in typical use cases, and
it will avoid the problem described above because at each stage, all
the inner plans we combine will have the same parent, so it will be
tractable to figure out their interactions with each other.

Some vestiges of the old _incorporateParent approach still remain; I
plan to remove them in a follow-up CL.

Change-Id: I9ecbdb1549b949177ffc5d66447d4aede77e2dd9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/131400
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
2020-01-13 23:26:33 +00:00
Paul Berry 9b0cf08847 Split EditPlan into a base and derived class.
The derived class, NodeProducingEditPlan, represents an edit plan
whose effect will be to transform the source AST node into another AST
node (e.g. replace an expression with one of its subexpressions).  The
base class, EditPlan, will be able to represent more general kinds of
edit plans.

Future CLs will introduce edit plans that don't result in an AST node,
e.g. an edit plan that removes a source statement or a collection
element, or an edit plan that replaces one source statement or
collection element with several.

Change-Id: I01455f643b858c6500d864be679b12768029cffb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/130941
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2020-01-10 22:10:32 +00:00
Brian Wilkerson c75e2796f1 Make AtomicEdit concrete and remove subclasses
Change-Id: Iff7d591e8278cae074f76ca2e8cf561503bf4149
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/131120
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
2020-01-10 21:06:41 +00:00
Paul Berry ccca7a009c Migration: move finalize method into EditPlanner.
In upcoming CLs, I'm going to need access to the EditPlanner when
finalizing edit plans, and I'd rather do so by making finalize a
method on EditPlanner.  Doing so now to make future code reviews
simpler.

Change-Id: I6182e2224b548b2919f0c8f454f558b72c17e5d5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/130926
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2020-01-10 19:45:21 +00:00
Paul Berry cab2998a27 EditPlanner: add the ability to comment out code rather than removing it.
Change-Id: I6ba621d6482321104f6741772fef46753c599179
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/129803
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2020-01-09 15:59:36 +00:00
Paul Berry 73561e5a29 Create a factory interface for creating EditPlans: EditPlanner.
This factory class serves three purposes:

- In a future CL, it will contain fields to customize the global
  behavior of the edit plans that are created (e.g. whether code
  should be removed or commented out).

- In a future CL, it will contain a pointer to the original (text)
  contents of the source file.  This will allow edit plans to make
  more sophisticated changes such as adjusting indentation.

- In a future CL, it will contain methods for creating common kinds of
  "extract" edit plans such as null-checking an expression or making a
  type nullable.  (Currently the client must create these edit plans
  using EditPlanner.extract, which has an error-prone API).

Change-Id: I87ac75b73003d0e7b416e3d2121eadb2770da5ed
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/129802
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2020-01-09 05:22:57 +00:00
Paul Berry f525ddeaf3 Add/fix return types to NNBD migration tests.
This will be necessary in order to pass pedantic lints.  See
discussion in https://dart-review.googlesource.com/c/sdk/+/129325.

Change-Id: I61011a082a5401401fa1b7bb0d0bd7aed87a9d10
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/129800
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Auto-Submit: Paul Berry <paulberry@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2019-12-27 18:13:15 +00:00
Paul Berry 4c2f3669b2 Introduce EditPlan, a builder class for creating source edits with automatic parenthesis management.
Initially this will be used in the NNBD migration engine to produce
migration output.  Assuming it proves useful, in a future CL I'd like
to consider moving it to the analyzer_plugin package and using it as a
basic for all of the analysis server's refactorings.

Change-Id: I41ffc578ace3945fcfebb8eb824b6b5706dfba6c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/129302
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
2019-12-20 22:43:54 +00:00