From a2ac685cb44a41b4be4660c910de47e239662948 Mon Sep 17 00:00:00 2001 From: Timothy Zakian Date: Tue, 5 Nov 2024 11:44:06 -0800 Subject: [PATCH] [move] Update source coverage gathering so we don't include any duplicate source spans. --- Cargo.lock | 1 + external-crates/move/Cargo.lock | 1 + .../move/crates/move-coverage/Cargo.toml | 1 + .../move-coverage/src/source_coverage.rs | 19 +++++++++++-------- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 37c9abee35739..0cc986734d022 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7519,6 +7519,7 @@ dependencies = [ "clap", "codespan", "colored", + "indexmap 2.2.6", "move-abstract-interpreter", "move-binary-format", "move-bytecode-source-map", diff --git a/external-crates/move/Cargo.lock b/external-crates/move/Cargo.lock index 03c8bc8d9f1ba..79134dc343a33 100644 --- a/external-crates/move/Cargo.lock +++ b/external-crates/move/Cargo.lock @@ -1892,6 +1892,7 @@ dependencies = [ "clap 4.5.17", "codespan", "colored", + "indexmap 2.5.0", "move-abstract-interpreter", "move-binary-format", "move-bytecode-source-map", diff --git a/external-crates/move/crates/move-coverage/Cargo.toml b/external-crates/move/crates/move-coverage/Cargo.toml index 0511e317289fc..be342a5fa8f70 100644 --- a/external-crates/move/crates/move-coverage/Cargo.toml +++ b/external-crates/move/crates/move-coverage/Cargo.toml @@ -25,6 +25,7 @@ move-ir-types.workspace = true move-binary-format.workspace = true move-bytecode-source-map.workspace = true move-abstract-interpreter.workspace = true +indexmap.workspace = true [features] default = [] diff --git a/external-crates/move/crates/move-coverage/src/source_coverage.rs b/external-crates/move/crates/move-coverage/src/source_coverage.rs index 734950c06a403..b7bbc8765aee2 100644 --- a/external-crates/move/crates/move-coverage/src/source_coverage.rs +++ b/external-crates/move/crates/move-coverage/src/source_coverage.rs @@ -7,6 +7,7 @@ use crate::coverage_map::CoverageMap; use codespan::{Files, Span}; use colored::*; +use indexmap::IndexSet; use move_binary_format::{ file_format::{CodeOffset, FunctionDefinitionIndex}, CompiledModule, @@ -34,7 +35,7 @@ pub struct SourceCoverageBuilder<'a> { source_map: &'a SourceMap, } -#[derive(Debug, Serialize, Eq, PartialEq, Ord, PartialOrd)] +#[derive(Debug, Serialize, Eq, PartialEq, Ord, PartialOrd, Hash)] pub enum AbstractSegment { Bounded { start: u32, end: u32 }, BoundedRight { end: u32 }, @@ -150,7 +151,7 @@ impl<'a> SourceCoverageBuilder<'a> { let end_line = end_loc.line.0; let segments = uncovered_segments .entry(start_line) - .or_insert_with(Vec::new); + .or_insert_with(IndexSet::new); if start_line == end_line { let segment = AbstractSegment::Bounded { start: start_loc.column.0, @@ -159,18 +160,20 @@ impl<'a> SourceCoverageBuilder<'a> { // TODO: There is some issue with the source map where we have multiple spans // from different functions. This can be seen in the source map for `Roles.move` if !segments.contains(&segment) { - segments.push(segment); + segments.insert(segment); } } else { - segments.push(AbstractSegment::BoundedLeft { + segments.insert(AbstractSegment::BoundedLeft { start: start_loc.column.0, }); for i in start_line + 1..end_line { - let segment = uncovered_segments.entry(i).or_insert_with(Vec::new); - segment.push(AbstractSegment::BoundedLeft { start: 0 }); + let segment = uncovered_segments.entry(i).or_insert_with(IndexSet::new); + segment.insert(AbstractSegment::BoundedLeft { start: 0 }); } - let last_segment = uncovered_segments.entry(end_line).or_insert_with(Vec::new); - last_segment.push(AbstractSegment::BoundedRight { + let last_segment = uncovered_segments + .entry(end_line) + .or_insert_with(IndexSet::new); + last_segment.insert(AbstractSegment::BoundedRight { end: end_loc.column.0, }); }