diff --git a/src/cargo/util/toml_mut/upgrade.rs b/src/cargo/util/toml_mut/upgrade.rs index 698fcab654c1..5757350bef6b 100644 --- a/src/cargo/util/toml_mut/upgrade.rs +++ b/src/cargo/util/toml_mut/upgrade.rs @@ -20,7 +20,10 @@ pub(crate) fn upgrade_requirement( .into_iter() .map(|p| set_comparator(p, version)) .collect(); - let comparators = comparators?; + let comparators: Vec<_> = comparators?.into_iter().flatten().collect(); + if comparators.is_empty() { + return Ok(None); + } let new_req = semver::VersionReq { comparators }; let mut new_req_text = new_req.to_string(); if new_req_text.starts_with('^') && !req.starts_with('^') { @@ -48,7 +51,7 @@ pub(crate) fn upgrade_requirement( fn set_comparator( mut pred: semver::Comparator, version: &semver::Version, -) -> CargoResult { +) -> CargoResult> { match pred.op { semver::Op::Wildcard => { pred.major = version.major; @@ -58,7 +61,7 @@ fn set_comparator( if pred.patch.is_some() { pred.patch = Some(version.patch); } - Ok(pred) + Ok(Some(pred)) } semver::Op::Exact => Ok(assign_partial_req(version, pred)), semver::Op::Greater | semver::Op::GreaterEq | semver::Op::Less | semver::Op::LessEq => { @@ -77,7 +80,10 @@ fn set_comparator( fn assign_partial_req( version: &semver::Version, mut pred: semver::Comparator, -) -> semver::Comparator { +) -> Option { + if !pred.pre.is_empty() { + return None; + } pred.major = version.major; if pred.minor.is_some() { pred.minor = Some(version.minor); @@ -86,7 +92,7 @@ fn assign_partial_req( pred.patch = Some(version.patch); } pred.pre = version.pre.clone(); - pred + Some(pred) } fn unsupported_version_req(req: impl Display) -> anyhow::Error { @@ -220,7 +226,7 @@ mod test { #[test] fn caret_prerelease() { - assert_req_bump("1.7.0", "2.0.0-beta.21", "1.7.0"); + assert_req_bump("1.7.0", "2.0.0-beta.21", None); } } } diff --git a/tests/testsuite/update.rs b/tests/testsuite/update.rs index 4d02ba6fb876..e99ea18af8a5 100644 --- a/tests/testsuite/update.rs +++ b/tests/testsuite/update.rs @@ -2646,9 +2646,6 @@ fn update_breaking_pre_release() { .masquerade_as_nightly_cargo(&["update-breaking"]) .with_stderr_data(str![[r#" [UPDATING] `dummy-registry` index -[UPGRADING] bar ^2.0.0-beta.21 -> ^1.7.0 -[LOCKING] 1 package to latest compatible version -[DOWNGRADING] bar v2.0.0-beta.21 -> v1.7.0 "#]]) .run();