Skip to content

Commit d7f8ce8

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

File tree

8 files changed

+100
-93
lines changed

8 files changed

+100
-93
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

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ rustversion = "1"
3131
proc-macro = []
3232
default = ["proc-macro"]
3333

34-
# Expose methods Span::start and Span::end which give the line/column location
34+
# Expose the old Span::start and Span::end methods which give the line/column location
3535
# of a token.
3636
span-locations = []
3737

src/fallback.rs

+29-22
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};
@@ -357,21 +355,16 @@ struct FileInfo {
357355

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

@@ -516,9 +509,26 @@ impl Span {
516509
}
517510

518511
#[cfg(span_locations)]
519-
pub fn start(&self) -> LineColumn {
512+
pub fn start(&self) -> Self {
513+
Self {
514+
lo: self.lo,
515+
hi: self.lo,
516+
}
517+
}
518+
519+
#[cfg(span_locations)]
520+
pub fn end(&self) -> Self {
521+
Self {
522+
lo: self.hi,
523+
hi: self.hi,
524+
}
525+
}
526+
527+
/// Helper: returns `(line, column)`.
528+
#[cfg(span_locations)]
529+
fn line_column(&self) -> (usize, usize) {
520530
#[cfg(fuzzing)]
521-
return LineColumn { line: 0, column: 0 };
531+
return (0, 0);
522532

523533
#[cfg(not(fuzzing))]
524534
SOURCE_MAP.with(|cm| {
@@ -529,16 +539,13 @@ impl Span {
529539
}
530540

531541
#[cfg(span_locations)]
532-
pub fn end(&self) -> LineColumn {
533-
#[cfg(fuzzing)]
534-
return LineColumn { line: 0, column: 0 };
542+
pub fn line(&self) -> usize {
543+
self.line_column().0
544+
}
535545

536-
#[cfg(not(fuzzing))]
537-
SOURCE_MAP.with(|cm| {
538-
let cm = cm.borrow();
539-
let fi = cm.fileinfo(*self);
540-
fi.offset_line_column(self.hi as usize)
541-
})
546+
#[cfg(span_locations)]
547+
pub fn column(&self) -> usize {
548+
self.line_column().1
542549
}
543550

544551
#[cfg(not(span_locations))]

src/lib.rs

+28-11
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,6 @@ mod imp;
142142

143143
#[cfg(span_locations)]
144144
mod convert;
145-
#[cfg(span_locations)]
146-
mod location;
147145

148146
use crate::extra::DelimSpan;
149147
use crate::marker::Marker;
@@ -157,9 +155,6 @@ use std::error::Error;
157155
#[cfg(procmacro2_semver_exempt)]
158156
use std::path::PathBuf;
159157

160-
#[cfg(span_locations)]
161-
pub use crate::location::LineColumn;
162-
163158
/// An abstract stream of tokens, or more concretely a sequence of token trees.
164159
///
165160
/// This type provides interfaces for iterating over token trees and for
@@ -461,7 +456,27 @@ impl Span {
461456
SourceFile::_new(self.inner.source_file())
462457
}
463458

464-
/// Get the starting line/column in the source file for this span.
459+
/// Creates an empty span pointing to directly before this span.
460+
///
461+
/// This method requires the `"span-locations"` feature to be enabled.
462+
#[cfg(span_locations)]
463+
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
464+
pub fn start(&self) -> Self {
465+
Self::_new(self.inner.start())
466+
}
467+
468+
/// Creates an empty span pointing to directly after this span.
469+
///
470+
/// This method requires the `"span-locations"` feature to be enabled.
471+
#[cfg(span_locations)]
472+
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
473+
pub fn end(&self) -> Self {
474+
Self::_new(self.inner.end())
475+
}
476+
477+
/// The one-indexed line of the source file where the span starts.
478+
///
479+
/// To obtain the line of the span's end, use `span.end().line()`.
465480
///
466481
/// This method requires the `"span-locations"` feature to be enabled.
467482
///
@@ -472,11 +487,13 @@ impl Span {
472487
/// line/column are always meaningful regardless of toolchain.
473488
#[cfg(span_locations)]
474489
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
475-
pub fn start(&self) -> LineColumn {
476-
self.inner.start()
490+
pub fn line(&self) -> usize {
491+
self.inner.line()
477492
}
478493

479-
/// Get the ending line/column in the source file for this span.
494+
/// The one-indexed column of the source file where the span starts.
495+
///
496+
/// To obtain the column of the span's end, use `span.end().column()`.
480497
///
481498
/// This method requires the `"span-locations"` feature to be enabled.
482499
///
@@ -487,8 +504,8 @@ impl Span {
487504
/// line/column are always meaningful regardless of toolchain.
488505
#[cfg(span_locations)]
489506
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
490-
pub fn end(&self) -> LineColumn {
491-
self.inner.end()
507+
pub fn column(&self) -> usize {
508+
self.inner.column()
492509
}
493510

494511
/// 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
use core::iter::FromIterator;
@@ -468,22 +466,38 @@ impl Span {
468466
}
469467

470468
#[cfg(span_locations)]
471-
pub fn start(&self) -> LineColumn {
469+
pub fn start(&self) -> Self {
472470
match self {
473-
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
474-
Span::Fallback(s) => s.start(),
471+
Self::Compiler(s) => Self::Compiler(s.start()),
472+
Self::Fallback(s) => Self::Fallback(s.start()),
475473
}
476474
}
477475

478476
#[cfg(span_locations)]
479-
pub fn end(&self) -> LineColumn {
477+
pub fn end(&self) -> Self {
480478
match self {
481-
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
482-
Span::Fallback(s) => s.end(),
479+
Self::Compiler(s) => Self::Compiler(s.end()),
480+
Self::Fallback(s) => Self::Fallback(s.end()),
483481
}
484482
}
485483

486-
pub fn join(&self, other: Span) -> Option<Span> {
484+
#[cfg(span_locations)]
485+
pub fn line(&self) -> usize {
486+
match self {
487+
Self::Compiler(s) => s.line(),
488+
Self::Fallback(s) => s.line(),
489+
}
490+
}
491+
492+
#[cfg(span_locations)]
493+
pub fn column(&self) -> usize {
494+
match self {
495+
Self::Compiler(s) => s.column(),
496+
Self::Fallback(s) => s.column(),
497+
}
498+
}
499+
500+
pub fn join(&self, other: Self) -> Option<Span> {
487501
let ret = match (self, other) {
488502
#[cfg(proc_macro_span)]
489503
(Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.join(b)?),

tests/marker.rs

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

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

6260
assert_impl!(SourceFile is not Send or Sync);
6361
}
@@ -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
@@ -331,10 +331,10 @@ fn literal_span() {
331331

332332
#[cfg(span_locations)]
333333
{
334-
assert_eq!(positive.span().start().column, 0);
335-
assert_eq!(positive.span().end().column, 3);
336-
assert_eq!(negative.span().start().column, 0);
337-
assert_eq!(negative.span().end().column, 4);
334+
assert_eq!(positive.span().start().column(), 0);
335+
assert_eq!(positive.span().end().column(), 3);
336+
assert_eq!(negative.span().start().column(), 0);
337+
assert_eq!(negative.span().end().column(), 4);
338338
assert_eq!(subspan.unwrap().source_text().unwrap(), ".");
339339
}
340340

@@ -432,11 +432,11 @@ testing 123
432432
#[test]
433433
fn default_span() {
434434
let start = Span::call_site().start();
435-
assert_eq!(start.line, 1);
436-
assert_eq!(start.column, 0);
435+
assert_eq!(start.line(), 1);
436+
assert_eq!(start.column(), 0);
437437
let end = Span::call_site().end();
438-
assert_eq!(end.line, 1);
439-
assert_eq!(end.column, 0);
438+
assert_eq!(end.line(), 1);
439+
assert_eq!(end.column(), 0);
440440
let source_file = Span::call_site().source_file();
441441
assert_eq!(source_file.path().to_string_lossy(), "<unspecified>");
442442
assert!(!source_file.is_real());
@@ -469,10 +469,10 @@ fn span_join() {
469469

470470
let start = joined1.unwrap().start();
471471
let end = joined1.unwrap().end();
472-
assert_eq!(start.line, 1);
473-
assert_eq!(start.column, 0);
474-
assert_eq!(end.line, 2);
475-
assert_eq!(end.column, 3);
472+
assert_eq!(start.line(), 1);
473+
assert_eq!(start.column(), 0);
474+
assert_eq!(end.line(), 2);
475+
assert_eq!(end.column(), 3);
476476

477477
assert_eq!(
478478
joined1.unwrap().source_file(),
@@ -717,12 +717,12 @@ fn check_spans_internal(ts: TokenStream, lines: &mut &[(usize, usize, usize, usi
717717
*lines = rest;
718718

719719
let start = i.span().start();
720-
assert_eq!(start.line, sline, "sline did not match for {}", i);
721-
assert_eq!(start.column, scol, "scol did not match for {}", i);
720+
assert_eq!(start.line(), sline, "sline did not match for {}", i);
721+
assert_eq!(start.column(), scol, "scol did not match for {}", i);
722722

723723
let end = i.span().end();
724-
assert_eq!(end.line, eline, "eline did not match for {}", i);
725-
assert_eq!(end.column, ecol, "ecol did not match for {}", i);
724+
assert_eq!(end.line(), eline, "eline did not match for {}", i);
725+
assert_eq!(end.column(), ecol, "ecol did not match for {}", i);
726726

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

0 commit comments

Comments
 (0)