Fix context type for conditionals of conditional expressions.
Fixes #48199.
Bug: https://github.com/dart-lang/sdk/issues/48199
Change-Id: I8483e87e4680c1f3ab97f58818eeaa57dec78b07
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/229500
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
diff --git a/pkg/analyzer/lib/src/generated/resolver.dart b/pkg/analyzer/lib/src/generated/resolver.dart
index ad8e1da..1e84b07 100644
--- a/pkg/analyzer/lib/src/generated/resolver.dart
+++ b/pkg/analyzer/lib/src/generated/resolver.dart
@@ -1342,6 +1342,7 @@
flow?.conditional_conditionBegin();
// TODO(scheglov) Do we need these checks for null?
+ InferenceContext.setType(node.condition, typeProvider.boolType);
condition.accept(this);
condition = node.condition;
var whyNotPromoted = flowAnalysis.flow?.whyNotPromoted(condition);
diff --git a/pkg/analyzer/test/src/diagnostics/undefined_method_test.dart b/pkg/analyzer/test/src/diagnostics/undefined_method_test.dart
index 483d1dc..9c11a5e 100644
--- a/pkg/analyzer/test/src/diagnostics/undefined_method_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/undefined_method_test.dart
@@ -15,6 +15,19 @@
@reflectiveTest
class UndefinedMethodTest extends PubPackageResolutionTest {
+ test_conditional_expression_condition_context() async {
+ await assertErrorsInCode('''
+T castObject<T>(Object value) => value as T;
+
+main() {
+ (castObject(true)..whatever()) ? 1 : 2;
+}
+''', [
+ error(CompileTimeErrorCode.UNDEFINED_METHOD, 76, 8,
+ messageContains: ["type 'bool'"]),
+ ]);
+ }
+
test_constructor_defined() async {
await assertNoErrorsInCode(r'''
class C {
diff --git a/tests/language/bool/conditional_expression_condition_context_error_test.dart b/tests/language/bool/conditional_expression_condition_context_error_test.dart
new file mode 100644
index 0000000..ade91d5
--- /dev/null
+++ b/tests/language/bool/conditional_expression_condition_context_error_test.dart
@@ -0,0 +1,12 @@
+// Copyright (c) 2022, 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.
+
+T castObject<T>(Object value) => value as T;
+
+main() {
+ print((castObject(true)..whatever()) ? 1 : 2);
+ // ^^^^^^^^
+ // [analyzer] COMPILE_TIME_ERROR.UNDEFINED_METHOD
+ // [cfe] The method 'whatever' isn't defined for the class 'bool'.
+}
diff --git a/tests/language_2/bool/conditional_expression_condition_context_error_test.dart b/tests/language_2/bool/conditional_expression_condition_context_error_test.dart
new file mode 100644
index 0000000..ade91d5
--- /dev/null
+++ b/tests/language_2/bool/conditional_expression_condition_context_error_test.dart
@@ -0,0 +1,12 @@
+// Copyright (c) 2022, 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.
+
+T castObject<T>(Object value) => value as T;
+
+main() {
+ print((castObject(true)..whatever()) ? 1 : 2);
+ // ^^^^^^^^
+ // [analyzer] COMPILE_TIME_ERROR.UNDEFINED_METHOD
+ // [cfe] The method 'whatever' isn't defined for the class 'bool'.
+}