Skip to content

Commit ce90061

Browse files
munificentcommit-bot@chromium.org
authored andcommitted
Migrate language_2/setter to NNBD.
Change-Id: I5c234c68d35d83b0b1f1e299303dd29cf972fdb7 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/150845 Reviewed-by: Nicholas Shahan <[email protected]> Commit-Queue: Bob Nystrom <[email protected]> Auto-Submit: Bob Nystrom <[email protected]>
1 parent b1442a1 commit ce90061

16 files changed

+526
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2013, 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+
// Test that implicit setters do a type check generic types.
6+
import "package:expect/expect.dart";
7+
8+
class A {
9+
late C<int> c;
10+
}
11+
12+
class B extends A {}
13+
14+
class C<T> {}
15+
16+
var array = <dynamic>[new B()];
17+
18+
main() {
19+
array[0].c = new C<int>();
20+
Expect.throwsTypeError(() => array[0].c = new C<bool>());
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2013, 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+
class A<T> {
8+
late T field;
9+
}
10+
11+
class B<T> {
12+
T field = 42 as dynamic;
13+
}
14+
15+
main() {
16+
var a = new A<String>();
17+
dynamic s = "string";
18+
19+
// This assignment is OK.
20+
a.field = s;
21+
22+
dynamic i = 42;
23+
Expect.throwsTypeError(() => a.field = i);
24+
25+
// Throws because the field initializer fails the implicit cast.
26+
Expect.throwsTypeError(() => new B<String>());
27+
28+
// Throws because the assigned value fails the implicit cast.
29+
var b = new B<int>();
30+
Expect.throwsTypeError(() => b.field = s);
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2013, 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+
// Test that implicit setters do a runtime type check.
6+
7+
import "package:expect/expect.dart";
8+
9+
class A {
10+
late C c;
11+
}
12+
13+
class B extends A {}
14+
15+
class C {}
16+
17+
var array = <dynamic>[new B()];
18+
19+
main() {
20+
array[0].c = new C();
21+
Expect.throwsTypeError(() => array[0].c = new B());
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2019, 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+
/// Test that a setter has a single argument.
6+
7+
set tooFew() {}
8+
// ^^^^^^
9+
// [analyzer] COMPILE_TIME_ERROR.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER
10+
// ^
11+
// [cfe] A setter should have exactly one formal parameter.
12+
13+
set tooMany(var value, var extra) {}
14+
// ^^^^^^^
15+
// [analyzer] COMPILE_TIME_ERROR.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER
16+
// ^
17+
// [cfe] A setter should have exactly one formal parameter.
18+
19+
main() {
20+
tooFew = 1;
21+
tooMany = 2;
22+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// TODO(multitest): This was automatically migrated from a multitest and may
2+
// contain strange or dead code.
3+
4+
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
5+
// for details. All rights reserved. Use of this source code is governed by a
6+
// BSD-style license that can be found in the LICENSE file.
7+
8+
import "package:expect/expect.dart";
9+
10+
var topLevelClosure;
11+
12+
13+
get topLevel => topLevelClosure;
14+
15+
set topLevel(var value) {}
16+
17+
initialize() {
18+
print("initializing");
19+
topLevelClosure = (x) => x * 2;
20+
}
21+
22+
main() {
23+
initialize();
24+
var x = topLevelClosure(2);
25+
Expect.equals(4, x);
26+
27+
x = topLevel(3);
28+
Expect.equals(6, x);
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
var topLevelClosure;
8+
9+
/*
10+
get topLevel => topLevelClosure;
11+
*/
12+
set topLevel(var value) {}
13+
14+
initialize() {
15+
print("initializing");
16+
topLevelClosure = (x) => x * 2;
17+
}
18+
19+
main() {
20+
initialize();
21+
var x = topLevelClosure(2);
22+
Expect.equals(4, x);
23+
24+
x = topLevel(3);
25+
// ^^^^^^^^
26+
// [analyzer] STATIC_TYPE_WARNING.UNDEFINED_FUNCTION
27+
// [cfe] Getter not found: 'topLevel'.
28+
Expect.equals(6, x);
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
set topLevel(var value) {}
6+
7+
class Example {
8+
set foo(var value) {}
9+
}
10+
11+
main() {
12+
print(topLevel++);
13+
// ^
14+
// [analyzer] unspecified
15+
// [cfe] Getter not found: 'topLevel'.
16+
17+
Example ex = new Example();
18+
print(ex.foo++);
19+
// ^^^
20+
// [analyzer] STATIC_TYPE_WARNING.UNDEFINED_GETTER
21+
// [cfe] The getter 'foo' isn't defined for the class 'Example'.
22+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) 2013, 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+
// Test that we do not report a compile-time error when an instance setter named
6+
// foo= is declared in a class inheriting an instance method, field, or getter
7+
// named foo, or an instance setter named foo=.
8+
9+
import "package:expect/expect.dart";
10+
import "package:meta/meta.dart" show virtual;
11+
12+
class A {
13+
var foo = 42; // //# 00: ok
14+
get foo => 42; // //# 01: ok
15+
foo() => 42; // //# 02: compile-time error
16+
set foo(value) {} // //# 03: ok
17+
}
18+
19+
class B extends A {
20+
var foo_;
21+
set foo(value) {
22+
foo_ = value;
23+
}
24+
}
25+
26+
main() {
27+
var b = new B();
28+
b.foo = 42;
29+
Expect.equals(42, b.foo_);
30+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2016, 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+
// Tests that a setter in a subclass does not shadow the getter in the
6+
// superclass.
7+
import "package:expect/expect.dart";
8+
9+
class A {
10+
int _x = 42;
11+
int get x => _x;
12+
}
13+
14+
class B extends A {
15+
void set x(int val) {
16+
_x = val;
17+
}
18+
}
19+
20+
void main() {
21+
var b = new B();
22+
Expect.equals(42, b.x);
23+
24+
b.x = 21;
25+
Expect.equals(21, b.x);
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) 2013, 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+
// Test that we do not report a compile-time error when a static setter named
6+
// foo= is declared in a class inheriting an instance method or getter named
7+
// foo, and that we do report an error if an instance setter named foo= or
8+
// instance field name foo is inherited.
9+
10+
import "package:expect/expect.dart";
11+
12+
class A {
13+
var foo = 42; // //# 00: compile-time error
14+
get foo => 42; // //# 01: compile-time error
15+
foo() => 42; // //# 02: compile-time error
16+
set foo(value) {} // //# 03: compile-time error
17+
}
18+
19+
class B extends A {
20+
static var foo_;
21+
static set foo(value) {
22+
foo_ = value;
23+
}
24+
}
25+
26+
main() {
27+
B.foo = 42;
28+
Expect.equals(42, B.foo_);
29+
}

0 commit comments

Comments
 (0)