Skip to content

Commit fe27dea

Browse files
authored
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 #64796 (comment)
2 parents c26dd4d + dd18c48 commit fe27dea

File tree

7 files changed

+108
-5
lines changed

7 files changed

+108
-5
lines changed

compiler/rustc_attr/src/builtin.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -586,12 +586,14 @@ pub fn eval_condition(
586586
return false;
587587
}
588588
};
589-
let channel = env!("CFG_RELEASE_CHANNEL");
590-
let nightly = channel == "nightly" || channel == "dev";
591589
let rustc_version = parse_version(env!("CFG_RELEASE"), true).unwrap();
592590

593-
// See https://github.com/rust-lang/rust/issues/64796#issuecomment-625474439 for details
594-
if nightly { rustc_version > min_version } else { rustc_version >= min_version }
591+
// See https://github.com/rust-lang/rust/issues/64796#issuecomment-640851454 for details
592+
if sess.assume_incomplete_release {
593+
rustc_version > min_version
594+
} else {
595+
rustc_version >= min_version
596+
}
595597
}
596598
ast::MetaItemKind::List(ref mis) => {
597599
for mi in mis.iter() {

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ fn test_debugging_options_tracking_hash() {
540540
// This list is in alphabetical order.
541541
tracked!(allow_features, Some(vec![String::from("lang_items")]));
542542
tracked!(always_encode_mir, true);
543+
tracked!(assume_incomplete_release, true);
543544
tracked!(asm_comments, true);
544545
tracked!(binary_dep_depinfo, true);
545546
tracked!(chalk, true);

compiler/rustc_session/src/options.rs

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

compiler/rustc_session/src/parse.rs

+3
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ pub struct ParseSess {
138138
pub env_depinfo: Lock<FxHashSet<(Symbol, Option<Symbol>)>>,
139139
/// All the type ascriptions expressions that have had a suggestion for likely path typo.
140140
pub type_ascription_path_suggestions: Lock<FxHashSet<Span>>,
141+
/// Whether cfg(version) should treat the current release as incomplete
142+
pub assume_incomplete_release: bool,
141143
}
142144

143145
impl ParseSess {
@@ -164,6 +166,7 @@ impl ParseSess {
164166
reached_eof: Lock::new(false),
165167
env_depinfo: Default::default(),
166168
type_ascription_path_suggestions: Default::default(),
169+
assume_incomplete_release: false,
167170
}
168171
}
169172

compiler/rustc_session/src/session.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1344,7 +1344,8 @@ pub fn build_session(
13441344
None
13451345
};
13461346

1347-
let parse_sess = ParseSess::with_span_handler(span_diagnostic, source_map);
1347+
let mut parse_sess = ParseSess::with_span_handler(span_diagnostic, source_map);
1348+
parse_sess.assume_incomplete_release = sopts.debugging_opts.assume_incomplete_release;
13481349
let sysroot = match &sopts.maybe_sysroot {
13491350
Some(sysroot) => sysroot.clone(),
13501351
None => filesearch::get_or_default_sysroot(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// run-pass
2+
// aux-build:ver-cfg-rel.rs
3+
// revisions: assume no_assume
4+
// [assume]compile-flags: -Z assume-incomplete-release
5+
6+
#![feature(cfg_version)]
7+
8+
extern crate ver_cfg_rel;
9+
10+
use ver_cfg_rel::ver_cfg_rel;
11+
12+
#[ver_cfg_rel("-2")]
13+
fn foo_2() { }
14+
15+
#[ver_cfg_rel("-1")]
16+
fn foo_1() { }
17+
18+
#[cfg(assume)]
19+
#[ver_cfg_rel("0")]
20+
fn foo() { compile_error!("wrong+0") }
21+
22+
#[cfg(no_assume)]
23+
#[ver_cfg_rel("0")]
24+
fn foo() { }
25+
26+
#[ver_cfg_rel("1")]
27+
fn bar() { compile_error!("wrong+1") }
28+
29+
#[ver_cfg_rel("2")]
30+
fn bar() { compile_error!("wrong+2") }
31+
32+
fn main() {
33+
foo_2();
34+
foo_1();
35+
36+
#[cfg(no_assume)]
37+
foo();
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// force-host
2+
// no-prefer-dynamic
3+
4+
#![crate_type = "proc-macro"]
5+
6+
extern crate proc_macro;
7+
use proc_macro::{TokenStream, TokenTree as Tt};
8+
use std::str::FromStr;
9+
10+
// String containing the current version number of the tip, i.e. "1.41.2"
11+
static VERSION_NUMBER: &str = include_str!("../../../../../version");
12+
13+
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
14+
struct Version {
15+
major: i16,
16+
minor: i16,
17+
patch: i16,
18+
}
19+
20+
fn parse_version(s: &str) -> Option<Version> {
21+
let mut digits = s.splitn(3, '.');
22+
let major = digits.next()?.parse().ok()?;
23+
let minor = digits.next()?.parse().ok()?;
24+
let patch = digits.next().unwrap_or("0").trim().parse().ok()?;
25+
Some(Version { major, minor, patch })
26+
}
27+
28+
#[proc_macro_attribute]
29+
/// Emits a #[cfg(version)] relative to the current one, so passing
30+
/// -1 as argument on compiler 1.50 will emit #[cfg(version("1.49.0"))],
31+
/// while 1 will emit #[cfg(version("1.51.0"))]
32+
pub fn ver_cfg_rel(attr: TokenStream, input: TokenStream) -> TokenStream {
33+
let mut v_rel = None;
34+
for a in attr.into_iter() {
35+
match a {
36+
Tt::Literal(l) => {
37+
let mut s = l.to_string();
38+
let s = s.trim_matches('"');
39+
let v: i16 = s.parse().unwrap();
40+
v_rel = Some(v);
41+
break;
42+
},
43+
_ => panic!("{:?}", a),
44+
}
45+
}
46+
let v_rel = v_rel.unwrap();
47+
48+
let mut v = parse_version(VERSION_NUMBER).unwrap();
49+
v.minor += v_rel;
50+
51+
let attr_str = format!("#[cfg(version(\"{}.{}.{}\"))]", v.major, v.minor, v.patch);
52+
let mut res = Vec::<Tt>::new();
53+
res.extend(TokenStream::from_str(&attr_str).unwrap().into_iter());
54+
res.extend(input.into_iter());
55+
res.into_iter().collect()
56+
}

0 commit comments

Comments
 (0)