Skip to content

Commit b769b45

Browse files
committed
Re-add updated Span::start()/end()/line()/column() span-locations methods
Updated for rust-lang/rust#111571
1 parent fe99b5f commit b769b45

File tree

7 files changed

+89
-95
lines changed

7 files changed

+89
-95
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/target
22
**/*.rs.bk
33
Cargo.lock
4+
/.vscode

src/fallback.rs

+19-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#[cfg(span_locations)]
2-
use crate::location::LineColumn;
31
use crate::parse::{self, Cursor};
42
use crate::rcvec::{RcVec, RcVecBuilder, RcVecIntoIter, RcVecMut};
53
use crate::{Delimiter, Spacing, TokenTree};
@@ -356,21 +354,16 @@ struct FileInfo {
356354

357355
#[cfg(all(span_locations, not(fuzzing)))]
358356
impl FileInfo {
359-
fn offset_line_column(&self, offset: usize) -> LineColumn {
357+
/// Returns `(line, column)`.
358+
fn offset_line_column(&self, offset: usize) -> (usize, usize) {
360359
assert!(self.span_within(Span {
361360
lo: offset as u32,
362361
hi: offset as u32,
363362
}));
364363
let offset = offset - self.span.lo as usize;
365364
match self.lines.binary_search(&offset) {
366-
Ok(found) => LineColumn {
367-
line: found + 1,
368-
column: 0,
369-
},
370-
Err(idx) => LineColumn {
371-
line: idx,
372-
column: offset - self.lines[idx - 1],
373-
},
365+
Ok(found) => (found + 1, 0),
366+
Err(idx) => (idx, offset - self.lines[idx - 1]),
374367
}
375368
}
376369

@@ -573,28 +566,31 @@ impl Span {
573566
}
574567

575568
#[cfg(span_locations)]
576-
pub fn start(&self) -> LineColumn {
577-
#[cfg(fuzzing)]
578-
return LineColumn { line: 0, column: 0 };
569+
pub fn start(&self) -> Self {
570+
Self {
571+
lo: self.lo,
572+
hi: self.lo,
573+
}
574+
}
579575

580-
#[cfg(not(fuzzing))]
581-
SOURCE_MAP.with(|sm| {
582-
let sm = sm.borrow();
583-
let fi = sm.fileinfo(*self);
584-
fi.offset_line_column(self.lo as usize)
585-
})
576+
#[cfg(span_locations)]
577+
pub fn end(&self) -> Self {
578+
Self {
579+
lo: self.hi,
580+
hi: self.hi,
581+
}
586582
}
587583

588584
#[cfg(span_locations)]
589-
pub fn end(&self) -> LineColumn {
585+
pub fn line(&self) -> usize {
590586
#[cfg(fuzzing)]
591-
return LineColumn { line: 0, column: 0 };
587+
return 0;
592588

593589
#[cfg(not(fuzzing))]
594590
SOURCE_MAP.with(|sm| {
595591
let sm = sm.borrow();
596592
let fi = sm.fileinfo(*self);
597-
fi.offset_line_column(self.hi as usize)
593+
fi.offset_line_column(self.hi as usize).0
598594
})
599595
}
600596

src/lib.rs

+28-13
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,6 @@ use crate::fallback as imp;
157157
#[cfg(wrap_proc_macro)]
158158
mod imp;
159159

160-
#[cfg(span_locations)]
161-
mod location;
162-
163160
use crate::extra::DelimSpan;
164161
use crate::marker::{ProcMacroAutoTraits, MARKER};
165162
use core::cmp::Ordering;
@@ -173,10 +170,6 @@ use std::error::Error;
173170
#[cfg(procmacro2_semver_exempt)]
174171
use std::path::PathBuf;
175172

176-
#[cfg(span_locations)]
177-
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
178-
pub use crate::location::LineColumn;
179-
180173
/// An abstract stream of tokens, or more concretely a sequence of token trees.
181174
///
182175
/// This type provides interfaces for iterating over token trees and for
@@ -490,7 +483,27 @@ impl Span {
490483
self.inner.byte_range()
491484
}
492485

493-
/// Get the starting line/column in the source file for this span.
486+
/// Creates an empty span pointing to directly before this span.
487+
///
488+
/// This method requires the `"span-locations"` feature to be enabled.
489+
#[cfg(span_locations)]
490+
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
491+
pub fn start(&self) -> Self {
492+
Self::_new(self.inner.start())
493+
}
494+
495+
/// Creates an empty span pointing to directly after this span.
496+
///
497+
/// This method requires the `"span-locations"` feature to be enabled.
498+
#[cfg(span_locations)]
499+
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
500+
pub fn end(&self) -> Self {
501+
Self::_new(self.inner.end())
502+
}
503+
504+
/// The one-indexed line of the source file where the span starts.
505+
///
506+
/// To obtain the line of the span's end, use `span.end().line()`.
494507
///
495508
/// This method requires the `"span-locations"` feature to be enabled.
496509
///
@@ -501,11 +514,13 @@ impl Span {
501514
/// line/column are always meaningful regardless of toolchain.
502515
#[cfg(span_locations)]
503516
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
504-
pub fn start(&self) -> LineColumn {
505-
self.inner.start()
517+
pub fn line(&self) -> usize {
518+
self.inner.line()
506519
}
507520

508-
/// Get the ending line/column in the source file for this span.
521+
/// The one-indexed column of the source file where the span starts.
522+
///
523+
/// To obtain the column of the span's end, use `span.end().column()`.
509524
///
510525
/// This method requires the `"span-locations"` feature to be enabled.
511526
///
@@ -516,8 +531,8 @@ impl Span {
516531
/// line/column are always meaningful regardless of toolchain.
517532
#[cfg(span_locations)]
518533
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
519-
pub fn end(&self) -> LineColumn {
520-
self.inner.end()
534+
pub fn column(&self) -> usize {
535+
self.inner.column()
521536
}
522537

523538
/// Create a new span encompassing `self` and `other`.

src/location.rs

-29
This file was deleted.

src/wrapper.rs

+23-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use crate::detection::inside_proc_macro;
2-
#[cfg(span_locations)]
3-
use crate::location::LineColumn;
42
use crate::{fallback, Delimiter, Punct, Spacing, TokenTree};
53
use core::fmt::{self, Debug, Display};
64
#[cfg(span_locations)]
@@ -474,22 +472,38 @@ impl Span {
474472
}
475473

476474
#[cfg(span_locations)]
477-
pub fn start(&self) -> LineColumn {
475+
pub fn start(&self) -> Self {
476+
match self {
477+
Self::Compiler(s) => Self::Compiler(s.start()),
478+
Self::Fallback(s) => Self::Fallback(s.start()),
479+
}
480+
}
481+
482+
#[cfg(span_locations)]
483+
pub fn end(&self) -> Self {
484+
match self {
485+
Self::Compiler(s) => Self::Compiler(s.end()),
486+
Self::Fallback(s) => Self::Fallback(s.end()),
487+
}
488+
}
489+
490+
#[cfg(span_locations)]
491+
pub fn line(&self) -> usize {
478492
match self {
479-
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
480-
Span::Fallback(s) => s.start(),
493+
Self::Compiler(s) => s.line(),
494+
Self::Fallback(s) => s.line(),
481495
}
482496
}
483497

484498
#[cfg(span_locations)]
485-
pub fn end(&self) -> LineColumn {
499+
pub fn column(&self) -> usize {
486500
match self {
487-
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
488-
Span::Fallback(s) => s.end(),
501+
Self::Compiler(s) => s.column(),
502+
Self::Fallback(s) => s.column(),
489503
}
490504
}
491505

492-
pub fn join(&self, other: Span) -> Option<Span> {
506+
pub fn join(&self, other: Self) -> Option<Span> {
493507
let ret = match (self, other) {
494508
#[cfg(proc_macro_span)]
495509
(Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.join(b)?),

tests/marker.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ assert_impl!(TokenTree is not Send or Sync);
5656

5757
#[cfg(procmacro2_semver_exempt)]
5858
mod semver_exempt {
59-
use proc_macro2::{LineColumn, SourceFile};
60-
61-
assert_impl!(LineColumn is Send and Sync);
59+
use proc_macro2::SourceFile;
6260

6361
assert_impl!(SourceFile is not Send or Sync);
6462
}
@@ -68,7 +66,7 @@ mod unwind_safe {
6866
Delimiter, Group, Ident, LexError, Literal, Punct, Spacing, Span, TokenStream, TokenTree,
6967
};
7068
#[cfg(procmacro2_semver_exempt)]
71-
use proc_macro2::{LineColumn, SourceFile};
69+
use proc_macro2::SourceFile;
7270
use std::panic::{RefUnwindSafe, UnwindSafe};
7371

7472
macro_rules! assert_unwind_safe {
@@ -94,7 +92,6 @@ mod unwind_safe {
9492

9593
#[cfg(procmacro2_semver_exempt)]
9694
assert_unwind_safe! {
97-
LineColumn
9895
SourceFile
9996
}
10097
}

tests/test.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,10 @@ fn literal_span() {
315315

316316
#[cfg(span_locations)]
317317
{
318-
assert_eq!(positive.span().start().column, 0);
319-
assert_eq!(positive.span().end().column, 3);
320-
assert_eq!(negative.span().start().column, 0);
321-
assert_eq!(negative.span().end().column, 4);
318+
assert_eq!(positive.span().start().column(), 0);
319+
assert_eq!(positive.span().end().column(), 3);
320+
assert_eq!(negative.span().start().column(), 0);
321+
assert_eq!(negative.span().end().column(), 4);
322322
assert_eq!(subspan.unwrap().source_text().unwrap(), ".");
323323
}
324324

@@ -434,11 +434,11 @@ testing 123
434434
#[test]
435435
fn default_span() {
436436
let start = Span::call_site().start();
437-
assert_eq!(start.line, 1);
438-
assert_eq!(start.column, 0);
437+
assert_eq!(start.line(), 1);
438+
assert_eq!(start.column(), 0);
439439
let end = Span::call_site().end();
440-
assert_eq!(end.line, 1);
441-
assert_eq!(end.column, 0);
440+
assert_eq!(end.line(), 1);
441+
assert_eq!(end.column(), 0);
442442
let source_file = Span::call_site().source_file();
443443
assert_eq!(source_file.path().to_string_lossy(), "<unspecified>");
444444
assert!(!source_file.is_real());
@@ -471,10 +471,10 @@ fn span_join() {
471471

472472
let start = joined1.unwrap().start();
473473
let end = joined1.unwrap().end();
474-
assert_eq!(start.line, 1);
475-
assert_eq!(start.column, 0);
476-
assert_eq!(end.line, 2);
477-
assert_eq!(end.column, 3);
474+
assert_eq!(start.line(), 1);
475+
assert_eq!(start.column(), 0);
476+
assert_eq!(end.line(), 2);
477+
assert_eq!(end.column(), 3);
478478

479479
assert_eq!(
480480
joined1.unwrap().source_file(),
@@ -719,12 +719,12 @@ fn check_spans_internal(ts: TokenStream, lines: &mut &[(usize, usize, usize, usi
719719
*lines = rest;
720720

721721
let start = i.span().start();
722-
assert_eq!(start.line, sline, "sline did not match for {}", i);
723-
assert_eq!(start.column, scol, "scol did not match for {}", i);
722+
assert_eq!(start.line(), sline, "sline did not match for {}", i);
723+
assert_eq!(start.column(), scol, "scol did not match for {}", i);
724724

725725
let end = i.span().end();
726-
assert_eq!(end.line, eline, "eline did not match for {}", i);
727-
assert_eq!(end.column, ecol, "ecol did not match for {}", i);
726+
assert_eq!(end.line(), eline, "eline did not match for {}", i);
727+
assert_eq!(end.column(), ecol, "ecol did not match for {}", i);
728728

729729
if let TokenTree::Group(g) = i {
730730
check_spans_internal(g.stream().clone(), lines);

0 commit comments

Comments
 (0)