Skip to content
Open
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
4 changes: 2 additions & 2 deletions crates/hir-def/src/attrs/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ struct DocExprSourceCtx<'db> {
resolver: Resolver<'db>,
file_id: HirFileId,
ast_id_map: &'db AstIdMap,
span_map: SpanMap,
span_map: SpanMap<'db>,
}

fn expand_doc_expr_via_macro_pipeline<'db>(
Expand Down Expand Up @@ -390,7 +390,7 @@ fn expand_doc_macro_call<'db>(
.value?;

expander.recursion_depth += 1;
let parse = expander.db.parse_macro_expansion(call_id).value.0;
let parse = expander.db.parse_macro_expansion(call_id).value.0.clone();
let expr = parse.cast::<ast::Expr>().map(|parse| parse.tree())?;
expander.recursion_depth -= 1;

Expand Down
24 changes: 11 additions & 13 deletions crates/hir-def/src/expr_store/expander.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ use std::mem;
use base_db::Crate;
use cfg::CfgOptions;
use drop_bomb::DropBomb;
use hir_expand::AstId;
use hir_expand::span_map::SpanMapRef;
use hir_expand::{
ExpandError, ExpandErrorKind, ExpandResult, HirFileId, InFile, Lookup, MacroCallId,
AstId, ExpandError, ExpandErrorKind, ExpandResult, HirFileId, InFile, Lookup, MacroCallId,
eager::EagerCallBackFn, mod_path::ModPath, span_map::SpanMap,
};
use span::{AstIdMap, SyntaxContext};
Expand All @@ -23,7 +21,7 @@ use crate::{

#[derive(Debug)]
pub(super) struct Expander<'db> {
span_map: SpanMap,
span_map: SpanMap<'db>,
current_file_id: HirFileId,
ast_id_map: &'db AstIdMap,
/// `recursion_depth == usize::MAX` indicates that the recursion limit has been reached.
Expand Down Expand Up @@ -58,11 +56,11 @@ impl<'db> Expander<'db> {
}

pub(super) fn hygiene_for_range(&self, db: &dyn DefDatabase, range: TextRange) -> HygieneId {
match self.span_map.as_ref() {
hir_expand::span_map::SpanMapRef::ExpansionSpanMap(span_map) => {
match self.span_map {
SpanMap::ExpansionSpanMap(span_map) => {
HygieneId::new(span_map.span_at(range.start()).ctx.opaque_and_semiopaque(db))
}
hir_expand::span_map::SpanMapRef::RealSpanMap(_) => HygieneId::ROOT,
SpanMap::RealSpanMap(_) => HygieneId::ROOT,
}
}

Expand Down Expand Up @@ -193,15 +191,15 @@ impl<'db> Expander<'db> {

let res = db.parse_macro_expansion(call_id);

let err = err.or(res.err);
let err = err.or_else(|| res.err.clone());
ExpandResult {
value: {
let parse = res.value.0.cast::<T>();
let parse = res.value.0.clone().cast::<T>();

self.recursion_depth += 1;
let old_file_id = std::mem::replace(&mut self.current_file_id, call_id.into());
let old_span_map =
std::mem::replace(&mut self.span_map, db.span_map(self.current_file_id));
std::mem::replace(&mut self.span_map, SpanMap::ExpansionSpanMap(&res.value.1));
let prev_ast_id_map =
mem::replace(&mut self.ast_id_map, db.ast_id_map(self.current_file_id));
let mark = Mark {
Expand All @@ -222,15 +220,15 @@ impl<'db> Expander<'db> {
}

#[inline]
pub(super) fn span_map(&self) -> SpanMapRef<'_> {
self.span_map.as_ref()
pub(super) fn span_map(&self) -> SpanMap<'_> {
self.span_map
}
}

#[derive(Debug)]
pub(super) struct Mark<'db> {
file_id: HirFileId,
span_map: SpanMap,
span_map: SpanMap<'db>,
ast_id_map: &'db AstIdMap,
bomb: DropBomb,
}
4 changes: 2 additions & 2 deletions crates/hir-def/src/expr_store/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use hir_expand::{
HirFileId, InFile, MacroDefId,
mod_path::ModPath,
name::{AsName, Name},
span_map::SpanMapRef,
span_map::SpanMap,
};
use intern::{Symbol, sym};
use rustc_hash::FxHashMap;
Expand Down Expand Up @@ -586,7 +586,7 @@ impl<'db> ExprCollector<'db> {
}

#[inline]
pub(crate) fn span_map(&self) -> SpanMapRef<'_> {
pub(crate) fn span_map(&self) -> SpanMap<'_> {
self.expander.span_map()
}

Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/find_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ mod tests {
let ast_path =
parsed_path_file.syntax_node().descendants().find_map(syntax::ast::Path::cast).unwrap();
let mod_path = ModPath::from_src(&db, ast_path, &mut |range| {
db.span_map(pos.file_id.into()).as_ref().span_for_range(range).ctx
db.span_map(pos.file_id.into()).span_for_range(range).ctx
})
.unwrap();

Expand Down
29 changes: 11 additions & 18 deletions crates/hir-def/src/item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ use std::{
fmt::{self, Debug},
hash::Hash,
ops::Index,
sync::OnceLock,
};

use ast::{AstNode, StructKind};
Expand All @@ -61,7 +60,6 @@ use span::{
use stdx::never;
use syntax::{SourceFile, SyntaxKind, ast, match_ast};
use thin_vec::ThinVec;
use triomphe::Arc;
use tt::TextRange;

use crate::{BlockId, Lookup, attrs::parse_extra_crate_attrs, db::DefDatabase};
Expand Down Expand Up @@ -121,14 +119,13 @@ fn lower_extra_crate_attrs<'a>(
AttrsOrCfg::lower(db, &crate_attrs_as_src, cfg_options, span_map)
}

#[salsa_macros::tracked(returns(deref))]
#[salsa_macros::tracked(returns(ref))]
pub(crate) fn file_item_tree_query(
db: &dyn DefDatabase,
file_id: HirFileId,
krate: Crate,
) -> Arc<ItemTree> {
) -> ItemTree {
let _p = tracing::info_span!("file_item_tree_query", ?file_id).entered();
static EMPTY: OnceLock<Arc<ItemTree>> = OnceLock::new();

let ctx = lower::Ctx::new(db, file_id, krate);
let syntax = db.parse_or_expand(file_id);
Expand Down Expand Up @@ -174,21 +171,17 @@ pub(crate) fn file_item_tree_query(
&& top_attrs.is_empty()
&& vis.arena.is_empty()
{
EMPTY
.get_or_init(|| {
Arc::new(ItemTree {
top_level: Box::new([]),
attrs: FxHashMap::default(),
small_data: FxHashMap::default(),
big_data: FxHashMap::default(),
top_attrs: AttrsOrCfg::empty(),
vis: ItemVisibilities { arena: ThinVec::new() },
})
})
.clone()
ItemTree {
top_level: Box::new([]),
attrs: FxHashMap::default(),
small_data: FxHashMap::default(),
big_data: FxHashMap::default(),
top_attrs: AttrsOrCfg::empty(),
vis: ItemVisibilities { arena: ThinVec::new() },
}
} else {
item_tree.shrink_to_fit();
Arc::new(item_tree)
item_tree
}
}

Expand Down
13 changes: 4 additions & 9 deletions crates/hir-def/src/item_tree/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ use std::cell::OnceCell;

use base_db::{Crate, FxIndexSet};
use cfg::CfgOptions;
use hir_expand::{
HirFileId,
mod_path::PathKind,
name::AsName,
span_map::{SpanMap, SpanMapRef},
};
use hir_expand::{HirFileId, mod_path::PathKind, name::AsName, span_map::SpanMap};
use la_arena::Arena;
use span::{AstIdMap, FileAstId, SyntaxContext};
use syntax::{
Expand All @@ -32,7 +27,7 @@ pub(super) struct Ctx<'db> {
pub(super) db: &'db dyn DefDatabase,
tree: ItemTree,
source_ast_id_map: &'db AstIdMap,
span_map: OnceCell<SpanMap>,
span_map: OnceCell<SpanMap<'db>>,
file: HirFileId,
cfg_options: OnceCell<&'db CfgOptions>,
krate: Crate,
Expand Down Expand Up @@ -60,8 +55,8 @@ impl<'db> Ctx<'db> {
self.cfg_options.get_or_init(|| self.krate.cfg_options(self.db))
}

pub(super) fn span_map(&self) -> SpanMapRef<'_> {
self.span_map.get_or_init(|| self.db.span_map(self.file)).as_ref()
pub(super) fn span_map(&self) -> SpanMap<'_> {
*self.span_map.get_or_init(|| self.db.span_map(self.file))
}

pub(super) fn lower_module_items(mut self, item_owner: &dyn HasModuleItem) -> ItemTree {
Expand Down
16 changes: 8 additions & 8 deletions crates/hir-def/src/macro_expansion_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use hir_expand::{
builtin::quote::quote,
db::ExpandDatabase,
proc_macro::{ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroKind},
span_map::SpanMapRef,
span_map::SpanMap,
};
use intern::{Symbol, sym};
use itertools::Itertools;
Expand Down Expand Up @@ -142,10 +142,10 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
}

let mut expn_text = String::new();
if let Some(err) = exp.err {
if let Some(err) = &exp.err {
format_to!(expn_text, "/* error: {} */", err.render_to_string(&db).message);
}
let (parse, token_map) = exp.value;
let (parse, token_map) = &exp.value;
if expect_errors {
assert!(!parse.errors().is_empty(), "no parse errors in expansion");
for e in parse.errors() {
Expand All @@ -161,7 +161,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
}
let pp = pretty_print_macro_expansion(
parse.syntax_node(),
SpanMapRef::ExpansionSpanMap(&token_map),
SpanMap::ExpansionSpanMap(token_map),
show_spans,
show_ctxt,
);
Expand Down Expand Up @@ -215,7 +215,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
}
let pp = pretty_print_macro_expansion(
src.value,
db.span_map(src.file_id).as_ref(),
db.span_map(src.file_id),
show_spans,
show_ctxt,
);
Expand All @@ -230,7 +230,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
if let Some(macro_file) = src.file_id.macro_file() {
let pp = pretty_print_macro_expansion(
src.value.syntax().clone(),
db.span_map(macro_file.into()).as_ref(),
db.span_map(macro_file.into()),
false,
false,
);
Expand All @@ -245,7 +245,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
{
let pp = pretty_print_macro_expansion(
src.value.syntax().clone(),
db.span_map(macro_file.into()).as_ref(),
db.span_map(macro_file.into()),
false,
false,
);
Expand Down Expand Up @@ -309,7 +309,7 @@ fn reindent(indent: IndentLevel, pp: String) -> String {

fn pretty_print_macro_expansion(
expn: SyntaxNode,
map: SpanMapRef<'_>,
map: SpanMap<'_>,
show_spans: bool,
show_ctxt: bool,
) -> String {
Expand Down
29 changes: 14 additions & 15 deletions crates/hir-def/src/nameres/assoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ struct AssocItemCollector<'db> {
def_map: &'db DefMap,
local_def_map: &'db LocalDefMap,
ast_id_map: &'db AstIdMap,
span_map: SpanMap,
span_map: SpanMap<'db>,
cfg_options: &'db CfgOptions,
file_id: HirFileId,
diagnostics: Vec<DefDiagnostic>,
Expand Down Expand Up @@ -191,19 +191,18 @@ impl<'db> AssocItemCollector<'db> {

fn collect_item(&mut self, item: ast::AssocItem) {
let ast_id = self.ast_id_map.ast_id(&item);
let attrs =
match AttrsOrCfg::lower(self.db, &item, &|| self.cfg_options, self.span_map.as_ref()) {
AttrsOrCfg::Enabled { attrs } => attrs,
AttrsOrCfg::CfgDisabled(cfg) => {
self.diagnostics.push(DefDiagnostic::unconfigured_code(
self.module_id,
InFile::new(self.file_id, ast_id.erase()),
cfg.0,
self.cfg_options.clone(),
));
return;
}
};
let attrs = match AttrsOrCfg::lower(self.db, &item, &|| self.cfg_options, self.span_map) {
AttrsOrCfg::Enabled { attrs } => attrs,
AttrsOrCfg::CfgDisabled(cfg) => {
self.diagnostics.push(DefDiagnostic::unconfigured_code(
self.module_id,
InFile::new(self.file_id, ast_id.erase()),
cfg.0,
self.cfg_options.clone(),
));
return;
}
};
let ast_id = InFile::new(self.file_id, ast_id.upcast());

'attrs: for (attr_id, attr) in attrs.as_ref().iter() {
Expand Down Expand Up @@ -357,7 +356,7 @@ impl<'db> AssocItemCollector<'db> {
return;
}

let (syntax, span_map) = self.db.parse_macro_expansion(macro_call_id).value;
let (syntax, span_map) = &self.db.parse_macro_expansion(macro_call_id).value;
let old_file_id = mem::replace(&mut self.file_id, macro_call_id.into());
let old_ast_id_map = mem::replace(&mut self.ast_id_map, self.db.ast_id_map(self.file_id));
let old_span_map = mem::replace(&mut self.span_map, SpanMap::ExpansionSpanMap(span_map));
Expand Down
Loading