From 36e89f845b41eab06111b7fc479a5169a2b05cb5 Mon Sep 17 00:00:00 2001 From: Paul Berry Date: Wed, 8 Jul 2020 01:37:30 +0000 Subject: [PATCH] Flow analysis: ensure that `restrict` method doesn't try to promote write-captured variables. Fixes #42066 Change-Id: Ieab80c004b3d331abb81916c3e158416bcd3ce86 Bug: https://github.com/dart-lang/sdk/issues/42066 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/153480 Reviewed-by: Konstantin Shcheglov Commit-Queue: Paul Berry --- .../lib/src/flow_analysis/flow_analysis.dart | 5 ++++- .../type_promotion/data/bug42066.dart | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 pkg/_fe_analyzer_shared/test/flow_analysis/type_promotion/data/bug42066.dart 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 {} +}