Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Capture dep info with Spanned #9752

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/future_incompat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/cargo/core/compiler/standard_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, 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};
Expand Down Expand Up @@ -36,6 +38,7 @@ pub fn resolve_std<'cfg>(
target_data: &RustcTargetData<'cfg>,
requested_targets: &[CompileKind],
crates: &[String],
span: Option<Span>,
) -> CargoResult<(PackageSet<'cfg>, Resolve, ResolvedFeatures)> {
let src_path = detect_sysroot_src_path(target_data)?;
let to_patch = [
Expand All @@ -47,7 +50,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::<CargoResult<Vec<_>>>()?;
Expand Down
37 changes: 35 additions & 2 deletions src/cargo/core/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -44,6 +45,28 @@ struct Inner {
// This dependency should be used only for this platform.
// `None` means *all platforms*.
platform: Option<Platform>,

span: Option<Span>,
}

#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub struct Span {
start: usize,
// NonZeroUsize so that Option<Span> is free.
end: NonZeroUsize,
}

impl Span {
//pub const EMPTY: Span = Span{ start: 0, end: 0 };
}

impl<T> From<&toml::Spanned<T>> for Span {
fn from(spanned_src: &toml::Spanned<T>) -> Self {
Span {
start: spanned_src.start(),
end: NonZeroUsize::new(spanned_src.end()).unwrap(),
}
}
}

#[derive(Serialize)]
Expand Down Expand Up @@ -117,6 +140,7 @@ impl Dependency {
name: impl Into<InternedString>,
version: Option<&str>,
source_id: SourceId,
span: Option<Span>,
) -> CargoResult<Dependency> {
let name = name.into();
let (specified_req, version_req) = match version {
Expand All @@ -132,7 +156,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;
Expand All @@ -142,7 +166,11 @@ 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<Span>,
) -> Dependency {
assert!(!name.is_empty());
Dependency {
inner: Rc::new(Inner {
Expand All @@ -159,6 +187,7 @@ impl Dependency {
specified_req: false,
platform: None,
explicit_name_in_toml: None,
span,
}),
}
}
Expand Down Expand Up @@ -217,6 +246,10 @@ impl Dependency {
self.inner.source_id
}

pub fn span(&self) -> Option<Span> {
self.inner.span.clone()
}

pub fn registry_id(&self) -> Option<SourceId> {
self.inner.registry_id
}
Expand Down
5 changes: 3 additions & 2 deletions src/cargo/core/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ impl<'cfg> PackageRegistry<'cfg> {
fn query_overrides(&mut self, dep: &Dependency) -> CargoResult<Option<Summary>> {
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)));
Expand Down Expand Up @@ -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());
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 {:?}: {:?}",
Expand Down
4 changes: 3 additions & 1 deletion src/cargo/core/resolver/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: ");
Expand Down
9 changes: 5 additions & 4 deletions src/cargo/core/resolver/version_prefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ impl VersionPreferences {
#[cfg(test)]
mod test {
use super::*;
use crate::core::dependency::Span;
use crate::core::SourceId;
use crate::util::Config;
use std::collections::BTreeMap;
Expand All @@ -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<Span>) -> 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 {
Expand Down Expand Up @@ -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"),
Expand All @@ -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"),
Expand Down
1 change: 1 addition & 0 deletions src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ impl<'cfg> Workspace<'cfg> {
Path::new("unused-relative-path"),
self.unstable_features(),
/* kind */ None,
None,
)
})
.collect::<CargoResult<Vec<_>>>()?,
Expand Down
9 changes: 7 additions & 2 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
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 {
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/cargo_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/sources/registry/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<CargoResult<Vec<_>>>()?;
if let Some(features2) = features2 {
for (name, values) in features2 {
Expand Down
6 changes: 3 additions & 3 deletions src/cargo/sources/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{dependency::Span, Package, PackageId, Source, SourceId, Summary};
use crate::sources::PathSource;
use crate::util::hex;
use crate::util::interning::InternedString;
Expand Down Expand Up @@ -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<Dependency> {
pub fn into_dep(self, default: SourceId, span: Option<Span>) -> CargoResult<Dependency> {
let RegistryDependency {
name,
req,
Expand All @@ -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);
}
Expand Down
Loading