diff --git a/pkg/_fe_analyzer_shared/lib/src/flow_analysis/flow_analysis.dart b/pkg/_fe_analyzer_shared/lib/src/flow_analysis/flow_analysis.dart index 6893a79c22ee..665ee287e399 100644 --- a/pkg/_fe_analyzer_shared/lib/src/flow_analysis/flow_analysis.dart +++ b/pkg/_fe_analyzer_shared/lib/src/flow_analysis/flow_analysis.dart @@ -1804,7 +1804,10 @@ class VariableModel { bool newUnassigned = unassigned && otherModel.unassigned; bool newWriteCaptured = writeCaptured || otherModel.writeCaptured; List newPromotedTypes; - if (unsafe) { + if (newWriteCaptured) { + // Write-captured variables can't be promoted + newPromotedTypes = null; + } else if (unsafe) { // There was an assignment to the variable in the "this" path, so none of // the promotions from the "other" path can be used. newPromotedTypes = thisPromotedTypes; diff --git a/pkg/_fe_analyzer_shared/test/flow_analysis/type_promotion/data/bug42066.dart b/pkg/_fe_analyzer_shared/test/flow_analysis/type_promotion/data/bug42066.dart new file mode 100644 index 000000000000..25956d58ad32 --- /dev/null +++ b/pkg/_fe_analyzer_shared/test/flow_analysis/type_promotion/data/bug42066.dart @@ -0,0 +1,15 @@ +// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +f() { + num par2; + par2 = 0; + if (par2 is! int) return; + try {} catch (exception) { + throw 'x'; + () { + par2 = 1; + }; + } finally {} +}