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>
This commit is contained in:
Paul Berry 2022-01-24 01:25:38 +00:00 committed by Commit Bot
parent f26ae5aede
commit 5106605af6
4 changed files with 38 additions and 0 deletions

View file

@ -1342,6 +1342,7 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
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);

View file

@ -15,6 +15,19 @@ main() {
@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 {

View file

@ -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'.
}

View file

@ -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'.
}