From be460b0be22b46072b8771a31ba3f147074f25e4 Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Sun, 1 Aug 2021 11:02:01 +0100 Subject: [PATCH 1/5] Capture info with Spanned --- src/cargo/util/toml/mod.rs | 56 ++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 6a928c4b0ce..cb561e32e90 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -13,6 +13,7 @@ use semver::{self, VersionReq}; use serde::de; use serde::ser; use serde::{Deserialize, Serialize}; +use toml::Spanned; use url::Url; use crate::core::compiler::{CompileKind, CompileTarget}; @@ -330,17 +331,17 @@ pub struct TomlManifest { example: Option>, test: Option>, bench: Option>, - dependencies: Option>, - dev_dependencies: Option>, + dependencies: Option>>, + dev_dependencies: Option>>, #[serde(rename = "dev_dependencies")] - dev_dependencies2: Option>, - build_dependencies: Option>, + dev_dependencies2: Option>>, + build_dependencies: Option>>, #[serde(rename = "build_dependencies")] - build_dependencies2: Option>, + build_dependencies2: Option>>, features: Option>>, target: Option>, - replace: Option>, - patch: Option>>, + replace: Option>>, + patch: Option>>>, workspace: Option, badges: Option>>, } @@ -1033,22 +1034,24 @@ impl TomlManifest { fn map_deps( config: &Config, - deps: Option<&BTreeMap>, + deps: Option<&BTreeMap>>, filter: impl Fn(&TomlDependency) -> bool, - ) -> CargoResult>> { + ) -> CargoResult>>> { let deps = match deps { Some(deps) => deps, None => return Ok(None), }; let deps = deps .iter() - .filter(|(_k, v)| filter(v)) + .filter(|(_k, v)| filter(v.get_ref())) .map(|(k, v)| Ok((k.clone(), map_dependency(config, v)?))) .collect::>>()?; Ok(Some(deps)) } - fn map_dependency(config: &Config, dep: &TomlDependency) -> CargoResult { + fn map_dependency(config: &Config, dep: &Spanned) -> CargoResult> { + let mut cloned = dep.clone(); + let dep = dep.get_ref(); match dep { TomlDependency::Detailed(d) => { let mut d = d.clone(); @@ -1064,12 +1067,16 @@ impl TomlManifest { let src = SourceId::alt_registry(config, ®istry)?; d.registry_index = Some(src.url().to_string()); } - Ok(TomlDependency::Detailed(d)) + std::mem::swap(cloned.get_mut(),&mut TomlDependency::Detailed(d)); + Ok(cloned) } - TomlDependency::Simple(s) => Ok(TomlDependency::Detailed(DetailedTomlDependency { - version: Some(s.clone()), - ..Default::default() - })), + TomlDependency::Simple(s) => { + std::mem::swap(cloned.get_mut(),&mut TomlDependency::Detailed(DetailedTomlDependency { + version: Some(s.clone()), + ..Default::default() + })); + Ok(cloned) + }, } } } @@ -1245,7 +1252,7 @@ impl TomlManifest { fn process_dependencies( cx: &mut Context<'_, '_>, - new_deps: Option<&BTreeMap>, + new_deps: Option<&BTreeMap>>, kind: Option, ) -> CargoResult<()> { let dependencies = match new_deps { @@ -1253,7 +1260,7 @@ impl TomlManifest { None => return Ok(()), }; for (n, v) in dependencies.iter() { - let dep = v.to_dependency(n, cx, kind)?; + let dep = v.get_ref().to_dependency(n, cx, kind)?; validate_package_name(dep.name_in_toml().as_str(), "dependency name", "")?; cx.deps.push(dep); } @@ -1570,6 +1577,7 @@ impl TomlManifest { spec.set_url(CRATES_IO_INDEX.parse().unwrap()); } + let replacement = replacement.get_ref(); if replacement.is_version_specified() { bail!( "replacements cannot specify a version \ @@ -1610,7 +1618,7 @@ impl TomlManifest { patch.insert( url, deps.iter() - .map(|(name, dep)| dep.to_dependency(name, cx, None)) + .map(|(name, dep)| dep.get_ref().to_dependency(name, cx, None)) .collect::>>()?, ); } @@ -2000,15 +2008,15 @@ impl ser::Serialize for PathValue { /// Corresponds to a `target` entry, but `TomlTarget` is already used. #[derive(Serialize, Deserialize, Debug)] struct TomlPlatform { - dependencies: Option>, + dependencies: Option>>, #[serde(rename = "build-dependencies")] - build_dependencies: Option>, + build_dependencies: Option>>, #[serde(rename = "build_dependencies")] - build_dependencies2: Option>, + build_dependencies2: Option>>, #[serde(rename = "dev-dependencies")] - dev_dependencies: Option>, + dev_dependencies: Option>>, #[serde(rename = "dev_dependencies")] - dev_dependencies2: Option>, + dev_dependencies2: Option>>, } impl TomlTarget { From 9cc0ee3665630184c01d0ac29e2cf8417ac6a12c Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Sun, 1 Aug 2021 18:20:16 +0100 Subject: [PATCH 2/5] cargo fmt --- src/cargo/util/toml/mod.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index cb561e32e90..1db2596ccfd 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -1049,7 +1049,10 @@ impl TomlManifest { Ok(Some(deps)) } - fn map_dependency(config: &Config, dep: &Spanned) -> CargoResult> { + fn map_dependency( + config: &Config, + dep: &Spanned, + ) -> CargoResult> { let mut cloned = dep.clone(); let dep = dep.get_ref(); match dep { @@ -1067,16 +1070,19 @@ impl TomlManifest { let src = SourceId::alt_registry(config, ®istry)?; d.registry_index = Some(src.url().to_string()); } - std::mem::swap(cloned.get_mut(),&mut TomlDependency::Detailed(d)); + std::mem::swap(cloned.get_mut(), &mut TomlDependency::Detailed(d)); Ok(cloned) } TomlDependency::Simple(s) => { - std::mem::swap(cloned.get_mut(),&mut TomlDependency::Detailed(DetailedTomlDependency { - version: Some(s.clone()), - ..Default::default() - })); + std::mem::swap( + cloned.get_mut(), + &mut TomlDependency::Detailed(DetailedTomlDependency { + version: Some(s.clone()), + ..Default::default() + }), + ); Ok(cloned) - }, + } } } } From 25b500dfd753a3e52d7f003ebefaf3eaaa039fd2 Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Mon, 2 Aug 2021 07:39:47 +0100 Subject: [PATCH 3/5] Putting Span on Dependency --- src/cargo/core/compiler/future_incompat.rs | 2 +- src/cargo/core/compiler/standard_lib.rs | 5 ++-- src/cargo/core/dependency.rs | 30 ++++++++++++++++++++-- src/cargo/core/registry.rs | 4 +-- src/cargo/core/resolver/errors.rs | 4 ++- src/cargo/core/resolver/version_prefs.rs | 9 ++++--- src/cargo/core/workspace.rs | 1 + src/cargo/ops/cargo_compile.rs | 2 +- src/cargo/ops/cargo_install.rs | 2 +- src/cargo/sources/registry/index.rs | 2 +- src/cargo/sources/registry/mod.rs | 6 ++--- src/cargo/util/toml/mod.rs | 18 ++++++++----- 12 files changed, 60 insertions(+), 25 deletions(-) diff --git a/src/cargo/core/compiler/future_incompat.rs b/src/cargo/core/compiler/future_incompat.rs index d9638deddac..f50df586dbf 100644 --- a/src/cargo/core/compiler/future_incompat.rs +++ b/src/cargo/core/compiler/future_incompat.rs @@ -257,7 +257,7 @@ fn render_suggestions( Some(s) => s, None => continue, }; - let dep = Dependency::parse(pkg_id.name(), None, pkg_id.source_id()).ok()?; + let dep = Dependency::parse(pkg_id.name(), None, pkg_id.source_id(), None).ok()?; let summaries = source.query_vec(&dep).ok()?; let versions = itertools::sorted( summaries diff --git a/src/cargo/core/compiler/standard_lib.rs b/src/cargo/core/compiler/standard_lib.rs index 6b76a5681be..cc5ffc0fc87 100644 --- a/src/cargo/core/compiler/standard_lib.rs +++ b/src/cargo/core/compiler/standard_lib.rs @@ -5,7 +5,7 @@ use crate::core::compiler::{CompileKind, CompileMode, RustcTargetData, Unit}; use crate::core::profiles::{Profiles, UnitFor}; use crate::core::resolver::features::{CliFeatures, FeaturesFor, ResolvedFeatures}; use crate::core::resolver::HasDevUnits; -use crate::core::{Dependency, PackageId, PackageSet, Resolve, SourceId, Workspace}; +use crate::core::{Dependency, dependency::Span, PackageId, PackageSet, Resolve, SourceId, Workspace}; use crate::ops::{self, Packages}; use crate::util::errors::CargoResult; use std::collections::{HashMap, HashSet}; @@ -36,6 +36,7 @@ pub fn resolve_std<'cfg>( target_data: &RustcTargetData<'cfg>, requested_targets: &[CompileKind], crates: &[String], + span: Option, ) -> CargoResult<(PackageSet<'cfg>, Resolve, ResolvedFeatures)> { let src_path = detect_sysroot_src_path(target_data)?; let to_patch = [ @@ -47,7 +48,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(name, None, source_path)?; + let dep = Dependency::parse(name, None, source_path, span.clone())?; Ok(dep) }) .collect::>>()?; diff --git a/src/cargo/core/dependency.rs b/src/cargo/core/dependency.rs index 4e8e1d205ff..d5677866259 100644 --- a/src/cargo/core/dependency.rs +++ b/src/cargo/core/dependency.rs @@ -3,6 +3,7 @@ use log::trace; use semver::VersionReq; use serde::ser; use serde::Serialize; +use std::num::NonZeroUsize; use std::path::PathBuf; use std::rc::Rc; @@ -44,6 +45,25 @@ struct Inner { // This dependency should be used only for this platform. // `None` means *all platforms*. platform: Option, + + span: Option +} + +#[derive(Clone,Debug, Hash, Eq, PartialEq)] +pub struct Span { + start: usize, + // NonZeroUsize so that Option is free. + end: NonZeroUsize, +} + +impl Span { + //pub const EMPTY: Span = Span{ start: 0, end: 0 }; +} + +impl From<&toml::Spanned> for Span { + fn from(spanned_src: &toml::Spanned) -> Self { + Span { start: spanned_src.start(), end: NonZeroUsize::new(spanned_src.end()).unwrap() } + } } #[derive(Serialize)] @@ -117,6 +137,7 @@ impl Dependency { name: impl Into, version: Option<&str>, source_id: SourceId, + span: Option, ) -> CargoResult { let name = name.into(); let (specified_req, version_req) = match version { @@ -132,7 +153,7 @@ impl Dependency { None => (false, OptVersionReq::Any), }; - let mut ret = Dependency::new_override(name, source_id); + let mut ret = Dependency::new_override(name, source_id, span); { let ptr = Rc::make_mut(&mut ret.inner); ptr.only_match_name = false; @@ -142,7 +163,7 @@ impl Dependency { Ok(ret) } - pub fn new_override(name: InternedString, source_id: SourceId) -> Dependency { + pub fn new_override(name: InternedString, source_id: SourceId, span: Option) -> Dependency { assert!(!name.is_empty()); Dependency { inner: Rc::new(Inner { @@ -159,6 +180,7 @@ impl Dependency { specified_req: false, platform: None, explicit_name_in_toml: None, + span }), } } @@ -217,6 +239,10 @@ impl Dependency { self.inner.source_id } + pub fn span(&self) -> Option { + self.inner.span.clone() + } + pub fn registry_id(&self) -> Option { self.inner.registry_id } diff --git a/src/cargo/core/registry.rs b/src/cargo/core/registry.rs index 35d30eb680e..8ba63c648f2 100644 --- a/src/cargo/core/registry.rs +++ b/src/cargo/core/registry.rs @@ -443,7 +443,7 @@ impl<'cfg> PackageRegistry<'cfg> { fn query_overrides(&mut self, dep: &Dependency) -> CargoResult> { for &s in self.overrides.iter() { let src = self.sources.get_mut(s).unwrap(); - let dep = Dependency::new_override(dep.package_name(), s); + let dep = Dependency::new_override(dep.package_name(), s, None); let mut results = src.query_vec(&dep)?; if !results.is_empty() { return Ok(Some(results.remove(0))); @@ -841,7 +841,7 @@ fn summary_for_patch( return Ok((summary, Some(locked.package_id))); } // Try checking if there are *any* packages that match this by name. - let name_only_dep = Dependency::new_override(orig_patch.package_name(), orig_patch.source_id()); + let name_only_dep = Dependency::new_override(orig_patch.package_name(), orig_patch.source_id(), None); let name_summaries = source.query_vec(&name_only_dep).unwrap_or_else(|e| { log::warn!( "failed to do name-only summary query for {:?}: {:?}", diff --git a/src/cargo/core/resolver/errors.rs b/src/cargo/core/resolver/errors.rs index 72b190ee05b..26a00824786 100644 --- a/src/cargo/core/resolver/errors.rs +++ b/src/cargo/core/resolver/errors.rs @@ -93,7 +93,9 @@ pub(super) fn activation_error( msg.push_str(&describe_path( &cx.parents.path_to_bottom(&parent.package_id()), )); - + if dep.span().is_some() { + msg.push_str(&format!("{:?}", dep.span())); + } msg.push_str("\nversions that meet the requirements `"); msg.push_str(&dep.version_req().to_string()); msg.push_str("` are: "); diff --git a/src/cargo/core/resolver/version_prefs.rs b/src/cargo/core/resolver/version_prefs.rs index 0add79d3c01..051501a428b 100644 --- a/src/cargo/core/resolver/version_prefs.rs +++ b/src/cargo/core/resolver/version_prefs.rs @@ -73,6 +73,7 @@ impl VersionPreferences { mod test { use super::*; use crate::core::SourceId; + use crate::core::dependency::Span; use crate::util::Config; use std::collections::BTreeMap; @@ -82,10 +83,10 @@ mod test { PackageId::new(name, version, src_id).unwrap() } - fn dep(name: &str, version: &str) -> Dependency { + fn dep(name: &str, version: &str, span: Option) -> Dependency { let src_id = SourceId::from_url("registry+https://github.com/rust-lang/crates.io-index").unwrap(); - Dependency::parse(name, Some(version), src_id).unwrap() + Dependency::parse(name, Some(version), src_id, span).unwrap() } fn summ(name: &str, version: &str) -> Summary { @@ -131,7 +132,7 @@ mod test { #[test] fn test_prefer_dependency() { let mut vp = VersionPreferences::default(); - vp.prefer_dependency(dep("foo", "=1.2.3")); + vp.prefer_dependency(dep("foo", "=1.2.3", None)); let mut summaries = vec![ summ("foo", "1.2.4"), @@ -157,7 +158,7 @@ mod test { fn test_prefer_both() { let mut vp = VersionPreferences::default(); vp.prefer_package_id(pkgid("foo", "1.2.3")); - vp.prefer_dependency(dep("foo", "=1.1.0")); + vp.prefer_dependency(dep("foo", "=1.1.0", None)); let mut summaries = vec![ summ("foo", "1.2.4"), diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index 789a69dd144..ff57065a7c2 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -400,6 +400,7 @@ impl<'cfg> Workspace<'cfg> { Path::new("unused-relative-path"), self.unstable_features(), /* kind */ None, + None ) }) .collect::>>()?, diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index d37f304644c..c8ec9da2ecf 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -398,7 +398,7 @@ pub fn create_bcx<'a, 'cfg>( anyhow::bail!("-Zbuild-std requires --target"); } let (std_package_set, std_resolve, std_features) = - standard_lib::resolve_std(ws, &target_data, &build_config.requested_kinds, crates)?; + standard_lib::resolve_std(ws, &target_data, &build_config.requested_kinds, crates, None)?; pkg_set.add_set(std_package_set); Some((std_resolve, std_features)) } else { diff --git a/src/cargo/ops/cargo_install.rs b/src/cargo/ops/cargo_install.rs index 8965b89480b..84f1cc749ca 100644 --- a/src/cargo/ops/cargo_install.rs +++ b/src/cargo/ops/cargo_install.rs @@ -180,7 +180,7 @@ fn install_one( } else { None }; - Some(Dependency::parse(krate, vers.as_deref(), source_id)?) + Some(Dependency::parse(krate, vers.as_deref(), source_id, None)?) } else { None } diff --git a/src/cargo/sources/registry/index.rs b/src/cargo/sources/registry/index.rs index fbf56eae7a5..691375e1882 100644 --- a/src/cargo/sources/registry/index.rs +++ b/src/cargo/sources/registry/index.rs @@ -823,7 +823,7 @@ impl IndexSummary { let pkgid = PackageId::new(name, &vers, source_id)?; let deps = deps .into_iter() - .map(|dep| dep.into_dep(source_id)) + .map(|dep| dep.into_dep(source_id, None)) .collect::>>()?; if let Some(features2) = features2 { for (name, values) in features2 { diff --git a/src/cargo/sources/registry/mod.rs b/src/cargo/sources/registry/mod.rs index bc3c73ad4a8..7bbabc893d9 100644 --- a/src/cargo/sources/registry/mod.rs +++ b/src/cargo/sources/registry/mod.rs @@ -174,7 +174,7 @@ use tar::Archive; use crate::core::dependency::{DepKind, Dependency}; use crate::core::source::MaybePackage; -use crate::core::{Package, PackageId, Source, SourceId, Summary}; +use crate::core::{Package, PackageId, Source, SourceId, Summary, dependency::Span}; use crate::sources::PathSource; use crate::util::hex; use crate::util::interning::InternedString; @@ -354,7 +354,7 @@ struct RegistryDependency<'a> { impl<'a> RegistryDependency<'a> { /// Converts an encoded dependency in the registry to a cargo dependency - pub fn into_dep(self, default: SourceId) -> CargoResult { + pub fn into_dep(self, default: SourceId, span: Option) -> CargoResult { let RegistryDependency { name, req, @@ -374,7 +374,7 @@ impl<'a> RegistryDependency<'a> { default }; - let mut dep = Dependency::parse(package.unwrap_or(name), Some(&req), id)?; + let mut dep = Dependency::parse(package.unwrap_or(name), Some(&req), id, span)?; 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 1db2596ccfd..0c927251ba6 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -22,7 +22,7 @@ use crate::core::manifest::{ManifestMetadata, TargetSourcePath, Warnings}; use crate::core::resolver::ResolveBehavior; use crate::core::{Dependency, Manifest, PackageId, Summary, Target}; use crate::core::{Edition, EitherManifest, Feature, Features, VirtualManifest, Workspace}; -use crate::core::{GitReference, PackageIdSpec, SourceId, WorkspaceConfig, WorkspaceRootConfig}; +use crate::core::{GitReference, dependency::Span, PackageIdSpec, SourceId, WorkspaceConfig, WorkspaceRootConfig}; use crate::sources::{CRATES_IO_INDEX, CRATES_IO_REGISTRY}; use crate::util::errors::{CargoResult, ManifestError}; use crate::util::interning::InternedString; @@ -1266,7 +1266,7 @@ impl TomlManifest { None => return Ok(()), }; for (n, v) in dependencies.iter() { - let dep = v.get_ref().to_dependency(n, cx, kind)?; + let dep = v.get_ref().to_dependency(n, cx, kind, Some(Span::from(v)))?; validate_package_name(dep.name_in_toml().as_str(), "dependency name", "")?; cx.deps.push(dep); } @@ -1592,7 +1592,7 @@ impl TomlManifest { ); } - let mut dep = replacement.to_dependency(spec.name().as_str(), cx, None)?; + let mut dep = replacement.to_dependency(spec.name().as_str(), cx, None, None)?; { let version = spec.version().ok_or_else(|| { anyhow!( @@ -1624,7 +1624,7 @@ impl TomlManifest { patch.insert( url, deps.iter() - .map(|(name, dep)| dep.get_ref().to_dependency(name, cx, None)) + .map(|(name, dep)| dep.get_ref().to_dependency(name, cx, None, None)) .collect::>>()?, ); } @@ -1717,6 +1717,7 @@ impl TomlDependency

{ root: &Path, features: &Features, kind: Option, + span: Option ) -> CargoResult { self.to_dependency( name, @@ -1731,6 +1732,7 @@ impl TomlDependency

{ features, }, kind, + span, ) } @@ -1739,14 +1741,15 @@ impl TomlDependency

{ name: &str, cx: &mut Context<'_, '_>, kind: Option, + span: Option, ) -> CargoResult { match *self { TomlDependency::Simple(ref version) => DetailedTomlDependency::

{ version: Some(version.clone()), ..Default::default() } - .to_dependency(name, cx, kind), - TomlDependency::Detailed(ref details) => details.to_dependency(name, cx, kind), + .to_dependency(name, cx, kind, span), + TomlDependency::Detailed(ref details) => details.to_dependency(name, cx, kind, span), } } @@ -1764,6 +1767,7 @@ impl DetailedTomlDependency

{ name_in_toml: &str, cx: &mut Context<'_, '_>, kind: Option, + span: Option ) -> CargoResult { if self.version.is_none() && self.path.is_none() && self.git.is_none() { bail!( @@ -1923,7 +1927,7 @@ impl DetailedTomlDependency

{ }; let version = self.version.as_deref(); - let mut dep = Dependency::parse(pkg_name, version, new_source_id)?; + let mut dep = Dependency::parse(pkg_name, version, new_source_id, span)?; dep.set_features(self.features.iter().flatten()) .set_default_features( self.default_features From 7954268a26fc9ef1aece01c05b4e35e571cb2468 Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Mon, 2 Aug 2021 07:44:22 +0100 Subject: [PATCH 4/5] cargo fmt --- src/cargo/core/compiler/standard_lib.rs | 4 +++- src/cargo/core/dependency.rs | 19 +++++++++++++------ src/cargo/core/registry.rs | 3 ++- src/cargo/core/resolver/version_prefs.rs | 2 +- src/cargo/core/workspace.rs | 2 +- src/cargo/ops/cargo_compile.rs | 9 +++++++-- src/cargo/sources/registry/mod.rs | 2 +- src/cargo/util/toml/mod.rs | 12 ++++++++---- 8 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/cargo/core/compiler/standard_lib.rs b/src/cargo/core/compiler/standard_lib.rs index cc5ffc0fc87..2e881bd95e0 100644 --- a/src/cargo/core/compiler/standard_lib.rs +++ b/src/cargo/core/compiler/standard_lib.rs @@ -5,7 +5,9 @@ use crate::core::compiler::{CompileKind, CompileMode, RustcTargetData, Unit}; use crate::core::profiles::{Profiles, UnitFor}; use crate::core::resolver::features::{CliFeatures, FeaturesFor, ResolvedFeatures}; use crate::core::resolver::HasDevUnits; -use crate::core::{Dependency, dependency::Span, PackageId, PackageSet, Resolve, SourceId, Workspace}; +use crate::core::{ + dependency::Span, Dependency, PackageId, PackageSet, Resolve, SourceId, Workspace, +}; use crate::ops::{self, Packages}; use crate::util::errors::CargoResult; use std::collections::{HashMap, HashSet}; diff --git a/src/cargo/core/dependency.rs b/src/cargo/core/dependency.rs index d5677866259..83b905fa5b3 100644 --- a/src/cargo/core/dependency.rs +++ b/src/cargo/core/dependency.rs @@ -46,10 +46,10 @@ struct Inner { // `None` means *all platforms*. platform: Option, - span: Option + span: Option, } -#[derive(Clone,Debug, Hash, Eq, PartialEq)] +#[derive(Clone, Debug, Hash, Eq, PartialEq)] pub struct Span { start: usize, // NonZeroUsize so that Option is free. @@ -60,9 +60,12 @@ impl Span { //pub const EMPTY: Span = Span{ start: 0, end: 0 }; } -impl From<&toml::Spanned> for Span { +impl From<&toml::Spanned> for Span { fn from(spanned_src: &toml::Spanned) -> Self { - Span { start: spanned_src.start(), end: NonZeroUsize::new(spanned_src.end()).unwrap() } + Span { + start: spanned_src.start(), + end: NonZeroUsize::new(spanned_src.end()).unwrap(), + } } } @@ -163,7 +166,11 @@ impl Dependency { Ok(ret) } - pub fn new_override(name: InternedString, source_id: SourceId, span: Option) -> Dependency { + pub fn new_override( + name: InternedString, + source_id: SourceId, + span: Option, + ) -> Dependency { assert!(!name.is_empty()); Dependency { inner: Rc::new(Inner { @@ -180,7 +187,7 @@ impl Dependency { specified_req: false, platform: None, explicit_name_in_toml: None, - span + span, }), } } diff --git a/src/cargo/core/registry.rs b/src/cargo/core/registry.rs index 8ba63c648f2..223b793c3e8 100644 --- a/src/cargo/core/registry.rs +++ b/src/cargo/core/registry.rs @@ -841,7 +841,8 @@ fn summary_for_patch( return Ok((summary, Some(locked.package_id))); } // Try checking if there are *any* packages that match this by name. - let name_only_dep = Dependency::new_override(orig_patch.package_name(), orig_patch.source_id(), None); + let name_only_dep = + Dependency::new_override(orig_patch.package_name(), orig_patch.source_id(), None); let name_summaries = source.query_vec(&name_only_dep).unwrap_or_else(|e| { log::warn!( "failed to do name-only summary query for {:?}: {:?}", diff --git a/src/cargo/core/resolver/version_prefs.rs b/src/cargo/core/resolver/version_prefs.rs index 051501a428b..7c51a8fc478 100644 --- a/src/cargo/core/resolver/version_prefs.rs +++ b/src/cargo/core/resolver/version_prefs.rs @@ -72,8 +72,8 @@ impl VersionPreferences { #[cfg(test)] mod test { use super::*; - use crate::core::SourceId; use crate::core::dependency::Span; + use crate::core::SourceId; use crate::util::Config; use std::collections::BTreeMap; diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index ff57065a7c2..0347c2a0719 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -400,7 +400,7 @@ impl<'cfg> Workspace<'cfg> { Path::new("unused-relative-path"), self.unstable_features(), /* kind */ None, - None + None, ) }) .collect::>>()?, diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index c8ec9da2ecf..7949a124abc 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -397,8 +397,13 @@ pub fn create_bcx<'a, 'cfg>( // requested_target to an enum, or some other approach. anyhow::bail!("-Zbuild-std requires --target"); } - let (std_package_set, std_resolve, std_features) = - standard_lib::resolve_std(ws, &target_data, &build_config.requested_kinds, crates, None)?; + let (std_package_set, std_resolve, std_features) = standard_lib::resolve_std( + ws, + &target_data, + &build_config.requested_kinds, + crates, + None, + )?; pkg_set.add_set(std_package_set); Some((std_resolve, std_features)) } else { diff --git a/src/cargo/sources/registry/mod.rs b/src/cargo/sources/registry/mod.rs index 7bbabc893d9..32b8ab3ffe8 100644 --- a/src/cargo/sources/registry/mod.rs +++ b/src/cargo/sources/registry/mod.rs @@ -174,7 +174,7 @@ use tar::Archive; use crate::core::dependency::{DepKind, Dependency}; use crate::core::source::MaybePackage; -use crate::core::{Package, PackageId, Source, SourceId, Summary, dependency::Span}; +use crate::core::{dependency::Span, Package, PackageId, Source, SourceId, Summary}; use crate::sources::PathSource; use crate::util::hex; use crate::util::interning::InternedString; diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 0c927251ba6..78606143066 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -20,9 +20,11 @@ use crate::core::compiler::{CompileKind, CompileTarget}; use crate::core::dependency::DepKind; use crate::core::manifest::{ManifestMetadata, TargetSourcePath, Warnings}; use crate::core::resolver::ResolveBehavior; +use crate::core::{ + dependency::Span, GitReference, PackageIdSpec, SourceId, WorkspaceConfig, WorkspaceRootConfig, +}; use crate::core::{Dependency, Manifest, PackageId, Summary, Target}; use crate::core::{Edition, EitherManifest, Feature, Features, VirtualManifest, Workspace}; -use crate::core::{GitReference, dependency::Span, PackageIdSpec, SourceId, WorkspaceConfig, WorkspaceRootConfig}; use crate::sources::{CRATES_IO_INDEX, CRATES_IO_REGISTRY}; use crate::util::errors::{CargoResult, ManifestError}; use crate::util::interning::InternedString; @@ -1266,7 +1268,9 @@ impl TomlManifest { None => return Ok(()), }; for (n, v) in dependencies.iter() { - let dep = v.get_ref().to_dependency(n, cx, kind, Some(Span::from(v)))?; + let dep = v + .get_ref() + .to_dependency(n, cx, kind, Some(Span::from(v)))?; validate_package_name(dep.name_in_toml().as_str(), "dependency name", "")?; cx.deps.push(dep); } @@ -1717,7 +1721,7 @@ impl TomlDependency

{ root: &Path, features: &Features, kind: Option, - span: Option + span: Option, ) -> CargoResult { self.to_dependency( name, @@ -1767,7 +1771,7 @@ impl DetailedTomlDependency

{ name_in_toml: &str, cx: &mut Context<'_, '_>, kind: Option, - span: Option + span: Option, ) -> CargoResult { if self.version.is_none() && self.path.is_none() && self.git.is_none() { bail!( From 4d825eda11b9cdbbe56a6488983885ddb255acb3 Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Sun, 8 Aug 2021 06:58:23 +0100 Subject: [PATCH 5/5] This gets me a different error message in the test failures. [dependency.foo] <-- foo isn't a Spanned<&str> --- src/cargo/util/toml/mod.rs | 32 ++++++++++++++++---------------- src/cargo/util/toml/targets.rs | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 78606143066..558568eaec8 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -333,17 +333,17 @@ pub struct TomlManifest { example: Option>, test: Option>, bench: Option>, - dependencies: Option>>, - dev_dependencies: Option>>, + dependencies: Option, Spanned>>, + dev_dependencies: Option, Spanned>>, #[serde(rename = "dev_dependencies")] - dev_dependencies2: Option>>, - build_dependencies: Option>>, + dev_dependencies2: Option, Spanned>>, + build_dependencies: Option, Spanned>>, #[serde(rename = "build_dependencies")] - build_dependencies2: Option>>, + build_dependencies2: Option, Spanned>>, features: Option>>, target: Option>, replace: Option>>, - patch: Option>>>, + patch: Option, Spanned>>>, workspace: Option, badges: Option>>, } @@ -1036,9 +1036,9 @@ impl TomlManifest { fn map_deps( config: &Config, - deps: Option<&BTreeMap>>, + deps: Option<&BTreeMap, Spanned>>, filter: impl Fn(&TomlDependency) -> bool, - ) -> CargoResult>>> { + ) -> CargoResult, Spanned>>> { let deps = match deps { Some(deps) => deps, None => return Ok(None), @@ -1260,7 +1260,7 @@ impl TomlManifest { fn process_dependencies( cx: &mut Context<'_, '_>, - new_deps: Option<&BTreeMap>>, + new_deps: Option<&BTreeMap, Spanned>>, kind: Option, ) -> CargoResult<()> { let dependencies = match new_deps { @@ -1270,7 +1270,7 @@ impl TomlManifest { for (n, v) in dependencies.iter() { let dep = v .get_ref() - .to_dependency(n, cx, kind, Some(Span::from(v)))?; + .to_dependency((*n).get_ref(), cx, kind, Some(Span::from(v)))?; validate_package_name(dep.name_in_toml().as_str(), "dependency name", "")?; cx.deps.push(dep); } @@ -1628,7 +1628,7 @@ impl TomlManifest { patch.insert( url, deps.iter() - .map(|(name, dep)| dep.get_ref().to_dependency(name, cx, None, None)) + .map(|(name, dep)| dep.get_ref().to_dependency(name.get_ref(), cx, None, None)) .collect::>>()?, ); } @@ -2022,15 +2022,15 @@ impl ser::Serialize for PathValue { /// Corresponds to a `target` entry, but `TomlTarget` is already used. #[derive(Serialize, Deserialize, Debug)] struct TomlPlatform { - dependencies: Option>>, + dependencies: Option, Spanned>>, #[serde(rename = "build-dependencies")] - build_dependencies: Option>>, + build_dependencies: Option, Spanned>>, #[serde(rename = "build_dependencies")] - build_dependencies2: Option>>, + build_dependencies2: Option, Spanned>>, #[serde(rename = "dev-dependencies")] - dev_dependencies: Option>>, + dev_dependencies: Option, Spanned>>, #[serde(rename = "dev_dependencies")] - dev_dependencies2: Option>>, + dev_dependencies2: Option, Spanned>>, } impl TomlTarget { diff --git a/src/cargo/util/toml/targets.rs b/src/cargo/util/toml/targets.rs index ffc72af65b1..8202d52859e 100644 --- a/src/cargo/util/toml/targets.rs +++ b/src/cargo/util/toml/targets.rs @@ -124,7 +124,7 @@ pub fn targets( // Verify names match available build deps. let bdeps = manifest.build_dependencies.as_ref(); for name in &metabuild.0 { - if !bdeps.map_or(false, |bd| bd.contains_key(name)) { + if !bdeps.map_or(false, |bd| bd.keys().any(|k|k.get_ref() == name)) { anyhow::bail!( "metabuild package `{}` must be specified in `build-dependencies`", name