Skip to content

Commit 2f3df16

Browse files
committed
Auto merge of #9508 - dtolnay-contrib:semver, r=ehuss
Update to semver 1.0.0 I am working on a 1.0.0 of the `semver` crate some time this week. It would be good to confirm Cargo will be able to use it, beforehand! It's a from-scratch rewrite, but dtolnay/semver#237 has code to compare against 0.10.0 (currently used by Cargo) how every possible version requirement currently published to crates.io matches against every possible crate version. The differences are all broken syntax like `^0-.11.0` previously parsing with ".11.0" as a pre-release string (which is invalid, because pre-release are not allowed to contain empty dot-separated identifiers) and `~2.0-2.2` previously parsing with "2.2" as a pre-release string, when the user almost certainly meant `>=2.0, <=2.2`. I'm not sure how much of those you want to add code into Cargo to preserve behavior, but I would be happy to do it.
2 parents cc75485 + 7ace447 commit 2f3df16

File tree

18 files changed

+148
-255
lines changed

18 files changed

+148
-255
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ num_cpus = "1.0"
5050
opener = "0.4"
5151
percent-encoding = "2.0"
5252
rustfix = "0.5.0"
53-
semver = { version = "0.10", features = ["serde"] }
53+
semver = { version = "1.0", features = ["serde"] }
5454
serde = { version = "1.0.123", features = ["derive"] }
5555
serde_ignored = "0.1.0"
5656
serde_json = { version = "1.0.30", features = ["raw_value"] }

crates/resolver-tests/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ pub trait ToDep {
506506

507507
impl ToDep for &'static str {
508508
fn to_dep(self) -> Dependency {
509-
Dependency::parse_no_deprecated(self, Some("1.0.0"), registry_loc()).unwrap()
509+
Dependency::parse(self, Some("1.0.0"), registry_loc()).unwrap()
510510
}
511511
}
512512

