Skip to content

Commit

Permalink
#2145. Add more local variable declaration tests (#2254)
Browse files Browse the repository at this point in the history
Rename Local Variable declaration tests. Delete duplicates.
  • Loading branch information
sgrekhov committed Sep 4, 2023
1 parent 90598f1 commit 7bf88af
Show file tree
Hide file tree
Showing 13 changed files with 495 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2023, 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.

/// @assertion The declared type of a local variable with a declaration of one
/// of the forms
/// late? T v = e; late? final T v = e; const T v = e; is T
///
/// @description Checks that the declared type of a local variable with the
/// specified type `T` is `T`
/// @author sgrekhov22@gmail.com
import '../../../Utils/static_type_helper.dart';

main() {
// We use `as dynamic` below to avoid a type promotion on an assignment
late num v1 = 1 as dynamic;
late final num v2 = 2 as dynamic;
num v3 = 3 as dynamic;
final num? v4 = 4 as dynamic;
const num v5 = 5 as dynamic;

v1.expectStaticType<Exactly<num>>();
v2.expectStaticType<Exactly<num>>();
v3.expectStaticType<Exactly<num>>();
v4.expectStaticType<Exactly<num?>>();
v5.expectStaticType<Exactly<num>>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,40 @@
/// as follows:
/// • If the static type of e is Null then the declared type of v is dynamic.
///
/// @description Checks that a variable declaration statement `var id;`
/// introduces a new variable `id` with `dynamic` static type into the innermost
/// enclosing scope.
/// @description Checks that the variable declaration statements
/// `late? var v = e; late? final v = e; const v = e;` introduce a new variable
/// `v` with `dynamic` static type into the innermost enclosing scope.
/// @author vasya
import '../../../Utils/expect.dart';

class C {}

main() {
test1() {
var id;
Expect.throws(() {
id.whatever;
});
id = false;
id = "";
id = 2;
id = new C();
id = C();
id = () {};
}

test2() {
late var id;
Expect.throws(() {
id.whatever;
});
id = false;
id = "";
id = 2;
id = C();
id = () {};
}

main() {
test1();
test2();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
/// as follows:
/// • If the static type of e is Null then the declared type of v is dynamic.
///
/// @description Checks that a variable declaration statement `var id = null;`
/// introduces a new variable `id` with `dynamic` static type into the innermost
/// enclosing scope.
/// @description Checks that the variable declaration statements
/// `late? var v = null; late? final v = null; const v = null;` introduce a new
/// variable `v` with `dynamic` static type into the innermost enclosing scope.
/// @author vasya
import '../../../Utils/expect.dart';

class C {}

main() {
test1() {
var id = null;
Expect.throws(() {
id.whatever;
Expand All @@ -27,3 +27,34 @@ main() {
id = new C();
id = () {};
}

test2() {
late var id = null;
Expect.throws(() {
id.whatever;
});
id = false;
id = "";
id = 2;
id = new C();
id = () {};
}

main() {
late final v1 = null;
final v2 = null;
const v3 = null;

Expect.throws(() {
v1.whatever;
});
Expect.throws(() {
v2.whatever;
});
Expect.throws(() {
v3.whatever;
});

test1();
test2();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2023, 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.

/// @assertion The declared type of a local variable with a declaration of one
/// of the forms late? var v = e; late? final v = e; const v = e; is determined
/// as follows:
/// ...
/// • If the static type of e is of the form X & T where X is a type variable,
/// the declared type of v is X. In this case v is immediately promoted to X & T
///
/// @description Checks that the static type of a variable declared by the
/// statements `late? var v = e; late? final v = e;` is `X` if the static type
/// of `e` is `X & T` where `X` is a type variable. Also test that `v` is
/// promoted to `X & T`
/// @author sgrekhov22@gmail.com
import '../../../Utils/static_type_helper.dart';

test1<T>(T t) {
if (t is int) {
var v = t;
v.isEven;
T x = v;
v = x;
}
}

test2<T>(T t) {
if (t is int) {
late var v = t;
v.isEven;
T x = v;
v = x;
}
}

test3<T>(T t) {
if (t is int) {
final v = t;
v.isEven;
T x = v;
}
}

test4<T>(T t) {
if (t is int) {
late final v = t;
v.isEven;
T x = v;
}
}

main() {
test1<int>(1);
test2<num>(2);
test3<int>(3);
test4<num>(4);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2023, 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.

/// @assertion The declared type of a local variable with a declaration of one
/// of the forms late? var v = e; late? final v = e; const v = e; is determined
/// as follows:
/// ...
/// • If the static type of e is of the form X & T where X is a type variable,
/// the declared type of v is X. In this case v is immediately promoted to X & T
///
/// @description Checks that the static type of a variable declared by the
/// statements `late? var v = e; late? final v = e;` is `X` if the static type
/// of `e` is where `X` is a type variable. Also check that `v` is not
/// erroneously promoted
/// @author sgrekhov22@gmail.com
test1<T>(T t) {
if (t is int) {
}
var v = t;
v.isEven;
// ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

test2<T>(T t) {
if (t is int) {
}
late var v = t;
v.isEven;
// ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

test3<T>(T t) {
if (t is int) {
}
final v = t;
v.isEven;
// ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

test4<T>(T t) {
if (t is int) {
}
late final v = t;
v.isEven;
// ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

main() {
test1<int>(1);
test2<num>(2);
test3<int>(3);
test4<num>(4);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2023, 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.

/// @assertion The declared type of a local variable with a declaration of one
/// of the forms late? var v = e; late? final v = e; const v = e; is determined
/// as follows:
/// ...
/// • Otherwise, the declared type of v is the static type of e.
///
/// @description Checks that static type of a variable declared by the
/// statements `late? var v = e; late? final v = e; const v = e;` is the static
/// type of `e`
/// @author sgrekhov22@gmail.com
import '../../../Utils/static_type_helper.dart';

test<T>(T t) {
if (t is int) {
late final v = t;
v.isEven;
T x = v;
}
}

main() {
late var v1 = "1";
v1.expectStaticType<Exactly<String>>();

var v2 = 2;
v2.expectStaticType<Exactly<int>>();

late final v3 = 3 as num;
v3.expectStaticType<Exactly<num>>();

final v4 = 4 as int?;
v4.expectStaticType<Exactly<int?>>();

const v5 = 5 as num?;
v5.expectStaticType<Exactly<num?>>();

test<int>(42);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2023, 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.

/// @assertion The declared type of a local variable with a declaration of one
/// of the forms late? var v = e; late? final v = e; const v = e; is determined
/// as follows:
/// ...
/// • Otherwise, the declared type of v is the static type of e.
///
/// @description Checks that the static type of a variable declared by the
/// statements `late? var v = e; late? final v = e; const v = e;` is the static
/// type of `e`. Check that `v` is not erroneously promoted
/// @author sgrekhov22@gmail.com
import '../../../Utils/static_type_helper.dart';

test<T>(T t) {
if (t is int) {
}
late final v = t;
v.isEven;
// ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

main() {
test<num>(3.14);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2023, 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.

/// @assertion Assume that D is a local variable declaration with the modifier
/// late that declares a variable v, which has an initializing expression e. It
/// is a compile-time error if e contains an await expression a, unless there is
/// a function f which is the immediately enclosing function for a, and f is not
/// the immediately enclosing function for D.
///
/// @description Checks that it is a compile-time error if initializing
/// expression of a late local variable contains an `await` expression
/// @author sgrekhov22@gmail.com
import 'dart:async';

Future<T> foo<T>(T t) => Future<T>.value(t);

main() async {
late var v1 = await Future<int>.value(1);
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified

late final v2 = await foo<int>(2);
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified

late String v3 = await foo<String>("3");
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified

late final int v4 = await Future<int>.value(4);
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified

return 0;
}

0 comments on commit 7bf88af

Please sign in to comment.