Skip to content

Commit c4acac6

Browse files
committed
proc_macro: Move subspan to be a method on Span in the bridge
This method is still only used for Literal::subspan, however the implementation only depends on the Span component, so it is simpler and more efficient for now to pass down only the information that is needed. In the future, if more information about the Literal is required in the implementation (e.g. to validate that spans line up as expected with source text), that extra information can be added back with extra arguments.
1 parent b34c79f commit c4acac6

File tree

3 files changed

+38
-44
lines changed

3 files changed

+38
-44
lines changed

compiler/rustc_expand/src/proc_macro_server.rs

+36-37
Original file line numberDiff line numberDiff line change
@@ -436,43 +436,6 @@ impl server::FreeFunctions for Rustc<'_, '_> {
436436
span: self.call_site,
437437
})
438438
}
439-
440-
fn literal_subspan(
441-
&mut self,
442-
literal: Literal<Self::Span, Self::Symbol>,
443-
start: Bound<usize>,
444-
end: Bound<usize>,
445-
) -> Option<Self::Span> {
446-
let span = literal.span;
447-
let length = span.hi().to_usize() - span.lo().to_usize();
448-
449-
let start = match start {
450-
Bound::Included(lo) => lo,
451-
Bound::Excluded(lo) => lo.checked_add(1)?,
452-
Bound::Unbounded => 0,
453-
};
454-
455-
let end = match end {
456-
Bound::Included(hi) => hi.checked_add(1)?,
457-
Bound::Excluded(hi) => hi,
458-
Bound::Unbounded => length,
459-
};
460-
461-
// Bounds check the values, preventing addition overflow and OOB spans.
462-
if start > u32::MAX as usize
463-
|| end > u32::MAX as usize
464-
|| (u32::MAX - start as u32) < span.lo().to_u32()
465-
|| (u32::MAX - end as u32) < span.lo().to_u32()
466-
|| start >= end
467-
|| end > length
468-
{
469-
return None;
470-
}
471-
472-
let new_lo = span.lo() + BytePos::from_usize(start);
473-
let new_hi = span.lo() + BytePos::from_usize(end);
474-
Some(span.with_lo(new_lo).with_hi(new_hi))
475-
}
476439
}
477440

478441
impl server::TokenStream for Rustc<'_, '_> {
@@ -697,6 +660,42 @@ impl server::Span for Rustc<'_, '_> {
697660
Some(first.to(second))
698661
}
699662

663+
fn subspan(
664+
&mut self,
665+
span: Self::Span,
666+
start: Bound<usize>,
667+
end: Bound<usize>,
668+
) -> Option<Self::Span> {
669+
let length = span.hi().to_usize() - span.lo().to_usize();
670+
671+
let start = match start {
672+
Bound::Included(lo) => lo,
673+
Bound::Excluded(lo) => lo.checked_add(1)?,
674+
Bound::Unbounded => 0,
675+
};
676+
677+
let end = match end {
678+
Bound::Included(hi) => hi.checked_add(1)?,
679+
Bound::Excluded(hi) => hi,
680+
Bound::Unbounded => length,
681+
};
682+
683+
// Bounds check the values, preventing addition overflow and OOB spans.
684+
if start > u32::MAX as usize
685+
|| end > u32::MAX as usize
686+
|| (u32::MAX - start as u32) < span.lo().to_u32()
687+
|| (u32::MAX - end as u32) < span.lo().to_u32()
688+
|| start >= end
689+
|| end > length
690+
{
691+
return None;
692+
}
693+
694+
let new_lo = span.lo() + BytePos::from_usize(start);
695+
let new_hi = span.lo() + BytePos::from_usize(end);
696+
Some(span.with_lo(new_lo).with_hi(new_hi))
697+
}
698+
700699
fn resolved_at(&mut self, span: Self::Span, at: Self::Span) -> Self::Span {
701700
span.with_ctxt(at.ctxt())
702701
}

library/proc_macro/src/bridge/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ macro_rules! with_api {
5757
fn track_env_var(var: &str, value: Option<&str>);
5858
fn track_path(path: &str);
5959
fn literal_from_str(s: &str) -> Result<Literal<$S::Span, $S::Symbol>, ()>;
60-
fn literal_subspan(lit: Literal<$S::Span, $S::Symbol>, start: Bound<usize>, end: Bound<usize>) -> Option<$S::Span>;
6160
},
6261
TokenStream {
6362
fn drop($self: $S::TokenStream);
@@ -114,6 +113,7 @@ macro_rules! with_api {
114113
fn before($self: $S::Span) -> $S::Span;
115114
fn after($self: $S::Span) -> $S::Span;
116115
fn join($self: $S::Span, other: $S::Span) -> Option<$S::Span>;
116+
fn subspan($self: $S::Span, start: Bound<usize>, end: Bound<usize>) -> Option<$S::Span>;
117117
fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span;
118118
fn source_text($self: $S::Span) -> Option<String>;
119119
fn save_span($self: $S::Span) -> usize;

library/proc_macro/src/lib.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1379,12 +1379,7 @@ impl Literal {
13791379
// was 'c' or whether it was '\u{63}'.
13801380
#[unstable(feature = "proc_macro_span", issue = "54725")]
13811381
pub fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span> {
1382-
bridge::client::FreeFunctions::literal_subspan(
1383-
self.0.clone(),
1384-
range.start_bound().cloned(),
1385-
range.end_bound().cloned(),
1386-
)
1387-
.map(Span)
1382+
self.0.span.subspan(range.start_bound().cloned(), range.end_bound().cloned()).map(Span)
13881383
}
13891384

13901385
fn with_symbol_and_suffix<R>(&self, f: impl FnOnce(&str, &str) -> R) -> R {

0 commit comments

Comments
 (0)