From 3fd1e1ed7f510e42f99da8253481633134d90170 Mon Sep 17 00:00:00 2001 From: Liam Appelbe Date: Wed, 18 May 2022 21:39:40 +0000 Subject: [PATCH] [vm] Exclude enum constructor from coverage 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 Commit-Queue: Liam Appelbe --- runtime/vm/source_report.cc | 8 ++++++++ runtime/vm/source_report_test.cc | 19 ++++++++----------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/runtime/vm/source_report.cc b/runtime/vm/source_report.cc index b5eb2c683be8..e9375c8f52f9 100644 --- a/runtime/vm/source_report.cc +++ b/runtime/vm/source_report.cc @@ -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; } diff --git a/runtime/vm/source_report_test.cc b/runtime/vm/source_report_test.cc index f445ff15c360..4e7a409cb8e9 100644 --- a/runtime/vm/source_report_test.cc +++ b/runtime/vm/source_report_test.cc @@ -1075,6 +1075,7 @@ 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]; @@ -1082,9 +1083,10 @@ ISOLATE_UNIT_TEST_CASE(SourceReport_Coverage_IssueCov386_EnhancedEnums) { "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(); @@ -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\":\"\","