Skip to content

Commit

Permalink
#1243 Runtime checks added for statements which can be optimized to '…
Browse files Browse the repository at this point in the history
…true' or 'false'
  • Loading branch information
sgrekhov committed Dec 13, 2021
1 parent a414462 commit 9c976fe
Show file tree
Hide file tree
Showing 28 changed files with 64 additions and 73 deletions.
4 changes: 2 additions & 2 deletions Language/Classes/Constructors/implicit_constructor_t04.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ typedef CAlias = C;

main() {
CAlias c = new CAlias();
Expect.isTrue(c is C);
checkType(checkIs<C>, true, c);
Expect.equals(null, c.x);

CAlias c2 = new C();
Expect.isTrue(c2 is C);
checkType(checkIs<C>, true, c2);
Expect.equals(null, c2.x);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ class C {
}

main() {
C c = new C();
c is C;
C();
}
2 changes: 1 addition & 1 deletion Language/Classes/Superclasses/extends_clause_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ class A {}
class B extends A {}

main() {
Expect.isTrue(new B() is A);
checkType(checkIs<A>, true, B());
}
2 changes: 1 addition & 1 deletion Language/Classes/Superclasses/extends_clause_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ typedef AAlias = A;
class B extends AAlias {}

main() {
Expect.isTrue(new B() is A);
checkType(checkIs<A>, true, B());
}
2 changes: 1 addition & 1 deletion Language/Classes/Superclasses/no_extends_clause_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ class A {}

main() {
A a = new A();
Expect.isTrue(a is Object);
checkType(checkIs<Object>, true, a);
}
6 changes: 3 additions & 3 deletions Language/Classes/Superclasses/transition_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class D extends C {}

main() {
D d = new D();
Expect.isTrue(d is B);
Expect.isTrue(d is A);
Expect.isTrue(d is Object);
checkType(checkIs<B>, true, d);
checkType(checkIs<A>, true, d);
checkType(checkIs<Object>, true, d);
}
6 changes: 3 additions & 3 deletions Language/Classes/Superclasses/transition_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class D extends CAlias {}

main() {
D d = new D();
Expect.isTrue(d is C);
Expect.isTrue(d is B);
Expect.isTrue(d is A);
checkType(checkIs<C>, true, d);
checkType(checkIs<B>, true, d);
checkType(checkIs<A>, true, d);
}
8 changes: 4 additions & 4 deletions Language/Classes/Superinterfaces/syntax_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class C extends B implements IC, ID {}

main() {
C c = new C();
Expect.isTrue(c is IA);
Expect.isTrue(c is IB);
Expect.isTrue(c is IC);
Expect.isTrue(c is ID);
checkType(checkIs<IA>, true, c);
checkType(checkIs<IB>, true, c);
checkType(checkIs<IC>, true, c);
checkType(checkIs<ID>, true, c);
}
8 changes: 4 additions & 4 deletions Language/Classes/Superinterfaces/syntax_t03.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class C extends B implements ICAlias, IDAlias {}

main() {
C c = new C();
Expect.isTrue(c is IA);
Expect.isTrue(c is IB);
Expect.isTrue(c is IC);
Expect.isTrue(c is ID);
checkType(checkIs<IA>, true, c);
checkType(checkIs<IB>, true, c);
checkType(checkIs<IC>, true, c);
checkType(checkIs<ID>, true, c);
}
12 changes: 6 additions & 6 deletions Language/Classes/implements_clause_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ main() {
B b = new B();
C c = new C();
D d = new D();
Expect.isTrue(b is I1);
Expect.isTrue(c is I1);
Expect.isTrue(c is I2);
Expect.isTrue(d is I1);
Expect.isTrue(d is I2);
Expect.isTrue(d is I3);
checkType(checkIs<I1>, true, b);
checkType(checkIs<I1>, true, c);
checkType(checkIs<I2>, true, c);
checkType(checkIs<I1>, true, d);
checkType(checkIs<I2>, true, d);
checkType(checkIs<I3>, true, d);
}

12 changes: 6 additions & 6 deletions Language/Classes/implements_clause_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ main() {
B b = new B();
C c = new C();
D d = new D();
Expect.isTrue(b is I1);
Expect.isTrue(c is I1);
Expect.isTrue(c is I2);
Expect.isTrue(d is I1);
Expect.isTrue(d is I2);
Expect.isTrue(d is I3);
checkType(checkIs<I1>, true, b);
checkType(checkIs<I1>, true, c);
checkType(checkIs<I2>, true, c);
checkType(checkIs<I1>, true, d);
checkType(checkIs<I2>, true, d);
checkType(checkIs<I3>, true, d);
}
4 changes: 2 additions & 2 deletions Language/Classes/implicit_interface_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class D implements C {}

main() {
var b = new B();
Expect.isTrue(b is A);
checkType(checkIs<A>, true, b);

var d = new D();
Expect.isTrue(d is C);
checkType(checkIs<C>, true, d);
}
6 changes: 3 additions & 3 deletions Language/Enums/declaration_equivalent_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import "../../Utils/expect.dart";
enum E {a, b, c}

main() {
Expect.isTrue(E.a is E);
Expect.isTrue(E.b is E);
Expect.isTrue(E.c is E);
checkType(checkIs<E>, true, E.a);
checkType(checkIs<E>, true, E.b);
checkType(checkIs<E>, true, E.c);
}
2 changes: 1 addition & 1 deletion Language/Enums/declaration_equivalent_t06.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ import "../../Utils/expect.dart";
enum E {a, b, c}

main() {
Expect.isTrue(E.values is List<E>);
checkType(checkIs<List<E>>, true, E.values);
}
4 changes: 2 additions & 2 deletions Language/Expressions/Booleans/class_bool_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
import '../../../Utils/expect.dart';

main() {
Expect.isTrue(false is bool);
Expect.isTrue(true is bool);
checkType(checkIs<bool>, true, false);
checkType(checkIs<bool>, true, true);
}
5 changes: 2 additions & 3 deletions Language/Expressions/Booleans/runtime_type_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
/// literal returns the Type object that is the value of the expression bool.
/// @author [email protected]

import '../../../Utils/expect.dart';

main() {
Expect.isTrue(true.runtimeType is Type);
checkType(checkIs<Type>, true, true.runtimeType);
Expect.isTrue(true.runtimeType == bool);
Expect.isTrue(false.runtimeType is Type);
checkType(checkIs<Type>, true, false.runtimeType);
Expect.isTrue(false.runtimeType == bool);
}
5 changes: 1 addition & 4 deletions Language/Expressions/Constants/bitwise_operators_t03.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
/// used to initialize a constant variable.
/// @author kaigorodov

final constList = const [
true & 1,
// ^
Expand All @@ -23,7 +22,5 @@ final constList = const [
];

main() {
try {
constList is List;
} catch (x) {}
print(constList);
}
5 changes: 1 addition & 4 deletions Language/Expressions/Constants/bitwise_operators_t04.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
/// initialize a constant variable.
/// @author kaigorodov

final constList = const [
~"oneoneeleven"
//^
Expand All @@ -23,7 +22,5 @@ final constList = const [
];

main() {
try {
constList is List;
} catch (x) {}
print(constList);
}
5 changes: 1 addition & 4 deletions Language/Expressions/Constants/bitwise_operators_t05.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
/// used to initialize a constant variable.
/// @author kaigorodov

final constList = const [
1 << "one and a half"
// ^
Expand All @@ -23,7 +22,5 @@ final constList = const [
];

main() {
try {
constList is List;
} catch (x) {}
print(constList);
}
5 changes: 1 addition & 4 deletions Language/Expressions/Constants/bitwise_operators_t06.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
/// used to initialize a constant variable.
/// @author kaigorodov

final constList = const [
true >> 25
// ^
Expand All @@ -23,7 +22,5 @@ final constList = const [
];

main() {
try {
constList is List;
} catch (x) {}
print(constList);
}
2 changes: 1 addition & 1 deletion Language/Expressions/Constants/bitwise_operators_t07.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
/// value, is used to initialize a constant variable.
/// @author [email protected]

final constList = const [
1 >>> "one and a half"
// ^
Expand All @@ -25,4 +24,5 @@ final constList = const [
];

main() {
print(constList);
}
2 changes: 1 addition & 1 deletion Language/Expressions/Constants/bitwise_operators_t08.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
/// used to initialize a constant variable.
/// @author [email protected]

final constList = const [
true >>> 25
// ^
Expand All @@ -25,4 +24,5 @@ final constList = const [
];

main() {
print(constList);
}
3 changes: 1 addition & 2 deletions Language/Expressions/Constants/constant_constructor_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
/// expression.
/// @author iefremov

import '../../../Utils/expect.dart';

class A {
Expand All @@ -26,5 +25,5 @@ final constList = const [
];

main() {
Expect.isTrue(constList is List);
checkType(checkIs<List>, true, constList);
}
3 changes: 1 addition & 2 deletions Language/Expressions/Constants/constant_constructor_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
/// a prefix is a constant expression.
/// @author [email protected]

import '../../../Utils/expect.dart';
import 'constants_lib.dart' as clib;

Expand All @@ -27,5 +26,5 @@ final constList = const [
];

main() {
Expect.isTrue(constList is List);
checkType(checkIs<List>, true, constList);
}
4 changes: 1 addition & 3 deletions Language/Expressions/Constants/constant_constructor_t03.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
/// a deferred prefix is not a constant expression.
/// @author [email protected]

import '../../../Utils/expect.dart';
import 'constants_lib.dart' deferred as clib;

class A {
Expand All @@ -32,5 +30,5 @@ final constList = const [
];

main() {
Expect.isTrue(constList is List);
print(constList);
}
3 changes: 1 addition & 2 deletions Language/Expressions/Constants/constant_list_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
/// a constant list literal and is, therefore, a constant expression.
/// @author iefremov

import '../../../Utils/expect.dart';

final constList = const [const ["hello", "world"]];

main() {
Expect.isTrue(constList is List);
checkType(checkIs<List>, true, constList);
}
3 changes: 1 addition & 2 deletions Language/Expressions/Constants/constant_map_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
/// a constant list literal and is, therefore, a constant expression.
/// @author iefremov

import '../../../Utils/expect.dart';

final constMap = const {"a" : 1, "b" : 2};

main() {
Expect.isTrue(constMap is Map);
checkType(checkIs<Map>, true, constMap);
}
11 changes: 11 additions & 0 deletions Utils/expect_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,14 @@ class CheckIdentical {
class CheckNotIdentical {
const CheckNotIdentical(Object? o1, Object? o2) : assert(!identical(o1, o2));
}

void checkIs<T>(bool expected, Object? o) {
Expect.equals(expected, o is T);
}

/// Call this function with `checkIs` as the first parameter to check the type
/// to prevent the compiler reducing the code to the answer
@pragma('dart2js:noInline')
void checkType(void Function(bool, Object?) checker, bool expected, Object? o) {
checker(expected, o);
}

0 comments on commit 9c976fe

Please sign in to comment.