Skip to content

Commit

Permalink
Perform own detection for proc_macro_span (fixes #39)
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Sep 25, 2023
1 parent f9b4f73 commit 00a4ad1
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 14 deletions.
35 changes: 35 additions & 0 deletions genco-macros/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::env;
use std::process::Command;
use std::str;

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

let version = rustc_version().unwrap_or(RustcVersion {
minor: u32::MAX,
nightly: false,
});

if version.nightly {
println!("cargo:rustc-cfg=proc_macro_span");
}
}

struct RustcVersion {
#[allow(unused)]
minor: u32,
nightly: bool,
}

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 nightly = version.contains("nightly") || version.contains("dev");
let mut pieces = version.split('.');
if pieces.next() != Some("rustc 1") {
return None;
}
let minor = pieces.next()?.parse().ok()?;
Some(RustcVersion { minor, nightly })
}
47 changes: 33 additions & 14 deletions genco-macros/src/fake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,35 @@ pub(crate) struct LineColumn {
}

impl LineColumn {
fn new(line_column: proc_macro2::LineColumn) -> Self {
#[cfg(proc_macro_span)]
fn from_start(span: Span) -> Self {
let span = span.unwrap().start();

Self {
line: line_column.line,
column: line_column.column,
line: span.line(),
column: span.column(),
}
}

#[cfg(proc_macro_span)]
fn from_end(span: Span) -> Self {
let span = span.unwrap().end();

Self {
line: span.line(),
column: span.column(),
}
}

#[cfg(not(proc_macro_span))]
fn from_start(span: Span) -> Self {
Self { line: 0, column: 0 }
}

#[cfg(not(proc_macro_span))]
fn from_end(span: Span) -> Self {
Self { line: 0, column: 0 }
}
}

#[derive(Default)]
Expand All @@ -42,8 +65,8 @@ impl Buf {

/// Construct a cursor from a span.
pub(crate) fn cursor(&mut self, span: Span) -> syn::Result<Cursor> {
let start = span.start();
let end = span.end();
let start = LineColumn::from_start(span);
let end = LineColumn::from_end(span);

if (start.line == 0 && start.column == 0) || (end.line == 0 && end.column == 0) {
// Try compat.
Expand All @@ -61,39 +84,35 @@ impl Buf {
},
))
} else {
Ok(Cursor::new(
span,
LineColumn::new(start),
LineColumn::new(end),
))
Ok(Cursor::new(span, start, end))
}
}

/// The start of the given span.
pub(crate) fn start(&mut self, span: Span) -> syn::Result<LineColumn> {
let start = span.start();
let start = LineColumn::from_start(span);

// Try to use compat layer.
if start.line == 0 && start.column == 0 {
// Try compat.
let (column, _) = self.find_line_column(span)?;
Ok(LineColumn { line: 1, column })
} else {
Ok(LineColumn::new(start))
Ok(start)
}
}

/// The start of the given span.
pub(crate) fn end(&mut self, span: Span) -> syn::Result<LineColumn> {
let end = span.end();
let end = LineColumn::from_end(span);

// Try to use compat layer.
if end.line == 0 && end.column == 0 {
// Try compat.
let (_, column) = self.find_line_column(span)?;
Ok(LineColumn { line: 1, column })
} else {
Ok(LineColumn::new(end))
Ok(end)
}
}

Expand Down
1 change: 1 addition & 0 deletions genco-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#![recursion_limit = "256"]
#![allow(clippy::type_complexity)]
#![cfg_attr(proc_macro_span, feature(proc_macro_span))]

extern crate proc_macro;

Expand Down

0 comments on commit 00a4ad1

Please sign in to comment.