Skip to content

Commit 16909d8

Browse files
alexmarkovCommit Queue
authored andcommitted
[vm] Simplify evaluation of asserts
Since Dart 2, assert statements only accept bool conditions, so evaluation of the assert condition can be simplified in the VM. TEST=existing Change-Id: I0f54ad66b9d9aef707fef4eac192128a3994e7c9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/425920 Reviewed-by: Alexander Aprelev <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
1 parent c644933 commit 16909d8

File tree

7 files changed

+8
-52
lines changed

7 files changed

+8
-52
lines changed

runtime/lib/errors.cc

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,10 @@
1111

1212
namespace dart {
1313

14+
#if !defined(DART_PRECOMPILED_RUNTIME)
1415
// Scan the stack until we hit the first function in the _AssertionError
1516
// class. We then return the next frame's script taking inlining into account.
1617
static ScriptPtr FindScript(DartFrameIterator* iterator) {
17-
#if defined(DART_PRECOMPILED_RUNTIME)
18-
// The precompiled runtime faces two issues in recovering the correct
19-
// assertion text. First, the precompiled runtime does not include
20-
// the inlining meta-data so we cannot walk the inline-aware stack trace.
21-
// Second, the script text itself is missing so whatever script is returned
22-
// from here will be missing the assertion expression text.
23-
iterator->NextFrame(); // Skip _AssertionError._evaluateAssertion frame
24-
return Exceptions::GetCallerScript(iterator);
25-
#else
2618
StackFrame* stack_frame = iterator->NextFrame();
2719
Code& code = Code::Handle();
2820
Function& func = Function::Handle();
@@ -60,15 +52,18 @@ static ScriptPtr FindScript(DartFrameIterator* iterator) {
6052
}
6153
UNREACHABLE();
6254
return Script::null();
63-
#endif // defined(DART_PRECOMPILED_RUNTIME)
6455
}
56+
#endif // !defined(DART_PRECOMPILED_RUNTIME)
6557

6658
// Allocate and throw a new AssertionError.
6759
// Arg0: index of the first token of the failed assertion.
6860
// Arg1: index of the first token after the failed assertion.
6961
// Arg2: Message object or null.
7062
// Return value: none, throws an exception.
7163
DEFINE_NATIVE_ENTRY(AssertionError_throwNew, 0, 3) {
64+
#if defined(DART_PRECOMPILED_RUNTIME)
65+
UNREACHABLE();
66+
#else
7267
// No need to type check the arguments. This function can only be called
7368
// internally from the VM.
7469
const TokenPosition assertion_start = TokenPosition::Deserialize(
@@ -114,6 +109,7 @@ DEFINE_NATIVE_ENTRY(AssertionError_throwNew, 0, 3) {
114109
Exceptions::ThrowByType(Exceptions::kAssertion, args);
115110
UNREACHABLE();
116111
return Object::null();
112+
#endif
117113
}
118114

119115
// Allocate and throw a new AssertionError.

runtime/vm/compiler/frontend/kernel_binary_flowgraph.cc

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,10 +1521,6 @@ Fragment StreamingFlowGraphBuilder::Return(TokenPosition position) {
15211521
/*omit_result_type_check=*/false);
15221522
}
15231523

1524-
Fragment StreamingFlowGraphBuilder::EvaluateAssertion() {
1525-
return flow_graph_builder_->EvaluateAssertion();
1526-
}
1527-
15281524
Fragment StreamingFlowGraphBuilder::RethrowException(TokenPosition position,
15291525
int catch_try_index) {
15301526
return flow_graph_builder_->RethrowException(position, catch_try_index);
@@ -4626,28 +4622,22 @@ Fragment StreamingFlowGraphBuilder::BuildAssertStatement(
46264622
TargetEntryInstr* otherwise;
46274623

46284624
Fragment instructions;
4629-
// Asserts can be of the following two kinds:
4630-
//
4631-
// * `assert(expr)`
4632-
// * `assert(() { ... })`
4633-
//
4634-
// The call to `_AssertionError._evaluateAssertion()` will take care of both
4635-
// and returns a boolean.
46364625
instructions += BuildExpression(position); // read condition.
46374626

46384627
const TokenPosition condition_start_offset =
46394628
ReadPosition(); // read condition start offset.
46404629
const TokenPosition condition_end_offset =
46414630
ReadPosition(); // read condition end offset.
46424631

4643-
instructions += EvaluateAssertion();
46444632
instructions += RecordCoverage(condition_start_offset);
46454633
instructions += Constant(Bool::True());
46464634
instructions += BranchIfEqual(&then, &otherwise);
46474635

46484636
const Class& klass =
46494637
Class::ZoneHandle(Z, Library::LookupCoreClass(Symbols::AssertionError()));
46504638
ASSERT(!klass.IsNull());
4639+
const auto& error = klass.EnsureIsFinalized(thread());
4640+
ASSERT(error == Error::null());
46514641

46524642
Fragment otherwise_fragment(otherwise);
46534643
if (CompilerState::Current().is_aot()) {

runtime/vm/compiler/frontend/kernel_binary_flowgraph.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ class StreamingFlowGraphBuilder : public KernelReaderHelper {
158158
Fragment LoadLocal(LocalVariable* variable);
159159
IndirectGotoInstr* IndirectGoto(intptr_t target_count);
160160
Fragment Return(TokenPosition position);
161-
Fragment EvaluateAssertion();
162161
Fragment RethrowException(TokenPosition position, int catch_try_index);
163162
Fragment ThrowNoSuchMethodError(TokenPosition position,
164163
const Function& target,

runtime/vm/compiler/frontend/kernel_to_il.cc

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,19 +2281,6 @@ bool FlowGraphBuilder::NeedsDebugStepCheck(Value* value,
22812281
return false;
22822282
}
22832283

2284-
Fragment FlowGraphBuilder::EvaluateAssertion() {
2285-
const Class& klass =
2286-
Class::ZoneHandle(Z, Library::LookupCoreClass(Symbols::AssertionError()));
2287-
ASSERT(!klass.IsNull());
2288-
const auto& error = klass.EnsureIsFinalized(H.thread());
2289-
ASSERT(error == Error::null());
2290-
const Function& target = Function::ZoneHandle(
2291-
Z, klass.LookupStaticFunctionAllowPrivate(Symbols::EvaluateAssertion()));
2292-
ASSERT(!target.IsNull());
2293-
return StaticCall(TokenPosition::kNoSource, target, /* argument_count = */ 1,
2294-
ICData::kStatic);
2295-
}
2296-
22972284
Fragment FlowGraphBuilder::CheckAssignable(const AbstractType& dst_type,
22982285
const String& dst_name,
22992286
AssertAssignableInstr::Kind kind,

runtime/vm/compiler/frontend/kernel_to_il.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ class FlowGraphBuilder : public BaseFlowGraphBuilder {
277277
Fragment BuildImplicitClosureCreation(TokenPosition position,
278278
const Function& target);
279279

280-
Fragment EvaluateAssertion();
281280
Fragment CheckVariableTypeInCheckedMode(const AbstractType& dst_type,
282281
const String& name_symbol);
283282
Fragment CheckAssignable(

runtime/vm/symbols.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ class ObjectPointerVisitor;
100100
V(EqualOperator, "==") \
101101
V(Error, "Error") \
102102
V(EvalSourceUri, "evaluate:source") \
103-
V(EvaluateAssertion, "_evaluateAssertion") \
104103
V(ExceptionHandlers, "ExceptionHandlers") \
105104
V(ExceptionVar, ":exception_var") \
106105
V(Expando, "Expando") \

sdk/lib/_internal/vm/lib/errors_patch.dart

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,6 @@ class _AssertionError extends Error implements AssertionError {
6666
Object? message,
6767
);
6868

69-
@pragma("vm:entry-point", "call")
70-
static bool _evaluateAssertion(condition) {
71-
if (identical(condition, true) || identical(condition, false)) {
72-
return condition;
73-
}
74-
if (condition is _Closure) {
75-
return (condition as dynamic Function())();
76-
}
77-
if (condition is Function) {
78-
condition = (condition as dynamic Function())();
79-
}
80-
return condition;
81-
}
82-
8369
String get _messageString {
8470
final msg = message;
8571
if (msg == null) return "is not true.";

0 commit comments

Comments
 (0)