Skip to content

Commit faa8bac

Browse files
authored
Make span_close() and span_open() more accurate after set_span() call.
When `Group::set_span()` is called the spans for `Group::span_open()` and `Group::span_close() ` methods just become the whole span instead of the spans the delimiters. This ensure this have at least a more useable value after a call to `Group::set_span()`.
1 parent fb505a7 commit faa8bac

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

library/proc_macro/src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,20 @@ impl Group {
880880
/// tokens at the level of the `Group`.
881881
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
882882
pub fn set_span(&mut self, span: Span) {
883-
self.0.span = bridge::DelimSpan::from_single(span.0);
883+
// This is an invisible delimiter so they have no width, so just use start and end.
884+
if matches!(self.0.delimiter, Delimiter::None) {
885+
self.0.span =
886+
bridge::DelimSpan { open: span.0.start(), close: span.0.end(), entire: span.0 };
887+
return;
888+
}
889+
890+
let rng = span.0.byte_range();
891+
let beg = (rng.end - rng.start).saturating_sub(1);
892+
self.0.span = bridge::DelimSpan {
893+
open: span.0.subspan(Bound::Included(0), Bound::Excluded(1)).unwrap_or(span.0.start()),
894+
close: span.0.subspan(Bound::Included(beg), Bound::Unbounded).unwrap_or(span.0.end()),
895+
entire: span.0,
896+
};
884897
}
885898
}
886899

0 commit comments

Comments
 (0)