Skip to content

'dart migrate' throws Exception: type '_NullAwareAccessContext<PromotableElement, DartType>' is not a subtype of type '_TryContext<PromotableElement, DartType>' in type cast #44349

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
knaeckeKami opened this issue Nov 30, 2020 · 11 comments
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 P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@knaeckeKami
Copy link
Contributor

When I run 'dart migrate' on a small Flutter project, I get this output with an Exception thrown:

Migrating /Users/kami/development/getx_benchmark

See https://dart.dev/go/null-safety-migration for a migration guide.

Analyzing project...
[------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------]No analysis issues found.

Generating migration suggestions...
[-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\                                    ]Aborting migration due to an exception.  This most likely is due to a
bug in the migration tool.  Please consider filing a bug report at:

https://github.com/dart-lang/sdk/issues/new
To attempt to perform migration anyway, you may re-run with
--ignore-exceptions.

Exception details:

type '_NullAwareAccessContext<PromotableElement, DartType>' is not a subtype of type '_TryContext<PromotableElement, DartType>' in type cast

#0      _FlowAnalysisImpl.tryCatchStatement_bodyEnd (package:_fe_analyzer_shared/src/flow_analysis/flow_analysis.dart:3222:21)
#1      ResolverVisitor.visitTryStatement (package:analyzer/src/generated/resolver.dart:1921:12)
#2      TryStatementImpl.accept (package:analyzer/src/dart/ast/ast.dart:9934:49)
#3      NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:7544:20)
#4      BlockImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:1073:17)
#5      ResolverVisitor.visitNode (package:analyzer/src/generated/resolver.dart:1691:10)
#6      UnifyingAstVisitor.visitBlock (package:analyzer/dart/ast/visitor.dart:3143:31)
#7      ScopedVisitor.visitBlock.<anonymous closure> (package:analyzer/src/generated/resolver.dart:2412:13)
#8      ScopedVisitor._withDeclaredLocals (package:analyzer/src/generated/resolver.dart:3009:8)
#9      ScopedVisitor.visitBlock (package:analyzer/src/generated/resolver.dart:2411:5)
#10     ScopedVisitor.visitStatementInScope (package:analyzer/src/generated/resolver.dart:2903:7)
#11     ResolverVisitor.visitDoStatementInScope (package:analyzer/src/generated/resolver.dart:1208:5)
#12     ScopedVisitor.visitDoStatement (package:analyzer/src/generated/resolver.dart:2555:7)
#13     DoStatementImpl.accept (package:analyzer/src/dart/ast/ast.dart:3095:49)
#14     NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:7544:20)
#15     BlockImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:1073:17)
#16     ResolverVisitor.visitNode (package:analyzer/src/generated/resolver.dart:1691:10)
#17     UnifyingAstVisitor.visitBlock (package:analyzer/dart/ast/visitor.dart:3143:31)
#18     ScopedVisitor.visitBlock.<anonymous closure> (package:analyzer/src/generated/resolver.dart:2412:13)
#19     ScopedVisitor._withDeclaredLocals (package:analyzer/src/generated/resolver.dart:3009:8)
#20     ScopedVisitor.visitBlock (package:analyzer/src/generated/resolver.dart:2411:5)
#21     BlockImpl.accept (package:analyzer/src/dart/ast/ast.dart:1069:49)
#22     BlockFunctionBodyImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:1028:13)
#23     ResolverVisitor.visitNode (package:analyzer/src/generated/resolver.dart:1691:10)
#24     UnifyingAstVisitor.visitBlockFunctionBody (package:analyzer/dart/ast/visitor.dart:3146:55)
#25     ScopedVisitor.visitBlockFunctionBody (package:analyzer/src/generated/resolver.dart:2421:13)
#26     ResolverVisitor.visitBlockFunctionBody (package:analyzer/src/generated/resolver.dart:903:13)
#27     BlockFunctionBodyImpl.accept (package:analyzer/src/dart/ast/ast.dart:1024:49)
#28     MethodDeclarationImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:6976:12)
#29     ResolverVisitor.visitNode (package:analyzer/src/generated/resolver.dart:1691:10)
#30     UnifyingAstVisitor.visitMethodDeclaration (package:analyzer/dart/ast/visitor.dart:3351:55)
#31     ScopedVisitor.visitMethodDeclarationInScope (package:analyzer/src/generated/resolver.dart:2859:11)
#32     ScopedVisitor.visitMethodDeclaration (package:analyzer/src/generated/resolver.dart:2852:7)
#33     ResolverVisitor.visitMethodDeclaration (package:analyzer/src/generated/resolver.dart:1610:11)
#34     MethodDeclarationImpl.accept (package:analyzer/src/dart/ast/ast.dart:6967:49)
#35     NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:7544:20)
#36     ScopedVisitor.visitClassMembersInScope (package:analyzer/src/generated/resolver.dart:2482:18)
#37     ScopedVisitor.visitClassDeclaration (package:analyzer/src/generated/resolver.dart:2463:7)
#38     ResolverVisitor.visitClassDeclaration (package:analyzer/src/generated/resolver.dart:961:13)
#39     ClassDeclarationImpl.accept (package:analyzer/src/dart/ast/ast.dart:1521:49)
#40     ResolverVisitor.visitCompilationUnit (package:analyzer/src/generated/resolver.dart:1037:23)
#41     CompilationUnitImpl.accept (package:analyzer/src/dart/ast/ast.dart:2112:49)
#42     FixBuilder.visitAll.<anonymous closure> (package:nnbd_migration/src/fix_builder.dart:207:14)
#43     _rootRun (dart:async/zone.dart:1190:13)
#44     _CustomZone.run (dart:async/zone.dart:1093:19)
#45     _runZoned (dart:async/zone.dart:1630:10)
#46     runZoned (dart:async/zone.dart:1550:10)
#47     NullSafetyUnderstandingFlag.enableNullSafetyTypes (package:analyzer/dart/element/null_safety_understanding_flag.dart:42:12)
#48     FixBuilder.visitAll (package:nnbd_migration/src/fix_builder.dart:204:35)
#49     NullabilityMigrationImpl.finalizeInput (package:nnbd_migration/src/nullability_migration_impl.dart:175:18)
#50     NonNullableFix.finalizeUnit (package:nnbd_migration/src/front_end/non_nullable_fix.dart:112:15)
#51     _FixCodeProcessor.runLaterPhases.<anonymous closure> (package:nnbd_migration/migration_cli.dart:1138:21)
#52     _FixCodeProcessor.runLaterPhases.<anonymous closure> (package:nnbd_migration/migration_cli.dart:1135:28)
#53     _FixCodeProcessor.processResources (package:nnbd_migration/migration_cli.dart:1057:30)
<asynchronous suspension>
#54     _FixCodeProcessor.runLaterPhases (package:nnbd_migration/migration_cli.dart:1135:5)
<asynchronous suspension>
#55     MigrationCliRunner.run (package:nnbd_migration/migration_cli.dart:699:24)
<asynchronous suspension>
#56     MigrateCommand.run (package:nnbd_migration/migration_cli.dart:209:7)
<asynchronous suspension>
#57     CommandRunner.runCommand (package:args/command_runner.dart:197:13)
<asynchronous suspension>
#58     DartdevRunner.runCommand (package:dartdev/dartdev.dart:249:24)
<asynchronous suspension>
#59     runDartdev (package:dartdev/dartdev.dart:112:14)
<asynchronous suspension>
#60     main (file:///opt/s/w/ir/cache/builder/src/third_party/dart/pkg/dartdev/bin/dartdev.dart:11:3)
<asynchronous suspension>
dart --version
Dart SDK version: 2.12.0-29.10.beta (beta) (Tue Nov 17 10:50:22 2020 +0100) on "macos_x64"

