mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-26 11:59:49 +00:00
parent
b3ef934ccb
commit
25242fe93f
395 changed files with 14569 additions and 5755 deletions
|
@ -66,7 +66,7 @@ impl WorkspaceBuildScripts {
|
|||
_ => {
|
||||
let mut cmd = Command::new(toolchain::cargo());
|
||||
|
||||
cmd.args(&["check", "--quiet", "--workspace", "--message-format=json"]);
|
||||
cmd.args(["check", "--quiet", "--workspace", "--message-format=json"]);
|
||||
|
||||
// --all-targets includes tests, benches and examples in addition to the
|
||||
// default lib and bins. This is an independent concept from the --target
|
||||
|
@ -74,7 +74,7 @@ impl WorkspaceBuildScripts {
|
|||
cmd.arg("--all-targets");
|
||||
|
||||
if let Some(target) = &config.target {
|
||||
cmd.args(&["--target", target]);
|
||||
cmd.args(["--target", target]);
|
||||
}
|
||||
|
||||
match &config.features {
|
||||
|
@ -122,7 +122,7 @@ impl WorkspaceBuildScripts {
|
|||
InvocationLocation::Root(root) if config.run_build_script_command.is_some() => {
|
||||
root.as_path()
|
||||
}
|
||||
_ => &workspace.workspace_root(),
|
||||
_ => workspace.workspace_root(),
|
||||
}
|
||||
.as_ref();
|
||||
|
||||
|
@ -133,7 +133,7 @@ impl WorkspaceBuildScripts {
|
|||
// building build scripts failed, attempt to build with --keep-going so
|
||||
// that we potentially get more build data
|
||||
let mut cmd = Self::build_command(config)?;
|
||||
cmd.args(&["-Z", "unstable-options", "--keep-going"]).env("RUSTC_BOOTSTRAP", "1");
|
||||
cmd.args(["-Z", "unstable-options", "--keep-going"]).env("RUSTC_BOOTSTRAP", "1");
|
||||
let mut res = Self::run_per_ws(cmd, workspace, current_dir, progress)?;
|
||||
res.error = Some(error);
|
||||
Ok(res)
|
||||
|
@ -295,7 +295,7 @@ impl WorkspaceBuildScripts {
|
|||
match message {
|
||||
Message::BuildScriptExecuted(mut message) => {
|
||||
with_output_for(&message.package_id.repr, &mut |name, data| {
|
||||
progress(format!("running build-script: {}", name));
|
||||
progress(format!("running build-script: {name}"));
|
||||
let cfgs = {
|
||||
let mut acc = Vec::new();
|
||||
for cfg in &message.cfgs {
|
||||
|
@ -303,8 +303,7 @@ impl WorkspaceBuildScripts {
|
|||
Ok(it) => acc.push(it),
|
||||
Err(err) => {
|
||||
push_err(&format!(
|
||||
"invalid cfg from cargo-metadata: {}",
|
||||
err
|
||||
"invalid cfg from cargo-metadata: {err}"
|
||||
));
|
||||
return;
|
||||
}
|
||||
|
@ -334,7 +333,7 @@ impl WorkspaceBuildScripts {
|
|||
}
|
||||
Message::CompilerArtifact(message) => {
|
||||
with_output_for(&message.package_id.repr, &mut |name, data| {
|
||||
progress(format!("building proc-macros: {}", name));
|
||||
progress(format!("building proc-macros: {name}"));
|
||||
if message.target.kind.iter().any(|k| k == "proc-macro") {
|
||||
// Skip rmeta file
|
||||
if let Some(filename) =
|
||||
|
|
|
@ -411,7 +411,7 @@ impl CargoWorkspace {
|
|||
CargoWorkspace { packages, targets, workspace_root }
|
||||
}
|
||||
|
||||
pub fn packages<'a>(&'a self) -> impl Iterator<Item = Package> + ExactSizeIterator + 'a {
|
||||
pub fn packages(&self) -> impl Iterator<Item = Package> + ExactSizeIterator + '_ {
|
||||
self.packages.iter().map(|(id, _pkg)| id)
|
||||
}
|
||||
|
||||
|
@ -427,7 +427,7 @@ impl CargoWorkspace {
|
|||
}
|
||||
|
||||
pub fn package_flag(&self, package: &PackageData) -> String {
|
||||
if self.is_unique(&*package.name) {
|
||||
if self.is_unique(&package.name) {
|
||||
package.name.clone()
|
||||
} else {
|
||||
format!("{}:{}", package.name, package.version)
|
||||
|
@ -517,7 +517,7 @@ fn cargo_config_build_target(
|
|||
cargo_config.envs(extra_env);
|
||||
cargo_config
|
||||
.current_dir(cargo_toml.parent())
|
||||
.args(&["-Z", "unstable-options", "config", "get", "build.target"])
|
||||
.args(["-Z", "unstable-options", "config", "get", "build.target"])
|
||||
.env("RUSTC_BOOTSTRAP", "1");
|
||||
// if successful we receive `build.target = "target-triple"`
|
||||
// or `build.target = ["<target 1>", ..]`
|
||||
|
|
|
@ -17,7 +17,7 @@ impl FromStr for CfgFlag {
|
|||
let res = match s.split_once('=') {
|
||||
Some((key, value)) => {
|
||||
if !(value.starts_with('"') && value.ends_with('"')) {
|
||||
return Err(format!("Invalid cfg ({:?}), value should be in quotes", s));
|
||||
return Err(format!("Invalid cfg ({s:?}), value should be in quotes"));
|
||||
}
|
||||
let key = key.to_string();
|
||||
let value = value[1..value.len() - 1].to_string();
|
||||
|
|
|
@ -25,6 +25,7 @@ mod sysroot;
|
|||
mod workspace;
|
||||
mod rustc_cfg;
|
||||
mod build_scripts;
|
||||
mod target_data_layout;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
@ -145,7 +146,7 @@ impl ProjectManifest {
|
|||
}
|
||||
|
||||
fn utf8_stdout(mut cmd: Command) -> Result<String> {
|
||||
let output = cmd.output().with_context(|| format!("{:?} failed", cmd))?;
|
||||
let output = cmd.output().with_context(|| format!("{cmd:?} failed"))?;
|
||||
if !output.status.success() {
|
||||
match String::from_utf8(output.stderr) {
|
||||
Ok(stderr) if !stderr.is_empty() => {
|
||||
|
|
|
@ -40,7 +40,7 @@ impl ops::Deref for ManifestPath {
|
|||
type Target = AbsPath;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&*self.file
|
||||
&self.file
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -197,5 +197,5 @@ where
|
|||
D: de::Deserializer<'de>,
|
||||
{
|
||||
let name = String::deserialize(de)?;
|
||||
CrateName::new(&name).map_err(|err| de::Error::custom(format!("invalid crate name: {:?}", err)))
|
||||
CrateName::new(&name).map_err(|err| de::Error::custom(format!("invalid crate name: {err:?}")))
|
||||
}
|
||||
|
|
|
@ -50,10 +50,10 @@ fn get_rust_cfgs(
|
|||
cargo_config.envs(extra_env);
|
||||
cargo_config
|
||||
.current_dir(cargo_toml.parent())
|
||||
.args(&["-Z", "unstable-options", "rustc", "--print", "cfg"])
|
||||
.args(["rustc", "-Z", "unstable-options", "--print", "cfg"])
|
||||
.env("RUSTC_BOOTSTRAP", "1");
|
||||
if let Some(target) = target {
|
||||
cargo_config.args(&["--target", target]);
|
||||
cargo_config.args(["--target", target]);
|
||||
}
|
||||
match utf8_stdout(cargo_config) {
|
||||
Ok(it) => return Ok(it),
|
||||
|
@ -63,9 +63,9 @@ fn get_rust_cfgs(
|
|||
// using unstable cargo features failed, fall back to using plain rustc
|
||||
let mut cmd = Command::new(toolchain::rustc());
|
||||
cmd.envs(extra_env);
|
||||
cmd.args(&["--print", "cfg", "-O"]);
|
||||
cmd.args(["--print", "cfg", "-O"]);
|
||||
if let Some(target) = target {
|
||||
cmd.args(&["--target", target]);
|
||||
cmd.args(["--target", target]);
|
||||
}
|
||||
utf8_stdout(cmd)
|
||||
}
|
||||
|
|
|
@ -104,7 +104,7 @@ impl Sysroot {
|
|||
|
||||
for path in SYSROOT_CRATES.trim().lines() {
|
||||
let name = path.split('/').last().unwrap();
|
||||
let root = [format!("{}/src/lib.rs", path), format!("lib{}/lib.rs", path)]
|
||||
let root = [format!("{path}/src/lib.rs"), format!("lib{path}/lib.rs")]
|
||||
.into_iter()
|
||||
.map(|it| sysroot.src_root.join(it))
|
||||
.filter_map(|it| ManifestPath::try_from(it).ok())
|
||||
|
@ -171,7 +171,7 @@ fn discover_sysroot_dir(
|
|||
) -> Result<AbsPathBuf> {
|
||||
let mut rustc = Command::new(toolchain::rustc());
|
||||
rustc.envs(extra_env);
|
||||
rustc.current_dir(current_dir).args(&["--print", "sysroot"]);
|
||||
rustc.current_dir(current_dir).args(["--print", "sysroot"]);
|
||||
tracing::debug!("Discovering sysroot by {:?}", rustc);
|
||||
let stdout = utf8_stdout(rustc)?;
|
||||
Ok(AbsPathBuf::assert(PathBuf::from(stdout)))
|
||||
|
@ -203,7 +203,7 @@ fn discover_sysroot_src_dir_or_add_component(
|
|||
.or_else(|| {
|
||||
let mut rustup = Command::new(toolchain::rustup());
|
||||
rustup.envs(extra_env);
|
||||
rustup.current_dir(current_dir).args(&["component", "add", "rust-src"]);
|
||||
rustup.current_dir(current_dir).args(["component", "add", "rust-src"]);
|
||||
tracing::info!("adding rust-src component by {:?}", rustup);
|
||||
utf8_stdout(rustup).ok()?;
|
||||
get_rust_src(sysroot_path)
|
||||
|
|
40
crates/project-model/src/target_data_layout.rs
Normal file
40
crates/project-model/src/target_data_layout.rs
Normal file
|
@ -0,0 +1,40 @@
|
|||
//! Runs `rustc --print target-spec-json` to get the target_data_layout.
|
||||
use std::process::Command;
|
||||
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
use crate::{utf8_stdout, ManifestPath};
|
||||
|
||||
pub(super) fn get(
|
||||
cargo_toml: Option<&ManifestPath>,
|
||||
target: Option<&str>,
|
||||
extra_env: &FxHashMap<String, String>,
|
||||
) -> Option<String> {
|
||||
let output = (|| {
|
||||
if let Some(cargo_toml) = cargo_toml {
|
||||
let mut cmd = Command::new(toolchain::rustc());
|
||||
cmd.envs(extra_env);
|
||||
cmd.current_dir(cargo_toml.parent())
|
||||
.args(["-Z", "unstable-options", "rustc", "--print", "target-spec-json"])
|
||||
.env("RUSTC_BOOTSTRAP", "1");
|
||||
if let Some(target) = target {
|
||||
cmd.args(["--target", target]);
|
||||
}
|
||||
match utf8_stdout(cmd) {
|
||||
Ok(it) => return Ok(it),
|
||||
Err(e) => tracing::debug!("{e:?}: falling back to querying rustc for cfgs"),
|
||||
}
|
||||
}
|
||||
// using unstable cargo features failed, fall back to using plain rustc
|
||||
let mut cmd = Command::new(toolchain::rustc());
|
||||
cmd.envs(extra_env)
|
||||
.args(["-Z", "unstable-options", "rustc", "--print", "target-spec-json"])
|
||||
.env("RUSTC_BOOTSTRAP", "1");
|
||||
if let Some(target) = target {
|
||||
cmd.args(["--target", target]);
|
||||
}
|
||||
utf8_stdout(cmd)
|
||||
})()
|
||||
.ok()?;
|
||||
Some(output.split_once(r#""data-layout": ""#)?.1.split_once('"')?.0.to_owned())
|
||||
}
|
|
@ -29,6 +29,7 @@ fn load_cargo_with_overrides(file: &str, cfg_overrides: CfgOverrides) -> CrateGr
|
|||
rustc_cfg: Vec::new(),
|
||||
cfg_overrides,
|
||||
toolchain: None,
|
||||
target_layout: None,
|
||||
};
|
||||
to_crate_graph(project_workspace)
|
||||
}
|
||||
|
@ -106,7 +107,7 @@ fn to_crate_graph(project_workspace: ProjectWorkspace) -> CrateGraph {
|
|||
}
|
||||
|
||||
fn check_crate_graph(crate_graph: CrateGraph, expect: Expect) {
|
||||
let mut crate_graph = format!("{:#?}", crate_graph);
|
||||
let mut crate_graph = format!("{crate_graph:#?}");
|
||||
replace_root(&mut crate_graph, false);
|
||||
expect.assert_eq(&crate_graph);
|
||||
}
|
||||
|
@ -150,6 +151,7 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
|
|||
"debug_assertions",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -219,6 +221,7 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
|
|||
"debug_assertions",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -297,6 +300,7 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
|
|||
"debug_assertions",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -375,6 +379,7 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
|
|||
"debug_assertions",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -462,6 +467,7 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
|
|||
"feature=use_std",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -547,6 +553,7 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
|
|||
"test",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -618,6 +625,7 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
|
|||
"test",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -698,6 +706,7 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
|
|||
"test",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -778,6 +787,7 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
|
|||
"test",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -865,6 +875,7 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
|
|||
"feature=use_std",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -941,6 +952,7 @@ fn cargo_hello_world_project_model() {
|
|||
"test",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -1012,6 +1024,7 @@ fn cargo_hello_world_project_model() {
|
|||
"test",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -1092,6 +1105,7 @@ fn cargo_hello_world_project_model() {
|
|||
"test",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -1172,6 +1186,7 @@ fn cargo_hello_world_project_model() {
|
|||
"test",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -1259,6 +1274,7 @@ fn cargo_hello_world_project_model() {
|
|||
"feature=use_std",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -1327,6 +1343,7 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
@ -1371,6 +1388,7 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
@ -1405,6 +1423,7 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
@ -1439,6 +1458,7 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
@ -1473,6 +1493,7 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
@ -1517,6 +1538,7 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
@ -1551,6 +1573,7 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
@ -1658,6 +1681,7 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
@ -1692,6 +1716,7 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
@ -1726,6 +1751,7 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
@ -1760,6 +1786,7 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
|
|
@ -21,8 +21,8 @@ use crate::{
|
|||
cfg_flag::CfgFlag,
|
||||
rustc_cfg,
|
||||
sysroot::SysrootCrate,
|
||||
utf8_stdout, CargoConfig, CargoWorkspace, InvocationStrategy, ManifestPath, Package,
|
||||
ProjectJson, ProjectManifest, Sysroot, TargetKind, WorkspaceBuildScripts,
|
||||
target_data_layout, utf8_stdout, CargoConfig, CargoWorkspace, InvocationStrategy, ManifestPath,
|
||||
Package, ProjectJson, ProjectManifest, Sysroot, TargetKind, WorkspaceBuildScripts,
|
||||
};
|
||||
|
||||
/// A set of cfg-overrides per crate.
|
||||
|
@ -79,6 +79,7 @@ pub enum ProjectWorkspace {
|
|||
rustc_cfg: Vec<CfgFlag>,
|
||||
cfg_overrides: CfgOverrides,
|
||||
toolchain: Option<Version>,
|
||||
target_layout: Option<String>,
|
||||
},
|
||||
/// Project workspace was manually specified using a `rust-project.json` file.
|
||||
Json { project: ProjectJson, sysroot: Option<Sysroot>, rustc_cfg: Vec<CfgFlag> },
|
||||
|
@ -93,7 +94,7 @@ pub enum ProjectWorkspace {
|
|||
// //
|
||||
/// Project with a set of disjoint files, not belonging to any particular workspace.
|
||||
/// Backed by basic sysroot crates for basic completion and highlighting.
|
||||
DetachedFiles { files: Vec<AbsPathBuf>, sysroot: Sysroot, rustc_cfg: Vec<CfgFlag> },
|
||||
DetachedFiles { files: Vec<AbsPathBuf>, sysroot: Option<Sysroot>, rustc_cfg: Vec<CfgFlag> },
|
||||
}
|
||||
|
||||
impl fmt::Debug for ProjectWorkspace {
|
||||
|
@ -108,6 +109,7 @@ impl fmt::Debug for ProjectWorkspace {
|
|||
rustc_cfg,
|
||||
cfg_overrides,
|
||||
toolchain,
|
||||
target_layout: data_layout,
|
||||
} => f
|
||||
.debug_struct("Cargo")
|
||||
.field("root", &cargo.workspace_root().file_name())
|
||||
|
@ -120,6 +122,7 @@ impl fmt::Debug for ProjectWorkspace {
|
|||
.field("n_rustc_cfg", &rustc_cfg.len())
|
||||
.field("n_cfg_overrides", &cfg_overrides.len())
|
||||
.field("toolchain", &toolchain)
|
||||
.field("data_layout", &data_layout)
|
||||
.finish(),
|
||||
ProjectWorkspace::Json { project, sysroot, rustc_cfg } => {
|
||||
let mut debug_struct = f.debug_struct("Json");
|
||||
|
@ -133,7 +136,7 @@ impl fmt::Debug for ProjectWorkspace {
|
|||
ProjectWorkspace::DetachedFiles { files, sysroot, rustc_cfg } => f
|
||||
.debug_struct("DetachedFiles")
|
||||
.field("n_files", &files.len())
|
||||
.field("n_sysroot_crates", &sysroot.crates().len())
|
||||
.field("sysroot", &sysroot.is_some())
|
||||
.field("n_rustc_cfg", &rustc_cfg.len())
|
||||
.finish(),
|
||||
}
|
||||
|
@ -191,10 +194,7 @@ impl ProjectWorkspace {
|
|||
let sysroot = match &config.sysroot {
|
||||
Some(RustcSource::Path(path)) => {
|
||||
Some(Sysroot::with_sysroot_dir(path.clone()).with_context(|| {
|
||||
format!(
|
||||
"Failed to find sysroot for Cargo.toml file {}.",
|
||||
cargo_toml.display()
|
||||
)
|
||||
format!("Failed to find sysroot at {}.", path.display())
|
||||
})?)
|
||||
}
|
||||
Some(RustcSource::Discover) => Some(
|
||||
|
@ -244,6 +244,11 @@ impl ProjectWorkspace {
|
|||
rustc_cfg::get(Some(&cargo_toml), config.target.as_deref(), &config.extra_env);
|
||||
|
||||
let cfg_overrides = config.cfg_overrides();
|
||||
let data_layout = target_data_layout::get(
|
||||
Some(&cargo_toml),
|
||||
config.target.as_deref(),
|
||||
&config.extra_env,
|
||||
);
|
||||
ProjectWorkspace::Cargo {
|
||||
cargo,
|
||||
build_scripts: WorkspaceBuildScripts::default(),
|
||||
|
@ -252,6 +257,7 @@ impl ProjectWorkspace {
|
|||
rustc_cfg,
|
||||
cfg_overrides,
|
||||
toolchain,
|
||||
target_layout: data_layout,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -291,14 +297,29 @@ impl ProjectWorkspace {
|
|||
Ok(ProjectWorkspace::Json { project: project_json, sysroot, rustc_cfg })
|
||||
}
|
||||
|
||||
pub fn load_detached_files(detached_files: Vec<AbsPathBuf>) -> Result<ProjectWorkspace> {
|
||||
let sysroot = Sysroot::discover(
|
||||
detached_files
|
||||
.first()
|
||||
.and_then(|it| it.parent())
|
||||
.ok_or_else(|| format_err!("No detached files to load"))?,
|
||||
&Default::default(),
|
||||
)?;
|
||||
pub fn load_detached_files(
|
||||
detached_files: Vec<AbsPathBuf>,
|
||||
config: &CargoConfig,
|
||||
) -> Result<ProjectWorkspace> {
|
||||
let sysroot = match &config.sysroot {
|
||||
Some(RustcSource::Path(path)) => Some(
|
||||
Sysroot::with_sysroot_dir(path.clone())
|
||||
.with_context(|| format!("Failed to find sysroot at {}.", path.display()))?,
|
||||
),
|
||||
Some(RustcSource::Discover) => {
|
||||
let dir = &detached_files
|
||||
.first()
|
||||
.and_then(|it| it.parent())
|
||||
.ok_or_else(|| format_err!("No detached files to load"))?;
|
||||
Some(Sysroot::discover(dir, &config.extra_env).with_context(|| {
|
||||
format!("Failed to find sysroot in {}. Is rust-src installed?", dir.display())
|
||||
})?)
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
if let Some(sysroot) = &sysroot {
|
||||
tracing::info!(src_root = %sysroot.src_root().display(), root = %sysroot.root().display(), "Using sysroot");
|
||||
}
|
||||
let rustc_cfg = rustc_cfg::get(None, None, &Default::default());
|
||||
Ok(ProjectWorkspace::DetachedFiles { files: detached_files, sysroot, rustc_cfg })
|
||||
}
|
||||
|
@ -386,7 +407,7 @@ impl ProjectWorkspace {
|
|||
["libexec", "lib"]
|
||||
.into_iter()
|
||||
.map(|segment| sysroot.root().join(segment).join(&standalone_server_name))
|
||||
.find(|server_path| std::fs::metadata(&server_path).is_ok())
|
||||
.find(|server_path| std::fs::metadata(server_path).is_ok())
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
|
@ -423,6 +444,7 @@ impl ProjectWorkspace {
|
|||
cfg_overrides: _,
|
||||
build_scripts,
|
||||
toolchain: _,
|
||||
target_layout: _,
|
||||
} => {
|
||||
cargo
|
||||
.packages()
|
||||
|
@ -479,21 +501,25 @@ impl ProjectWorkspace {
|
|||
include: vec![detached_file.clone()],
|
||||
exclude: Vec::new(),
|
||||
})
|
||||
.chain(mk_sysroot(Some(sysroot)))
|
||||
.chain(mk_sysroot(sysroot.as_ref()))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn n_packages(&self) -> usize {
|
||||
match self {
|
||||
ProjectWorkspace::Json { project, .. } => project.n_crates(),
|
||||
ProjectWorkspace::Json { project, sysroot, .. } => {
|
||||
let sysroot_package_len = sysroot.as_ref().map_or(0, |it| it.crates().len());
|
||||
sysroot_package_len + project.n_crates()
|
||||
}
|
||||
ProjectWorkspace::Cargo { cargo, sysroot, rustc, .. } => {
|
||||
let rustc_package_len = rustc.as_ref().map_or(0, |it| it.packages().len());
|
||||
let sysroot_package_len = sysroot.as_ref().map_or(0, |it| it.crates().len());
|
||||
cargo.packages().len() + sysroot_package_len + rustc_package_len
|
||||
}
|
||||
ProjectWorkspace::DetachedFiles { sysroot, files, .. } => {
|
||||
sysroot.crates().len() + files.len()
|
||||
let sysroot_package_len = sysroot.as_ref().map_or(0, |it| it.crates().len());
|
||||
sysroot_package_len + files.len()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -514,6 +540,7 @@ impl ProjectWorkspace {
|
|||
project,
|
||||
sysroot,
|
||||
extra_env,
|
||||
None,
|
||||
),
|
||||
ProjectWorkspace::Cargo {
|
||||
cargo,
|
||||
|
@ -523,6 +550,7 @@ impl ProjectWorkspace {
|
|||
cfg_overrides,
|
||||
build_scripts,
|
||||
toolchain: _,
|
||||
target_layout,
|
||||
} => cargo_to_crate_graph(
|
||||
load_proc_macro,
|
||||
load,
|
||||
|
@ -532,9 +560,10 @@ impl ProjectWorkspace {
|
|||
rustc_cfg.clone(),
|
||||
cfg_overrides,
|
||||
build_scripts,
|
||||
target_layout.as_deref().map(Arc::from),
|
||||
),
|
||||
ProjectWorkspace::DetachedFiles { files, sysroot, rustc_cfg } => {
|
||||
detached_files_to_crate_graph(rustc_cfg.clone(), load, files, sysroot)
|
||||
detached_files_to_crate_graph(rustc_cfg.clone(), load, files, sysroot, None)
|
||||
}
|
||||
};
|
||||
if crate_graph.patch_cfg_if() {
|
||||
|
@ -553,11 +582,18 @@ fn project_json_to_crate_graph(
|
|||
project: &ProjectJson,
|
||||
sysroot: &Option<Sysroot>,
|
||||
extra_env: &FxHashMap<String, String>,
|
||||
target_layout: Option<Arc<str>>,
|
||||
) -> CrateGraph {
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
let sysroot_deps = sysroot
|
||||
.as_ref()
|
||||
.map(|sysroot| sysroot_to_crate_graph(&mut crate_graph, sysroot, rustc_cfg.clone(), load));
|
||||
let sysroot_deps = sysroot.as_ref().map(|sysroot| {
|
||||
sysroot_to_crate_graph(
|
||||
&mut crate_graph,
|
||||
sysroot,
|
||||
rustc_cfg.clone(),
|
||||
target_layout.clone(),
|
||||
load,
|
||||
)
|
||||
});
|
||||
|
||||
let mut cfg_cache: FxHashMap<&str, Vec<CfgFlag>> = FxHashMap::default();
|
||||
let crates: NoHashHashMap<CrateId, CrateId> = project
|
||||
|
@ -609,6 +645,7 @@ fn project_json_to_crate_graph(
|
|||
} else {
|
||||
CrateOrigin::CratesIo { repo: None, name: None }
|
||||
},
|
||||
target_layout.clone(),
|
||||
),
|
||||
)
|
||||
})
|
||||
|
@ -649,11 +686,18 @@ fn cargo_to_crate_graph(
|
|||
rustc_cfg: Vec<CfgFlag>,
|
||||
override_cfg: &CfgOverrides,
|
||||
build_scripts: &WorkspaceBuildScripts,
|
||||
target_layout: Option<Arc<str>>,
|
||||
) -> CrateGraph {
|
||||
let _p = profile::span("cargo_to_crate_graph");
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
let (public_deps, libproc_macro) = match sysroot {
|
||||
Some(sysroot) => sysroot_to_crate_graph(&mut crate_graph, sysroot, rustc_cfg.clone(), load),
|
||||
Some(sysroot) => sysroot_to_crate_graph(
|
||||
&mut crate_graph,
|
||||
sysroot,
|
||||
rustc_cfg.clone(),
|
||||
target_layout.clone(),
|
||||
load,
|
||||
),
|
||||
None => (SysrootPublicDeps::default(), None),
|
||||
};
|
||||
|
||||
|
@ -716,6 +760,7 @@ fn cargo_to_crate_graph(
|
|||
file_id,
|
||||
&cargo[tgt].name,
|
||||
cargo[tgt].is_proc_macro,
|
||||
target_layout.clone(),
|
||||
);
|
||||
if cargo[tgt].kind == TargetKind::Lib {
|
||||
lib_tgt = Some((crate_id, cargo[tgt].name.clone()));
|
||||
|
@ -795,6 +840,7 @@ fn cargo_to_crate_graph(
|
|||
&cfg_options,
|
||||
override_cfg,
|
||||
build_scripts,
|
||||
target_layout,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -805,12 +851,21 @@ fn detached_files_to_crate_graph(
|
|||
rustc_cfg: Vec<CfgFlag>,
|
||||
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
|
||||
detached_files: &[AbsPathBuf],
|
||||
sysroot: &Sysroot,
|
||||
sysroot: &Option<Sysroot>,
|
||||
target_layout: Option<Arc<str>>,
|
||||
) -> CrateGraph {
|
||||
let _p = profile::span("detached_files_to_crate_graph");
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
let (public_deps, _libproc_macro) =
|
||||
sysroot_to_crate_graph(&mut crate_graph, sysroot, rustc_cfg.clone(), load);
|
||||
let (public_deps, _libproc_macro) = match sysroot {
|
||||
Some(sysroot) => sysroot_to_crate_graph(
|
||||
&mut crate_graph,
|
||||
sysroot,
|
||||
rustc_cfg.clone(),
|
||||
target_layout.clone(),
|
||||
load,
|
||||
),
|
||||
None => (SysrootPublicDeps::default(), None),
|
||||
};
|
||||
|
||||
let mut cfg_options = CfgOptions::default();
|
||||
cfg_options.extend(rustc_cfg);
|
||||
|
@ -841,6 +896,7 @@ fn detached_files_to_crate_graph(
|
|||
repo: None,
|
||||
name: display_name.map(|n| n.canonical_name().to_string()),
|
||||
},
|
||||
target_layout.clone(),
|
||||
);
|
||||
|
||||
public_deps.add_to_crate_graph(&mut crate_graph, detached_file_crate);
|
||||
|
@ -861,6 +917,7 @@ fn handle_rustc_crates(
|
|||
cfg_options: &CfgOptions,
|
||||
override_cfg: &CfgOverrides,
|
||||
build_scripts: &WorkspaceBuildScripts,
|
||||
target_layout: Option<Arc<str>>,
|
||||
) {
|
||||
let mut rustc_pkg_crates = FxHashMap::default();
|
||||
// The root package of the rustc-dev component is rustc_driver, so we match that
|
||||
|
@ -917,6 +974,7 @@ fn handle_rustc_crates(
|
|||
file_id,
|
||||
&rustc_workspace[tgt].name,
|
||||
rustc_workspace[tgt].is_proc_macro,
|
||||
target_layout.clone(),
|
||||
);
|
||||
pkg_to_lib_crate.insert(pkg, crate_id);
|
||||
// Add dependencies on core / std / alloc for this crate
|
||||
|
@ -981,6 +1039,7 @@ fn add_target_crate_root(
|
|||
file_id: FileId,
|
||||
cargo_name: &str,
|
||||
is_proc_macro: bool,
|
||||
target_layout: Option<Arc<str>>,
|
||||
) -> CrateId {
|
||||
let edition = pkg.edition;
|
||||
let mut potential_cfg_options = cfg_options.clone();
|
||||
|
@ -1027,6 +1086,7 @@ fn add_target_crate_root(
|
|||
proc_macro,
|
||||
is_proc_macro,
|
||||
CrateOrigin::CratesIo { repo: pkg.repository.clone(), name: Some(pkg.name.clone()) },
|
||||
target_layout,
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -1048,6 +1108,7 @@ fn sysroot_to_crate_graph(
|
|||
crate_graph: &mut CrateGraph,
|
||||
sysroot: &Sysroot,
|
||||
rustc_cfg: Vec<CfgFlag>,
|
||||
target_layout: Option<Arc<str>>,
|
||||
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
|
||||
) -> (SysrootPublicDeps, Option<CrateId>) {
|
||||
let _p = profile::span("sysroot_to_crate_graph");
|
||||
|
@ -1071,6 +1132,7 @@ fn sysroot_to_crate_graph(
|
|||
Err("no proc macro loaded for sysroot crate".into()),
|
||||
false,
|
||||
CrateOrigin::Lang(LangCrateOrigin::from(&*sysroot[krate].name)),
|
||||
target_layout.clone(),
|
||||
);
|
||||
Some((krate, crate_id))
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue