Skip to content

Commit

Permalink
Merge #54
Browse files Browse the repository at this point in the history
54: Detect if the proc_macro_def_site feature is not disallowed by -Zallow-features r=taiki-e a=taiki-e

Apart from what I mentioned in #53 (comment), there are cases where you want to completely avoid using the nightly function in the first place.

Refs: dtolnay/proc-macro2#175

Co-authored-by: Taiki Endo <te316e89@gmail.com>
  • Loading branch information
bors[bot] and taiki-e authored Aug 29, 2019
2 parents 418cd83 + c2b6819 commit 558a833
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
.idea/
.vscode/
target/
Expand Down
27 changes: 24 additions & 3 deletions pin-project-internal/build.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
// Based on https://stackoverflow.com/a/49250753/1290530

use std::env;

use rustc_version::{version_meta, Channel};

fn main() {
// Set cfg flags depending on release channel
match version_meta().unwrap().channel {
// Enable our feature on nightly, or when using a
// locally build rustc
Channel::Nightly | Channel::Dev => {
println!("cargo:rustc-cfg=feature=\"RUSTC_IS_NIGHTLY\"");
// locally build rustc, unless `-Zallow-features`
// in RUSTFLAGS disallows unstable features.
Channel::Nightly | Channel::Dev if feature_allowed("proc_macro_def_site") => {
println!("cargo:rustc-cfg=proc_macro_def_site");
}
_ => {}
}
}

// Based on https://github.com/alexcrichton/proc-macro2/pull/176
fn feature_allowed(feature: &str) -> bool {
if let Some(rustflags) = env::var_os("RUSTFLAGS") {
for mut flag in rustflags.to_string_lossy().split(' ') {
if flag.starts_with("-Z") {
flag = &flag["-Z".len()..];
}
if flag.starts_with("allow-features=") {
flag = &flag["allow-features=".len()..];
return flag.split(',').any(|allowed| allowed == feature);
}
}
}

// No allow-features= flag, allowed by default.
true
}
2 changes: 1 addition & 1 deletion pin-project-internal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#![warn(single_use_lifetimes)]
#![warn(clippy::all, clippy::pedantic)]
#![allow(clippy::use_self)]
#![cfg_attr(feature = "RUSTC_IS_NIGHTLY", feature(proc_macro_def_site))]
#![cfg_attr(proc_macro_def_site, feature(proc_macro_def_site))]

extern crate proc_macro;

Expand Down
6 changes: 3 additions & 3 deletions pin-project-internal/src/pin_project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ impl Context {
}
} else {
let make_span = || {
#[cfg(feature = "RUSTC_IS_NIGHTLY")]
#[cfg(proc_macro_def_site)]
{
proc_macro::Span::def_site().into()
}

#[cfg(not(feature = "RUSTC_IS_NIGHTLY"))]
#[cfg(not(proc_macro_def_site))]
{
Span::call_site()
}
Expand Down Expand Up @@ -229,7 +229,7 @@ impl Context {
impl #impl_generics ::core::marker::Unpin for #orig_ident #ty_generics #full_where_clause {}
};

if cfg!(feature = "RUSTC_IS_NIGHTLY") {
if cfg!(proc_macro_def_site) {
// On nightly, we use def-site hygiene to make it impossible
// for user code to refer to any of the types we define.
// This allows us to omit wrapping the generated types
Expand Down

0 comments on commit 558a833

Please sign in to comment.