Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: dtolnay/proc-macro2
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1.0.68
Choose a base ref
...
head repository: dtolnay/proc-macro2
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 1.0.69
Choose a head ref
  • 11 commits
  • 4 files changed
  • 1 contributor

Commits on Oct 7, 2023

  1. Ignore iter_without_into_iter pedantic clippy lint

        warning: `iter` method without an `IntoIterator` impl for `&RcVec<T>`
          --> src/rcvec.rs:33:5
           |
        33 | /     pub fn iter(&self) -> slice::Iter<T> {
        34 | |         self.inner.iter()
        35 | |     }
           | |_____^
           |
           = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#iter_without_into_iter
           = note: `-W clippy::iter-without-into-iter` implied by `-W clippy::pedantic`
           = help: to override `-W clippy::pedantic` add `#[allow(clippy::iter_without_into_iter)]`
        help: consider implementing `IntoIterator` for `&RcVec<T>`
           |
        24 +
        25 + impl IntoIterator for &RcVec<T> {
        26 +     type IntoIter = std::slice::Iter<'_, T>;
        27 +     type Iter = &T;
        28 +     fn into_iter() -> Self::IntoIter {
        29 +         self.iter()
        30 +     }
        31 + }
           |
    dtolnay committed Oct 7, 2023

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    Copy the full SHA
    4d8410e View commit details

Commits on Oct 8, 2023

  1. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    Copy the full SHA
    12eddc0 View commit details
  2. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    Copy the full SHA
    4c0bd28 View commit details
  3. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    Copy the full SHA
    137ae0a View commit details

Commits on Oct 9, 2023

  1. Merge pull request #411 from dtolnay/sourcetext

    Fix source_text treating span.lo as byte offset not char index
    dtolnay authored Oct 9, 2023

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    Copy the full SHA
    90b8e1e View commit details
  2. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    Copy the full SHA
    0e15461 View commit details
  3. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    Copy the full SHA
    31b14c3 View commit details
  4. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    Copy the full SHA
    c4c3251 View commit details
  5. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    Copy the full SHA
    6461c2d View commit details
  6. Merge pull request #412 from dtolnay/sourcetext

    Cache and lazily build the mapping from char index to byte offset
    dtolnay authored Oct 9, 2023

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    Copy the full SHA
    42dc36e View commit details
  7. Release 1.0.69

    dtolnay committed Oct 9, 2023

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    Copy the full SHA
    937bbcd View commit details
