From 7a8c4c001ba82e360d77d17a5815f2c6fd50e3e5 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 28 Nov 2023 16:50:05 +0100 Subject: [PATCH] Turn macro_expand from query to normal function --- crates/base-db/src/span.rs | 12 ++++-------- .../hir-def/src/macro_expansion_tests/proc_macros.rs | 1 - crates/hir-expand/src/db.rs | 5 +---- crates/hir-expand/src/files.rs | 1 + crates/hir-expand/src/fixup.rs | 4 ++-- crates/hir-expand/src/span.rs | 2 ++ crates/hir/src/db.rs | 4 ++-- crates/ide-completion/src/tests/proc_macros.rs | 2 -- crates/ide-db/src/apply_change.rs | 2 +- crates/ide-db/src/lib.rs | 7 ------- crates/proc-macro-api/src/msg/flat.rs | 8 +------- crates/proc-macro-srv/src/lib.rs | 3 +-- 12 files changed, 15 insertions(+), 36 deletions(-) diff --git a/crates/base-db/src/span.rs b/crates/base-db/src/span.rs index d2b40ecdd21..420c669641c 100644 --- a/crates/base-db/src/span.rs +++ b/crates/base-db/src/span.rs @@ -1,4 +1,4 @@ -/// File and span related types. +//! File and span related types. // FIXME: This should probably be moved into its own crate. use std::fmt; @@ -26,19 +26,15 @@ impl fmt::Display for SyntaxContextId { impl SyntaxContext for SyntaxContextId { const DUMMY: Self = Self::ROOT; - // veykril(HACK): salsa doesn't allow us fetching the id of the current input to be allocated so - // we need a special value that behaves as the current context. } // inherent trait impls please tyvm impl SyntaxContextId { - // TODO: This is very much UB, salsa exposes no way to create an InternId in a const context - // currently (which kind of makes sense but we need it here!) pub const ROOT: Self = SyntaxContextId(unsafe { InternId::new_unchecked(0) }); - // TODO: This is very much UB, salsa exposes no way to create an InternId in a const context - // currently (which kind of makes sense but we need it here!) + // veykril(HACK): salsa doesn't allow us fetching the id of the current input to be allocated so + // we need a special value that behaves as the current context. pub const SELF_REF: Self = SyntaxContextId(unsafe { InternId::new_unchecked(InternId::MAX - 1) }); - /// Used syntax fixups + // Used for syntax fixups pub const FAKE: Self = SyntaxContextId(unsafe { InternId::new_unchecked(InternId::MAX - 2) }); pub fn is_root(self) -> bool { diff --git a/crates/hir-def/src/macro_expansion_tests/proc_macros.rs b/crates/hir-def/src/macro_expansion_tests/proc_macros.rs index 548f22499b3..060b8aa8c19 100644 --- a/crates/hir-def/src/macro_expansion_tests/proc_macros.rs +++ b/crates/hir-def/src/macro_expansion_tests/proc_macros.rs @@ -74,7 +74,6 @@ fn foo() { } #[test] -#[ignore] // TODO fn attribute_macro_syntax_completion_2() { // common case of dot completion while typing check( diff --git a/crates/hir-expand/src/db.rs b/crates/hir-expand/src/db.rs index 1d55aaf1703..00a7b0cb3fe 100644 --- a/crates/hir-expand/src/db.rs +++ b/crates/hir-expand/src/db.rs @@ -146,9 +146,6 @@ pub trait ExpandDatabase: SourceDatabase { id: AstId, ) -> Arc; - /// Expand macro call to a token tree. - // This query is LRU cached - fn macro_expand(&self, macro_call: MacroCallId) -> ExpandResult>; #[salsa::invoke(crate::builtin_fn_macro::include_arg_to_tt)] fn include_expand( &self, @@ -315,7 +312,7 @@ fn parse_macro_expansion( macro_file: MacroFileId, ) -> ExpandResult<(Parse, Arc)> { let _p = profile::span("parse_macro_expansion"); - let mbe::ValueResult { value: tt, err } = db.macro_expand(macro_file.macro_call_id); + let mbe::ValueResult { value: tt, err } = macro_expand(db, macro_file.macro_call_id); let expand_to = macro_expand_to(db, macro_file.macro_call_id); diff --git a/crates/hir-expand/src/files.rs b/crates/hir-expand/src/files.rs index 57a7fa5ec39..36eb2dbb27c 100644 --- a/crates/hir-expand/src/files.rs +++ b/crates/hir-expand/src/files.rs @@ -1,3 +1,4 @@ +//! Things to wrap other things in file ids. use std::iter; use base_db::{ diff --git a/crates/hir-expand/src/fixup.rs b/crates/hir-expand/src/fixup.rs index 326ff1dec48..8fa7d576295 100644 --- a/crates/hir-expand/src/fixup.rs +++ b/crates/hir-expand/src/fixup.rs @@ -52,7 +52,7 @@ pub(crate) fn fixup_syntax(node: &SyntaxNode) -> SyntaxFixups { while let Some(event) = preorder.next() { let syntax::WalkEvent::Enter(node) = event else { continue }; - /* TODO + /* if can_handle_error(&node) && has_error_to_handle(&node) { // the node contains an error node, we have to completely replace it by something valid let (original_tree, new_tmap, new_next_id) = @@ -295,7 +295,7 @@ pub(crate) fn reverse_fixups(tt: &mut Subtree, undo_info: &SyntaxFixupUndoInfo) tt::TokenTree::Leaf(leaf) => leaf.span().ctx != SyntaxContextId::FAKE, tt::TokenTree::Subtree(st) => st.delimiter.open.ctx != SyntaxContextId::FAKE, }) - // .flat_map(|tt| match tt { TODO + // .flat_map(|tt| match tt { // tt::TokenTree::Subtree(mut tt) => { // reverse_fixups(&mut tt, undo_info); // SmallVec::from_const([tt.into()]) diff --git a/crates/hir-expand/src/span.rs b/crates/hir-expand/src/span.rs index c2399259fac..1703923d11a 100644 --- a/crates/hir-expand/src/span.rs +++ b/crates/hir-expand/src/span.rs @@ -1,3 +1,5 @@ +//! Spanmaps allow turning absolute ranges into relative ranges for incrementality purposes as well +//! as associating spans with text ranges in a particular file. use base_db::{ span::{ErasedFileAstId, SpanAnchor, SpanData, SyntaxContextId, ROOT_ERASED_FILE_AST_ID}, FileId, diff --git a/crates/hir/src/db.rs b/crates/hir/src/db.rs index b9112dee316..ff6f987aa12 100644 --- a/crates/hir/src/db.rs +++ b/crates/hir/src/db.rs @@ -23,7 +23,7 @@ pub use hir_def::db::{ }; pub use hir_expand::db::{ AstIdMapQuery, DeclMacroExpanderQuery, ExpandDatabase, ExpandDatabaseStorage, - ExpandProcMacroQuery, IncludeExpandQuery, InternMacroCallQuery, MacroArgQuery, - MacroExpandQuery, ParseMacroExpansionErrorQuery, ParseMacroExpansionQuery, RealSpanMapQuery, + ExpandProcMacroQuery, IncludeExpandQuery, InternMacroCallQuery, InternSyntaxContextQuery, + MacroArgQuery, ParseMacroExpansionErrorQuery, ParseMacroExpansionQuery, RealSpanMapQuery, }; pub use hir_ty::db::*; diff --git a/crates/ide-completion/src/tests/proc_macros.rs b/crates/ide-completion/src/tests/proc_macros.rs index e55d1f315fe..2d6234e310c 100644 --- a/crates/ide-completion/src/tests/proc_macros.rs +++ b/crates/ide-completion/src/tests/proc_macros.rs @@ -9,7 +9,6 @@ fn check(ra_fixture: &str, expect: Expect) { } #[test] -#[ignore] // todo fn complete_dot_in_attr() { check( r#" @@ -41,7 +40,6 @@ fn main() { } #[test] -#[ignore] // TODO fn complete_dot_in_attr2() { check( r#" diff --git a/crates/ide-db/src/apply_change.rs b/crates/ide-db/src/apply_change.rs index 2efe3ee1ed7..67e686dad11 100644 --- a/crates/ide-db/src/apply_change.rs +++ b/crates/ide-db/src/apply_change.rs @@ -101,8 +101,8 @@ impl RootDatabase { hir::db::ExpandProcMacroQuery hir::db::IncludeExpandQuery hir::db::InternMacroCallQuery + hir::db::InternSyntaxContextQuery hir::db::MacroArgQuery - hir::db::MacroExpandQuery hir::db::ParseMacroExpansionQuery hir::db::RealSpanMapQuery diff --git a/crates/ide-db/src/lib.rs b/crates/ide-db/src/lib.rs index 38c9a3538f3..cbd51e67810 100644 --- a/crates/ide-db/src/lib.rs +++ b/crates/ide-db/src/lib.rs @@ -157,7 +157,6 @@ impl RootDatabase { base_db::ParseQuery.in_db_mut(self).set_lru_capacity(lru_capacity); // macro expansions are usually rather small, so we can afford to keep more of them alive hir::db::ParseMacroExpansionQuery.in_db_mut(self).set_lru_capacity(4 * lru_capacity); - hir::db::MacroExpandQuery.in_db_mut(self).set_lru_capacity(4 * lru_capacity); } pub fn update_lru_capacities(&mut self, lru_capacities: &FxHashMap, usize>) { @@ -175,12 +174,6 @@ impl RootDatabase { .copied() .unwrap_or(4 * base_db::DEFAULT_PARSE_LRU_CAP), ); - hir_db::MacroExpandQuery.in_db_mut(self).set_lru_capacity( - lru_capacities - .get(stringify!(MacroExpandQuery)) - .copied() - .unwrap_or(4 * base_db::DEFAULT_PARSE_LRU_CAP), - ); macro_rules! update_lru_capacity_per_query { ($( $module:ident :: $query:ident )*) => {$( diff --git a/crates/proc-macro-api/src/msg/flat.rs b/crates/proc-macro-api/src/msg/flat.rs index baf8bbad4bf..43840fa333d 100644 --- a/crates/proc-macro-api/src/msg/flat.rs +++ b/crates/proc-macro-api/src/msg/flat.rs @@ -43,7 +43,7 @@ use serde::{Deserialize, Serialize}; use crate::msg::ENCODE_CLOSE_SPAN_VERSION; -pub type SpanDataIndexMap = IndexSet; +type SpanDataIndexMap = IndexSet; #[derive(Clone, Copy, PartialEq, Eq, Hash)] pub struct TokenId(pub u32); @@ -54,12 +54,6 @@ impl std::fmt::Debug for TokenId { } } -impl TokenId { - pub const DEF_SITE: Self = TokenId(0); - pub const CALL_SITE: Self = TokenId(0); - pub const MIXED_SITE: Self = TokenId(0); -} - impl tt::Span for TokenId { const DUMMY: Self = TokenId(!0); diff --git a/crates/proc-macro-srv/src/lib.rs b/crates/proc-macro-srv/src/lib.rs index 32a07a84777..dd327681c4c 100644 --- a/crates/proc-macro-srv/src/lib.rs +++ b/crates/proc-macro-srv/src/lib.rs @@ -31,9 +31,8 @@ use std::{ time::SystemTime, }; -use ::tt::Span; use proc_macro_api::{ - msg::{self, ExpnGlobals, TokenId, CURRENT_API_VERSION, HAS_GLOBAL_SPANS}, + msg::{self, ExpnGlobals, TokenId, CURRENT_API_VERSION}, ProcMacroKind, };