Rollup merge of #81468 - est31:cfg_version, r=petrochenkov

cfg(version): treat nightlies as complete

This PR makes cfg(version) treat the nightlies
for version 1.n.0 as 1.n.0, even though that nightly
version might not have all stabilizations and features
of the released 1.n.0. This is done for greater
convenience for people who want to test a newly
stabilized feature on nightly, or in other words,
give newly stabilized features as many eyeballs
as possible.

For users who wish to pin nightlies, this commit adds
a -Z assume-incomplete-release option that they can
enable if they run into any issues due to this change.
Implements the suggestion in https://github.com/rust-lang/rust/issues/64796#issuecomment-640851454
This commit is contained in:
Yuki Okushi 2021-01-30 13:36:50 +09:00 committed by GitHub
commit fe27dea4b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 108 additions and 5 deletions

View File

@ -586,12 +586,14 @@ pub fn eval_condition(
return false;
}
};
let channel = env!("CFG_RELEASE_CHANNEL");
let nightly = channel == "nightly" || channel == "dev";
let rustc_version = parse_version(env!("CFG_RELEASE"), true).unwrap();
// See https://github.com/rust-lang/rust/issues/64796#issuecomment-625474439 for details
if nightly { rustc_version > min_version } else { rustc_version >= min_version }
// See https://github.com/rust-lang/rust/issues/64796#issuecomment-640851454 for details
if sess.assume_incomplete_release {
rustc_version > min_version
} else {
rustc_version >= min_version
}
}
ast::MetaItemKind::List(ref mis) => {
for mi in mis.iter() {

View File

@ -540,6 +540,7 @@ fn test_debugging_options_tracking_hash() {
// This list is in alphabetical order.
tracked!(allow_features, Some(vec![String::from("lang_items")]));
tracked!(always_encode_mir, true);
tracked!(assume_incomplete_release, true);
tracked!(asm_comments, true);
tracked!(binary_dep_depinfo, true);
tracked!(chalk, true);

View File

@ -854,6 +854,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
"only allow the listed language features to be enabled in code (space separated)"),
always_encode_mir: bool = (false, parse_bool, [TRACKED],
"encode MIR of all functions into the crate metadata (default: no)"),
assume_incomplete_release: bool = (false, parse_bool, [TRACKED],
"make cfg(version) treat the current version as incomplete (default: no)"),
asm_comments: bool = (false, parse_bool, [TRACKED],
"generate comments into the assembly (may change behavior) (default: no)"),
ast_json: bool = (false, parse_bool, [UNTRACKED],

View File

@ -138,6 +138,8 @@ pub struct ParseSess {
pub env_depinfo: Lock<FxHashSet<(Symbol, Option<Symbol>)>>,
/// All the type ascriptions expressions that have had a suggestion for likely path typo.
pub type_ascription_path_suggestions: Lock<FxHashSet<Span>>,
/// Whether cfg(version) should treat the current release as incomplete
pub assume_incomplete_release: bool,
}
impl ParseSess {
@ -164,6 +166,7 @@ impl ParseSess {
reached_eof: Lock::new(false),
env_depinfo: Default::default(),
type_ascription_path_suggestions: Default::default(),
assume_incomplete_release: false,
}
}

View File

@ -1344,7 +1344,8 @@ pub fn build_session(
None
};
let parse_sess = ParseSess::with_span_handler(span_diagnostic, source_map);
let mut parse_sess = ParseSess::with_span_handler(span_diagnostic, source_map);
parse_sess.assume_incomplete_release = sopts.debugging_opts.assume_incomplete_release;
let sysroot = match &sopts.maybe_sysroot {
Some(sysroot) => sysroot.clone(),
None => filesearch::get_or_default_sysroot(),

View File

@ -0,0 +1,38 @@
// run-pass
// aux-build:ver-cfg-rel.rs
// revisions: assume no_assume
// [assume]compile-flags: -Z assume-incomplete-release
#![feature(cfg_version)]
extern crate ver_cfg_rel;
use ver_cfg_rel::ver_cfg_rel;
#[ver_cfg_rel("-2")]
fn foo_2() { }
#[ver_cfg_rel("-1")]
fn foo_1() { }
#[cfg(assume)]
#[ver_cfg_rel("0")]
fn foo() { compile_error!("wrong+0") }
#[cfg(no_assume)]
#[ver_cfg_rel("0")]
fn foo() { }
#[ver_cfg_rel("1")]
fn bar() { compile_error!("wrong+1") }
#[ver_cfg_rel("2")]
fn bar() { compile_error!("wrong+2") }
fn main() {
foo_2();
foo_1();
#[cfg(no_assume)]
foo();
}

View File

@ -0,0 +1,56 @@
// force-host
// no-prefer-dynamic
#![crate_type = "proc-macro"]
extern crate proc_macro;
use proc_macro::{TokenStream, TokenTree as Tt};
use std::str::FromStr;
// String containing the current version number of the tip, i.e. "1.41.2"
static VERSION_NUMBER: &str = include_str!("../../../../../version");
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Version {
major: i16,
minor: i16,
patch: i16,
}
fn parse_version(s: &str) -> Option<Version> {
let mut digits = s.splitn(3, '.');
let major = digits.next()?.parse().ok()?;
let minor = digits.next()?.parse().ok()?;
let patch = digits.next().unwrap_or("0").trim().parse().ok()?;
Some(Version { major, minor, patch })
}
#[proc_macro_attribute]
/// Emits a #[cfg(version)] relative to the current one, so passing
/// -1 as argument on compiler 1.50 will emit #[cfg(version("1.49.0"))],
/// while 1 will emit #[cfg(version("1.51.0"))]
pub fn ver_cfg_rel(attr: TokenStream, input: TokenStream) -> TokenStream {
let mut v_rel = None;
for a in attr.into_iter() {
match a {
Tt::Literal(l) => {
let mut s = l.to_string();
let s = s.trim_matches('"');
let v: i16 = s.parse().unwrap();
v_rel = Some(v);
break;
},
_ => panic!("{:?}", a),
}
}
let v_rel = v_rel.unwrap();
let mut v = parse_version(VERSION_NUMBER).unwrap();
v.minor += v_rel;
let attr_str = format!("#[cfg(version(\"{}.{}.{}\"))]", v.major, v.minor, v.patch);
let mut res = Vec::<Tt>::new();
res.extend(TokenStream::from_str(&attr_str).unwrap().into_iter());
res.extend(input.into_iter());
res.into_iter().collect()
}