Skip to content

Commit 5d4e85b

Browse files
harryterkelsencommit-bot@chromium.org
authored andcommitted
Allow x == null to be a constant value whenever x is const.
This is the fix for #33408 for dart2js Change-Id: Ifddeacb68884da308d279ce6d5cbbb6adc5385ca Reviewed-on: https://dart-review.googlesource.com/69020 Commit-Queue: Harry Terkelsen <[email protected]> Reviewed-by: Sigmund Cherem <[email protected]>
1 parent 7c77ed0 commit 5d4e85b

File tree

4 files changed

+60
-18
lines changed

4 files changed

+60
-18
lines changed

pkg/compiler/lib/src/constant_system_dart.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,14 @@ class EqualsOperation implements BinaryOperation {
342342
}
343343

344344
if (left.isConstructedObject) {
345+
if (right.isNull) {
346+
return DART_CONSTANT_SYSTEM.createBool(false);
347+
}
345348
// Unless we know that the user-defined object does not implement the
346349
// equality operator we cannot fold here.
347350
return null;
348351
}
352+
349353
return DART_CONSTANT_SYSTEM.createBool(left == right);
350354
}
351355

pkg/compiler/lib/src/constants/expressions.dart

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,22 +1051,26 @@ class BinaryConstantExpression extends ConstantExpression {
10511051
case BinaryOperatorKind.EQ:
10521052
case BinaryOperatorKind.NOT_EQ:
10531053
if (!leftValue.isPrimitive) {
1054-
environment.reportError(
1055-
left, MessageKind.INVALID_CONSTANT_BINARY_PRIMITIVE_TYPE, {
1056-
'constant': left,
1057-
'type': leftValue.getType(environment.commonElements),
1058-
'operator': operator
1059-
});
1060-
isValid = false;
1054+
if (!rightValue.isNull) {
1055+
environment.reportError(
1056+
left, MessageKind.INVALID_CONSTANT_BINARY_PRIMITIVE_TYPE, {
1057+
'constant': left,
1058+
'type': leftValue.getType(environment.commonElements),
1059+
'operator': operator
1060+
});
1061+
isValid = false;
1062+
}
10611063
}
10621064
if (!rightValue.isPrimitive) {
1063-
environment.reportError(
1064-
right, MessageKind.INVALID_CONSTANT_BINARY_PRIMITIVE_TYPE, {
1065-
'constant': right,
1066-
'type': rightValue.getType(environment.commonElements),
1067-
'operator': operator
1068-
});
1069-
isValid = false;
1065+
if (!leftValue.isNull) {
1066+
environment.reportError(
1067+
right, MessageKind.INVALID_CONSTANT_BINARY_PRIMITIVE_TYPE, {
1068+
'constant': right,
1069+
'type': rightValue.getType(environment.commonElements),
1070+
'operator': operator
1071+
});
1072+
isValid = false;
1073+
}
10701074
}
10711075
break;
10721076
case BinaryOperatorKind.ADD:

tests/compiler/dart2js/model/constant_expression_evaluate_test.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ const List<TestData> DATA = const [
164164
const ConstantData('identical', 'FunctionConstant(identical)'),
165165
const ConstantData('true ? 0 : 1', 'IntConstant(0)'),
166166
const ConstantData('proxy', 'ConstructedConstant(_Proxy())'),
167+
const ConstantData('const [] == null', 'BoolConstant(false)'),
168+
const ConstantData('proxy == null', 'BoolConstant(false)'),
167169
const ConstantData('Object', 'TypeConstant(Object)'),
168170
const ConstantData('null ?? 0', 'IntConstant(0)'),
169171
const ConstantData(
@@ -414,10 +416,6 @@ class B extends A {
414416
expectedErrors: MessageKind.INVALID_CONSTANT_ADD_TYPES),
415417
const ConstantData('boolean + false', 'NonConstant',
416418
expectedErrors: MessageKind.INVALID_CONSTANT_ADD_TYPES),
417-
const ConstantData('const [] == null', 'NonConstant',
418-
expectedErrors: MessageKind.INVALID_CONSTANT_BINARY_PRIMITIVE_TYPE),
419-
const ConstantData('proxy == null', 'NonConstant',
420-
expectedErrors: MessageKind.INVALID_CONSTANT_BINARY_PRIMITIVE_TYPE),
421419
const ConstantData('0 * ""', 'NonConstant',
422420
expectedErrors: MessageKind.INVALID_CONSTANT_BINARY_NUM_TYPE),
423421
const ConstantData('0 * string', 'NonConstant',
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import "package:expect/expect.dart";
6+
7+
// Make sure we can assert(const Foo() != null) in const initializers.
8+
class Color {
9+
const Color(this.value);
10+
final int value;
11+
}
12+
13+
class ColorHaver {
14+
const ColorHaver({this.color = const Color(0xFF000000)})
15+
: assert(color != null);
16+
final Color color;
17+
}
18+
19+
const c = const ColorHaver(color: const Color(0xFF00FF00));
20+
21+
enum Enum {
22+
a,
23+
b,
24+
}
25+
26+
class EnumHaver {
27+
const EnumHaver({this.myEnum: Enum.a}) : assert(myEnum != null);
28+
final Enum myEnum;
29+
}
30+
31+
const e = const EnumHaver(myEnum: Enum.b);
32+
33+
main() {
34+
Expect.equals(c.value, 0xFF00FF00);
35+
Expect.equals(e.myEnum, Enum.b);
36+
}

0 commit comments

Comments
 (0)