about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorest31 <MTest31@outlook.com>2021-01-22 06:57:33 +0100
committerest31 <MTest31@outlook.com>2021-01-24 01:56:54 +0100
commit14aa12fcc2ebee6edfd6092a51122a6a6ba78d4d (patch)
treebaa736c03f2345d7ab829b8ee159e7e4db087355 /compiler
parent202720bf483088dbdb343f78c0aa77067fdd8156 (diff)
downloadrust-14aa12fcc2ebee6edfd6092a51122a6a6ba78d4d.tar.gz
rust-14aa12fcc2ebee6edfd6092a51122a6a6ba78d4d.zip
Replace version_check dependency with own version parsing code
This gives compiler maintainers a better degree of control
over how the version gets parsed and is a good way to ensure
that there are no changes of behaviour in the future.

Also, issue a warning if the version is invalid instead of erroring
so that we stay forwards compatible with possible future changes
of the versioning scheme.

Last, this improves the present test a little.
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_attr/Cargo.toml1
-rw-r--r--compiler/rustc_attr/src/builtin.rs32
2 files changed, 28 insertions, 5 deletions
diff --git a/compiler/rustc_attr/Cargo.toml b/compiler/rustc_attr/Cargo.toml
index 5f941a0a650..dc0711a5b0f 100644
--- a/compiler/rustc_attr/Cargo.toml
+++ b/compiler/rustc_attr/Cargo.toml
@@ -18,4 +18,3 @@ rustc_lexer = { path = "../rustc_lexer" }
 rustc_macros = { path = "../rustc_macros" }
 rustc_session = { path = "../rustc_session" }
 rustc_ast = { path = "../rustc_ast" }
-version_check = "0.9"
diff --git a/compiler/rustc_attr/src/builtin.rs b/compiler/rustc_attr/src/builtin.rs
index ead90f23ce7..26baaf07880 100644
--- a/compiler/rustc_attr/src/builtin.rs
+++ b/compiler/rustc_attr/src/builtin.rs
@@ -10,7 +10,6 @@ use rustc_session::Session;
 use rustc_span::hygiene::Transparency;
 use rustc_span::{symbol::sym, symbol::Symbol, Span};
 use std::num::NonZeroU32;
-use version_check::Version;
 
 pub fn is_builtin_attr(attr: &Attribute) -> bool {
     attr.is_doc_comment() || attr.ident().filter(|ident| is_builtin_attr_name(ident.name)).is_some()
@@ -526,6 +525,26 @@ fn gate_cfg(gated_cfg: &GatedCfg, cfg_span: Span, sess: &ParseSess, features: &F
     }
 }
 
+#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
+struct Version {
+    major: u16,
+    minor: u16,
+    patch: u16,
+}
+
+fn parse_version(s: &str, allow_appendix: bool) -> Option<Version> {
+    let mut components = s.split('-');
+    let d = components.next()?;
+    if !allow_appendix && components.next().is_some() {
+        return None;
+    }
+    let mut digits = d.splitn(3, '.');
+    let major = digits.next()?.parse().ok()?;
+    let minor = digits.next()?.parse().ok()?;
+    let patch = digits.next().unwrap_or("0").parse().ok()?;
+    Some(Version { major, minor, patch })
+}
+
 /// Evaluate a cfg-like condition (with `any` and `all`), using `eval` to
 /// evaluate individual items.
 pub fn eval_condition(
@@ -555,16 +574,21 @@ pub fn eval_condition(
                     return false;
                 }
             };
-            let min_version = match Version::parse(&min_version.as_str()) {
+            let min_version = match parse_version(&min_version.as_str(), false) {
                 Some(ver) => ver,
                 None => {
-                    sess.span_diagnostic.struct_span_err(*span, "invalid version literal").emit();
+                    sess.span_diagnostic
+                        .struct_span_warn(
+                            *span,
+                            "unknown version literal format, assuming it refers to a future version",
+                        )
+                        .emit();
                     return false;
                 }
             };
             let channel = env!("CFG_RELEASE_CHANNEL");
             let nightly = channel == "nightly" || channel == "dev";
-            let rustc_version = Version::parse(env!("CFG_RELEASE")).unwrap();
+            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 }