From 3b62e466ec2a9dfbc3d8db32d4dfcf3fa2bab0d2 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 25 May 2021 16:46:11 -0700 Subject: [PATCH 1/3] Update to semver 1.0.0-rc --- Cargo.toml | 2 +- src/cargo/core/compiler/compilation.rs | 23 +---- .../compiler/context/compilation_files.rs | 2 +- src/cargo/core/dependency.rs | 90 ++++++++++--------- src/cargo/core/registry.rs | 2 +- src/cargo/core/resolver/errors.rs | 2 +- src/cargo/ops/cargo_install.rs | 2 +- src/cargo/sources/registry/index.rs | 10 +-- src/cargo/sources/registry/mod.rs | 6 +- src/cargo/util/mod.rs | 2 + src/cargo/util/semver_ext.rs | 76 ++++++++++++++++ src/cargo/util/to_semver.rs | 2 +- src/cargo/util/toml/mod.rs | 29 +++++- tests/testsuite/build.rs | 4 +- 14 files changed, 170 insertions(+), 82 deletions(-) create mode 100644 src/cargo/util/semver_ext.rs diff --git a/Cargo.toml b/Cargo.toml index 024ef416e07..648cf3153a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,7 +50,7 @@ num_cpus = "1.0" opener = "0.4" percent-encoding = "2.0" rustfix = "0.5.0" -semver = { version = "0.10", features = ["serde"] } +semver = { version = "1.0.0-rc.2", features = ["serde"] } serde = { version = "1.0.123", features = ["derive"] } serde_ignored = "0.1.0" serde_json = { version = "1.0.30", features = ["raw_value"] } diff --git a/src/cargo/core/compiler/compilation.rs b/src/cargo/core/compiler/compilation.rs index cdb62231b80..83cdda7121e 100644 --- a/src/cargo/core/compiler/compilation.rs +++ b/src/cargo/core/compiler/compilation.rs @@ -5,7 +5,6 @@ use std::path::PathBuf; use cargo_platform::CfgExpr; use cargo_util::{paths, ProcessBuilder}; -use semver::Version; use super::BuildContext; use crate::core::compiler::{CompileKind, Metadata, Unit}; @@ -316,10 +315,7 @@ impl<'cfg> Compilation<'cfg> { .env("CARGO_PKG_VERSION_MAJOR", &pkg.version().major.to_string()) .env("CARGO_PKG_VERSION_MINOR", &pkg.version().minor.to_string()) .env("CARGO_PKG_VERSION_PATCH", &pkg.version().patch.to_string()) - .env( - "CARGO_PKG_VERSION_PRE", - &pre_version_component(pkg.version()), - ) + .env("CARGO_PKG_VERSION_PRE", pkg.version().pre.as_str()) .env("CARGO_PKG_VERSION", &pkg.version().to_string()) .env("CARGO_PKG_NAME", &*pkg.name()) .env( @@ -368,23 +364,6 @@ fn fill_rustc_tool_env(mut cmd: ProcessBuilder, unit: &Unit) -> ProcessBuilder { cmd } -fn pre_version_component(v: &Version) -> String { - if v.pre.is_empty() { - return String::new(); - } - - let mut ret = String::new(); - - for (i, x) in v.pre.iter().enumerate() { - if i != 0 { - ret.push('.') - }; - ret.push_str(&x.to_string()); - } - - ret -} - fn target_runner( bcx: &BuildContext<'_, '_>, kind: CompileKind, diff --git a/src/cargo/core/compiler/context/compilation_files.rs b/src/cargo/core/compiler/context/compilation_files.rs index c47bf42bd92..ad1ae2f8fe4 100644 --- a/src/cargo/core/compiler/context/compilation_files.rs +++ b/src/cargo/core/compiler/context/compilation_files.rs @@ -595,7 +595,7 @@ fn hash_rustc_version(bcx: &BuildContext<'_, '_>, hasher: &mut StableHasher) { // // This assumes that the first segment is the important bit ("nightly", // "beta", "dev", etc.). Skip other parts like the `.3` in `-beta.3`. - vers.pre[0].hash(hasher); + vers.pre.split('.').next().hash(hasher); // Keep "host" since some people switch hosts to implicitly change // targets, (like gnu vs musl or gnu vs msvc). In the future, we may want // to consider hashing `unit.kind.short_name()` instead. diff --git a/src/cargo/core/dependency.rs b/src/cargo/core/dependency.rs index 71bab190185..a50534d6c8e 100644 --- a/src/cargo/core/dependency.rs +++ b/src/cargo/core/dependency.rs @@ -1,7 +1,5 @@ -use anyhow::Context as _; use cargo_platform::Platform; use log::trace; -use semver::ReqParseError; use semver::VersionReq; use serde::ser; use serde::Serialize; @@ -11,17 +9,17 @@ use std::rc::Rc; use crate::core::{PackageId, SourceId, Summary}; use crate::util::errors::CargoResult; use crate::util::interning::InternedString; -use crate::util::Config; +use crate::util::{Config, OptVersionReq}; /// Information about a dependency requested by a Cargo manifest. /// Cheap to copy. -#[derive(PartialEq, Eq, Hash, Ord, PartialOrd, Clone, Debug)] +#[derive(PartialEq, Eq, Hash, Clone, Debug)] pub struct Dependency { inner: Rc, } /// The data underlying a `Dependency`. -#[derive(PartialEq, Eq, Hash, Ord, PartialOrd, Clone, Debug)] +#[derive(PartialEq, Eq, Hash, Clone, Debug)] struct Inner { name: InternedString, source_id: SourceId, @@ -32,7 +30,7 @@ struct Inner { /// `registry` is specified. Or in the case of a crates.io dependency, /// `source_id` will be crates.io and this will be None. registry_id: Option, - req: VersionReq, + req: OptVersionReq, specified_req: bool, kind: DepKind, only_match_name: bool, @@ -104,14 +102,32 @@ fn parse_req_with_deprecated( req: &str, extra: Option<(PackageId, &Config)>, ) -> CargoResult { - match VersionReq::parse(req) { - Err(ReqParseError::DeprecatedVersionRequirement(requirement)) => { - let (inside, config) = match extra { - Some(pair) => pair, - None => return Err(ReqParseError::DeprecatedVersionRequirement(requirement).into()), - }; - let msg = format!( - "\ + let err = match VersionReq::parse(req) { + Ok(req) => return Ok(req), + Err(err) => err, + }; + + let (inside, config) = match extra { + Some(pair) => pair, + None => return Err(err.into()), + }; + + let corrected = match req { + ".*" => "*", + "0.1.0." => "0.1.0", + "0.3.1.3" => "0.3.13", + "0.2*" => "0.2.*", + "*.0" => "*", + _ => { + return Err(anyhow::Error::new(err).context(format!( + "failed to parse the version requirement `{}` for dependency `{}`", + req, name, + ))); + } + }; + + let msg = format!( + "\ parsed version requirement `{}` is no longer valid Previous versions of Cargo accepted this malformed requirement, @@ -122,27 +138,15 @@ This will soon become a hard error, so it's either recommended to update to a fixed version or contact the upstream maintainer about this warning. ", - req, - inside.name(), - inside.version(), - requirement - ); - config.shell().warn(&msg)?; - - Ok(requirement) - } - Err(e) => { - let err: CargoResult = Err(e.into()); - let v: VersionReq = err.with_context(|| { - format!( - "failed to parse the version requirement `{}` for dependency `{}`", - req, name - ) - })?; - Ok(v) - } - Ok(v) => Ok(v), - } + req, + inside.name(), + inside.version(), + corrected, + ); + + config.shell().warn(&msg)?; + + Ok(VersionReq::parse(corrected).unwrap()) } impl ser::Serialize for DepKind { @@ -171,8 +175,8 @@ impl Dependency { let name = name.into(); let arg = Some((inside, config)); let (specified_req, version_req) = match version { - Some(v) => (true, parse_req_with_deprecated(name, v, arg)?), - None => (false, VersionReq::any()), + Some(v) => (true, parse_req_with_deprecated(name, v, arg)?.into()), + None => (false, OptVersionReq::Any), }; let mut ret = Dependency::new_override(name, source_id); @@ -193,8 +197,8 @@ impl Dependency { ) -> CargoResult { let name = name.into(); let (specified_req, version_req) = match version { - Some(v) => (true, parse_req_with_deprecated(name, v, None)?), - None => (false, VersionReq::any()), + Some(v) => (true, parse_req_with_deprecated(name, v, None)?.into()), + None => (false, OptVersionReq::Any), }; let mut ret = Dependency::new_override(name, source_id); @@ -214,7 +218,7 @@ impl Dependency { name, source_id, registry_id: None, - req: VersionReq::any(), + req: OptVersionReq::Any, kind: DepKind::Normal, only_match_name: true, optional: false, @@ -228,7 +232,7 @@ impl Dependency { } } - pub fn version_req(&self) -> &VersionReq { + pub fn version_req(&self) -> &OptVersionReq { &self.inner.req } @@ -365,7 +369,7 @@ impl Dependency { /// Sets the version requirement for this dependency. pub fn set_version_req(&mut self, req: VersionReq) -> &mut Dependency { - Rc::make_mut(&mut self.inner).req = req; + Rc::make_mut(&mut self.inner).req = OptVersionReq::Req(req); self } @@ -394,7 +398,7 @@ impl Dependency { id ); let me = Rc::make_mut(&mut self.inner); - me.req = VersionReq::exact(id.version()); + me.req = OptVersionReq::exact(id.version()); // Only update the `precise` of this source to preserve other // information about dependency's source which may not otherwise be diff --git a/src/cargo/core/registry.rs b/src/cargo/core/registry.rs index 029dc49a099..ccaee527c72 100644 --- a/src/cargo/core/registry.rs +++ b/src/cargo/core/registry.rs @@ -5,7 +5,7 @@ use crate::core::{Dependency, PackageId, Source, SourceId, SourceMap, Summary}; use crate::sources::config::SourceConfigMap; use crate::util::errors::CargoResult; use crate::util::interning::InternedString; -use crate::util::{profile, CanonicalUrl, Config}; +use crate::util::{profile, CanonicalUrl, Config, VersionReqExt}; use anyhow::{bail, Context as _}; use log::{debug, trace}; use semver::VersionReq; diff --git a/src/cargo/core/resolver/errors.rs b/src/cargo/core/resolver/errors.rs index 8e9968db4e8..72832f635bb 100644 --- a/src/cargo/core/resolver/errors.rs +++ b/src/cargo/core/resolver/errors.rs @@ -2,7 +2,7 @@ use std::fmt; use crate::core::{Dependency, PackageId, Registry, Summary}; use crate::util::lev_distance::lev_distance; -use crate::util::Config; +use crate::util::{Config, VersionExt}; use anyhow::Error; use super::context::Context; diff --git a/src/cargo/ops/cargo_install.rs b/src/cargo/ops/cargo_install.rs index 1df7e56f532..f0d6e5d971b 100644 --- a/src/cargo/ops/cargo_install.rs +++ b/src/cargo/ops/cargo_install.rs @@ -8,7 +8,7 @@ use crate::core::{Dependency, Edition, Package, PackageId, Source, SourceId, Wor use crate::ops::common_for_install_and_uninstall::*; use crate::sources::{GitSource, PathSource, SourceConfigMap}; use crate::util::errors::CargoResult; -use crate::util::{Config, Filesystem, Rustc, ToSemver}; +use crate::util::{Config, Filesystem, Rustc, ToSemver, VersionReqExt}; use crate::{drop_println, ops}; use anyhow::{bail, format_err, Context as _}; diff --git a/src/cargo/sources/registry/index.rs b/src/cargo/sources/registry/index.rs index 0be4a591f83..ea9b4f63466 100644 --- a/src/cargo/sources/registry/index.rs +++ b/src/cargo/sources/registry/index.rs @@ -70,11 +70,11 @@ use crate::core::dependency::Dependency; use crate::core::{PackageId, SourceId, Summary}; use crate::sources::registry::{RegistryData, RegistryPackage, INDEX_V_MAX}; use crate::util::interning::InternedString; -use crate::util::{internal, CargoResult, Config, Filesystem, ToSemver}; +use crate::util::{internal, CargoResult, Config, Filesystem, OptVersionReq, ToSemver}; use anyhow::bail; use cargo_util::paths; use log::{debug, info}; -use semver::{Version, VersionReq}; +use semver::Version; use std::collections::{HashMap, HashSet}; use std::convert::TryInto; use std::fs; @@ -264,7 +264,7 @@ impl<'cfg> RegistryIndex<'cfg> { /// Returns the hash listed for a specified `PackageId`. pub fn hash(&mut self, pkg: PackageId, load: &mut dyn RegistryData) -> CargoResult<&str> { - let req = VersionReq::exact(pkg.version()); + let req = OptVersionReq::exact(pkg.version()); let summary = self .summaries(pkg.name(), &req, load)? .next() @@ -285,7 +285,7 @@ impl<'cfg> RegistryIndex<'cfg> { pub fn summaries<'a, 'b>( &'a mut self, name: InternedString, - req: &'b VersionReq, + req: &'b OptVersionReq, load: &mut dyn RegistryData, ) -> CargoResult + 'b> where @@ -489,7 +489,7 @@ impl<'cfg> RegistryIndex<'cfg> { } pub fn is_yanked(&mut self, pkg: PackageId, load: &mut dyn RegistryData) -> CargoResult { - let req = VersionReq::exact(pkg.version()); + let req = OptVersionReq::exact(pkg.version()); let found = self .summaries(pkg.name(), &req, load)? .any(|summary| summary.yanked); diff --git a/src/cargo/sources/registry/mod.rs b/src/cargo/sources/registry/mod.rs index 7b08cb3bb72..be2c32c1dd3 100644 --- a/src/cargo/sources/registry/mod.rs +++ b/src/cargo/sources/registry/mod.rs @@ -168,7 +168,7 @@ use std::path::{Path, PathBuf}; use anyhow::Context as _; use flate2::read::GzDecoder; use log::debug; -use semver::{Version, VersionReq}; +use semver::Version; use serde::Deserialize; use tar::Archive; @@ -179,7 +179,7 @@ use crate::sources::PathSource; use crate::util::hex; use crate::util::interning::InternedString; use crate::util::into_url::IntoUrl; -use crate::util::{restricted_names, CargoResult, Config, Filesystem}; +use crate::util::{restricted_names, CargoResult, Config, Filesystem, OptVersionReq}; const PACKAGE_SOURCE_LOCK: &str = ".cargo-ok"; pub const CRATES_IO_INDEX: &str = "https://github.com/rust-lang/crates.io-index"; @@ -671,7 +671,7 @@ impl<'cfg> RegistrySource<'cfg> { // After we've loaded the package configure its summary's `checksum` // field with the checksum we know for this `PackageId`. - let req = VersionReq::exact(package.version()); + let req = OptVersionReq::exact(package.version()); let summary_with_cksum = self .index .summaries(package.name(), &req, &mut *self.ops)? diff --git a/src/cargo/util/mod.rs b/src/cargo/util/mod.rs index e00727c4968..4b8604f92fb 100644 --- a/src/cargo/util/mod.rs +++ b/src/cargo/util/mod.rs @@ -20,6 +20,7 @@ pub use self::progress::{Progress, ProgressStyle}; pub use self::queue::Queue; pub use self::restricted_names::validate_package_name; pub use self::rustc::Rustc; +pub use self::semver_ext::{OptVersionReq, VersionExt, VersionReqExt}; pub use self::to_semver::ToSemver; pub use self::vcs::{existing_vcs_repo, FossilRepo, GitRepo, HgRepo, PijulRepo}; pub use self::workspace::{ @@ -53,6 +54,7 @@ mod progress; mod queue; pub mod restricted_names; pub mod rustc; +mod semver_ext; pub mod to_semver; pub mod toml; mod vcs; diff --git a/src/cargo/util/semver_ext.rs b/src/cargo/util/semver_ext.rs new file mode 100644 index 00000000000..fa9e7a26f2f --- /dev/null +++ b/src/cargo/util/semver_ext.rs @@ -0,0 +1,76 @@ +use semver::{Comparator, Op, Version, VersionReq}; +use std::fmt::{self, Display}; + +#[derive(PartialEq, Eq, Hash, Clone, Debug)] +pub enum OptVersionReq { + Any, + Req(VersionReq), +} + +pub trait VersionExt { + fn is_prerelease(&self) -> bool; +} + +pub trait VersionReqExt { + fn exact(version: &Version) -> Self; +} + +impl VersionExt for Version { + fn is_prerelease(&self) -> bool { + !self.pre.is_empty() + } +} + +impl VersionReqExt for VersionReq { + fn exact(version: &Version) -> Self { + VersionReq { + comparators: vec![Comparator { + op: Op::Exact, + major: version.major, + minor: Some(version.minor), + patch: Some(version.patch), + pre: version.pre.clone(), + }], + } + } +} + +impl OptVersionReq { + pub fn exact(version: &Version) -> Self { + OptVersionReq::Req(VersionReq::exact(version)) + } + + pub fn is_exact(&self) -> bool { + match self { + OptVersionReq::Any => false, + OptVersionReq::Req(req) => { + req.comparators.len() == 1 && { + let cmp = &req.comparators[0]; + cmp.op == Op::Exact && cmp.minor.is_some() && cmp.patch.is_some() + } + } + } + } + + pub fn matches(&self, version: &Version) -> bool { + match self { + OptVersionReq::Any => true, + OptVersionReq::Req(req) => req.matches(version), + } + } +} + +impl Display for OptVersionReq { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + OptVersionReq::Any => formatter.write_str("*"), + OptVersionReq::Req(req) => Display::fmt(req, formatter), + } + } +} + +impl From for OptVersionReq { + fn from(req: VersionReq) -> Self { + OptVersionReq::Req(req) + } +} diff --git a/src/cargo/util/to_semver.rs b/src/cargo/util/to_semver.rs index a6cd0ec96a7..25da9dfb975 100644 --- a/src/cargo/util/to_semver.rs +++ b/src/cargo/util/to_semver.rs @@ -13,7 +13,7 @@ impl ToSemver for Version { impl<'a> ToSemver for &'a str { fn to_semver(self) -> CargoResult { - match Version::parse(self) { + match Version::parse(self.trim()) { Ok(v) => Ok(v), Err(..) => Err(anyhow::format_err!("cannot parse '{}' as a semver", self)), } diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 5ceb19a636e..5718fa4ea06 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -25,7 +25,9 @@ use crate::core::{GitReference, PackageIdSpec, SourceId, WorkspaceConfig, Worksp use crate::sources::{CRATES_IO_INDEX, CRATES_IO_REGISTRY}; use crate::util::errors::{CargoResult, ManifestError}; use crate::util::interning::InternedString; -use crate::util::{self, config::ConfigRelativePath, validate_package_name, Config, IntoUrl}; +use crate::util::{ + self, config::ConfigRelativePath, validate_package_name, Config, IntoUrl, VersionReqExt, +}; mod targets; use self::targets::targets; @@ -778,6 +780,30 @@ impl<'de> de::Deserialize<'de> for VecStringOrBool { } } +fn version_trim_whitespace<'de, D>(deserializer: D) -> Result +where + D: de::Deserializer<'de>, +{ + struct Visitor; + + impl<'de> de::Visitor<'de> for Visitor { + type Value = semver::Version; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("SemVer version") + } + + fn visit_str(self, string: &str) -> Result + where + E: de::Error, + { + string.trim().parse().map_err(de::Error::custom) + } + } + + deserializer.deserialize_str(Visitor) +} + /// Represents the `package`/`project` sections of a `Cargo.toml`. /// /// Note that the order of the fields matters, since this is the order they @@ -790,6 +816,7 @@ pub struct TomlProject { edition: Option, rust_version: Option, name: InternedString, + #[serde(deserialize_with = "version_trim_whitespace")] version: semver::Version, authors: Option>, build: Option, diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index b24ddc358b6..277aa6b41f6 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -269,7 +269,7 @@ fn cargo_compile_with_invalid_version() { [ERROR] failed to parse manifest at `[..]` Caused by: - Expected dot for key `package.version` + unexpected end of input while parsing minor version number for key `package.version` ", ) .run(); @@ -544,7 +544,7 @@ Caused by: failed to parse the version requirement `y` for dependency `crossbeam` Caused by: - the given version requirement is invalid + unexpected character 'y' while parsing major version number ", ) .run(); From 396bdd3a89fe4279374096f95be2f0134f62ea83 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 26 May 2021 14:26:44 -0700 Subject: [PATCH 2/3] Remove parsing of broken version syntax from 5 years ago --- crates/resolver-tests/src/lib.rs | 6 +- src/cargo/core/compiler/standard_lib.rs | 2 +- src/cargo/core/dependency.rs | 89 +++---------------- src/cargo/core/workspace.rs | 1 - src/cargo/ops/cargo_install.rs | 6 +- src/cargo/sources/registry/mod.rs | 2 +- src/cargo/util/toml/mod.rs | 10 +-- tests/testsuite/registry.rs | 113 ------------------------ 8 files changed, 17 insertions(+), 212 deletions(-) diff --git a/crates/resolver-tests/src/lib.rs b/crates/resolver-tests/src/lib.rs index af023f44fcb..de20d975173 100644 --- a/crates/resolver-tests/src/lib.rs +++ b/crates/resolver-tests/src/lib.rs @@ -506,7 +506,7 @@ pub trait ToDep { impl ToDep for &'static str { fn to_dep(self) -> Dependency { - Dependency::parse_no_deprecated(self, Some("1.0.0"), registry_loc()).unwrap() + Dependency::parse(self, Some("1.0.0"), registry_loc()).unwrap() } } @@ -626,7 +626,7 @@ pub fn dep(name: &str) -> Dependency { dep_req(name, "*") } pub fn dep_req(name: &str, req: &str) -> Dependency { - Dependency::parse_no_deprecated(name, Some(req), registry_loc()).unwrap() + Dependency::parse(name, Some(req), registry_loc()).unwrap() } pub fn dep_req_kind(name: &str, req: &str, kind: DepKind, public: bool) -> Dependency { let mut dep = dep_req(name, req); @@ -639,7 +639,7 @@ pub fn dep_loc(name: &str, location: &str) -> Dependency { let url = location.into_url().unwrap(); let master = GitReference::Branch("master".to_string()); let source_id = SourceId::for_git(&url, master).unwrap(); - Dependency::parse_no_deprecated(name, Some("1.0.0"), source_id).unwrap() + Dependency::parse(name, Some("1.0.0"), source_id).unwrap() } pub fn dep_kind(name: &str, kind: DepKind) -> Dependency { dep(name).set_kind(kind).clone() diff --git a/src/cargo/core/compiler/standard_lib.rs b/src/cargo/core/compiler/standard_lib.rs index b9986d3badf..0c554feb219 100644 --- a/src/cargo/core/compiler/standard_lib.rs +++ b/src/cargo/core/compiler/standard_lib.rs @@ -47,7 +47,7 @@ pub fn resolve_std<'cfg>( .iter() .map(|&name| { let source_path = SourceId::for_path(&src_path.join("library").join(name))?; - let dep = Dependency::parse_no_deprecated(name, None, source_path)?; + let dep = Dependency::parse(name, None, source_path)?; Ok(dep) }) .collect::>>()?; diff --git a/src/cargo/core/dependency.rs b/src/cargo/core/dependency.rs index a50534d6c8e..94419d0ca96 100644 --- a/src/cargo/core/dependency.rs +++ b/src/cargo/core/dependency.rs @@ -9,7 +9,7 @@ use std::rc::Rc; use crate::core::{PackageId, SourceId, Summary}; use crate::util::errors::CargoResult; use crate::util::interning::InternedString; -use crate::util::{Config, OptVersionReq}; +use crate::util::OptVersionReq; /// Information about a dependency requested by a Cargo manifest. /// Cheap to copy. @@ -97,58 +97,6 @@ pub enum DepKind { Build, } -fn parse_req_with_deprecated( - name: InternedString, - req: &str, - extra: Option<(PackageId, &Config)>, -) -> CargoResult { - let err = match VersionReq::parse(req) { - Ok(req) => return Ok(req), - Err(err) => err, - }; - - let (inside, config) = match extra { - Some(pair) => pair, - None => return Err(err.into()), - }; - - let corrected = match req { - ".*" => "*", - "0.1.0." => "0.1.0", - "0.3.1.3" => "0.3.13", - "0.2*" => "0.2.*", - "*.0" => "*", - _ => { - return Err(anyhow::Error::new(err).context(format!( - "failed to parse the version requirement `{}` for dependency `{}`", - req, name, - ))); - } - }; - - let msg = format!( - "\ -parsed version requirement `{}` is no longer valid - -Previous versions of Cargo accepted this malformed requirement, -but it is being deprecated. This was found when parsing the manifest -of {} {}, and the correct version requirement is `{}`. - -This will soon become a hard error, so it's either recommended to -update to a fixed version or contact the upstream maintainer about -this warning. -", - req, - inside.name(), - inside.version(), - corrected, - ); - - config.shell().warn(&msg)?; - - Ok(VersionReq::parse(corrected).unwrap()) -} - impl ser::Serialize for DepKind { fn serialize(&self, s: S) -> Result where @@ -169,35 +117,18 @@ impl Dependency { name: impl Into, version: Option<&str>, source_id: SourceId, - inside: PackageId, - config: &Config, - ) -> CargoResult { - let name = name.into(); - let arg = Some((inside, config)); - let (specified_req, version_req) = match version { - Some(v) => (true, parse_req_with_deprecated(name, v, arg)?.into()), - None => (false, OptVersionReq::Any), - }; - - let mut ret = Dependency::new_override(name, source_id); - { - let ptr = Rc::make_mut(&mut ret.inner); - ptr.only_match_name = false; - ptr.req = version_req; - ptr.specified_req = specified_req; - } - Ok(ret) - } - - /// Attempt to create a `Dependency` from an entry in the manifest. - pub fn parse_no_deprecated( - name: impl Into, - version: Option<&str>, - source_id: SourceId, ) -> CargoResult { let name = name.into(); let (specified_req, version_req) = match version { - Some(v) => (true, parse_req_with_deprecated(name, v, None)?.into()), + Some(v) => match VersionReq::parse(v) { + Ok(req) => (true, OptVersionReq::Req(req)), + Err(err) => { + return Err(anyhow::Error::new(err).context(format!( + "failed to parse the version requirement `{}` for dependency `{}`", + v, name, + ))) + } + }, None => (false, OptVersionReq::Any), }; diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index 45f4ef28670..9cc94f520be 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -396,7 +396,6 @@ impl<'cfg> Workspace<'cfg> { .map(|(name, dep)| { dep.to_dependency_split( name, - /* pkg_id */ None, source, &mut nested_paths, self.config, diff --git a/src/cargo/ops/cargo_install.rs b/src/cargo/ops/cargo_install.rs index f0d6e5d971b..10dd8e091c6 100644 --- a/src/cargo/ops/cargo_install.rs +++ b/src/cargo/ops/cargo_install.rs @@ -180,11 +180,7 @@ fn install_one( } else { None }; - Some(Dependency::parse_no_deprecated( - krate, - vers.as_deref(), - source_id, - )?) + Some(Dependency::parse(krate, vers.as_deref(), source_id)?) } else { None } diff --git a/src/cargo/sources/registry/mod.rs b/src/cargo/sources/registry/mod.rs index be2c32c1dd3..f7a48519494 100644 --- a/src/cargo/sources/registry/mod.rs +++ b/src/cargo/sources/registry/mod.rs @@ -373,7 +373,7 @@ impl<'a> RegistryDependency<'a> { default }; - let mut dep = Dependency::parse_no_deprecated(package.unwrap_or(name), Some(&req), id)?; + let mut dep = Dependency::parse(package.unwrap_or(name), Some(&req), id)?; if package.is_some() { dep.set_explicit_name_in_toml(name); } diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 5718fa4ea06..8bc808a318c 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -874,7 +874,6 @@ impl TomlProject { } struct Context<'a, 'b> { - pkgid: Option, deps: &'a mut Vec, source_id: SourceId, nested_paths: &'a mut Vec, @@ -1189,7 +1188,6 @@ impl TomlManifest { { let mut cx = Context { - pkgid: Some(pkgid), deps: &mut deps, source_id, nested_paths: &mut nested_paths, @@ -1457,7 +1455,6 @@ impl TomlManifest { let (replace, patch) = { let mut cx = Context { - pkgid: None, deps: &mut deps, source_id, nested_paths: &mut nested_paths, @@ -1653,7 +1650,6 @@ impl TomlDependency

{ pub(crate) fn to_dependency_split( &self, name: &str, - pkgid: Option, source_id: SourceId, nested_paths: &mut Vec, config: &Config, @@ -1666,7 +1662,6 @@ impl TomlDependency

{ self.to_dependency( name, &mut Context { - pkgid, deps: &mut Vec::new(), source_id, nested_paths, @@ -1875,10 +1870,7 @@ impl DetailedTomlDependency

{ }; let version = self.version.as_deref(); - let mut dep = match cx.pkgid { - Some(id) => Dependency::parse(pkg_name, version, new_source_id, id, cx.config)?, - None => Dependency::parse_no_deprecated(pkg_name, version, new_source_id)?, - }; + let mut dep = Dependency::parse(pkg_name, version, new_source_id)?; dep.set_features(self.features.iter().flatten()) .set_default_features( self.default_features diff --git a/tests/testsuite/registry.rs b/tests/testsuite/registry.rs index 9b93c02b1e3..854ad1af024 100644 --- a/tests/testsuite/registry.rs +++ b/tests/testsuite/registry.rs @@ -1695,119 +1695,6 @@ fn bump_version_dont_update_registry() { .run(); } -#[cargo_test] -fn old_version_req() { - let p = project() - .file( - "Cargo.toml", - r#" - [project] - name = "bar" - version = "0.5.0" - authors = [] - - [dependencies] - remote = "0.2*" - "#, - ) - .file("src/main.rs", "fn main() {}") - .build(); - - Package::new("remote", "0.2.0").publish(); - - p.cargo("build") - .with_stderr( - "\ -warning: parsed version requirement `0.2*` is no longer valid - -Previous versions of Cargo accepted this malformed requirement, -but it is being deprecated. This was found when parsing the manifest -of bar 0.5.0, and the correct version requirement is `0.2.*`. - -This will soon become a hard error, so it's either recommended to -update to a fixed version or contact the upstream maintainer about -this warning. - -warning: parsed version requirement `0.2*` is no longer valid - -Previous versions of Cargo accepted this malformed requirement, -but it is being deprecated. This was found when parsing the manifest -of bar 0.5.0, and the correct version requirement is `0.2.*`. - -This will soon become a hard error, so it's either recommended to -update to a fixed version or contact the upstream maintainer about -this warning. - -[UPDATING] [..] -[DOWNLOADING] crates ... -[DOWNLOADED] [..] -[COMPILING] [..] -[COMPILING] [..] -[FINISHED] [..] -", - ) - .run(); -} - -#[cargo_test] -fn old_version_req_upstream() { - let p = project() - .file( - "Cargo.toml", - r#" - [project] - name = "bar" - version = "0.5.0" - authors = [] - - [dependencies] - remote = "0.3" - "#, - ) - .file("src/main.rs", "fn main() {}") - .build(); - - Package::new("remote", "0.3.0") - .file( - "Cargo.toml", - r#" - [project] - name = "remote" - version = "0.3.0" - authors = [] - - [dependencies] - bar = "0.2*" - "#, - ) - .file("src/lib.rs", "") - .publish(); - Package::new("bar", "0.2.0").publish(); - - p.cargo("build") - .with_stderr( - "\ -[UPDATING] [..] -[DOWNLOADING] crates ... -[DOWNLOADED] [..] -warning: parsed version requirement `0.2*` is no longer valid - -Previous versions of Cargo accepted this malformed requirement, -but it is being deprecated. This was found when parsing the manifest -of remote 0.3.0, and the correct version requirement is `0.2.*`. - -This will soon become a hard error, so it's either recommended to -update to a fixed version or contact the upstream maintainer about -this warning. - -[COMPILING] [..] -[COMPILING] [..] -[FINISHED] [..] -", - ) - .run(); -} - #[cargo_test] fn toml_lies_but_index_is_truth() { Package::new("foo", "0.2.0").publish(); From 7ace4470c4b1bf4e9d21a24ac9683e97095f65ff Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 26 May 2021 14:39:13 -0700 Subject: [PATCH 3/3] Update to semver 1.0.0 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 648cf3153a4..4d7d1e06d26 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,7 +50,7 @@ num_cpus = "1.0" opener = "0.4" percent-encoding = "2.0" rustfix = "0.5.0" -semver = { version = "1.0.0-rc.2", features = ["serde"] } +semver = { version = "1.0", features = ["serde"] } serde = { version = "1.0.123", features = ["derive"] } serde_ignored = "0.1.0" serde_json = { version = "1.0.30", features = ["raw_value"] }