Skip to content

Commit b5dd3c6

Browse files
authored
Merge pull request #517 from dtolnay/startend
Use Span's start, end, line, column methods on stable 1.88+
2 parents 4f5845e + 1d0ffc0 commit b5dd3c6

File tree

4 files changed

+69
-29
lines changed

4 files changed

+69
-29
lines changed

build.rs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ fn main() {
2020
println!("cargo:rustc-check-cfg=cfg(no_literal_c_string)");
2121
println!("cargo:rustc-check-cfg=cfg(no_source_text)");
2222
println!("cargo:rustc-check-cfg=cfg(proc_macro_span)");
23+
println!("cargo:rustc-check-cfg=cfg(proc_macro_span_location)");
2324
println!("cargo:rustc-check-cfg=cfg(procmacro2_backtrace)");
2425
println!("cargo:rustc-check-cfg=cfg(procmacro2_build_probe)");
2526
println!("cargo:rustc-check-cfg=cfg(procmacro2_nightly_testing)");
@@ -68,18 +69,16 @@ fn main() {
6869
return;
6970
}
7071

71-
println!("cargo:rerun-if-changed=src/probe/proc_macro_span.rs");
72-
7372
let proc_macro_span;
7473
let consider_rustc_bootstrap;
75-
if compile_probe(false) {
74+
if compile_probe_unstable("proc_macro_span", false) {
7675
// This is a nightly or dev compiler, so it supports unstable features
7776
// regardless of RUSTC_BOOTSTRAP. No need to rerun build script if
7877
// RUSTC_BOOTSTRAP is changed.
7978
proc_macro_span = true;
8079
consider_rustc_bootstrap = false;
8180
} else if let Some(rustc_bootstrap) = env::var_os("RUSTC_BOOTSTRAP") {
82-
if compile_probe(true) {
81+
if compile_probe_unstable("proc_macro_span", true) {
8382
// This is a stable or beta compiler for which the user has set
8483
// RUSTC_BOOTSTRAP to turn on unstable features. Rerun build script
8584
// if they change it.
@@ -116,13 +115,19 @@ fn main() {
116115
}
117116

118117
if proc_macro_span {
119-
// Enable non-dummy behavior of Span::start and Span::end methods which
120-
// requires an unstable compiler feature. Enabled when building with
121-
// nightly, unless `-Z allow-feature` in RUSTFLAGS disallows unstable
122-
// features.
118+
// Enable non-dummy behavior of Span::byte_range and Span::join methods
119+
// which requires an unstable compiler feature. Enabled when building
120+
// with nightly, unless `-Z allow-feature` in RUSTFLAGS disallows
121+
// unstable features.
123122
println!("cargo:rustc-cfg=proc_macro_span");
124123
}
125124

125+
if proc_macro_span || (rustc >= 88 && compile_probe_stable("proc_macro_span_location")) {
126+
// Enable non-dummy behavior of Span::start and Span::end methods on
127+
// Rust 1.88+.
128+
println!("cargo:rustc-cfg=proc_macro_span_location");
129+
}
130+
126131
if semver_exempt && proc_macro_span {
127132
// Implement the semver exempt API in terms of the nightly-only
128133
// proc_macro API.
@@ -134,22 +139,31 @@ fn main() {
134139
}
135140
}
136141

137-
fn compile_probe(rustc_bootstrap: bool) -> bool {
138-
if env::var_os("RUSTC_STAGE").is_some() {
139-
// We are running inside rustc bootstrap. This is a highly non-standard
140-
// environment with issues such as:
141-
//
142-
// https://github.com/rust-lang/cargo/issues/11138
143-
// https://github.com/rust-lang/rust/issues/114839
144-
//
145-
// Let's just not use nightly features here.
146-
return false;
147-
}
142+
fn compile_probe_unstable(feature: &str, rustc_bootstrap: bool) -> bool {
143+
// RUSTC_STAGE indicates that this crate is being compiled as a dependency
144+
// of a multistage rustc bootstrap. This environment uses Cargo in a highly
145+
// non-standard way with issues such as:
146+
//
147+
// https://github.com/rust-lang/cargo/issues/11138
148+
// https://github.com/rust-lang/rust/issues/114839
149+
//
150+
env::var_os("RUSTC_STAGE").is_none() && do_compile_probe(feature, rustc_bootstrap)
151+
}
152+
153+
fn compile_probe_stable(feature: &str) -> bool {
154+
env::var_os("RUSTC_STAGE").is_some() || do_compile_probe(feature, true)
155+
}
156+
157+
fn do_compile_probe(feature: &str, rustc_bootstrap: bool) -> bool {
158+
println!("cargo:rerun-if-changed=src/probe/{}.rs", feature);
148159

149160
let rustc = cargo_env_var("RUSTC");
150161
let out_dir = cargo_env_var("OUT_DIR");
151162
let out_subdir = Path::new(&out_dir).join("probe");
152-
let probefile = Path::new("src").join("probe").join("proc_macro_span.rs");
163+
let probefile = Path::new("src")
164+
.join("probe")
165+
.join(feature)
166+
.with_extension("rs");
153167

154168
if let Err(err) = fs::create_dir(&out_subdir) {
155169
if err.kind() != ErrorKind::AlreadyExists {

src/probe.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22

33
#[cfg(proc_macro_span)]
44
pub(crate) mod proc_macro_span;
5+
6+
#[cfg(proc_macro_span_location)]
7+
pub(crate) mod proc_macro_span_location;

src/probe/proc_macro_span_location.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// The subset of Span's API stabilized in Rust 1.88.
2+
3+
extern crate proc_macro;
4+
5+
use proc_macro::Span;
6+
7+
pub fn start(this: &Span) -> Span {
8+
this.start()
9+
}
10+
11+
pub fn end(this: &Span) -> Span {
12+
this.end()
13+
}
14+
15+
pub fn line(this: &Span) -> usize {
16+
this.line()
17+
}
18+
19+
pub fn column(this: &Span) -> usize {
20+
this.column()
21+
}

src/wrapper.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use crate::fallback::{self, FromStr2 as _};
44
use crate::location::LineColumn;
55
#[cfg(proc_macro_span)]
66
use crate::probe::proc_macro_span;
7+
#[cfg(all(span_locations, proc_macro_span_location))]
8+
use crate::probe::proc_macro_span_location;
79
use crate::{Delimiter, Punct, Spacing, TokenTree};
810
use core::fmt::{self, Debug, Display};
911
#[cfg(span_locations)]
@@ -433,12 +435,12 @@ impl Span {
433435
#[cfg(span_locations)]
434436
pub(crate) fn start(&self) -> LineColumn {
435437
match self {
436-
#[cfg(proc_macro_span)]
438+
#[cfg(proc_macro_span_location)]
437439
Span::Compiler(s) => LineColumn {
438-
line: proc_macro_span::line(s),
439-
column: proc_macro_span::column(s).saturating_sub(1),
440+
line: proc_macro_span_location::line(s),
441+
column: proc_macro_span_location::column(s).saturating_sub(1),
440442
},
441-
#[cfg(not(proc_macro_span))]
443+
#[cfg(not(proc_macro_span_location))]
442444
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
443445
Span::Fallback(s) => s.start(),
444446
}
@@ -447,15 +449,15 @@ impl Span {
447449
#[cfg(span_locations)]
448450
pub(crate) fn end(&self) -> LineColumn {
449451
match self {
450-
#[cfg(proc_macro_span)]
452+
#[cfg(proc_macro_span_location)]
451453
Span::Compiler(s) => {
452-
let end = proc_macro_span::end(s);
454+
let end = proc_macro_span_location::end(s);
453455
LineColumn {
454-
line: proc_macro_span::line(&end),
455-
column: proc_macro_span::column(&end).saturating_sub(1),
456+
line: proc_macro_span_location::line(&end),
457+
column: proc_macro_span_location::column(&end).saturating_sub(1),
456458
}
457459
}
458-
#[cfg(not(proc_macro_span))]
460+
#[cfg(not(proc_macro_span_location))]
459461
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
460462
Span::Fallback(s) => s.end(),
461463
}

0 commit comments

Comments
 (0)