Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-add updated Span::start()/end()/line()/column() span-locations methods #397

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
**/*.rs.bk
Cargo.lock
/.vscode
57 changes: 32 additions & 25 deletions src/fallback.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[cfg(span_locations)]
use crate::location::LineColumn;
use crate::parse::{self, Cursor};
use crate::rcvec::{RcVec, RcVecBuilder, RcVecIntoIter, RcVecMut};
use crate::{Delimiter, Spacing, TokenTree};
Expand Down Expand Up @@ -356,21 +354,16 @@ struct FileInfo {

#[cfg(all(span_locations, not(fuzzing)))]
impl FileInfo {
fn offset_line_column(&self, offset: usize) -> LineColumn {
/// Returns `(line, column)`.
fn offset_line_column(&self, offset: usize) -> (usize, usize) {
assert!(self.span_within(Span {
lo: offset as u32,
hi: offset as u32,
}));
let offset = offset - self.span.lo as usize;
match self.lines.binary_search(&offset) {
Ok(found) => LineColumn {
line: found + 1,
column: 0,
},
Err(idx) => LineColumn {
line: idx,
column: offset - self.lines[idx - 1],
},
Ok(found) => (found + 1, 0),
Err(idx) => (idx, offset - self.lines[idx - 1]),
}
}

Expand Down Expand Up @@ -573,29 +566,43 @@ impl Span {
}

#[cfg(span_locations)]
pub fn start(&self) -> LineColumn {
pub fn start(&self) -> Self {
Self {
lo: self.lo,
hi: self.lo,
}
}

#[cfg(span_locations)]
pub fn end(&self) -> Self {
Self {
lo: self.hi,
hi: self.hi,
}
}

/// Helper: returns `(line, column)`.
#[cfg(span_locations)]
fn line_column(&self) -> (usize, usize) {
#[cfg(fuzzing)]
return LineColumn { line: 0, column: 0 };
return (0, 0);

#[cfg(not(fuzzing))]
SOURCE_MAP.with(|sm| {
let sm = sm.borrow();
let fi = sm.fileinfo(*self);
SOURCE_MAP.with(|cm| {
let cm = cm.borrow();
let fi = cm.fileinfo(*self);
fi.offset_line_column(self.lo as usize)
})
}

#[cfg(span_locations)]
pub fn end(&self) -> LineColumn {
#[cfg(fuzzing)]
return LineColumn { line: 0, column: 0 };
pub fn line(&self) -> usize {
self.line_column().0
}

#[cfg(not(fuzzing))]
SOURCE_MAP.with(|sm| {
let sm = sm.borrow();
let fi = sm.fileinfo(*self);
fi.offset_line_column(self.hi as usize)
})
#[cfg(span_locations)]
pub fn column(&self) -> usize {
self.line_column().1
}

#[cfg(not(span_locations))]
Expand Down
41 changes: 28 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,6 @@ use crate::fallback as imp;
#[cfg(wrap_proc_macro)]
mod imp;

#[cfg(span_locations)]
mod location;

use crate::extra::DelimSpan;
use crate::marker::{ProcMacroAutoTraits, MARKER};
use core::cmp::Ordering;
Expand All @@ -173,10 +170,6 @@ use std::error::Error;
#[cfg(procmacro2_semver_exempt)]
use std::path::PathBuf;

#[cfg(span_locations)]
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
pub use crate::location::LineColumn;

/// An abstract stream of tokens, or more concretely a sequence of token trees.
///
/// This type provides interfaces for iterating over token trees and for
Expand Down Expand Up @@ -490,7 +483,27 @@ impl Span {
self.inner.byte_range()
}

/// Get the starting line/column in the source file for this span.
/// Creates an empty span pointing to directly before this span.
///
/// This method requires the `"span-locations"` feature to be enabled.
#[cfg(span_locations)]
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
pub fn start(&self) -> Self {
Self::_new(self.inner.start())
}

/// Creates an empty span pointing to directly after this span.
///
/// This method requires the `"span-locations"` feature to be enabled.
#[cfg(span_locations)]
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
pub fn end(&self) -> Self {
Self::_new(self.inner.end())
}

/// The one-indexed line of the source file where the span starts.
///
/// To obtain the line of the span's end, use `span.end().line()`.
///
/// This method requires the `"span-locations"` feature to be enabled.
///
Expand All @@ -501,11 +514,13 @@ impl Span {
/// line/column are always meaningful regardless of toolchain.
#[cfg(span_locations)]
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
pub fn start(&self) -> LineColumn {
self.inner.start()
pub fn line(&self) -> usize {
self.inner.line()
}

/// Get the ending line/column in the source file for this span.
/// The one-indexed column of the source file where the span starts.
///
/// To obtain the column of the span's end, use `span.end().column()`.
///
/// This method requires the `"span-locations"` feature to be enabled.
///
Expand All @@ -516,8 +531,8 @@ impl Span {
/// line/column are always meaningful regardless of toolchain.
#[cfg(span_locations)]
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
pub fn end(&self) -> LineColumn {
self.inner.end()
pub fn column(&self) -> usize {
self.inner.column()
}

/// Create a new span encompassing `self` and `other`.
Expand Down
29 changes: 0 additions & 29 deletions src/location.rs

This file was deleted.

32 changes: 23 additions & 9 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::detection::inside_proc_macro;
#[cfg(span_locations)]
use crate::location::LineColumn;
use crate::{fallback, Delimiter, Punct, Spacing, TokenTree};
use core::fmt::{self, Debug, Display};
#[cfg(span_locations)]
Expand Down Expand Up @@ -474,22 +472,38 @@ impl Span {
}

#[cfg(span_locations)]
pub fn start(&self) -> LineColumn {
pub fn start(&self) -> Self {
match self {
Self::Compiler(s) => Self::Compiler(s.start()),
Self::Fallback(s) => Self::Fallback(s.start()),
}
}

#[cfg(span_locations)]
pub fn end(&self) -> Self {
match self {
Self::Compiler(s) => Self::Compiler(s.end()),
Self::Fallback(s) => Self::Fallback(s.end()),
}
}

#[cfg(span_locations)]
pub fn line(&self) -> usize {
match self {
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
Span::Fallback(s) => s.start(),
Self::Compiler(s) => s.line(),
Self::Fallback(s) => s.line(),
}
}

#[cfg(span_locations)]
pub fn end(&self) -> LineColumn {
pub fn column(&self) -> usize {
match self {
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
Span::Fallback(s) => s.end(),
Self::Compiler(s) => s.column(),
Self::Fallback(s) => s.column(),
}
}

pub fn join(&self, other: Span) -> Option<Span> {
pub fn join(&self, other: Self) -> Option<Span> {
let ret = match (self, other) {
#[cfg(proc_macro_span)]
(Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.join(b)?),
Expand Down
9 changes: 3 additions & 6 deletions tests/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,17 @@ assert_impl!(TokenTree is not Send or Sync);

#[cfg(procmacro2_semver_exempt)]
mod semver_exempt {
use proc_macro2::{LineColumn, SourceFile};

assert_impl!(LineColumn is Send and Sync);
use proc_macro2::SourceFile;

assert_impl!(SourceFile is not Send or Sync);
}

mod unwind_safe {
#[cfg(procmacro2_semver_exempt)]
use proc_macro2::SourceFile;
use proc_macro2::{
Delimiter, Group, Ident, LexError, Literal, Punct, Spacing, Span, TokenStream, TokenTree,
};
#[cfg(procmacro2_semver_exempt)]
use proc_macro2::{LineColumn, SourceFile};
use std::panic::{RefUnwindSafe, UnwindSafe};

macro_rules! assert_unwind_safe {
Expand All @@ -94,7 +92,6 @@ mod unwind_safe {

#[cfg(procmacro2_semver_exempt)]
assert_unwind_safe! {
LineColumn
SourceFile
}
}
32 changes: 16 additions & 16 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,10 @@ fn literal_span() {

#[cfg(span_locations)]
{
assert_eq!(positive.span().start().column, 0);
assert_eq!(positive.span().end().column, 3);
assert_eq!(negative.span().start().column, 0);
assert_eq!(negative.span().end().column, 4);
assert_eq!(positive.span().start().column(), 0);
assert_eq!(positive.span().end().column(), 3);
assert_eq!(negative.span().start().column(), 0);
assert_eq!(negative.span().end().column(), 4);
assert_eq!(subspan.unwrap().source_text().unwrap(), ".");
}

Expand Down Expand Up @@ -434,11 +434,11 @@ testing 123
#[test]
fn default_span() {
let start = Span::call_site().start();
assert_eq!(start.line, 1);
assert_eq!(start.column, 0);
assert_eq!(start.line(), 1);
assert_eq!(start.column(), 0);
let end = Span::call_site().end();
assert_eq!(end.line, 1);
assert_eq!(end.column, 0);
assert_eq!(end.line(), 1);
assert_eq!(end.column(), 0);
let source_file = Span::call_site().source_file();
assert_eq!(source_file.path().to_string_lossy(), "<unspecified>");
assert!(!source_file.is_real());
Expand Down Expand Up @@ -471,10 +471,10 @@ fn span_join() {

let start = joined1.unwrap().start();
let end = joined1.unwrap().end();
assert_eq!(start.line, 1);
assert_eq!(start.column, 0);
assert_eq!(end.line, 2);
assert_eq!(end.column, 3);
assert_eq!(start.line(), 1);
assert_eq!(start.column(), 0);
assert_eq!(end.line(), 2);
assert_eq!(end.column(), 3);

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

let start = i.span().start();
assert_eq!(start.line, sline, "sline did not match for {}", i);
assert_eq!(start.column, scol, "scol did not match for {}", i);
assert_eq!(start.line(), sline, "sline did not match for {}", i);
assert_eq!(start.column(), scol, "scol did not match for {}", i);

let end = i.span().end();
assert_eq!(end.line, eline, "eline did not match for {}", i);
assert_eq!(end.column, ecol, "ecol did not match for {}", i);
assert_eq!(end.line(), eline, "eline did not match for {}", i);
assert_eq!(end.column(), ecol, "ecol did not match for {}", i);

if let TokenTree::Group(g) = i {
check_spans_internal(g.stream().clone(), lines);
Expand Down
Loading