Skip to content

Commit

Permalink
fix: Don't attempt cfg-if patch if querying sysroot metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Mar 22, 2024
1 parent fc0d51a commit c2359ca
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 72 deletions.
7 changes: 0 additions & 7 deletions crates/base-db/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,13 +500,6 @@ impl CrateGraph {
}
}

// FIXME: this only finds one crate with the given root; we could have multiple
pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> {
let (crate_id, _) =
self.arena.iter().find(|(_crate_id, data)| data.root_file_id == file_id)?;
Some(crate_id)
}

pub fn sort_deps(&mut self) {
self.arena
.iter_mut()
Expand Down
7 changes: 4 additions & 3 deletions crates/hir-def/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ impl ProcMacroData {
(
def.name,
match def.kind {
ProcMacroKind::CustomDerive { helpers } => Some(helpers),
ProcMacroKind::Derive { helpers } => Some(helpers),
ProcMacroKind::FnLike | ProcMacroKind::Attr => None,
},
)
Expand Down Expand Up @@ -484,10 +484,11 @@ impl ExternCrateDeclData {
let extern_crate = &item_tree[loc.id.value];

let name = extern_crate.name.clone();
let krate = loc.container.krate();
let crate_id = if name == hir_expand::name![self] {
Some(loc.container.krate())
Some(krate)
} else {
db.crate_def_map(loc.container.krate())
db.crate_def_map(krate)
.extern_prelude()
.find(|&(prelude_name, ..)| *prelude_name == name)
.map(|(_, (root, _))| root.krate())
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-def/src/nameres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,8 @@ impl MacroSubNs {
MacroId::MacroRulesId(it) => it.lookup(db).expander,
MacroId::ProcMacroId(it) => {
return match it.lookup(db).kind {
ProcMacroKind::CustomDerive | ProcMacroKind::Attr => Self::Attr,
ProcMacroKind::FuncLike => Self::Bang,
ProcMacroKind::Derive | ProcMacroKind::Attr => Self::Attr,
ProcMacroKind::FnLike => Self::Bang,
};
}
};
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ impl DefCollector<'_> {
.intern(self.db);
self.define_proc_macro(def.name.clone(), proc_macro_id);
let crate_data = Arc::get_mut(&mut self.def_map.data).unwrap();
if let ProcMacroKind::CustomDerive { helpers } = def.kind {
if let ProcMacroKind::Derive { helpers } = def.kind {
crate_data.exported_derives.insert(self.db.macro_def(proc_macro_id.into()), helpers);
}
crate_data.fn_proc_macro_mapping.insert(fn_id, proc_macro_id);
Expand Down
10 changes: 4 additions & 6 deletions crates/hir-def/src/nameres/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@ pub struct ProcMacroDef {

#[derive(Debug, PartialEq, Eq)]
pub enum ProcMacroKind {
CustomDerive { helpers: Box<[Name]> },
Derive { helpers: Box<[Name]> },
FnLike,
Attr,
}

impl ProcMacroKind {
pub(super) fn to_basedb_kind(&self) -> hir_expand::proc_macro::ProcMacroKind {
match self {
ProcMacroKind::CustomDerive { .. } => {
hir_expand::proc_macro::ProcMacroKind::CustomDerive
}
ProcMacroKind::FnLike => hir_expand::proc_macro::ProcMacroKind::FuncLike,
ProcMacroKind::Derive { .. } => hir_expand::proc_macro::ProcMacroKind::Derive,
ProcMacroKind::FnLike => hir_expand::proc_macro::ProcMacroKind::FnLike,
ProcMacroKind::Attr => hir_expand::proc_macro::ProcMacroKind::Attr,
}
}
Expand All @@ -40,7 +38,7 @@ impl Attrs {
} else if self.by_key("proc_macro_derive").exists() {
let derive = self.by_key("proc_macro_derive").tt_values().next()?;
let def = parse_macro_name_and_helper_attrs(&derive.token_trees)
.map(|(name, helpers)| ProcMacroDef { name, kind: ProcMacroKind::CustomDerive { helpers } });
.map(|(name, helpers)| ProcMacroDef { name, kind: ProcMacroKind::Derive { helpers } });

if def.is_none() {
tracing::trace!("malformed `#[proc_macro_derive]`: {}", derive);
Expand Down
5 changes: 3 additions & 2 deletions crates/hir-expand/src/cfg_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ pub(crate) fn process_cfg_attrs(
) -> Option<FxHashSet<SyntaxElement>> {
// FIXME: #[cfg_eval] is not implemented. But it is not stable yet
let is_derive = match loc.def.kind {
MacroDefKind::BuiltInDerive(..)
| MacroDefKind::ProcMacro(_, ProcMacroKind::CustomDerive, _) => true,
MacroDefKind::BuiltInDerive(..) | MacroDefKind::ProcMacro(_, ProcMacroKind::Derive, _) => {
true
}
MacroDefKind::BuiltInAttr(expander, _) => expander.is_derive(),
_ => false,
};
Expand Down
11 changes: 5 additions & 6 deletions crates/hir-expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ pub struct EagerCallInfo {
/// Call id of the eager macro's input file (this is the macro file for its fully expanded input).
arg_id: MacroCallId,
error: Option<ExpandError>,
/// TODO: Doc
/// The call site span of the eager macro
span: Span,
}

Expand All @@ -211,7 +211,7 @@ pub enum MacroCallKind {
expand_to: ExpandTo,
/// Some if this is a macro call for an eager macro. Note that this is `None`
/// for the eager input macro file.
// FIXME: This is being interned, subtrees can vary quickly differ just slightly causing
// FIXME: This is being interned, subtrees can vary quickly differing just slightly causing
// leakage problems here
eager: Option<Arc<EagerCallInfo>>,
},
Expand Down Expand Up @@ -379,7 +379,7 @@ impl MacroFileIdExt for MacroFileId {
fn is_custom_derive(&self, db: &dyn ExpandDatabase) -> bool {
matches!(
db.lookup_intern_macro_call(self.macro_call_id).def.kind,
MacroDefKind::ProcMacro(_, ProcMacroKind::CustomDerive, _)
MacroDefKind::ProcMacro(_, ProcMacroKind::Derive, _)
)
}

Expand Down Expand Up @@ -477,16 +477,15 @@ impl MacroDefId {
pub fn is_derive(&self) -> bool {
matches!(
self.kind,
MacroDefKind::BuiltInDerive(..)
| MacroDefKind::ProcMacro(_, ProcMacroKind::CustomDerive, _)
MacroDefKind::BuiltInDerive(..) | MacroDefKind::ProcMacro(_, ProcMacroKind::Derive, _)
)
}

pub fn is_fn_like(&self) -> bool {
matches!(
self.kind,
MacroDefKind::BuiltIn(..)
| MacroDefKind::ProcMacro(_, ProcMacroKind::FuncLike, _)
| MacroDefKind::ProcMacro(_, ProcMacroKind::FnLike, _)
| MacroDefKind::BuiltInEager(..)
| MacroDefKind::Declarative(..)
)
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-expand/src/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ impl ProcMacroId {

#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub enum ProcMacroKind {
CustomDerive,
FuncLike,
Derive,
FnLike,
Attr,
}

Expand Down
4 changes: 2 additions & 2 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2591,8 +2591,8 @@ impl Macro {
MacroExpander::BuiltInDerive(_) => MacroKind::Derive,
},
MacroId::ProcMacroId(it) => match it.lookup(db.upcast()).kind {
ProcMacroKind::CustomDerive => MacroKind::Derive,
ProcMacroKind::FuncLike => MacroKind::ProcMacro,
ProcMacroKind::Derive => MacroKind::Derive,
ProcMacroKind::FnLike => MacroKind::ProcMacro,
ProcMacroKind::Attr => MacroKind::Attr,
},
}
Expand Down
4 changes: 2 additions & 2 deletions crates/load-cargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ fn expander_to_proc_macro(
) -> ProcMacro {
let name = From::from(expander.name());
let kind = match expander.kind() {
proc_macro_api::ProcMacroKind::CustomDerive => ProcMacroKind::CustomDerive,
proc_macro_api::ProcMacroKind::FuncLike => ProcMacroKind::FuncLike,
proc_macro_api::ProcMacroKind::Derive => ProcMacroKind::Derive,
proc_macro_api::ProcMacroKind::FnLike => ProcMacroKind::FnLike,
proc_macro_api::ProcMacroKind::Attr => ProcMacroKind::Attr,
};
let disabled = ignored_macros.iter().any(|replace| **replace == name);
Expand Down
4 changes: 2 additions & 2 deletions crates/proc-macro-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ pub use version::{read_dylib_info, read_version, RustCInfo};

#[derive(Copy, Clone, Eq, PartialEq, Debug, Serialize, Deserialize)]
pub enum ProcMacroKind {
CustomDerive,
FuncLike,
Derive,
FnLike,
Attr,
}

Expand Down
4 changes: 2 additions & 2 deletions crates/proc-macro-srv/src/proc_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ impl ProcMacros {
.iter()
.map(|proc_macro| match proc_macro {
bridge::client::ProcMacro::CustomDerive { trait_name, .. } => {
(trait_name.to_string(), ProcMacroKind::CustomDerive)
(trait_name.to_string(), ProcMacroKind::Derive)
}
bridge::client::ProcMacro::Bang { name, .. } => {
(name.to_string(), ProcMacroKind::FuncLike)
(name.to_string(), ProcMacroKind::FnLike)
}
bridge::client::ProcMacro::Attr { name, .. } => {
(name.to_string(), ProcMacroKind::Attr)
Expand Down
22 changes: 11 additions & 11 deletions crates/proc-macro-srv/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,19 @@ fn list_test_macros() {
let res = list().join("\n");

expect![[r#"
fn_like_noop [FuncLike]
fn_like_panic [FuncLike]
fn_like_error [FuncLike]
fn_like_clone_tokens [FuncLike]
fn_like_mk_literals [FuncLike]
fn_like_mk_idents [FuncLike]
fn_like_span_join [FuncLike]
fn_like_span_ops [FuncLike]
fn_like_noop [FnLike]
fn_like_panic [FnLike]
fn_like_error [FnLike]
fn_like_clone_tokens [FnLike]
fn_like_mk_literals [FnLike]
fn_like_mk_idents [FnLike]
fn_like_span_join [FnLike]
fn_like_span_ops [FnLike]
attr_noop [Attr]
attr_panic [Attr]
attr_error [Attr]
DeriveEmpty [CustomDerive]
DerivePanic [CustomDerive]
DeriveError [CustomDerive]"#]]
DeriveEmpty [Derive]
DerivePanic [Derive]
DeriveError [Derive]"#]]
.assert_eq(&res);
}
53 changes: 34 additions & 19 deletions crates/project-model/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,19 +719,22 @@ impl ProjectWorkspace {
) -> (CrateGraph, ProcMacroPaths) {
let _p = tracing::span!(tracing::Level::INFO, "ProjectWorkspace::to_crate_graph").entered();

let (mut crate_graph, proc_macros) = match self {
let ((mut crate_graph, proc_macros), sysroot) = match self {
ProjectWorkspace::Json {
project,
sysroot,
rustc_cfg,
toolchain: _,
target_layout: _,
} => project_json_to_crate_graph(
rustc_cfg.clone(),
load,
project,
sysroot.as_ref().ok(),
extra_env,
} => (
project_json_to_crate_graph(
rustc_cfg.clone(),
load,
project,
sysroot.as_ref().ok(),
extra_env,
),
sysroot,
),
ProjectWorkspace::Cargo {
cargo,
Expand All @@ -743,26 +746,38 @@ impl ProjectWorkspace {
toolchain: _,
target_layout: _,
cargo_config_extra_env: _,
} => cargo_to_crate_graph(
load,
rustc.as_ref().map(|a| a.as_ref()).ok(),
cargo,
sysroot.as_ref().ok(),
rustc_cfg.clone(),
cfg_overrides,
build_scripts,
} => (
cargo_to_crate_graph(
load,
rustc.as_ref().map(|a| a.as_ref()).ok(),
cargo,
sysroot.as_ref().ok(),
rustc_cfg.clone(),
cfg_overrides,
build_scripts,
),
sysroot,
),
ProjectWorkspace::DetachedFiles {
files,
sysroot,
rustc_cfg,
toolchain: _,
target_layout: _,
} => {
detached_files_to_crate_graph(rustc_cfg.clone(), load, files, sysroot.as_ref().ok())
}
} => (
detached_files_to_crate_graph(
rustc_cfg.clone(),
load,
files,
sysroot.as_ref().ok(),
),
sysroot,
),
};
if crate_graph.patch_cfg_if() {

if matches!(sysroot.as_ref().map(|it| it.mode()), Ok(SysrootMode::Workspace(_)))
&& crate_graph.patch_cfg_if()
{
tracing::debug!("Patched std to depend on cfg-if")
} else {
tracing::debug!("Did not patch std to depend on cfg-if")
Expand Down
3 changes: 1 addition & 2 deletions crates/rust-analyzer/src/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use itertools::Itertools;
use load_cargo::{load_proc_macro, ProjectFolders};
use proc_macro_api::ProcMacroServer;
use project_model::{ProjectWorkspace, WorkspaceBuildScripts};
use rustc_hash::FxHashSet;
use stdx::{format_to, thread::ThreadIntent};
use triomphe::Arc;
use vfs::{AbsPath, AbsPathBuf, ChangeKind};
Expand Down Expand Up @@ -526,7 +525,7 @@ impl GlobalState {
fn recreate_crate_graph(&mut self, cause: String) {
// crate graph construction relies on these paths, record them so when one of them gets
// deleted or created we trigger a reconstruction of the crate graph
let mut crate_graph_file_dependencies = FxHashSet::default();
let mut crate_graph_file_dependencies = mem::take(&mut self.crate_graph_file_dependencies);
self.report_progress(
"Building CrateGraph",
crate::lsp::utils::Progress::Begin,
Expand Down
6 changes: 3 additions & 3 deletions crates/test-fixture/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ pub fn derive_identity(item: TokenStream) -> TokenStream {
.into(),
ProcMacro {
name: "DeriveIdentity".into(),
kind: ProcMacroKind::CustomDerive,
kind: ProcMacroKind::Derive,
expander: sync::Arc::new(IdentityProcMacroExpander),
disabled: false,
},
Expand Down Expand Up @@ -402,7 +402,7 @@ pub fn mirror(input: TokenStream) -> TokenStream {
.into(),
ProcMacro {
name: "mirror".into(),
kind: ProcMacroKind::FuncLike,
kind: ProcMacroKind::FnLike,
expander: sync::Arc::new(MirrorProcMacroExpander),
disabled: false,
},
Expand All @@ -417,7 +417,7 @@ pub fn shorten(input: TokenStream) -> TokenStream {
.into(),
ProcMacro {
name: "shorten".into(),
kind: ProcMacroKind::FuncLike,
kind: ProcMacroKind::FnLike,
expander: sync::Arc::new(ShortenProcMacroExpander),
disabled: false,
},
Expand Down

0 comments on commit c2359ca

Please sign in to comment.