Skip to content

Commit

Permalink
(revert c2258d8) Use cargo env instead of crate_workspace_data for pr…
Browse files Browse the repository at this point in the history
…ocmacro expansion
  • Loading branch information
infiniteregrets committed Feb 15, 2025
1 parent c661e5e commit c1a9c43
Show file tree
Hide file tree
Showing 20 changed files with 742 additions and 693 deletions.
35 changes: 24 additions & 11 deletions crates/base-db/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ pub struct CrateData {
pub dependencies: Vec<Dependency>,
pub origin: CrateOrigin,
pub is_proc_macro: bool,
pub cwd: Option<AbsPathBuf>,
}

#[derive(Default, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -360,8 +361,9 @@ impl CrateGraph {
cfg_options: Arc<CfgOptions>,
potential_cfg_options: Option<Arc<CfgOptions>>,
mut env: Env,
is_proc_macro: bool,
origin: CrateOrigin,
is_proc_macro: bool,
cwd: Option<AbsPathBuf>,
) -> CrateId {
env.entries.shrink_to_fit();
let data = CrateData {
Expand All @@ -375,6 +377,7 @@ impl CrateGraph {
dependencies: Vec::new(),
origin,
is_proc_macro,
cwd,
};
self.arena.alloc(data)
}
Expand Down Expand Up @@ -698,8 +701,9 @@ mod tests {
Default::default(),
Default::default(),
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
let crate2 = graph.add_crate_root(
FileId::from_raw(2u32),
Expand All @@ -709,8 +713,9 @@ mod tests {
Default::default(),
Default::default(),
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
let crate3 = graph.add_crate_root(
FileId::from_raw(3u32),
Expand All @@ -720,8 +725,9 @@ mod tests {
Default::default(),
Default::default(),
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
assert!(graph
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2,))
Expand All @@ -745,8 +751,9 @@ mod tests {
Default::default(),
Default::default(),
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
let crate2 = graph.add_crate_root(
FileId::from_raw(2u32),
Expand All @@ -756,8 +763,9 @@ mod tests {
Default::default(),
Default::default(),
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
assert!(graph
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2,))
Expand All @@ -778,8 +786,9 @@ mod tests {
Default::default(),
Default::default(),
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
let crate2 = graph.add_crate_root(
FileId::from_raw(2u32),
Expand All @@ -789,8 +798,9 @@ mod tests {
Default::default(),
Default::default(),
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
let crate3 = graph.add_crate_root(
FileId::from_raw(3u32),
Expand All @@ -800,8 +810,9 @@ mod tests {
Default::default(),
Default::default(),
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
assert!(graph
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2,))
Expand All @@ -822,8 +833,9 @@ mod tests {
Default::default(),
Default::default(),
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
let crate2 = graph.add_crate_root(
FileId::from_raw(2u32),
Expand All @@ -833,8 +845,9 @@ mod tests {
Default::default(),
Default::default(),
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
assert!(graph
.add_dep(
Expand Down
4 changes: 1 addition & 3 deletions crates/base-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_hash::FxHashMap;
use span::EditionedFileId;
use syntax::{ast, Parse, SourceFile, SyntaxError};
use triomphe::Arc;
use vfs::{AbsPathBuf, FileId};
use vfs::FileId;

pub use crate::{
change::FileChange,
Expand Down Expand Up @@ -85,8 +85,6 @@ pub trait SourceDatabase: FileLoader + std::fmt::Debug {
/// Crate related data shared by the whole workspace.
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct CrateWorkspaceData {
/// The working directory to run proc-macros in. This is usually the workspace root of cargo workspaces.
pub proc_macro_cwd: Option<AbsPathBuf>,
// FIXME: Consider removing this, making HirDatabase::target_data_layout an input query
pub data_layout: TargetLayoutLoadResult,
/// Toolchain version used to compile the crate.
Expand Down
7 changes: 3 additions & 4 deletions crates/hir-expand/src/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,16 @@ impl CustomProcMacroExpander {
let krate_graph = db.crate_graph();
// Proc macros have access to the environment variables of the invoking crate.
let env = &krate_graph[calling_crate].env;
let current_dir = krate_graph[calling_crate].cwd.as_ref().map(|it| it.to_string());

match proc_macro.expander.expand(
tt,
attr_arg,
env,
def_site,
call_site,
mixed_site,
db.crate_workspace_data()[&calling_crate]
.proc_macro_cwd
.as_ref()
.map(ToString::to_string),
current_dir,
) {
Ok(t) => ExpandResult::ok(t),
Err(err) => match err {
Expand Down
4 changes: 2 additions & 2 deletions crates/ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,14 @@ impl Analysis {
Arc::new(cfg_options),
None,
Env::default(),
false,
CrateOrigin::Local { repo: None, name: None },
false,
None,
);
change.change_file(file_id, Some(text));
let ws_data = crate_graph
.iter()
.zip(iter::repeat(Arc::new(CrateWorkspaceData {
proc_macro_cwd: None,
data_layout: Err("fixture has no layout".into()),
toolchain: None,
})))
Expand Down
2 changes: 2 additions & 0 deletions crates/ide/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub(crate) fn status(db: &RootDatabase, file_id: Option<FileId>) -> String {
dependencies,
origin,
is_proc_macro,
cwd,
} = &crate_graph[crate_id];
format_to!(
buf,
Expand All @@ -85,6 +86,7 @@ pub(crate) fn status(db: &RootDatabase, file_id: Option<FileId>) -> String {
format_to!(buf, " Env: {:?}\n", env);
format_to!(buf, " Origin: {:?}\n", origin);
format_to!(buf, " Is a proc macro crate: {}\n", is_proc_macro);
format_to!(buf, " Cwd: {:?}\n", cwd);
let deps = dependencies
.iter()
.map(|dep| format!("{}={}", dep.name, dep.crate_id.into_raw()))
Expand Down
1 change: 0 additions & 1 deletion crates/load-cargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,6 @@ fn load_crate_graph(
let ws_data = crate_graph
.iter()
.zip(iter::repeat(From::from(CrateWorkspaceData {
proc_macro_cwd: None,
data_layout: target_layout.clone(),
toolchain: toolchain.clone(),
})))
Expand Down
4 changes: 4 additions & 0 deletions crates/project-model/src/project_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ impl ProjectJson {
is_proc_macro: crate_data.is_proc_macro,
repository: crate_data.repository,
build,
cwd: crate_data.cwd.map(absolutize_on_base),
}
})
.collect(),
Expand Down Expand Up @@ -240,6 +241,7 @@ pub struct Crate {
pub(crate) include: Vec<AbsPathBuf>,
pub(crate) exclude: Vec<AbsPathBuf>,
pub(crate) is_proc_macro: bool,
pub(crate) cwd: Option<AbsPathBuf>,
pub(crate) repository: Option<String>,
pub build: Option<Build>,
}
Expand Down Expand Up @@ -362,6 +364,8 @@ struct CrateData {
repository: Option<String>,
#[serde(default)]
build: Option<BuildData>,
#[serde(default)]
cwd: Option<Utf8PathBuf>,
}

mod cfg_ {
Expand Down
5 changes: 3 additions & 2 deletions crates/project-model/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ fn load_workspace_from_metadata(file: &str) -> ProjectWorkspace {
build_scripts: WorkspaceBuildScripts::default(),
rustc: Err(None),
error: None,
set_test: true,
},
cfg_overrides: Default::default(),
sysroot: Sysroot::empty(),
rustc_cfg: Vec::new(),
toolchain: None,
target_layout: Err("target_data_layout not loaded".into()),
extra_includes: Vec::new(),
set_test: true,
}
}

Expand All @@ -65,6 +65,7 @@ fn load_rust_project(file: &str) -> (CrateGraph, ProcMacroPaths) {
target_layout: Err(Arc::from("test has no data layout")),
cfg_overrides: Default::default(),
extra_includes: Vec::new(),
set_test: true,
};
to_crate_graph(project_workspace, &mut Default::default())
}
Expand Down Expand Up @@ -285,14 +286,14 @@ fn smoke_test_real_sysroot_cargo() {
build_scripts: WorkspaceBuildScripts::default(),
rustc: Err(None),
error: None,
set_test: true,
},
sysroot,
rustc_cfg: Vec::new(),
cfg_overrides: Default::default(),
toolchain: None,
target_layout: Err("target_data_layout not loaded".into()),
extra_includes: Vec::new(),
set_test: true,
};
project_workspace.to_crate_graph(
&mut {
Expand Down
Loading

0 comments on commit c1a9c43

Please sign in to comment.