@@ -626,7 +626,7 @@ pub fn dep(name: &str) -> Dependency {
626626
dep_req(name, "*")
627627
}
628628
pub fn dep_req(name: &str, req: &str) -> Dependency {
629-
Dependency::parse_no_deprecated(name, Some(req), registry_loc()).unwrap()
629+
Dependency::parse(name, Some(req), registry_loc()).unwrap()
630630
}
631631
pub fn dep_req_kind(name: &str, req: &str, kind: DepKind, public: bool) -> Dependency {
632632
let mut dep = dep_req(name, req);
@@ -639,7 +639,7 @@ pub fn dep_loc(name: &str, location: &str) -> Dependency {
639639
let url = location.into_url().unwrap();
640640
let master = GitReference::Branch("master".to_string());
641641
let source_id = SourceId::for_git(&url, master).unwrap();
642-
Dependency::parse_no_deprecated(name, Some("1.0.0"), source_id).unwrap()
642+
Dependency::parse(name, Some("1.0.0"), source_id).unwrap()
643643
}
644644
pub fn dep_kind(name: &str, kind: DepKind) -> Dependency {
645645
dep(name).set_kind(kind).clone()

src/cargo/core/compiler/compilation.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::path::PathBuf;
55

66
use cargo_platform::CfgExpr;
77
use cargo_util::{paths, ProcessBuilder};
8-
use semver::Version;
98

109
use super::BuildContext;
1110
use crate::core::compiler::{CompileKind, Metadata, Unit};
@@ -316,10 +315,7 @@ impl<'cfg> Compilation<'cfg> {
316315
.env("CARGO_PKG_VERSION_MAJOR", &pkg.version().major.to_string())
317316
.env("CARGO_PKG_VERSION_MINOR", &pkg.version().minor.to_string())
318317
.env("CARGO_PKG_VERSION_PATCH", &pkg.version().patch.to_string())
319-
.env(
320-
"CARGO_PKG_VERSION_PRE",
321-
&pre_version_component(pkg.version()),
322-
)
318+
.env("CARGO_PKG_VERSION_PRE", pkg.version().pre.as_str())
323319
.env("CARGO_PKG_VERSION", &pkg.version().to_string())
324320
.env("CARGO_PKG_NAME", &*pkg.name())
325321
.env(
@@ -368,23 +364,6 @@ fn fill_rustc_tool_env(mut cmd: ProcessBuilder, unit: &Unit) -> ProcessBuilder {
368364
cmd
369365
}
370366

371-
fn pre_version_component(v: &Version) -> String {
372-
if v.pre.is_empty() {
373-
return String::new();
374-
}
375-
376-
let mut ret = String::new();
377-
378-
for (i, x) in v.pre.iter().enumerate() {
379-
if i != 0 {
380-
ret.push('.')
381-
};
382-
ret.push_str(&x.to_string());
383-
}
384-
385-
ret
386-
}
387-
388367
fn target_runner(
389368
bcx: &BuildContext<'_, '_>,
390369
kind: CompileKind,

src/cargo/core/compiler/context/compilation_files.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ fn hash_rustc_version(bcx: &BuildContext<'_, '_>, hasher: &mut StableHasher) {
595595
//
596596
// This assumes that the first segment is the important bit ("nightly",
597597
// "beta", "dev", etc.). Skip other parts like the `.3` in `-beta.3`.
598-
vers.pre[0].hash(hasher);
598+
vers.pre.split('.').next().hash(hasher);
599599
// Keep "host" since some people switch hosts to implicitly change
600600
// targets, (like gnu vs musl or gnu vs msvc). In the future, we may want
601601
// to consider hashing `unit.kind.short_name()` instead.

src/cargo/core/compiler/standard_lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn resolve_std<'cfg>(
4747
.iter()
4848
.map(|&name| {
4949
let source_path = SourceId::for_path(&src_path.join("library").join(name))?;
50-
let dep = Dependency::parse_no_deprecated(name, None, source_path)?;
50+
let dep = Dependency::parse(name, None, source_path)?;
5151
Ok(dep)
5252
})
5353
.collect::<CargoResult<Vec<_>>>()?;

src/cargo/core/dependency.rs

Lines changed: 18 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
use anyhow::Context as _;
21
use cargo_platform::Platform;
32
use log::trace;
4-
use semver::ReqParseError;
53
use semver::VersionReq;
64
use serde::ser;
75
use serde::Serialize;
@@ -11,17 +9,17 @@ use std::rc::Rc;
119
use crate::core::{PackageId, SourceId, Summary};
1210
use crate::util::errors::CargoResult;
1311
use crate::util::interning::InternedString;
14-
use crate::util::Config;
12+
use crate::util::OptVersionReq;
1513

1614
/// Information about a dependency requested by a Cargo manifest.
1715
/// Cheap to copy.
18-
#[derive(PartialEq, Eq, Hash, Ord, PartialOrd, Clone, Debug)]
16+
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
1917
pub struct Dependency {
2018
inner: Rc<Inner>,
2119
}
2220

2321
/// The data underlying a `Dependency`.
24-
#[derive(PartialEq, Eq, Hash, Ord, PartialOrd, Clone, Debug)]
22+
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
2523
struct Inner {
2624
name: InternedString,
2725
source_id: SourceId,
@@ -32,7 +30,7 @@ struct Inner {
3230
/// `registry` is specified. Or in the case of a crates.io dependency,
3331
/// `source_id` will be crates.io and this will be None.
3432
registry_id: Option<SourceId>,
35-
req: VersionReq,
33+
req: OptVersionReq,
3634
specified_req: bool,
3735
kind: DepKind,
3836
only_match_name: bool,
@@ -99,52 +97,6 @@ pub enum DepKind {
9997
Build,
10098
}
10199

102-
fn parse_req_with_deprecated(
103-
name: InternedString,
104-
req: &str,
105-
extra: Option<(PackageId, &Config)>,
106-
) -> CargoResult<VersionReq> {
107-
match VersionReq::parse(req) {
108-
Err(ReqParseError::DeprecatedVersionRequirement(requirement)) => {
109-
let (inside, config) = match extra {
110-
Some(pair) => pair,
111-
None => return Err(ReqParseError::DeprecatedVersionRequirement(requirement).into()),
112-
};
113-
let msg = format!(
114-
"\
115-
parsed version requirement `{}` is no longer valid
116-
117-
Previous versions of Cargo accepted this malformed requirement,
118-
but it is being deprecated. This was found when parsing the manifest
119-
of {} {}, and the correct version requirement is `{}`.
120-
121-
This will soon become a hard error, so it's either recommended to
122-
update to a fixed version or contact the upstream maintainer about
123-
this warning.
124-
",
125-
req,
126-
inside.name(),
127-
inside.version(),
128-
requirement
129-
);
130-
config.shell().warn(&msg)?;
131-
132-
Ok(requirement)
133-
}
134-
Err(e) => {
135-
let err: CargoResult<VersionReq> = Err(e.into());
136-
let v: VersionReq = err.with_context(|| {
137-
format!(
138-
"failed to parse the version requirement `{}` for dependency `{}`",
139-
req, name
140-
)
141-
})?;
142-
Ok(v)
143-
}
144-
Ok(v) => Ok(v),
145-
}
146-
}
147-
148100
impl ser::Serialize for DepKind {
149101
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
150102
where
@@ -165,36 +117,19 @@ impl Dependency {
165117
name: impl Into<InternedString>,
166118
version: Option<&str>,
167119
source_id: SourceId,
168-
inside: PackageId,
169-
config: &Config,
170-
) -> CargoResult<Dependency> {
171-
let name = name.into();
172-
let arg = Some((inside, config));
173-
let (specified_req, version_req) = match version {
174-
Some(v) => (true, parse_req_with_deprecated(name, v, arg)?),
175-
None => (false, VersionReq::any()),
176-
};
177-
178-
let mut ret = Dependency::new_override(name, source_id);
179-
{
180-
let ptr = Rc::make_mut(&mut ret.inner);
181-
ptr.only_match_name = false;
182-
ptr.req = version_req;
183-
ptr.specified_req = specified_req;
184-
}
185-
Ok(ret)
186-
}
187-
188-
/// Attempt to create a `Dependency` from an entry in the manifest.
189-
pub fn parse_no_deprecated(
190-
name: impl Into<InternedString>,
191-
version: Option<&str>,
192-
source_id: SourceId,
193120
) -> CargoResult<Dependency> {
194121
let name = name.into();
195122
let (specified_req, version_req) = match version {
196-
Some(v) => (true, parse_req_with_deprecated(name, v, None)?),
197-
None => (false, VersionReq::any()),
123+
Some(v) => match VersionReq::parse(v) {
124+
Ok(req) => (true, OptVersionReq::Req(req)),
125+
Err(err) => {
126+
return Err(anyhow::Error::new(err).context(format!(
127+
"failed to parse the version requirement `{}` for dependency `{}`",
128+
v, name,
129+
)))
130+
}
131+
},
132+
None => (false, OptVersionReq::Any),
198133
};
199134

200135
let mut ret = Dependency::new_override(name, source_id);
@@ -214,7 +149,7 @@ impl Dependency {
214149
name,
215150
source_id,
216151
registry_id: None,
217-
req: VersionReq::any(),
152+
req: OptVersionReq::Any,
218153
kind: DepKind::Normal,
219154
only_match_name: true,
220155
optional: false,
@@ -228,7 +163,7 @@ impl Dependency {
228163
}
229164
}
230165

231-
pub fn version_req(&self) -> &VersionReq {
166+
pub fn version_req(&self) -> &OptVersionReq {
232167
&self.inner.req
233168
}
234169

@@ -365,7 +300,7 @@ impl Dependency {
365300

366301
/// Sets the version requirement for this dependency.
367302
pub fn set_version_req(&mut self, req: VersionReq) -> &mut Dependency {
368-
Rc::make_mut(&mut self.inner).req = req;
303+
Rc::make_mut(&mut self.inner).req = OptVersionReq::Req(req);
369304
self
370305
}
371306

@@ -394,7 +329,7 @@ impl Dependency {
394329
id
395330
);
396331
let me = Rc::make_mut(&mut self.inner);
397-
me.req = VersionReq::exact(id.version());
332+
me.req = OptVersionReq::exact(id.version());
398333

399334
// Only update the `precise` of this source to preserve other
400335
// information about dependency's source which may not otherwise be

src/cargo/core/registry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::core::{Dependency, PackageId, Source, SourceId, SourceMap, Summary};
55
use crate::sources::config::SourceConfigMap;
66
use crate::util::errors::CargoResult;
77
use crate::util::interning::InternedString;
8-
use crate::util::{profile, CanonicalUrl, Config};
8+
use crate::util::{profile, CanonicalUrl, Config, VersionReqExt};
99
use anyhow::{bail, Context as _};
1010
use log::{debug, trace};
1111
use semver::VersionReq;

src/cargo/core/resolver/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::fmt;
22

33
use crate::core::{Dependency, PackageId, Registry, Summary};
44
use crate::util::lev_distance::lev_distance;
5-
use crate::util::Config;
5+
use crate::util::{Config, VersionExt};
66
use anyhow::Error;
77

88
use super::context::Context;

src/cargo/core/workspace.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,6 @@ impl<'cfg> Workspace<'cfg> {
396396
.map(|(name, dep)| {
397397
dep.to_dependency_split(
398398
name,
399-
/* pkg_id */ None,
400399
source,
401400
&mut nested_paths,
402401
self.config,

src/cargo/ops/cargo_install.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::core::{Dependency, Edition, Package, PackageId, Source, SourceId, Wor
88
use crate::ops::common_for_install_and_uninstall::*;
99
use crate::sources::{GitSource, PathSource, SourceConfigMap};
1010
use crate::util::errors::CargoResult;
11-
use crate::util::{Config, Filesystem, Rustc, ToSemver};
11+
use crate::util::{Config, Filesystem, Rustc, ToSemver, VersionReqExt};
1212
use crate::{drop_println, ops};
1313

1414
use anyhow::{bail, format_err, Context as _};
@@ -180,11 +180,7 @@ fn install_one(
180180
} else {
181181
None
182182
};
183-
Some(Dependency::parse_no_deprecated(
184-
krate,
185-
vers.as_deref(),
186-
source_id,
187-
)?)
183+
Some(Dependency::parse(krate, vers.as_deref(), source_id)?)
188184
} else {
189185
None
190186
}

0 commit comments

Comments
 (0)