Skip to content

Commit 77564cb

Browse files
MingweiSamueludoprog
authored andcommitted
Re-add updated Span::start()/end()/line()/column() span-locations methods
Updated for rust-lang/rust#111571
1 parent 53167c1 commit 77564cb

File tree

7 files changed

+100
-94
lines changed

7 files changed

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

+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};
@@ -341,21 +339,16 @@ struct FileInfo {
341339

342340
#[cfg(all(span_locations, not(fuzzing)))]
343341
impl FileInfo {
344-
fn offset_line_column(&self, offset: usize) -> LineColumn {
342+
/// Returns `(line, column)`.
343+
fn offset_line_column(&self, offset: usize) -> (usize, usize) {
345344
assert!(self.span_within(Span {
346345
lo: offset as u32,
347346
hi: offset as u32
348347
}));
349348
let offset = offset - self.span.lo as usize;
350349
match self.lines.binary_search(&offset) {
351-
Ok(found) => LineColumn {
352-
line: found + 1,
353-
column: 0,
354-
},
355-
Err(idx) => LineColumn {
356-
line: idx,
357-
column: offset - self.lines[idx - 1],
358-
},
350+
Ok(found) => (found + 1, 0),
351+
Err(idx) => (idx, offset - self.lines[idx - 1]),
359352
}
360353
}
361354

@@ -499,9 +492,26 @@ impl Span {
499492
}
500493

501494
#[cfg(span_locations)]
502-
pub fn start(&self) -> LineColumn {
495+
pub fn start(&self) -> Self {
496+
Self {
497+
lo: self.lo,
498+
hi: self.lo,
499+
}
500+
}
501+
502+
#[cfg(span_locations)]
503+
pub fn end(&self) -> Self {
504+
Self {
505+
lo: self.hi,
506+
hi: self.hi,
507+
}
508+
}
509+
510+
/// Helper: returns `(line, column)`.
511+
#[cfg(span_locations)]
512+
fn line_column(&self) -> (usize, usize) {
503513
#[cfg(fuzzing)]
504-
return LineColumn { line: 0, column: 0 };
514+
return (0, 0);
505515

506516
#[cfg(not(fuzzing))]
507517
SOURCE_MAP.with(|cm| {
@@ -512,16 +522,13 @@ impl Span {
512522
}
513523

514524
#[cfg(span_locations)]
515-
pub fn end(&self) -> LineColumn {
516-
#[cfg(fuzzing)]
517-
return LineColumn { line: 0, column: 0 };
525+
pub fn line(&self) -> usize {
526+
self.line_column().0
527+
}
518528

519-
#[cfg(not(fuzzing))]
520-
SOURCE_MAP.with(|cm| {
521-
let cm = cm.borrow();
522-
let fi = cm.fileinfo(*self);
523-
fi.offset_line_column(self.hi as usize)
524-
})
529+
#[cfg(span_locations)]
530+
pub fn column(&self) -> usize {
531+
self.line_column().1
525532
}
526533

527534
#[cfg(not(span_locations))]

src/lib.rs

+28-12
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,6 @@ use crate::fallback as imp;
143143
#[cfg(wrap_proc_macro)]
144144
mod imp;
145145

146-
#[cfg(span_locations)]
147-
mod location;
148-
149146
use crate::extra::DelimSpan;
150147
use crate::marker::Marker;
151148
use core::cmp::Ordering;
@@ -157,9 +154,6 @@ use std::error::Error;
157154
#[cfg(procmacro2_semver_exempt)]
158155
use std::path::PathBuf;
159156

160-
#[cfg(span_locations)]
161-
pub use crate::location::LineColumn;
162-
163157
/// An abstract stream of tokens, or more concretely a sequence of token trees.
164158
///
165159
/// This type provides interfaces for iterating over token trees and for
@@ -458,7 +452,27 @@ impl Span {
458452
SourceFile::_new(self.inner.source_file())
459453
}
460454

461-
/// Get the starting line/column in the source file for this span.
455+
/// Creates an empty span pointing to directly before this span.
456+
///
457+
/// This method requires the `"span-locations"` feature to be enabled.
458+
#[cfg(span_locations)]
459+
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
460+
pub fn start(&self) -> Self {
461+
Self::_new(self.inner.start())
462+
}
463+
464+
/// Creates an empty span pointing to directly after this span.
465+
///
466+
/// This method requires the `"span-locations"` feature to be enabled.
467+
#[cfg(span_locations)]
468+
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
469+
pub fn end(&self) -> Self {
470+
Self::_new(self.inner.end())
471+
}
472+
473+
/// The one-indexed line of the source file where the span starts.
474+
///
475+
/// To obtain the line of the span's end, use `span.end().line()`.
462476
///
463477
/// This method requires the `"span-locations"` feature to be enabled.
464478
///
@@ -469,11 +483,13 @@ impl Span {
469483
/// line/column are always meaningful regardless of toolchain.
470484
#[cfg(span_locations)]
471485
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
472-
pub fn start(&self) -> LineColumn {
473-
self.inner.start()
486+
pub fn line(&self) -> usize {
487+
self.inner.line()
474488
}
475489

476-
/// Get the ending line/column in the source file for this span.
490+
/// The one-indexed column of the source file where the span starts.
491+
///
492+
/// To obtain the column of the span's end, use `span.end().column()`.
477493
///
478494
/// This method requires the `"span-locations"` feature to be enabled.
479495
///
@@ -484,8 +500,8 @@ impl Span {
484500
/// line/column are always meaningful regardless of toolchain.
485501
#[cfg(span_locations)]
486502
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
487-
pub fn end(&self) -> LineColumn {
488-
self.inner.end()
503+
pub fn column(&self) -> usize {
504+
self.inner.column()
489505
}
490506

491507
/// 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::ops::RangeBounds;
@@ -446,22 +444,38 @@ impl Span {
446444
}
447445

448446
#[cfg(span_locations)]
449-
pub fn start(&self) -> LineColumn {
447+
pub fn start(&self) -> Self {
450448
match self {
451-
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
452-
Span::Fallback(s) => s.start(),
449+
Self::Compiler(s) => Self::Compiler(s.start()),
450+
Self::Fallback(s) => Self::Fallback(s.start()),
453451
}
454452
}
455453

456454
#[cfg(span_locations)]
457-
pub fn end(&self) -> LineColumn {
455+
pub fn end(&self) -> Self {
458456
match self {
459-
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
460-
Span::Fallback(s) => s.end(),
457+
Self::Compiler(s) => Self::Compiler(s.end()),
458+
Self::Fallback(s) => Self::Fallback(s.end()),
461459
}
462460
}
463461

464-
pub fn join(&self, other: Span) -> Option<Span> {
462+
#[cfg(span_locations)]
463+
pub fn line(&self) -> usize {
464+
match self {
465+
Self::Compiler(s) => s.line(),
466+
Self::Fallback(s) => s.line(),
467+
}
468+
}
469+
470+
#[cfg(span_locations)]
471+
pub fn column(&self) -> usize {
472+
match self {
473+
Self::Compiler(s) => s.column(),
474+
Self::Fallback(s) => s.column(),
475+
}
476+
}
477+
478+
pub fn join(&self, other: Self) -> Option<Span> {
465479
let ret = match (self, other) {
466480
#[cfg(proc_macro_span)]
467481
(Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.join(b)?),

tests/marker.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,17 @@ 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
}
6462

6563
mod unwind_safe {
64+
#[cfg(procmacro2_semver_exempt)]
65+
use proc_macro2::SourceFile;
6666
use proc_macro2::{
6767
Delimiter, Group, Ident, LexError, Literal, Punct, Spacing, Span, TokenStream, TokenTree,
6868
};
69-
#[cfg(procmacro2_semver_exempt)]
70-
use proc_macro2::{LineColumn, SourceFile};
7169
use std::panic::{RefUnwindSafe, UnwindSafe};
7270

7371
macro_rules! assert_unwind_safe {
@@ -93,7 +91,6 @@ mod unwind_safe {
9391

9492
#[cfg(procmacro2_semver_exempt)]
9593
assert_unwind_safe! {
96-
LineColumn
9794
SourceFile
9895
}
9996
}

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

@@ -416,11 +416,11 @@ testing 123
416416
#[test]
417417
fn default_span() {
418418
let start = Span::call_site().start();
419-
assert_eq!(start.line, 1);
420-
assert_eq!(start.column, 0);
419+
assert_eq!(start.line(), 1);
420+
assert_eq!(start.column(), 0);
421421
let end = Span::call_site().end();
422-
assert_eq!(end.line, 1);
423-
assert_eq!(end.column, 0);
422+
assert_eq!(end.line(), 1);
423+
assert_eq!(end.column(), 0);
424424
let source_file = Span::call_site().source_file();
425425
assert_eq!(source_file.path().to_string_lossy(), "<unspecified>");
426426
assert!(!source_file.is_real());
@@ -453,10 +453,10 @@ fn span_join() {
453453

454454
let start = joined1.unwrap().start();
455455
let end = joined1.unwrap().end();
456-
assert_eq!(start.line, 1);
457-
assert_eq!(start.column, 0);
458-
assert_eq!(end.line, 2);
459-
assert_eq!(end.column, 3);
456+
assert_eq!(start.line(), 1);
457+
assert_eq!(start.column(), 0);
458+
assert_eq!(end.line(), 2);
459+
assert_eq!(end.column(), 3);
460460

461461
assert_eq!(
462462
joined1.unwrap().source_file(),
@@ -701,12 +701,12 @@ fn check_spans_internal(ts: TokenStream, lines: &mut &[(usize, usize, usize, usi
701701
*lines = rest;
702702

703703
let start = i.span().start();
704-
assert_eq!(start.line, sline, "sline did not match for {}", i);
705-
assert_eq!(start.column, scol, "scol did not match for {}", i);
704+
assert_eq!(start.line(), sline, "sline did not match for {}", i);
705+
assert_eq!(start.column(), scol, "scol did not match for {}", i);
706706

707707
let end = i.span().end();
708-
assert_eq!(end.line, eline, "eline did not match for {}", i);
709-
assert_eq!(end.column, ecol, "ecol did not match for {}", i);
708+
assert_eq!(end.line(), eline, "eline did not match for {}", i);
709+
assert_eq!(end.column(), ecol, "ecol did not match for {}", i);
710710

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

0 commit comments

Comments
 (0)