Skip to content
Draft
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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/rustfix/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustfix"
version = "0.8.7"
version = "0.8.8"
authors = [
"Pascal Hertleif <killercup@gmail.com>",
"Oliver Schneider <oli-obk@users.noreply.github.com>",
Expand Down
4 changes: 1 addition & 3 deletions crates/rustfix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ pub fn collect_suggestions<S: ::std::hash::BuildHasher>(
}
}

let snippets = diagnostic.spans.iter().map(span_to_snippet).collect();

let solutions: Vec<_> = diagnostic
.children
.iter()
Expand Down Expand Up @@ -204,7 +202,7 @@ pub fn collect_suggestions<S: ::std::hash::BuildHasher>(
} else {
Some(Suggestion {
message: diagnostic.message.clone(),
snippets,
snippets: diagnostic.spans.iter().map(span_to_snippet).collect(),
solutions,
})
}
Expand Down
161 changes: 76 additions & 85 deletions crates/rustfix/src/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl State {
}

/// Span with a change [`State`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Clone, PartialEq, Eq)]
struct Span {
/// Start of this span in parent data
start: usize,
Expand All @@ -34,6 +34,17 @@ struct Span {
data: State,
}

impl std::fmt::Debug for Span {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let state = match self.data {
State::Initial => "initial",
State::Replaced(_) => "replaced",
State::Inserted(_) => "inserted",
};
write!(f, "({}, {}: {state})", self.start, self.end)
}
}

