Skip to content

Commit

Permalink
Fix source spans
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake-Shadle committed Jan 19, 2024
1 parent e1b52c0 commit b1c438e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/bans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ pub fn check(
let mut kids = smallvec::SmallVec::<[Dupe; 2]>::new();

for dup in multi_detector.dupes.iter().cloned() {
let span = &ctx.krate_spans[dup];
let span = &ctx.krate_spans[dup].total;

if span.start < all_start {
all_start = span.start;
Expand Down
35 changes: 15 additions & 20 deletions src/diag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ where
pub type Span = std::ops::Range<usize>;

pub struct KrateSpan {
span: Span,
pub total: Span,
pub source: usize,
}

pub struct KrateSpans {
Expand All @@ -155,11 +156,11 @@ pub struct KrateSpans {
}

impl std::ops::Index<usize> for KrateSpans {
type Output = Span;
type Output = KrateSpan;

#[inline]
fn index(&self, i: usize) -> &Self::Output {
&self.spans[i].span
&self.spans[i]
}
}

Expand All @@ -182,24 +183,18 @@ impl KrateSpans {
krates.sort_unstable_by_key(|a| (&a.name, &a.version));
for krate in krates {
let span_start = sl.len();
match &krate.source {
Some(src) => writeln!(sl, "{} {} {}", krate.name, krate.version, src)
.expect("unable to synthesize lockfile"),
None => writeln!(
sl,
"{} {} {}",
krate.name,
krate.version,
krate.manifest_path.parent().unwrap()
)
.expect("unable to synthesize lockfile"),
let source = if krate.source.is_some() {
krate.id.source()
} else {
krate.manifest_path.parent().unwrap().as_str()
};

let span_end = sl.len() - 1;
writeln!(sl, "{} {} {source}", krate.name, krate.version)
.expect("unable to synthesize lockfile");

spans.push(KrateSpan {
span: span_start..span_end,
});
let total = span_start..sl.len() - 1;
let source = total.end - source.len();
spans.push(KrateSpan { total, source });

let mut sl2 = String::with_capacity(4 * 1024);
let mut deps_map = HashMap::new();
Expand All @@ -223,14 +218,14 @@ impl KrateSpans {

#[inline]
pub fn label_for_index(&self, krate_index: usize, msg: impl Into<String>) -> Label {
Label::secondary(self.file_id, self.spans[krate_index].span.clone()).with_message(msg)
Label::secondary(self.file_id, self.spans[krate_index].total.clone()).with_message(msg)
}

#[inline]
pub fn get_coord(&self, krate_index: usize) -> KrateCoord {
KrateCoord {
file: self.file_id,
span: self.spans[krate_index].span.clone(),
span: self.spans[krate_index].total.clone(),
}
}
}
Expand Down
15 changes: 3 additions & 12 deletions src/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,9 @@ pub fn check(ctx: crate::CheckCtx<'_, ValidConfig>, sink: impl Into<ErrorSink>)

let mut sl = None;
let label = || {
let mut span = ctx.krate_spans[i].clone();

// The krate span is the complete id, but we only want
// to highlight the source component
if let Some(last_space) = krate.id.repr.rfind(' ') {
span.start = span.start + last_space + 1;
} else {
// Nightly 1.77.0 has changed the internal representation
span.end = krate.id.repr.rfind('#').unwrap() - 1;
}

Label::primary(ctx.krate_spans.file_id, span).with_message("source")
let span = &ctx.krate_spans[i];
Label::primary(ctx.krate_spans.file_id, span.source..span.total.end)
.with_message("source")
};

// get allowed list of sources to check
Expand Down

0 comments on commit b1c438e

Please sign in to comment.