fix DDC explicit field initialization to null

This was unintentionally regressed during a fix to field init order. It
does not typically show up in Dart code because undefined and null are
both handled, but it can break JS interop.

Change-Id: I771895826ee09bb6c5e2f9b016eb7673caf61cd1
Reviewed-on: https://dart-review.googlesource.com/40202
Reviewed-by: Vijay Menon <vsm@google.com>
This commit is contained in:
Jenny Messerly 2018-02-09 02:48:07 +00:00
parent ab5c97ff1c
commit 5ef7920d84
4 changed files with 39 additions and 21 deletions

View file

@ -2476,14 +2476,12 @@ class CodeGenerator extends Object
for (var field in fieldDecls) {
var f = field.element;
if (f.isStatic) continue;
var init = field.initializer;
if (init == null ||
ctorFields != null &&
ctorFields.contains(f) &&
_constants.isFieldInitConstant(field)) {
if (ctorFields != null &&
ctorFields.contains(f) &&
_constants.isFieldInitConstant(field)) {
continue;
}
emitFieldInit(f, init, field.name);
emitFieldInit(f, field.initializer, field.name);
}
if (ctor != null) {

View file

@ -1470,10 +1470,9 @@ class ProgramCompiler
for (var f in fields) {
if (f.isStatic) continue;
var init = f.initializer;
if (init == null ||
ctorFields != null &&
ctorFields.contains(f) &&
_constants.isConstant(init)) {
if (ctorFields != null &&
ctorFields.contains(f) &&
(init == null || _constants.isConstant(init))) {
continue;
}
emitFieldInit(f, init);

View file

@ -4,13 +4,10 @@
// Dart test program for testing setting/getting of instance fields.
import "package:expect/expect.dart";
import "package:meta/meta.dart" show virtual;
class First {
First() {}
@virtual
var a;
@virtual
var b;
addFields() {
@ -25,8 +22,6 @@ class First {
}
class Second extends First {
// TODO: consider removing once http://b/4254120 is fixed.
Second() : super() {}
var c;
get a {
return -12;
@ -37,6 +32,21 @@ class Second extends First {
}
}
class FieldInitializedToNull {
int x, y;
static void test() {
var f = new FieldInitializedToNull();
int missingArg([int x = 42]) => x;
Expect.isNull(f.x);
Expect.isNull(f.y);
// Regression tests for a DDC bug, where undefined gets initialized in the
// fields, and is incorrect recognized as a missing argument.
Expect.isNull(missingArg(f.x));
Expect.isNull(missingArg(f.y));
}
}
class FieldTest {
static one() {
var f = new First();
@ -68,13 +78,10 @@ class FieldTest {
o.b = o;
Expect.equals(12, o.c);
}
static testMain() {
// FieldTest.one();
FieldTest.two();
}
}
main() {
FieldTest.testMain();
FieldTest.one();
FieldTest.two();
FieldInitializedToNull.test();
}

View file

@ -78,6 +78,8 @@ _injectJs() {
function confuse(obj) { return obj; }
function isUndefined(obj) { return obj === void 0; }
function StringWrapper(str) {
this.str = str;
}
@ -191,6 +193,9 @@ class StringWrapper {
@JS()
external confuse(obj);
@JS()
external isUndefined(obj);
@JS()
external CanvasRenderingContext2D getCanvasContext();
@ -200,6 +205,10 @@ external num get propertyOnDocument;
@JS('window.self.window.window.windowProperty')
external num get propertyOnWindow;
class DartClassWithNullField {
int x;
}
main() {
_injectJs();
@ -433,6 +442,7 @@ main() {
expect(selection is List, isTrue);
});
});
group('html', () {
test('return html type', () {
expect(getCanvasContext() is CanvasRenderingContext2D, isTrue);
@ -442,4 +452,8 @@ main() {
expect(propertyOnDocument, equals(45));
});
});
test('Dart field is null instead of undefined', () {
expect(isUndefined(new DartClassWithNullField().x), false);
});
}