Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2145. Add more local variable declaration tests #2254

Merged
merged 5 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement review recommendations
  • Loading branch information
sgrekhov committed Sep 4, 2023
commit 20315aa86a493c53b6bb51164a041aa7f17e6c2a
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
import '../../../Utils/static_type_helper.dart';

main() {
late num v1 = 1;
late final num v2 = 2;
num v3 = 3;
final num? v4 = 4;
const num v5 = 5;
// 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>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/// as follows:
/// • If the static type of e is Null then the declared type of v is dynamic.
///
/// @description Checks that a variable declaration statements
/// @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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/// as follows:
/// • If the static type of e is Null then the declared type of v is dynamic.
///
/// @description Checks that a variable declaration statements
/// @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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/// • 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 static type of a variable declared by the
/// @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`
Expand All @@ -21,40 +21,34 @@ test1<T>(T t) {
if (t is int) {
var v = t;
eernstg marked this conversation as resolved.
Show resolved Hide resolved
v.isEven;
v.expectStaticType<Exactly<T>>();
T x = v;
v = x;
}
var v = t;
v.expectStaticType<Exactly<T>>();
}

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

test3<T>(T t) {
if (t is int) {
final v = t;
eernstg marked this conversation as resolved.
Show resolved Hide resolved
v.isEven;
v.expectStaticType<Exactly<T>>();
T x = v;
}
final v = t;
v.expectStaticType<Exactly<T>>();
}

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

main() {
Expand Down
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
Expand Up @@ -15,14 +15,12 @@

import '../../../Utils/static_type_helper.dart';

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

main() {
Expand All @@ -40,4 +38,6 @@ main() {

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 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
Expand Up @@ -7,9 +7,8 @@
/// if any, and otherwise before the declaring occurrence of the identifier
/// which names the variable.
///
/// @description Checks that a variable is introduced into the scope after a
/// variable declaration statement is evaluated, and the name of this variable
/// cannot be used in its initialization.
/// @description Checks that a variable is introduced into the scope and the
/// name of this variable cannot be used in its initialization.
/// @author sgrekhov22@gmail.com

var x = 0;
Expand Down