Skip to content

Commit

Permalink
Use "bool" as the downward inference context for assert conditions.
Browse files Browse the repository at this point in the history
This addresses the type inference part of #30326.

R=scheglov@google.com

Review-Url: https://codereview.chromium.org/2997513002 .
  • Loading branch information
stereotype441 committed Aug 7, 2017
1 parent 62f4509 commit ae4cbb5
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 13 deletions.
8 changes: 8 additions & 0 deletions pkg/analyzer/lib/src/generated/resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5195,8 +5195,16 @@ class ResolverVisitor extends ScopedVisitor {
return null;
}

@override
Object visitAssertInitializer(AssertInitializer node) {
InferenceContext.setType(node.condition, typeProvider.boolType);
super.visitAssertInitializer(node);
return null;
}

@override
Object visitAssertStatement(AssertStatement node) {
InferenceContext.setType(node.condition, typeProvider.boolType);
super.visitAssertStatement(node);
_propagateTrueState(node.condition);
return null;
Expand Down
3 changes: 2 additions & 1 deletion pkg/front_end/lib/src/fasta/kernel/kernel_shadow_ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ class KernelAssertStatement extends AssertStatement implements KernelStatement {
@override
void _inferStatement(KernelTypeInferrer inferrer) {
inferrer.listener.assertStatementEnter(this);
inferrer.inferExpression(condition, null, false);
inferrer.inferExpression(
condition, inferrer.coreTypes.boolClass.rawType, false);
if (message != null) {
inferrer.inferExpression(message, null, false);
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/front_end/testcases/inference/assert.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ library test;
T f<T>() => null;

void test() {
assert(/*@typeArgs=dynamic*/ f());
assert(/*@typeArgs=dynamic*/ f(), /*@typeArgs=dynamic*/ f());
assert(/*@typeArgs=bool*/ f());
assert(/*@typeArgs=bool*/ f(), /*@typeArgs=dynamic*/ f());
}

main() {}
4 changes: 2 additions & 2 deletions pkg/front_end/testcases/inference/assert.dart.strong.expect
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "dart:core" as core;
static method f<T extends core::Object>() → self::f::T
return null;
static method test() → void {
assert(self::f<dynamic>());
assert(self::f<dynamic>(), self::f<dynamic>());
assert(self::f<core::bool>());
assert(self::f<core::bool>(), self::f<dynamic>());
}
static method main() → dynamic {}
8 changes: 4 additions & 4 deletions pkg/front_end/testcases/inference/assert_initializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ library test;
T f<T>() => null;

class C {
C.expressionOnly() : assert(/*@typeArgs=dynamic*/ f());
C.expressionOnly() : assert(/*@typeArgs=bool*/ f());
C.expressionAndMessage()
: assert(/*@typeArgs=dynamic*/ f(), /*@typeArgs=dynamic*/ f());
: assert(/*@typeArgs=bool*/ f(), /*@typeArgs=dynamic*/ f());
}

main() {
// Test type inference of assert statements just to verify that the behavior
// is the same.
assert(/*@typeArgs=dynamic*/ f());
assert(/*@typeArgs=dynamic*/ f(), /*@typeArgs=dynamic*/ f());
assert(/*@typeArgs=bool*/ f());
assert(/*@typeArgs=bool*/ f(), /*@typeArgs=dynamic*/ f());
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import "dart:core" as core;
class C extends core::Object {
constructor expressionOnly() → void
: final dynamic #t1 = () → dynamic
assert(self::f<dynamic>());
assert(self::f<core::bool>());
.call(), super core::Object::•()
;
constructor expressionAndMessage() → void
: final dynamic #t2 = () → dynamic
assert(self::f<dynamic>(), self::f<dynamic>());
assert(self::f<core::bool>(), self::f<dynamic>());
.call(), super core::Object::•()
;
}
static method f<T extends core::Object>() → self::f::T
return null;
static method main() → dynamic {
assert(self::f<dynamic>());
assert(self::f<dynamic>(), self::f<dynamic>());
assert(self::f<core::bool>());
assert(self::f<core::bool>(), self::f<dynamic>());
}

0 comments on commit ae4cbb5

Please sign in to comment.