Showing with 62 additions and 12 deletions.
  1. +1 −1 Cargo.toml
  2. +46 −6 src/fallback.rs
  3. +2 −1 src/lib.rs
  4. +13 −4 tests/test.rs
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "proc-macro2"
version = "1.0.68" # remember to update html_root_url
version = "1.0.69" # remember to update html_root_url
authors = ["David Tolnay <dtolnay@gmail.com>", "Alex Crichton <alex@alexcrichton.com>"]
autobenches = false
categories = ["development-tools::procedural-macro-helpers"]
52 changes: 46 additions & 6 deletions src/fallback.rs
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@ use crate::parse::{self, Cursor};
use crate::rcvec::{RcVec, RcVecBuilder, RcVecIntoIter, RcVecMut};
use crate::{Delimiter, Spacing, TokenTree};
#[cfg(all(span_locations, not(fuzzing)))]
use alloc::collections::BTreeMap;
#[cfg(all(span_locations, not(fuzzing)))]
use core::cell::RefCell;
#[cfg(span_locations)]
use core::cmp;
@@ -321,12 +323,13 @@ impl Debug for SourceFile {
#[cfg(all(span_locations, not(fuzzing)))]
thread_local! {
static SOURCE_MAP: RefCell<SourceMap> = RefCell::new(SourceMap {
// NOTE: We start with a single dummy file which all call_site() and
// def_site() spans reference.
// Start with a single dummy file which all call_site() and def_site()
// spans reference.
files: vec![FileInfo {
source_text: String::new(),
span: Span { lo: 0, hi: 0 },
lines: vec![0],
char_index_to_byte_offset: BTreeMap::new(),
}],
});
}
@@ -336,6 +339,7 @@ struct FileInfo {
source_text: String,
span: Span,
lines: Vec<usize>,
char_index_to_byte_offset: BTreeMap<usize, usize>,
}

#[cfg(all(span_locations, not(fuzzing)))]
@@ -362,9 +366,34 @@ impl FileInfo {
span.lo >= self.span.lo && span.hi <= self.span.hi
}

fn source_text(&self, span: Span) -> String {
let lo = (span.lo - self.span.lo) as usize;
let trunc_lo = &self.source_text[lo..];
fn source_text(&mut self, span: Span) -> String {
let lo_char = (span.lo - self.span.lo) as usize;

// Look up offset of the largest already-computed char index that is
// less than or equal to the current requested one. We resume counting
// chars from that point.
let (&last_char_index, &last_byte_offset) = self
.char_index_to_byte_offset
.range(..=lo_char)
.next_back()
.unwrap_or((&0, &0));

let lo_byte = if last_char_index == lo_char {
last_byte_offset
} else {
let total_byte_offset = match self.source_text[last_byte_offset..]
.char_indices()
.nth(lo_char - last_char_index)
{
Some((additional_offset, _ch)) => last_byte_offset + additional_offset,
None => self.source_text.len(),
};
self.char_index_to_byte_offset
.insert(lo_char, total_byte_offset);
total_byte_offset
};

let trunc_lo = &self.source_text[lo_byte..];
let char_len = (span.hi - span.lo) as usize;
let source_text = match trunc_lo.char_indices().nth(char_len) {
Some((offset, _ch)) => &trunc_lo[..offset],
@@ -418,6 +447,8 @@ impl SourceMap {
source_text: src.to_owned(),
span,
lines,
// Populated lazily by source_text().
char_index_to_byte_offset: BTreeMap::new(),
});

span
@@ -445,6 +476,15 @@ impl SourceMap {
}
unreachable!("Invalid span with no related FileInfo!");
}

fn fileinfo_mut(&mut self, span: Span) -> &mut FileInfo {
for file in &mut self.files {
if file.span_within(span) {
return file;
}
}
unreachable!("Invalid span with no related FileInfo!");
}
}

#[derive(Clone, Copy, PartialEq, Eq)]
@@ -569,7 +609,7 @@ impl Span {
if self.is_call_site() {
None
} else {
Some(SOURCE_MAP.with(|cm| cm.borrow().fileinfo(*self).source_text(*self)))
Some(SOURCE_MAP.with(|cm| cm.borrow_mut().fileinfo_mut(*self).source_text(*self)))
}
}
}
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -86,7 +86,7 @@
//! a different thread.
// Proc-macro2 types in rustdoc of other crates get linked to here.
#![doc(html_root_url = "https://docs.rs/proc-macro2/1.0.68")]
#![doc(html_root_url = "https://docs.rs/proc-macro2/1.0.69")]
#![cfg_attr(any(proc_macro_span, super_unstable), feature(proc_macro_span))]
#![cfg_attr(super_unstable, feature(proc_macro_def_site))]
#![cfg_attr(doc_cfg, feature(doc_cfg))]
@@ -95,6 +95,7 @@
clippy::cast_possible_truncation,
clippy::doc_markdown,
clippy::items_after_statements,
clippy::iter_without_into_iter,
clippy::let_underscore_untyped,
clippy::manual_assert,
clippy::manual_range_contains,
17 changes: 13 additions & 4 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -328,10 +328,19 @@ fn literal_span() {
#[cfg(span_locations)]
#[test]
fn source_text() {
let input = " 𓀕 ";
let tokens = input.parse::<proc_macro2::TokenStream>().unwrap();
let ident = tokens.into_iter().next().unwrap();
assert_eq!("𓀕", ident.span().source_text().unwrap());
let input = " 𓀕 a z ";
let mut tokens = input
.parse::<proc_macro2::TokenStream>()
.unwrap()
.into_iter();

let first = tokens.next().unwrap();
assert_eq!("𓀕", first.span().source_text().unwrap());

let second = tokens.next().unwrap();
let third = tokens.next().unwrap();
assert_eq!("z", third.span().source_text().unwrap());
assert_eq!("a", second.span().source_text().unwrap());
}

#[test]