Skip to content

Make .start() and .end() work on Rust 1.88. #508

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
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
21 changes: 17 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fn main() {
println!("cargo:rustc-check-cfg=cfg(no_literal_c_string)");
println!("cargo:rustc-check-cfg=cfg(no_source_text)");
println!("cargo:rustc-check-cfg=cfg(proc_macro_span)");
println!("cargo:rustc-check-cfg=cfg(proc_macro_span_location)");
println!("cargo:rustc-check-cfg=cfg(procmacro2_backtrace)");
println!("cargo:rustc-check-cfg=cfg(procmacro2_nightly_testing)");
println!("cargo:rustc-check-cfg=cfg(procmacro2_semver_exempt)");
Expand Down Expand Up @@ -71,37 +72,43 @@ fn main() {
println!("cargo:rerun-if-changed=build/probe.rs");

let proc_macro_span;
let proc_macro_span_location;
let consider_rustc_bootstrap;
if compile_probe(false) {
// This is a nightly or dev compiler, so it supports unstable features
// regardless of RUSTC_BOOTSTRAP. No need to rerun build script if
// RUSTC_BOOTSTRAP is changed.
proc_macro_span = true;
proc_macro_span_location = true;
consider_rustc_bootstrap = false;
} else if let Some(rustc_bootstrap) = env::var_os("RUSTC_BOOTSTRAP") {
if compile_probe(true) {
// This is a stable or beta compiler for which the user has set
// RUSTC_BOOTSTRAP to turn on unstable features. Rerun build script
// if they change it.
proc_macro_span = true;
proc_macro_span_location = true;
consider_rustc_bootstrap = true;
} else if rustc_bootstrap == "1" {
// This compiler does not support the proc macro Span API in the
// form that proc-macro2 expects. No need to pay attention to
// RUSTC_BOOTSTRAP.
proc_macro_span = false;
proc_macro_span_location = rustc >= 88;
consider_rustc_bootstrap = false;
} else {
// This is a stable or beta compiler for which RUSTC_BOOTSTRAP is
// set to restrict the use of unstable features by this crate.
proc_macro_span = false;
proc_macro_span_location = rustc >= 88;
consider_rustc_bootstrap = true;
}
} else {
// Without RUSTC_BOOTSTRAP, this compiler does not support the proc
// macro Span API in the form that proc-macro2 expects, but try again if
// the user turns on unstable features.
proc_macro_span = false;
proc_macro_span_location = rustc >= 88;
consider_rustc_bootstrap = true;
}

Expand All @@ -116,13 +123,19 @@ fn main() {
}

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

if proc_macro_span_location {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if proc_macro_span_location {
if proc_macro_span || rustc >= 88 {

// Enable non-dummy behavior of Span::start and Span::end methods which
// requires Rust 1.88.
println!("cargo:rustc-cfg=proc_macro_span_location");
}

if semver_exempt && proc_macro_span {
// Implement the semver exempt API in terms of the nightly-only
// proc_macro API.
Expand Down
8 changes: 4 additions & 4 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,12 @@ impl Span {
#[cfg(span_locations)]
pub(crate) fn start(&self) -> LineColumn {
match self {
#[cfg(proc_macro_span)]
#[cfg(proc_macro_span_location)]
Span::Compiler(s) => LineColumn {
line: s.line(),
column: s.column().saturating_sub(1),
},
#[cfg(not(proc_macro_span))]
#[cfg(not(proc_macro_span_location))]
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
Span::Fallback(s) => s.start(),
}
Expand All @@ -445,15 +445,15 @@ impl Span {
#[cfg(span_locations)]
pub(crate) fn end(&self) -> LineColumn {
match self {
#[cfg(proc_macro_span)]
#[cfg(proc_macro_span_location)]
Span::Compiler(s) => {
let end = s.end();
LineColumn {
line: end.line(),
column: end.column().saturating_sub(1),
}
}
#[cfg(not(proc_macro_span))]
#[cfg(not(proc_macro_span_location))]
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
Span::Fallback(s) => s.end(),
}
Expand Down