Skip to content

Commit

Permalink
[vm] Exclude enum constructor from coverage
Browse files Browse the repository at this point in the history
It's not possible for the user to invoke these constructors.

Fixes: https://github.com/dart-lang/coverage/issues/377
TEST=Updated existing enhanced enum coverage test
Change-Id: I8b1fea269b278fcb92f38ba09f039dd8bccbfd31
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/245201
Reviewed-by: Alexander Markov <[email protected]>
Commit-Queue: Liam Appelbe <[email protected]>
  • Loading branch information
liamappelbe authored and Commit Bot committed May 18, 2022
1 parent 58c8427 commit 3fd1e1e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
8 changes: 8 additions & 0 deletions runtime/vm/source_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ bool SourceReport::ShouldSkipFunction(const Function& func) {
}
}

// Enum constuctors cannot be invoked by the user, so ignore them.
if (func.IsGenerativeConstructor()) {
Class& cls = Class::Handle(func.Owner());
if (cls.is_enum_class()) {
return true;
}
}

return false;
}

Expand Down
19 changes: 8 additions & 11 deletions runtime/vm/source_report_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1075,16 +1075,18 @@ ISOLATE_UNIT_TEST_CASE(SourceReport_Coverage_IssueCov341_LateFinalVars) {

ISOLATE_UNIT_TEST_CASE(SourceReport_Coverage_IssueCov386_EnhancedEnums) {
// https://github.com/dart-lang/coverage/issues/386
// https://github.com/dart-lang/coverage/issues/377
// WARNING: This MUST be big enough for the serialised JSON string.
const int kBufferSize = 1024;
char buffer[kBufferSize];
const char* kScript =
"enum FoodType {\n"
" candy();\n"
" const FoodType();\n"
" factory FoodType.candyFactory() => candy;\n"
"}\n"
"void main() {\n"
" final food = FoodType.candy;\n"
" final food = FoodType.candyFactory();\n"
"}\n";

Library& lib = Library::Handle();
Expand All @@ -1103,19 +1105,14 @@ ISOLATE_UNIT_TEST_CASE(SourceReport_Coverage_IssueCov386_EnhancedEnums) {
EXPECT_STREQ(
"{\"type\":\"SourceReport\",\"ranges\":["

// There are two ranges at the FoodType constructor. This one is missed,
// but the one below is hit, so it's ok. The point is that the synthetic
// toString method doesn't appear in this hitmap.
"{\"scriptIndex\":0,\"startPos\":29,\"endPos\":45,\"compiled\":true,"
"\"coverage\":{\"hits\":[],\"misses\":[29]}},"

// Main is hit.
"{\"scriptIndex\":0,\"startPos\":49,\"endPos\":94,\"compiled\":true,"
"{\"scriptIndex\":0,\"startPos\":49,\"endPos\":89,\"compiled\":true,"
"\"coverage\":{\"hits\":[49],\"misses\":[]}},"

// FoodType constructor is hit.
"{\"scriptIndex\":0,\"compiled\":true,\"startPos\":29,\"endPos\":45,"
"\"coverage\":{\"hits\":[29],\"misses\":[]}}],"
// The enum's constructor, and toString, are not included in the hitmap,
// but the factory is included.
"{\"scriptIndex\":0,\"startPos\":93,\"endPos\":147,\"compiled\":true,"
"\"coverage\":{\"hits\":[93,131],\"misses\":[]}}],"

// Only one script in the script table.
"\"scripts\":[{\"type\":\"@Script\",\"fixedId\":true,\"id\":\"\","
Expand Down

0 comments on commit 3fd1e1e

Please sign in to comment.