Skip to content

Commit

Permalink
Merge pull request #224 from mystor/rm_invalid_span_workaround
Browse files Browse the repository at this point in the history
Disable the invalid span workaround in spanned.rs on patched versions of rustc
  • Loading branch information
dtolnay committed Jun 20, 2022
2 parents eeabf0d + 00052cc commit 3f694ca
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
37 changes: 37 additions & 0 deletions build.rs
@@ -0,0 +1,37 @@
use std::env;
use std::process::{self, Command};
use std::str;

fn main() {
println!("cargo:rerun-if-changed=build.rs");

let version = match rustc_version() {
Some(version) => version,
None => return,
};

if version.minor < 31 {
eprintln!("Minimum supported rustc version is 1.31");
process::exit(1);
}

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

struct RustcVersion {
minor: u32,
}

fn rustc_version() -> Option<RustcVersion> {
let rustc = env::var_os("RUSTC")?;
let output = Command::new(rustc).arg("--version").output().ok()?;
let version = str::from_utf8(&output.stdout).ok()?;
let mut pieces = version.split('.');
if pieces.next() != Some("rustc 1") {
return None;
}
let minor = pieces.next()?.parse().ok()?;
Some(RustcVersion { minor })
}
7 changes: 4 additions & 3 deletions src/spanned.rs
Expand Up @@ -18,10 +18,11 @@ impl<T: ?Sized + ToTokens> Spanned for T {
}

fn join_spans(tokens: TokenStream) -> Span {
#[cfg(not(needs_invalid_span_workaround))]
let mut iter = tokens.into_iter().map(|tt| tt.span());

#[cfg(needs_invalid_span_workaround)]
let mut iter = tokens.into_iter().filter_map(|tt| {
// FIXME: This shouldn't be required, since optimally spans should
// never be invalid. This filter_map can probably be removed when
// https://github.com/rust-lang/rust/issues/43081 is resolved.
let span = tt.span();
let debug = format!("{:?}", span);
if debug.ends_with("bytes(0..0)") {
Expand Down

0 comments on commit 3f694ca

Please sign in to comment.