-
Notifications
You must be signed in to change notification settings - Fork 28
#2145. Add more local variable declaration tests #2254
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
645f959
#2145. Rename Local Variable declaration tests. Delete duplicates
sgrekhov af88c17
Merge branch 'master' into co19-2145-4
sgrekhov 4beb74f
#2145. Add more local variable declaration tests
sgrekhov 20315aa
Implement review recommendations
sgrekhov ed115dd
Update declared_type_A04_t02.dart
eernstg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
Language/Statements/Local_Variable_Declaration/declared_type_A01_t01.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>>(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
Language/Statements/Local_Variable_Declaration/declared_type_A03_t01.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
} |
63 changes: 63 additions & 0 deletions
63
Language/Statements/Local_Variable_Declaration/declared_type_A03_t02.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
43 changes: 43 additions & 0 deletions
43
Language/Statements/Local_Variable_Declaration/declared_type_A04_t01.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
30 changes: 30 additions & 0 deletions
30
Language/Statements/Local_Variable_Declaration/declared_type_A04_t02.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
41 changes: 41 additions & 0 deletions
41
Language/Statements/Local_Variable_Declaration/late_await_A01_t01.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.