Skip to content

Commit 4f5845e

Browse files
authored
Merge pull request #516 from dtolnay/probe
Make all use of nightly go through probed interface
2 parents 8768125 + fbf3268 commit 4f5845e

File tree

5 files changed

+21
-11
lines changed

5 files changed

+21
-11
lines changed

build.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ fn main() {
2121
println!("cargo:rustc-check-cfg=cfg(no_source_text)");
2222
println!("cargo:rustc-check-cfg=cfg(proc_macro_span)");
2323
println!("cargo:rustc-check-cfg=cfg(procmacro2_backtrace)");
24+
println!("cargo:rustc-check-cfg=cfg(procmacro2_build_probe)");
2425
println!("cargo:rustc-check-cfg=cfg(procmacro2_nightly_testing)");
2526
println!("cargo:rustc-check-cfg=cfg(procmacro2_semver_exempt)");
2627
println!("cargo:rustc-check-cfg=cfg(randomize_layout)");
@@ -67,7 +68,7 @@ fn main() {
6768
return;
6869
}
6970

70-
println!("cargo:rerun-if-changed=build/probe.rs");
71+
println!("cargo:rerun-if-changed=src/probe/proc_macro_span.rs");
7172

7273
let proc_macro_span;
7374
let consider_rustc_bootstrap;
@@ -148,7 +149,7 @@ fn compile_probe(rustc_bootstrap: bool) -> bool {
148149
let rustc = cargo_env_var("RUSTC");
149150
let out_dir = cargo_env_var("OUT_DIR");
150151
let out_subdir = Path::new(&out_dir).join("probe");
151-
let probefile = Path::new("build").join("probe.rs");
152+
let probefile = Path::new("src").join("probe").join("proc_macro_span.rs");
152153

153154
if let Err(err) = fs::create_dir(&out_subdir) {
154155
if err.kind() != ErrorKind::AlreadyExists {
@@ -172,6 +173,7 @@ fn compile_probe(rustc_bootstrap: bool) -> bool {
172173
}
173174

174175
cmd.stderr(Stdio::null())
176+
.arg("--cfg=procmacro2_build_probe")
175177
.arg("--edition=2021")
176178
.arg("--crate-name=proc_macro2")
177179
.arg("--crate-type=lib")

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ extern crate proc_macro;
141141

142142
mod marker;
143143
mod parse;
144+
mod probe;
144145
mod rcvec;
145146

146147
#[cfg(wrap_proc_macro)]

src/probe.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![allow(dead_code)]
2+
3+
#[cfg(proc_macro_span)]
4+
pub(crate) mod proc_macro_span;

build/probe.rs renamed to src/probe/proc_macro_span.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// If the current toolchain is able to compile it, then proc-macro2 is able to
33
// offer these APIs too.
44

5-
#![feature(proc_macro_span)]
5+
#![cfg_attr(procmacro2_build_probe, feature(proc_macro_span))]
66

77
extern crate proc_macro;
88

@@ -38,4 +38,5 @@ pub fn subspan<R: RangeBounds<usize>>(this: &Literal, range: R) -> Option<Span>
3838
}
3939

4040
// Include in sccache cache key.
41+
#[cfg(procmacro2_build_probe)]
4142
const _: Option<&str> = option_env!("RUSTC_BOOTSTRAP");

src/wrapper.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use crate::detection::inside_proc_macro;
22
use crate::fallback::{self, FromStr2 as _};
33
#[cfg(span_locations)]
44
use crate::location::LineColumn;
5+
#[cfg(proc_macro_span)]
6+
use crate::probe::proc_macro_span;
57
use crate::{Delimiter, Punct, Spacing, TokenTree};
68
use core::fmt::{self, Debug, Display};
79
#[cfg(span_locations)]
@@ -421,7 +423,7 @@ impl Span {
421423
pub(crate) fn byte_range(&self) -> Range<usize> {
422424
match self {
423425
#[cfg(proc_macro_span)]
424-
Span::Compiler(s) => s.byte_range(),
426+
Span::Compiler(s) => proc_macro_span::byte_range(s),
425427
#[cfg(not(proc_macro_span))]
426428
Span::Compiler(_) => 0..0,
427429
Span::Fallback(s) => s.byte_range(),
@@ -433,8 +435,8 @@ impl Span {
433435
match self {
434436
#[cfg(proc_macro_span)]
435437
Span::Compiler(s) => LineColumn {
436-
line: s.line(),
437-
column: s.column().saturating_sub(1),
438+
line: proc_macro_span::line(s),
439+
column: proc_macro_span::column(s).saturating_sub(1),
438440
},
439441
#[cfg(not(proc_macro_span))]
440442
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
@@ -447,10 +449,10 @@ impl Span {
447449
match self {
448450
#[cfg(proc_macro_span)]
449451
Span::Compiler(s) => {
450-
let end = s.end();
452+
let end = proc_macro_span::end(s);
451453
LineColumn {
452-
line: end.line(),
453-
column: end.column().saturating_sub(1),
454+
line: proc_macro_span::line(&end),
455+
column: proc_macro_span::column(&end).saturating_sub(1),
454456
}
455457
}
456458
#[cfg(not(proc_macro_span))]
@@ -478,7 +480,7 @@ impl Span {
478480
pub(crate) fn join(&self, other: Span) -> Option<Span> {
479481
let ret = match (self, other) {
480482
#[cfg(proc_macro_span)]
481-
(Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.join(b)?),
483+
(Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(proc_macro_span::join(a, b)?),
482484
(Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.join(b)?),
483485
_ => return None,
484486
};
@@ -921,7 +923,7 @@ impl Literal {
921923
pub(crate) fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span> {
922924
match self {
923925
#[cfg(proc_macro_span)]
924-
Literal::Compiler(lit) => lit.subspan(range).map(Span::Compiler),
926+
Literal::Compiler(lit) => proc_macro_span::subspan(lit, range).map(Span::Compiler),
925927
#[cfg(not(proc_macro_span))]
926928
Literal::Compiler(_lit) => None,
927929
Literal::Fallback(lit) => lit.subspan(range).map(Span::Fallback),

0 commit comments

Comments
 (0)