Skip to content

Migration tool gets distracted by assert functions #42380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
a14n opened this issue Jun 17, 2020 · 2 comments
Closed

Migration tool gets distracted by assert functions #42380

a14n opened this issue Jun 17, 2020 · 2 comments
Assignees
Labels
area-migration (deprecated) Deprecated: this label is no longer actively used (was: issues with the `dart migrate` tool). NNBD Issues related to NNBD Release nnbd-migration-correctness-example Concrete examples of the migration engine producing an incorrect result on a phase 1 package

Comments

@a14n
Copy link
Contributor

a14n commented Jun 17, 2020

With the following code:

int get i {
  int result;
  assert(() {
    if (false) {
      result = 1;
    }
    return true;
  }());
  if (result == null) {
    throw Error();
  }
  return result;
}

the migration tool suggests:

int? get i {
  int? result;
  assert(() {
    if (false) {
      result = 1;
    }
    return true;
  }());
  if (result == null) {
    throw Error();
  }
  return result;
}

but the return type of the method should be int instead of int? because of the null gard at the end.

Without assert the migration tool works correctly and suggest:

int  get i {
  int? result;
  // assert(() {
  //   if (false) {
  //     result = 1;
  //   }
  //   return true;
  // }());
  if (result == null) {
    throw Error();
  }
  return result;
}
@a14n
Copy link
Contributor Author

a14n commented Jun 17, 2020

/cc @stereotype441

@stereotype441 stereotype441 added analyzer-nnbd-migration legacy-area-analyzer Use area-devexp instead. NNBD Issues related to NNBD Release nnbd-migration-correctness-example Concrete examples of the migration engine producing an incorrect result on a phase 1 package labels Jun 17, 2020
@MichaelRFairhurst MichaelRFairhurst self-assigned this Jun 19, 2020
@MichaelRFairhurst
Copy link
Contributor

Simpler repro:

int? get i {
  int? result;
  () {
    result = 1;
  };
  if (result == null) {
    throw Error();
  }
  return result;
}

What's happening here is that type promotion doesn't work for variables that are referenced in anonymous functions. So I think this is WAI, should be resolved by adding a hint.

int /*!*/ get i {
  int? result;
  () {
    result = 1;
  };
  if (result == null) {
    throw Error();
  }
  return result!;
}

(notice, for instance, the ! on return result! to manually de-null the type since type promotion won't do so)

A last additional note: we have considered that flow analysis is underpowered for the migration itself, because its overly conservative. The classic example is fields. #40566 is the issue for us to create our own more aggressive flow analysis that does things the default language spec one cannot. However, the main motivating case for this is fields, which are essentially a very subtle soundness problem. I don't believe we can really confidently say the same of function literals. I think those are more not handled due to an inherent essential complexity.

So potentially we could handle some limited aspect of this, such as the fact that the original example inclused a self executing anonymous function? So I'll close this but link to it from that issue.

In terms of usability, it also would be nice to better describe these things. We have an issue for that in terms of the dart analyzer itself, and if we solve that, it would be nice to try to apply that same info to the nullability trace view. #38773

@stereotype441 stereotype441 added area-migration (deprecated) Deprecated: this label is no longer actively used (was: issues with the `dart migrate` tool). and removed analyzer-nnbd-migration legacy-area-analyzer Use area-devexp instead. labels Nov 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-migration (deprecated) Deprecated: this label is no longer actively used (was: issues with the `dart migrate` tool). NNBD Issues related to NNBD Release nnbd-migration-correctness-example Concrete examples of the migration engine producing an incorrect result on a phase 1 package
Projects
None yet
Development

No branches or pull requests

3 participants