Skip to content

Change proc_macro::Span::byte_range -> byte_offset. #139901

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

Closed
Closed
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
10 changes: 4 additions & 6 deletions compiler/rustc_expand/src/proc_macro_server.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::{Bound, Range};
use std::ops::Bound;

use ast::token::IdentIsRaw;
use pm::bridge::{
@@ -704,14 +704,12 @@ impl server::Span for Rustc<'_, '_> {
span.source_callsite()
}

fn byte_range(&mut self, span: Self::Span) -> Range<usize> {
fn byte_offset(&mut self, span: Self::Span) -> usize {
let source_map = self.psess().source_map();

let relative_start_pos = source_map.lookup_byte_offset(span.lo()).pos;
let relative_end_pos = source_map.lookup_byte_offset(span.hi()).pos;

Range { start: relative_start_pos.0 as usize, end: relative_end_pos.0 as usize }
source_map.lookup_byte_offset(span.lo()).pos.0 as usize
}

fn start(&mut self, span: Self::Span) -> Self::Span {
span.shrink_to_lo()
}
8 changes: 2 additions & 6 deletions library/proc_macro/src/bridge/mod.rs
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@
#![allow(wasm_c_abi)]

use std::hash::Hash;
use std::ops::{Bound, Range};
use std::ops::Bound;
use std::sync::Once;
use std::{fmt, marker, mem, panic, thread};

@@ -85,7 +85,7 @@ macro_rules! with_api {
fn debug($self: $S::Span) -> String;
fn parent($self: $S::Span) -> Option<$S::Span>;
fn source($self: $S::Span) -> $S::Span;
fn byte_range($self: $S::Span) -> Range<usize>;
fn byte_offset($self: $S::Span) -> usize;
fn start($self: $S::Span) -> $S::Span;
fn end($self: $S::Span) -> $S::Span;
fn line($self: $S::Span) -> usize;
@@ -534,7 +534,3 @@ pub struct ExpnGlobals<Span> {
compound_traits!(
struct ExpnGlobals<Span> { def_site, call_site, mixed_site }
);

compound_traits!(
struct Range<T> { start, end }
);
16 changes: 9 additions & 7 deletions library/proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ mod escape;
mod to_tokens;

use std::ffi::CStr;
use std::ops::{Range, RangeBounds};
use std::ops::RangeBounds;
use std::path::PathBuf;
use std::str::FromStr;
use std::{error, fmt};
@@ -506,12 +506,6 @@ impl Span {
Span(self.0.source())
}

/// Returns the span's byte position range in the source file.
#[unstable(feature = "proc_macro_span", issue = "54725")]
pub fn byte_range(&self) -> Range<usize> {
self.0.byte_range()
}

/// Creates an empty span pointing to directly before this span.
#[unstable(feature = "proc_macro_span", issue = "54725")]
pub fn start(&self) -> Span {
@@ -540,6 +534,14 @@ impl Span {
self.0.column()
}

/// The span's byte position in the source file.
///
/// To obtain the byte position of the end of the span, use `span.end().byte_offset()`.
#[unstable(feature = "proc_macro_span", issue = "54725")]
pub fn byte_offset(&self) -> usize {
self.0.byte_offset()
}

/// The path to the source file in which this span occurs, for display purposes.
///
/// This might not correspond to a valid file system path.
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
//! change their representation to be compatible with rust-analyzer's.
use std::{
collections::{HashMap, HashSet},
ops::{Bound, Range},
ops::Bound,
};

use intern::Symbol;
@@ -281,10 +281,6 @@ impl server::Span for RaSpanServer {
// FIXME requires db, returns the top level call site
span
}
fn byte_range(&mut self, span: Self::Span) -> Range<usize> {
// FIXME requires db to resolve the ast id, THIS IS NOT INCREMENTAL
Range { start: span.range.start().into(), end: span.range.end().into() }
}
fn join(&mut self, first: Self::Span, second: Self::Span) -> Option<Self::Span> {
// We can't modify the span range for fixup spans, those are meaningful to fixup, so just
// prefer the non-fixup span.
@@ -386,6 +382,10 @@ impl server::Span for RaSpanServer {
// FIXME requires db to resolve line index, THIS IS NOT INCREMENTAL
1
}
fn byte_offset(&mut self, span: Self::Span) -> usize {
// FIXME requires db to resolve the ast id, THIS IS NOT INCREMENTAL
span.range.start().into()
}
}

impl server::Symbol for RaSpanServer {
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! proc-macro server backend based on [`proc_macro_api::msg::TokenId`] as the backing span.
//! This backend is rather inflexible, used by RustRover and older rust-analyzer versions.
use std::ops::{Bound, Range};
use std::ops::Bound;

use intern::Symbol;
use proc_macro::bridge::{self, server};
@@ -250,9 +250,6 @@ impl server::Span for TokenIdServer {
fn source(&mut self, span: Self::Span) -> Self::Span {
span
}
fn byte_range(&mut self, _span: Self::Span) -> Range<usize> {
Range { start: 0, end: 0 }
}
fn join(&mut self, first: Self::Span, _second: Self::Span) -> Option<Self::Span> {
// Just return the first span again, because some macros will unwrap the result.
Some(first)
@@ -285,6 +282,10 @@ impl server::Span for TokenIdServer {
fn column(&mut self, _span: Self::Span) -> usize {
1
}

fn byte_offset(&mut self, _span: Self::Span) -> usize {
0
}
}

impl server::Symbol for TokenIdServer {