How to reproduce

  • git clone -b nullsafety --single-branch https://github.com/knaeckeKami/changenotifier_benchmark.git
  • cd changenotifier_benchmark/
  • dart pub get
  • dart migrate
@stereotype441 stereotype441 added the area-migration (deprecated) Deprecated: this label is no longer actively used (was: issues with the `dart migrate` tool). label Dec 1, 2020
@stereotype441
Copy link
Member

@knaeckeKami thanks for the bug report! I was able to reproduce your bug with changenotifier_benchmark as of knaeckeKami/changenotifier_benchmark@4679f8f. To help us prioritize, were you able to make reasonable progress using the --ignore-exceptions flag?

@stereotype441 stereotype441 added the P1 A high priority bug; for example, a single project is unusable or has many test failures label Dec 1, 2020
@knaeckeKami
Copy link
Contributor Author

Yes, with --ignore-exceptions it worked almost perfectly (with some small manual corrections needed, not sure if related to the exception).

I had issues with:

migration of

 List<_Listener> _listeners = List<_Listener>();

to

 List<_Listener>? _listeners = List<_Listener>();

(error: The default 'List' constructor isn't available when null safety is enabled. (default_list_constructor at [getx_benchmark] lib/notifiers/clever_value_notifier.dart:17))

(Multiple instances of this error, but easily fixed)


Migration of

 bold(resultsByApproach[approach]
                  .map((e) => e.time)
                  .fold(0, (a, b) => a + b)
                  .toString())

to

bold(resultsByApproach[approach]!
                  .map(((e) => e.time) as _ Function(TestResult))
                  .fold(0, ((dynamic a, b) => a + b) as _ Function(_, dynamic))
                  .toString())

With error: Undefined class '_'. (undefined_class at [getx_benchmark] lib/print_table.dart:77), seem like type inference failed there

@stereotype441
Copy link
Member

Yes, with --ignore-exceptions it worked almost perfectly (with some small manual corrections needed, not sure if related to the exception).

Ok, good to know, thanks.

I had issues with:

migration of

 List<_Listener> _listeners = List<_Listener>();

to

 List<_Listener>? _listeners = List<_Listener>();

(error: The default 'List' constructor isn't available when null safety is enabled. (default_list_constructor at [getx_benchmark] lib/notifiers/clever_value_notifier.dart:17))

(Multiple instances of this error, but easily fixed)

Glad this was easily fixed. FYI, we're hoping to improve the migration tool's handling of the List constructor prior to the stable release - see #44198.

Migration of

 bold(resultsByApproach[approach]
                  .map((e) => e.time)
                  .fold(0, (a, b) => a + b)
                  .toString())

to

bold(resultsByApproach[approach]!
                  .map(((e) => e.time) as _ Function(TestResult))
                  .fold(0, ((dynamic a, b) => a + b) as _ Function(_, dynamic))
                  .toString())

With error: Undefined class '_'. (undefined_class at [getx_benchmark] lib/print_table.dart:77), seem like type inference failed there

Interesting: that's not one I've seen before. I filed #44357 to track it.

@franklinyow franklinyow added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) NNBD Issues related to NNBD Release labels Dec 1, 2020
@knaeckeKami
Copy link
Contributor Author

knaeckeKami commented Dec 2, 2020

Actually, there's another issue :)

The tool migrated

 results.toMultiMap(keyFunc: (r) => r.approach, valueFunc: (v) => v);

to

 final resultsByApproach =
      results.toMultiMap(keyFunc: ((r) => r.approach) as String Function(TestResult), valueFunc: (v) => v);

(print_table.dart, line 12).
https://github.com/knaeckeKami/changenotifier_benchmark/blob/nullsafety2/lib/print_table.dart#L12

This throws an exception at runtime:
type '(dynamic) => dynamic' is not a subtype of type '(TestResult) => String' in type cast

@stereotype441
Copy link
Member

Actually, there's another issue :)

The tool migrated

 results.toMultiMap(keyFunc: (r) => r.approach, valueFunc: (v) => v);

to

 final resultsByApproach =
      results.toMultiMap(keyFunc: ((r) => r.approach) as String Function(TestResult), valueFunc: (v) => v);

(print_table.dart, line 12).
https://github.com/knaeckeKami/changenotifier_benchmark/blob/nullsafety2/lib/print_table.dart#L12

This throws an exception at runtime:
type '(dynamic) => dynamic' is not a subtype of type '(TestResult) => String' in type cast

This looks very similar to the issue discussed in #44345 (comment), which was fixed in e047dd6. I believe what's happening is that the presence of as String Function(TestResult) is defeating the normal type inference process, so a proper type is no longer being inferred for ((r) => r.approach). You should be able to just drop as String Function(TestResult) entirely, and then type inference should assign the proper types at compilation time, so you won't get the runtime exception anymore.

Alternatively, you can try running the migration tool again with a more recent build that contains the bug fix (Dart version 2.12.0-77.0.dev or higher).

@knaeckeKami
Copy link
Contributor Author

Yeah, removing the cast works, of course, it just wanted to report the issue, but great to see that it's already fixed on master.

@leafpetersen
Copy link
Member

@stereotype441 @srawlins this is currently marked P1, is this still an issue, and if so do we plan to fix it?

@stereotype441
Copy link
Member

I'm dropping priority to P2 considering that --ignore-exceptions was able to work around the problem.

@stereotype441 stereotype441 added P2 A bug or feature request we're likely to work on and removed P1 A high priority bug; for example, a single project is unusable or has many test failures labels Jan 12, 2021
@franklinyow
Copy link
Contributor

@stereotype441 Are we still targeting this for January?

@stereotype441
Copy link
Member

@stereotype441 Are we still targeting this for January?

At this point it looks unlikely that we'll be able to get to this in time for the stable release. Removing from the milestone.

@stereotype441 stereotype441 removed this from the January Beta Release (2.12) milestone Jan 20, 2021
@stereotype441 stereotype441 removed their assignment Apr 13, 2023
@stereotype441
Copy link
Member

Unassigning myself because I'm not actively working on the migration tool right now.

@knaeckeKami knaeckeKami closed this as not planned Won't fix, can't repro, duplicate, stale Nov 13, 2023
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 P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

4 participants