/// A container that allows easily replacing chunks of its data
#[derive(Debug, Clone, Default)]
pub struct Data {
Expand Down Expand Up @@ -97,102 +108,82 @@ impl Data {
// [^empty]: Leading and trailing ones might be empty if we replace
// the whole chunk. As an optimization and without loss of generality we
// don't add empty parts.
let new_parts = {
let Some(index_of_part_to_split) = self.parts.iter().position(|p| {
!p.data.is_inserted() && p.start <= range.start && p.end >= range.end
}) else {
if tracing::enabled!(tracing::Level::DEBUG) {
let slices = self
.parts
.iter()
.map(|p| {
(
p.start,
p.end,
match p.data {
State::Initial => "initial",
State::Replaced(..) => "replaced",
State::Inserted(..) => "inserted",
},
)
})
.collect::<Vec<_>>();
tracing::debug!(
"no single slice covering {}..{}, current slices: {:?}",
range.start,
range.end,
slices,
);
}
let Some(index_of_part_to_split) = self
.parts
.iter()
.position(|p| !p.data.is_inserted() && p.start <= range.start && p.end >= range.end)
else {
tracing::debug!(
"no single slice covering {}..{}, current slices: {:?}",
range.start,
range.end,
self.parts,
);
return Err(Error::MaybeAlreadyReplaced(range));
};

return Err(Error::MaybeAlreadyReplaced(range));
};
let part_to_split = &self.parts[index_of_part_to_split];

let part_to_split = &self.parts[index_of_part_to_split];

// If this replacement matches exactly the part that we would
// otherwise split then we ignore this for now. This means that you
// can replace the exact same range with the exact same content
// multiple times and we'll process and allow it.
//
// This is currently done to alleviate issues like
// rust-lang/rust#51211 although this clause likely wants to be
// removed if that's fixed deeper in the compiler.
if part_to_split.start == range.start && part_to_split.end == range.end {
if let State::Replaced(ref replacement) = part_to_split.data {
if &**replacement == data {
return Ok(());
}
// If this replacement matches exactly the part that we would
// otherwise split then we ignore this for now. This means that you
// can replace the exact same range with the exact same content
// multiple times and we'll process and allow it.
//
// This is currently done to alleviate issues like
// rust-lang/rust#51211 although this clause likely wants to be
// removed if that's fixed deeper in the compiler.
if part_to_split.start == range.start && part_to_split.end == range.end {
if let State::Replaced(ref replacement) = part_to_split.data {
if &**replacement == data {
return Ok(());
}
}
}

if part_to_split.data != State::Initial {
return Err(Error::AlreadyReplaced);
}

let mut new_parts = Vec::with_capacity(self.parts.len() + 2);
if part_to_split.data != State::Initial {
return Err(Error::AlreadyReplaced);
}

// Previous parts
if let Some(ps) = self.parts.get(..index_of_part_to_split) {
new_parts.extend_from_slice(ps);
}
let mut new_parts = Vec::with_capacity(self.parts.len() + 2);

// Keep initial data on left side of part
if range.start > part_to_split.start {
new_parts.push(Span {
start: part_to_split.start,
end: range.start,
data: State::Initial,
});
}
// Previous parts
if let Some(ps) = self.parts.get(..index_of_part_to_split) {
new_parts.extend_from_slice(ps);
}

// New part
// Keep initial data on left side of part
if range.start > part_to_split.start {
new_parts.push(Span {
start: range.start,
end: range.end,
data: if insert_only {
State::Inserted(data.into())
} else {
State::Replaced(data.into())
},
start: part_to_split.start,
end: range.start,
data: State::Initial,
});
}

// Keep initial data on right side of part
if range.end < part_to_split.end {
new_parts.push(Span {
start: range.end,
end: part_to_split.end,
data: State::Initial,
});
}

// Following parts
if let Some(ps) = self.parts.get(index_of_part_to_split + 1..) {
new_parts.extend_from_slice(ps);
}
// New part
new_parts.push(Span {
start: range.start,
end: range.end,
data: if insert_only {
State::Inserted(data.into())
} else {
State::Replaced(data.into())
},
});

// Keep initial data on right side of part
if range.end < part_to_split.end {
new_parts.push(Span {
start: range.end,
end: part_to_split.end,
data: State::Initial,
});
}

new_parts
};
// Following parts
if let Some(ps) = self.parts.get(index_of_part_to_split + 1..) {
new_parts.extend_from_slice(ps);
}

self.parts = new_parts;

Expand Down
4 changes: 4 additions & 0 deletions src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,10 +987,14 @@ impl<'gctx> RustcTargetData<'gctx> {
pub fn dep_platform_activated(&self, dep: &Dependency, kind: CompileKind) -> bool {
// If this dependency is only available for certain platforms,
// make sure we're only enabling it for that platform.
// println!("Is platform activated? {:?}, artifact: {:?} Platform: {:?}", dep.name_in_toml(), dep.artifact(), dep.platform());
// println!("Is platform specified? {:?}. artifact: {:?} kind: {:?} Platform: {:?}", dep.name_in_toml(), dep.artifact(), kind, dep.platform());
let Some(platform) = dep.platform() else {
return true;
};
let name = self.short_name(&kind);
println!("self.cfg(kind) {:#?}", self.cfg(kind));
println!("Platform specified. {:?} - artifact: {:?} - kind: {:?} - dep.kind(): {:?} - Activated: {}", dep.name_in_toml(), dep.artifact(), kind, dep.kind(), platform.matches(name, self.cfg(kind)));
platform.matches(name, self.cfg(kind))
}

Expand Down
5 changes: 5 additions & 0 deletions src/cargo/core/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ impl Dependency {
self.inner.name
}

// // This probably won't work because the version requirement is prob different from the resolved version
// pub fn package_id(&self) -> PackageId {
// PackageId::new(self.package_name(), self.version_req(), self.source_id())
// }

pub fn source_id(&self) -> SourceId {
self.inner.source_id
}
Expand Down
41 changes: 39 additions & 2 deletions src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ impl<'gctx> PackageSet<'gctx> {

// let's not flood crates.io with connections
multi.set_max_host_connections(2)?;
// println!("LazyCell called on this from PackageSet: {:?}", package_ids);

Ok(PackageSet {
packages: package_ids
Expand All @@ -425,7 +426,9 @@ impl<'gctx> PackageSet<'gctx> {
pub fn package_ids(&self) -> impl Iterator<Item = PackageId> + '_ {
self.packages.keys().cloned()
}

pub fn packages_debug(&self) -> impl Iterator<Item = Option<&Package>> {
self.packages.values().map(|p| p.borrow())
}
pub fn packages(&self) -> impl Iterator<Item = &Package> {
self.packages.values().filter_map(|p| p.borrow())
}
Expand Down Expand Up @@ -498,6 +501,7 @@ impl<'gctx> PackageSet<'gctx> {
target_data: &RustcTargetData<'gctx>,
force_all_targets: ForceAllTargets,
) -> CargoResult<()> {
println!("In download_accessible: requested_kinds {:?}", requested_kinds);
fn collect_used_deps(
used: &mut BTreeSet<PackageId>,
resolve: &Resolve,
Expand Down Expand Up @@ -548,6 +552,7 @@ impl<'gctx> PackageSet<'gctx> {
force_all_targets,
)?;
}
println!("to_download btree {:?}", to_download);
self.get_many(to_download.into_iter())?;
Ok(())
}
Expand Down Expand Up @@ -616,21 +621,53 @@ impl<'gctx> PackageSet<'gctx> {
target_data: &'a RustcTargetData<'_>,
force_all_targets: ForceAllTargets,
) -> impl Iterator<Item = (PackageId, &'a HashSet<Dependency>)> + 'a {
println!("Filtering deps of {pkg_id:?}. Requested kinds: {:?}", requested_kinds);
resolve
.deps(pkg_id)
.filter(move |&(_id, deps)| {
deps.iter().any(|dep| {
println!("DEP: {:?}", dep.name_in_toml());
if dep.kind() == DepKind::Development && has_dev_units == HasDevUnits::No {
return false;
}
println!("\tWAS NOT A DEV DEP");
if force_all_targets == ForceAllTargets::No {
// let chain = match dep.artifact()
// .and_then(|artifact| artifact.target())
// .and_then(|target| target.to_resolved_compile_target(requested_kind)) {
// Some(target) => [&CompileKind::Target(target), &CompileKind::Host],
// None => &CompileKind::Host
// };
// let kind = match (node_kind, dep.kind()) {
// (CompileKind::Host, _) => CompileKind::Host,
// (_, DepKind::Build) => CompileKind::Host,
// (_, DepKind::Normal) => node_kind,
// (_, DepKind::Development) => node_kind,
// };
// let activated = if dep.artifact()
// .and_then(|artifact| artifact.target())
// .and_then(|target| target.to_resolved_compile_target(requested_kind))
println!("REQUESTED KINDS {:?}", requested_kinds);
let activated = requested_kinds
.iter()
.chain(Some(&CompileKind::Host))
.any(|kind| target_data.dep_platform_activated(dep, *kind));
.chain(other)
.any(|kind| {
println!("REQUESTED KIND {:?}", kind);
let req_kind = dep.artifact()
.and_then(|artifact| artifact.target())
.and_then(|target| target.to_resolved_compile_target(*kind))
.and_then(|ctarget| Some(CompileKind::Target(ctarget)))
.unwrap_or(*kind);

println!("Artifact resolved compile target: {:?}", req_kind);
target_data.dep_platform_activated(dep, req_kind)
});
if !activated {
println!("\tWAS NOT ACTIVATED");
return false;
}
println!("\tWAS ACTIVATED");
}
true
})
Expand Down
13 changes: 8 additions & 5 deletions src/cargo/ops/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ use crate::util::toml_mut::dependency::MaybeWorkspace;
use crate::util::toml_mut::dependency::PathSource;
use crate::util::toml_mut::dependency::Source;
use crate::util::toml_mut::dependency::WorkspaceSource;
use crate::util::toml_mut::is_sorted;
use crate::util::toml_mut::manifest::DepTable;
use crate::util::toml_mut::manifest::LocalManifest;
use crate::CargoResult;
Expand Down Expand Up @@ -111,10 +110,14 @@ pub fn add(workspace: &Workspace<'_>, options: &AddOptions<'_>) -> CargoResult<(
.map(TomlItem::as_table)
.map_or(true, |table_option| {
table_option.map_or(true, |table| {
is_sorted(table.get_values().iter_mut().map(|(key, _)| {
// get_values key paths always have at least one key.
key.remove(0)
}))
table
.get_values()
.iter_mut()
.map(|(key, _)| {
// get_values key paths always have at least one key.
key.remove(0)
})
.is_sorted()
})
});
for dep in deps {
Expand Down
3 changes: 1 addition & 2 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::core::{Edition, Shell, Workspace};
use crate::util::errors::CargoResult;
use crate::util::important_paths::find_root_manifest_for_wd;
use crate::util::toml_mut::is_sorted;
use crate::util::{existing_vcs_repo, FossilRepo, GitRepo, HgRepo, PijulRepo};
use crate::util::{restricted_names, GlobalContext};
use anyhow::{anyhow, Context as _};
Expand Down Expand Up @@ -995,7 +994,7 @@ fn update_manifest_with_new_member(
}
}

let was_sorted = is_sorted(members.iter().map(Value::as_str));
let was_sorted = members.iter().map(Value::as_str).is_sorted();
members.push(display_path);
if was_sorted {
members.sort_by(|lhs, rhs| lhs.as_str().cmp(&rhs.as_str()));
Expand Down
1 change: 1 addition & 0 deletions src/cargo/ops/cargo_output_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ fn build_resolve_graph_r(
let lib_target = targets.iter().find(|t| t.is_lib());

for dep in deps.iter() {
// this filtering is VERY IMPORTANT
if let Some(target) = lib_target {
// When we do have a library target, include them in deps if...
let included = match dep.artifact() {
Expand Down
Loading