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.52
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.53
Choose a head ref
  • 4 commits
  • 5 files changed
  • 1 contributor

Commits on Mar 22, 2023

  1. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    Copy the full SHA
    67c6cba View commit details
  2. Call site has no source text

    dtolnay committed Mar 22, 2023

    Verified

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

    Expose proc_macro's source_text() on Span
    dtolnay authored Mar 22, 2023

    Verified

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

    dtolnay committed Mar 22, 2023

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    Copy the full SHA
    98645fd View commit details
Showing with 56 additions and 3 deletions.
  1. +1 −1 Cargo.toml
  2. +4 −0 build.rs
  3. +29 −1 src/fallback.rs
  4. +12 −1 src/lib.rs
  5. +10 −0 src/wrapper.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.52" # remember to update html_root_url
version = "1.0.53" # 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"]
4 changes: 4 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -100,6 +100,10 @@ fn main() {
println!("cargo:rustc-cfg=no_is_available");
}

if version.minor < 66 {
println!("cargo:rustc-cfg=no_source_text");
}

let target = env::var("TARGET").unwrap();
if !enable_use_proc_macro(&target) {
return;
30 changes: 29 additions & 1 deletion src/fallback.rs
Original file line number Diff line number Diff line change
@@ -342,6 +342,7 @@ thread_local! {
files: vec![FileInfo {
#[cfg(procmacro2_semver_exempt)]
name: "<unspecified>".to_owned(),
source_text: String::new(),
span: Span { lo: 0, hi: 0 },
lines: vec![0],
}],
@@ -352,6 +353,7 @@ thread_local! {
struct FileInfo {
#[cfg(procmacro2_semver_exempt)]
name: String,
source_text: String,
span: Span,
lines: Vec<usize>,
}
@@ -379,6 +381,12 @@ impl FileInfo {
fn span_within(&self, span: Span) -> bool {
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 hi = (span.hi - self.span.lo) as usize;
self.source_text[lo..hi].to_owned()
}
}

/// Computes the offsets of each line in the given source string
@@ -425,6 +433,7 @@ impl SourceMap {
self.files.push(FileInfo {
#[cfg(procmacro2_semver_exempt)]
name: name.to_owned(),
source_text: src.to_owned(),
span,
lines,
});
@@ -554,6 +563,20 @@ impl Span {
})
}

#[cfg(not(span_locations))]
pub fn source_text(&self) -> Option<String> {
None
}

#[cfg(span_locations)]
pub fn source_text(&self) -> Option<String> {
if self.is_call_site() {
None
} else {
Some(SOURCE_MAP.with(|cm| cm.borrow().fileinfo(*self).source_text(*self)))
}
}

#[cfg(not(span_locations))]
pub(crate) fn first_byte(self) -> Self {
self
@@ -579,6 +602,11 @@ impl Span {
hi: self.hi,
}
}

#[cfg(span_locations)]
fn is_call_site(&self) -> bool {
self.lo == 0 && self.hi == 0
}
}

impl Debug for Span {
@@ -594,7 +622,7 @@ impl Debug for Span {
pub(crate) fn debug_span_field_if_nontrivial(debug: &mut fmt::DebugStruct, span: Span) {
#[cfg(span_locations)]
{
if span.lo == 0 && span.hi == 0 {
if span.is_call_site() {
return;
}
}
13 changes: 12 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.52")]
#![doc(html_root_url = "https://docs.rs/proc-macro2/1.0.53")]
#![cfg_attr(
any(proc_macro_span, super_unstable),
feature(proc_macro_span, proc_macro_span_shrink)
@@ -528,6 +528,17 @@ impl Span {
pub fn eq(&self, other: &Span) -> bool {
self.inner.eq(&other.inner)
}

/// Returns the source text behind a span. This preserves the original
/// source code, including spaces and comments. It only returns a result if
/// the span corresponds to real source code.
///
/// Note: The observable result of a macro should only rely on the tokens
/// and not on this source text. The result of this function is a best
/// effort to be used for diagnostics only.
pub fn source_text(&self) -> Option<String> {
self.inner.source_text()
}
}

/// Prints a span in a form convenient for debugging.
10 changes: 10 additions & 0 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
@@ -530,6 +530,16 @@ impl Span {
}
}

pub fn source_text(&self) -> Option<String> {
match self {
#[cfg(not(no_source_text))]
Span::Compiler(s) => s.source_text(),
#[cfg(no_source_text)]
Span::Compiler(_) => None,
Span::Fallback(s) => s.source_text(),
}
}

fn unwrap_nightly(self) -> proc_macro::Span {
match self {
Span::Compiler(s) => s,