Migrate language_2/instance to NNBD.

Change-Id: I058f81e4af6e962b2620810dbfe75c7ac86b9d11
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/148948
Auto-Submit: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Erik Ernst <eernst@google.com>
Commit-Queue: Bob Nystrom <rnystrom@google.com>
This commit is contained in:
Robert Nystrom 2020-05-26 18:35:20 +00:00 committed by commit-bot@chromium.org
parent d35702e83a
commit d3ef98ea3b
7 changed files with 269 additions and 0 deletions

View file

@ -0,0 +1,20 @@
// Copyright (c) 2019, 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.
/// Test mismatch in argument counts.
class Niederhorn {
int goodCall(int a, int b, int c) {
return a + b;
}
}
main() {
var niederhorn = Niederhorn();
niederhorn.goodCall(1, 2, 3);
niederhorn.goodCall(1, 2, 3, 4);
// ^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.EXTRA_POSITIONAL_ARGUMENTS
// [cfe] Too many positional arguments: 3 allowed, but 4 found.
}

View file

@ -0,0 +1,86 @@
// Copyright (c) 2012, 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.
// Test correct instance compound assignment operator.
import "package:expect/expect.dart";
class A {
A() : f = 2 {}
var f;
operator [](index) => f;
operator []=(index, value) => f = value;
var _g = 0;
var gGetCount = 0;
var gSetCount = 0;
get g {
gGetCount++;
return _g;
}
set g(value) {
gSetCount++;
_g = value;
}
}
class B {
B()
: _a = new A(),
count = 0 {}
get a {
count++;
return _a;
}
var _a;
var count;
}
var globalA;
var fooCounter = 0;
foo() {
fooCounter++;
return globalA;
}
main() {
B b = new B();
Expect.equals(0, b.count);
Expect.equals(2, b.a.f);
Expect.equals(1, b.count);
var o = b.a;
Expect.equals(2, b.count);
b.a.f = 1;
Expect.equals(3, b.count);
Expect.equals(1, b._a.f);
b.a.f += 1;
Expect.equals(4, b.count);
Expect.equals(2, b._a.f);
b.count = 0;
b._a.f = 2;
Expect.equals(0, b.count);
Expect.equals(2, b.a[0]);
Expect.equals(1, b.count);
o = b.a;
Expect.equals(2, b.count);
b.a[0] = 1;
Expect.equals(3, b.count);
Expect.equals(1, b._a.f);
b.a[0] += 1;
Expect.equals(4, b.count);
Expect.equals(2, b._a.f);
b._a.g++;
Expect.equals(1, b._a.gGetCount);
Expect.equals(1, b._a.gSetCount);
Expect.equals(1, b._a._g);
globalA = b._a;
globalA.f = 0;
foo().f += 1;
Expect.equals(1, fooCounter);
Expect.equals(1, globalA.f);
}

View file

@ -0,0 +1,34 @@
// Copyright (c) 2011, 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.
import "package:expect/expect.dart";
class A {
int x = 1;
A() {}
A.reassign() : x = 2 {}
A.reassign2(this.x) {}
}
class B extends A {
B() : super() {}
B.reassign() : super.reassign() {}
B.reassign2() : super.reassign2(3) {}
}
class InstanceFieldInitializerTest {
static testMain() {
Expect.equals(1, new A().x);
Expect.equals(2, new A.reassign().x);
Expect.equals(3, new A.reassign2(3).x);
Expect.equals(1, new B().x);
Expect.equals(2, new B.reassign().x);
Expect.equals(3, new B.reassign2().x);
}
}
main() {
InstanceFieldInitializerTest.testMain();
}

View file

@ -0,0 +1,56 @@
// Copyright (c) 2011, 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.
// VMOptions=--optimization-counter-threshold=10 --no-use-osr --no-background-compilation
import "package:expect/expect.dart";
// Check correct deoptimization of instance field increment.
main() {
var a = new A();
var aa = new A();
for (int i = 0; i < 20; i++) {
a.Incr();
myIncr(aa);
conditionalIncr(false, a);
}
Expect.equals(20, a.f);
Expect.equals(20, aa.f);
a.f = 1.0;
// Deoptimize ++ part of instance increment.
a.Incr();
Expect.equals(2.0, a.f);
var b = new B();
// Deoptimize getfield part of instance increment.
myIncr(b);
Expect.equals(1.0, b.f);
// Deoptimize since no type feedback was collected.
var old = a.f;
conditionalIncr(true, a);
Expect.equals(old + 1, a.f);
}
myIncr(var a) {
a.f++;
}
conditionalIncr(var f, var a) {
if (f) {
a.f++;
}
}
class A {
A() : f = 0;
Incr() {
f++;
}
var f;
}
class B {
B() : f = 0;
var f;
}

View file

@ -0,0 +1,27 @@
// Copyright (c) 2012, 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.
import "package:expect/expect.dart";
// Test inlining of assignments in parameter passing. If [StringScanner.charAt]
// is inlined, the argument expression `++byteOffset` should not be duplicated.
class StringScanner {
final String string;
int byteOffset = -1;
StringScanner(this.string);
int nextByte() => charAt(++byteOffset);
int charAt(index) => (string.length > index) ? string.codeUnitAt(index) : -1;
}
void main() {
var scanner = new StringScanner('az9');
Expect.equals(0x61, scanner.nextByte()); // Expect a.
Expect.equals(0x7A, scanner.nextByte()); // Expect z.
Expect.equals(0x39, scanner.nextByte()); // Expect 9.
Expect.equals(-1, scanner.nextByte());
}

View file

@ -0,0 +1,23 @@
// Copyright (c) 2019, 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.
/// Check that we correctly flag the use of an instance method (as a closure)
/// from a static method.
class Goofy {
String instMethod() {
return "woof";
}
static Function bark() {
return instMethod;
// ^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.INSTANCE_MEMBER_ACCESS_FROM_STATIC
// [cfe] Getter not found: 'instMethod'.
}
}
main() {
var s = Goofy.bark();
}

View file

@ -0,0 +1,23 @@
// Copyright (c) 2019, 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.
/// Check that we correctly flag the use of an instance method from a static
/// method.
class Goofy {
String instMethod() {
return "woof";
}
static String bark() {
return instMethod();
// ^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.INSTANCE_MEMBER_ACCESS_FROM_STATIC
// [cfe] Method not found: 'instMethod'.
}
}
main() {
Goofy.bark();
}