Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(638)

Unified Diff: runtime/vm/flow_graph_builder.cc

Issue 10704216: Fix type checking of void type. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: runtime/vm/flow_graph_builder.cc
===================================================================
--- runtime/vm/flow_graph_builder.cc (revision 9649)
+++ runtime/vm/flow_graph_builder.cc (working copy)
@@ -340,16 +340,6 @@
return true;
}
- // It is a compile-time error to explicitly return a value (including null)
- // from a void function. However, functions that do not explicitly return a
- // value, implicitly return null. This includes void functions. Therefore, we
- // skip the type test here and trust the parser to only return null in void
- // function.
- if (dst_type.IsVoidType()) {
- // TODO(regis): Should we perform this null test at run-time?
- return true;
- }
-
// Do not perform type check elimination if this optimization is turned off.
if (!FLAG_eliminate_type_checks) {
return false;
@@ -362,15 +352,31 @@
return false;
}
+ // If the value is the null constant, its type (NullType) is more specific
+ // than the destination type, even if the destination type is the void type,
+ // since a void function is allowed to return null.
+ if (value->IsConstant() && value->AsConstant()->value().IsNull()) {
+ return true;
+ }
+
+ // Functions that do not explicitly return a value, implicitly return null,
+ // except generative constructors, which return the object being constructed.
+ // It is therefore acceptable for void functions to return null.
+ // In case of a null constant, we have already returned true above, else we
+ // return false here.
+ if (dst_type.IsVoidType()) {
+ return false;
+ }
+
// Consider the static type of the value.
const AbstractType& static_type = AbstractType::Handle(value->StaticType());
ASSERT(!static_type.IsMalformed());
- // If the static type of the value is void, the only allowed value is null,
- // which must be verified by the type test.
- // TODO(regis): Eliminate the test if the value is constant null.
+ // If the static type of the value is void, we are type checking the result of
+ // a void function, which was checked to be null at the return statement
+ // inside the function.
if (static_type.IsVoidType()) {
- return false;
+ return true;
}
// If the static type of the value is NullType, the type test is eliminated.

Powered by Google App Engine
This is Rietveld 408576698