Skip to content
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

Add a cargo feature to automatically detect usage of a nightly compiler. #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ trybuild = { version = "1.0.99", features = ["diff"] }
default = ["syn-error"]
syn-error = ["dep:syn"]
nightly = []
detect-nightly = []
Comment on lines 34 to +35
Copy link

Choose a reason for hiding this comment

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

Is there a good reason not to simplify things by getting rid of both these feature flags and always using the build script?

There is a reason: to ensure compatibility with future nightly rustc using an incompatible reporting interface. This seems a moot point since (a) a fix can likely be published quickly as a patch release (no one supports old nightlies thus there will not be any supported working configuration to break), (b) stable will still work and (c) it shouldn't be that hard to work around using [patch.crates-io.proc-macro-error-2].

Otherwise...

  1. The existing nightly feature needs to be forwarded from whatever proc-macro lib uses this crate, and that lib needs to tell its users to add their own feature forwarding nightly and to use this when developing, as well as to add a CI test which uses this feature and denies warnings. This path is theoretically workable but has a lot of overhead.
  2. This detect-nightly feature needs to be forwarded from whatever proc-macro lib uses this crate, and that lib needs to tell its users to enable it (either permanently or with their own feature flag used in testing as above). Since even the crate using the proc-macro crate which uses this crate is probably itself a library, it still is likely in the same situation as this crate about what it may assume.
  3. The proc-macro built from this crate may permanently enable detect-nightly since this crate doesn't.

Further, the crate docs still seem to imply that warnings happen on nightly automatically.

My take: both these feature flags should be removed and detection should be automatic.

Copy link
Author

Choose a reason for hiding this comment

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

Further, the crate docs still seem to imply that warnings happen on nightly automatically.

That's because this crate is forked from proc-macro-error which had build scripts for nightly detection. The authors of this crate removed the scripts on the path to syn2.

I think the authors of this repo should list out their goals with the project. Namely where does compatibility with pme1 stack up to our own thoughts on build systems? proc-macro-error used a build script and we're only here because folks are still using it.

Copy link
Author

Choose a reason for hiding this comment

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

My take: both these feature flags should be removed and detection should be automatic.

I agree.

Copy link

Choose a reason for hiding this comment

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

I'm here because (1) this was an easy transition and (2) the alternatives did not support reporting-as-a-side-effect (emit_*!).

So, yes. And I'm not so happy to see that warnings are no longer being reported (without extra set up on my end that I didn't know I had to do).

The best option would be rustc stabilising rust-lang/rust#54140 or equivalent. The next best option is something like proc-macro-error.

Copy link

Choose a reason for hiding this comment

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

Also on this topic: SergioBenitez/version_check#23

Clearly some people have good reasons for not wanting any automatic opt-in to nightly features. This goes as far as affecting development of the compiler, since it complicates testing whether a nightly compiler has regressions over stable. To me this is a good argument not to automatically opt in to nightly features.


[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(run_ui_tests)'] }
unexpected_cfgs = { level = "warn", check-cfg = [
'cfg(run_ui_tests)',
'cfg(detected_nightly)',
] }

[lints.clippy]
pedantic = { level = "warn", priority = -1 }
Expand Down
20 changes: 20 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#[cfg(all(feature = "detect-nightly", not(feature = "nightly")))]
pub fn main() {
let rustc = std::env::var("RUSTC").unwrap_or_else(|_| "rustc".to_string());

let rustc_version = std::process::Command::new(rustc)
.arg("--version")
.output()
.map(|c| c.stdout)
.unwrap_or_default();

let rustc_version = String::from_utf8(rustc_version).unwrap_or_default();
if rustc_version.contains("nightly") {
println!("cargo:rustc-cfg=detected_nightly");
}
}

#[cfg(not(feature = "detect-nightly"))]
pub fn main() {
// do nothing if the 'detect-nightly' feature is not enabled
}
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,10 @@
//! [`proc-macro2::Span`]: https://docs.rs/proc-macro2/1.0.10/proc_macro2/struct.Span.html
//! [`ToTokens`]: https://docs.rs/quote/1.0.3/quote/trait.ToTokens.html
//!

#![cfg_attr(feature = "nightly", feature(proc_macro_diagnostic))]
#![cfg_attr(
any(feature = "nightly", detected_nightly),
feature(proc_macro_diagnostic)
)]
#![forbid(unsafe_code)]

extern crate proc_macro;
Expand All @@ -294,11 +296,11 @@ mod diagnostic;
mod macros;
mod sealed;

#[cfg(not(feature = "nightly"))]
#[cfg(not(any(feature = "nightly", detected_nightly)))]
#[path = "imp/fallback.rs"]
mod imp;

#[cfg(feature = "nightly")]
#[cfg(any(feature = "nightly", detected_nightly))]
#[path = "imp/delegate.rs"]
mod imp;

Expand Down