Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds two source span utility functions used in source-based coverage #76003

Merged
merged 1 commit into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,13 @@ impl Span {
span.with_lo(span.hi)
}

#[inline]
/// Returns true if hi == lo
pub fn is_empty(&self) -> bool {
let span = self.data();
span.hi == span.lo
}

/// Returns `self` if `self` is not the dummy span, and `other` otherwise.
pub fn substitute_dummy(self, other: Span) -> Span {
if self.is_dummy() { other } else { self }
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,15 @@ impl SourceMap {
}
}

/// Returns a new `Span` covering the start and end `BytePos`s of the file containing the given
/// `pos`. This can be used to quickly determine if another `BytePos` or `Span` is from the same
/// file.
pub fn lookup_file_span(&self, pos: BytePos) -> Span {
let idx = self.lookup_source_file_idx(pos);
let SourceFile { start_pos, end_pos, .. } = *(*self.files.borrow().source_files)[idx];
Span::with_root_ctxt(start_pos, end_pos)
}

/// Returns `Some(span)`, a union of the LHS and RHS span. The LHS must precede the RHS. If
/// there are gaps between LHS and RHS, the resulting union will cross these gaps.
/// For this to work,
Expand Down