2019-11-24 11:39:48 -06:00
|
|
|
//! Defines `Body`: a lowered representation of bodies of functions, statics and
|
|
|
|
//! consts.
|
2019-11-12 06:09:25 -06:00
|
|
|
mod lower;
|
2020-10-23 12:27:04 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
2019-11-14 02:56:13 -06:00
|
|
|
pub mod scope;
|
2022-08-15 06:51:45 -05:00
|
|
|
mod pretty;
|
2019-11-12 09:46:57 -06:00
|
|
|
|
2022-07-20 19:00:58 -05:00
|
|
|
use std::{ops::Index, sync::Arc};
|
2019-11-12 09:46:57 -06:00
|
|
|
|
2020-08-13 09:25:38 -05:00
|
|
|
use base_db::CrateId;
|
internal: move diagnostics to hir
The idea here is to eventually get rid of `dyn Diagnostic` and
`DiagnosticSink` infrastructure altogether, and just have a `enum
hir::Diagnostic` instead.
The problem with `dyn Diagnostic` is that it is defined in the lowest
level of the stack (hir_expand), but is used by the highest level (ide).
As a first step, we free hir_expand and hir_def from `dyn Diagnostic`
and kick the can up to `hir_ty`, as an intermediate state. The plan is
then to move DiagnosticSink similarly to the hir crate, and, as final
third step, remove its usage from the ide.
One currently unsolved problem is testing. You can notice that the test
which checks precise diagnostic ranges, unresolved_import_in_use_tree,
was moved to the ide layer. Logically, only IDE should have the infra to
render a specific range.
At the same time, the range is determined with the data produced in
hir_def and hir crates, so this layering is rather unfortunate. Working
on hir_def shouldn't require compiling `ide` for testing.
2021-05-23 15:31:59 -05:00
|
|
|
use cfg::{CfgExpr, CfgOptions};
|
2019-12-20 04:19:09 -06:00
|
|
|
use drop_bomb::DropBomb;
|
2019-12-03 10:07:56 -06:00
|
|
|
use either::Either;
|
2023-01-09 12:29:28 -06:00
|
|
|
use hir_expand::{
|
|
|
|
attrs::RawAttrs, hygiene::Hygiene, ExpandError, ExpandResult, HirFileId, InFile, MacroCallId,
|
|
|
|
};
|
2021-01-14 18:11:07 -06:00
|
|
|
use la_arena::{Arena, ArenaMap};
|
2021-07-10 15:49:17 -05:00
|
|
|
use limit::Limit;
|
2021-01-27 03:16:24 -06:00
|
|
|
use profile::Count;
|
2019-11-12 09:46:57 -06:00
|
|
|
use rustc_hash::FxHashMap;
|
2023-02-08 05:11:54 -06:00
|
|
|
use syntax::{ast, AstPtr, SyntaxNode, SyntaxNodePtr};
|
2019-11-12 09:46:57 -06:00
|
|
|
|
|
|
|
use crate::{
|
2023-01-09 12:29:28 -06:00
|
|
|
attr::Attrs,
|
2019-11-23 05:44:43 -06:00
|
|
|
db::DefDatabase,
|
2023-03-14 03:45:16 -05:00
|
|
|
expr::{
|
|
|
|
dummy_expr_id, Binding, BindingId, Expr, ExprId, Label, LabelId, Pat, PatId, RecordFieldPat,
|
|
|
|
},
|
2019-12-20 08:43:45 -06:00
|
|
|
item_scope::BuiltinShadowMode,
|
2022-03-08 14:41:19 -06:00
|
|
|
macro_id_to_def_id,
|
2021-01-18 13:18:05 -06:00
|
|
|
nameres::DefMap,
|
2019-12-18 10:41:33 -06:00
|
|
|
path::{ModPath, Path},
|
2022-08-06 11:50:21 -05:00
|
|
|
src::{HasChildSource, HasSource},
|
2022-08-07 11:22:18 -05:00
|
|
|
AsMacroCall, BlockId, DefWithBodyId, HasModule, LocalModuleId, Lookup, MacroId, ModuleId,
|
|
|
|
UnresolvedMacro,
|
2019-11-12 09:46:57 -06:00
|
|
|
};
|
2020-04-11 10:52:26 -05:00
|
|
|
|
2021-05-13 05:44:47 -05:00
|
|
|
pub use lower::LowerCtx;
|
|
|
|
|
2020-05-21 03:48:42 -05:00
|
|
|
/// A subset of Expander that only deals with cfg attributes. We only need it to
|
2020-04-11 10:52:26 -05:00
|
|
|
/// avoid cyclic queries in crate def map during enum processing.
|
2021-04-18 12:56:13 -05:00
|
|
|
#[derive(Debug)]
|
2020-04-11 10:52:26 -05:00
|
|
|
pub(crate) struct CfgExpander {
|
|
|
|
cfg_options: CfgOptions,
|
|
|
|
hygiene: Hygiene,
|
2020-12-17 17:23:46 -06:00
|
|
|
krate: CrateId,
|
2020-04-11 10:52:26 -05:00
|
|
|
}
|
2019-11-12 09:46:57 -06:00
|
|
|
|
2021-04-18 12:56:13 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Expander {
|
2020-04-11 10:52:26 -05:00
|
|
|
cfg_expander: CfgExpander,
|
2021-02-01 06:19:55 -06:00
|
|
|
def_map: Arc<DefMap>,
|
2019-11-14 00:37:33 -06:00
|
|
|
current_file_id: HirFileId,
|
2021-02-04 08:04:21 -06:00
|
|
|
module: LocalModuleId,
|
2023-02-08 05:11:54 -06:00
|
|
|
/// `recursion_depth == usize::MAX` indicates that the recursion limit has been reached.
|
|
|
|
recursion_depth: usize,
|
2019-11-12 09:46:57 -06:00
|
|
|
}
|
|
|
|
|
2020-04-11 10:52:26 -05:00
|
|
|
impl CfgExpander {
|
|
|
|
pub(crate) fn new(
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
current_file_id: HirFileId,
|
|
|
|
krate: CrateId,
|
|
|
|
) -> CfgExpander {
|
|
|
|
let hygiene = Hygiene::new(db.upcast(), current_file_id);
|
|
|
|
let cfg_options = db.crate_graph()[krate].cfg_options.clone();
|
2020-12-17 17:23:46 -06:00
|
|
|
CfgExpander { cfg_options, hygiene, krate }
|
2020-04-11 10:52:26 -05:00
|
|
|
}
|
|
|
|
|
2021-09-27 05:54:24 -05:00
|
|
|
pub(crate) fn parse_attrs(&self, db: &dyn DefDatabase, owner: &dyn ast::HasAttrs) -> Attrs {
|
2023-01-09 12:29:28 -06:00
|
|
|
Attrs::filter(db, self.krate, RawAttrs::new(db.upcast(), owner, &self.hygiene))
|
2020-04-11 10:52:26 -05:00
|
|
|
}
|
|
|
|
|
2021-09-27 05:54:24 -05:00
|
|
|
pub(crate) fn is_cfg_enabled(&self, db: &dyn DefDatabase, owner: &dyn ast::HasAttrs) -> bool {
|
2020-12-17 17:23:46 -06:00
|
|
|
let attrs = self.parse_attrs(db, owner);
|
2020-04-11 10:52:26 -05:00
|
|
|
attrs.is_cfg_enabled(&self.cfg_options)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 00:38:25 -06:00
|
|
|
impl Expander {
|
2021-04-18 12:56:13 -05:00
|
|
|
pub fn new(db: &dyn DefDatabase, current_file_id: HirFileId, module: ModuleId) -> Expander {
|
2020-04-11 10:52:26 -05:00
|
|
|
let cfg_expander = CfgExpander::new(db, current_file_id, module.krate);
|
2021-03-06 17:20:13 -06:00
|
|
|
let def_map = module.def_map(db);
|
2020-04-11 10:09:50 -05:00
|
|
|
Expander {
|
2020-04-11 10:52:26 -05:00
|
|
|
cfg_expander,
|
2021-03-06 17:20:13 -06:00
|
|
|
def_map,
|
2020-04-11 10:09:50 -05:00
|
|
|
current_file_id,
|
2021-02-04 08:04:21 -06:00
|
|
|
module: module.local_id,
|
2023-02-08 05:11:54 -06:00
|
|
|
recursion_depth: 0,
|
2020-04-11 10:09:50 -05:00
|
|
|
}
|
2019-11-14 00:52:03 -06:00
|
|
|
}
|
|
|
|
|
2021-04-18 12:56:13 -05:00
|
|
|
pub fn enter_expand<T: ast::AstNode>(
|
2019-11-14 01:04:39 -06:00
|
|
|
&mut self,
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-11-14 01:04:39 -06:00
|
|
|
macro_call: ast::MacroCall,
|
2021-04-17 10:38:38 -05:00
|
|
|
) -> Result<ExpandResult<Option<(Mark, T)>>, UnresolvedMacro> {
|
2023-02-08 05:11:54 -06:00
|
|
|
let mut unresolved_macro_err = None;
|
|
|
|
|
|
|
|
let result = self.within_limit(db, |this| {
|
|
|
|
let macro_call = InFile::new(this.current_file_id, ¯o_call);
|
|
|
|
|
|
|
|
let resolver =
|
|
|
|
|path| this.resolve_path_as_macro(db, &path).map(|it| macro_id_to_def_id(db, it));
|
|
|
|
|
|
|
|
let mut err = None;
|
|
|
|
let call_id = match macro_call.as_call_id_with_errors(
|
|
|
|
db,
|
|
|
|
this.def_map.krate(),
|
|
|
|
resolver,
|
|
|
|
&mut |e| {
|
|
|
|
err.get_or_insert(e);
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
Ok(call_id) => call_id,
|
|
|
|
Err(resolve_err) => {
|
|
|
|
unresolved_macro_err = Some(resolve_err);
|
|
|
|
return ExpandResult { value: None, err: None };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
ExpandResult { value: call_id.ok(), err }
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(err) = unresolved_macro_err {
|
|
|
|
Err(err)
|
|
|
|
} else {
|
|
|
|
Ok(result)
|
2020-03-21 10:55:20 -05:00
|
|
|
}
|
2022-01-06 05:30:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enter_expand_id<T: ast::AstNode>(
|
|
|
|
&mut self,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
call_id: MacroCallId,
|
|
|
|
) -> ExpandResult<Option<(Mark, T)>> {
|
2023-02-08 05:11:54 -06:00
|
|
|
self.within_limit(db, |_this| ExpandResult::ok(Some(call_id)))
|
2022-01-06 05:30:16 -06:00
|
|
|
}
|
|
|
|
|
2023-02-08 05:11:54 -06:00
|
|
|
fn enter_expand_inner(
|
2022-01-06 05:30:16 -06:00
|
|
|
db: &dyn DefDatabase,
|
|
|
|
call_id: MacroCallId,
|
|
|
|
mut err: Option<ExpandError>,
|
2023-02-08 05:11:54 -06:00
|
|
|
) -> ExpandResult<Option<(HirFileId, SyntaxNode)>> {
|
2020-12-02 09:52:14 -06:00
|
|
|
if err.is_none() {
|
|
|
|
err = db.macro_expand_error(call_id);
|
|
|
|
}
|
2020-11-30 13:26:35 -06:00
|
|
|
|
|
|
|
let file_id = call_id.as_file();
|
|
|
|
|
|
|
|
let raw_node = match db.parse_or_expand(file_id) {
|
|
|
|
Some(it) => it,
|
|
|
|
None => {
|
|
|
|
// Only `None` if the macro expansion produced no usable AST.
|
|
|
|
if err.is_none() {
|
2021-08-15 07:46:13 -05:00
|
|
|
tracing::warn!("no error despite `parse_or_expand` failing");
|
2019-11-14 01:04:39 -06:00
|
|
|
}
|
2020-11-30 13:26:35 -06:00
|
|
|
|
2022-01-06 05:30:16 -06:00
|
|
|
return ExpandResult::only_err(err.unwrap_or_else(|| {
|
2022-02-21 12:14:06 -06:00
|
|
|
ExpandError::Other("failed to parse macro invocation".into())
|
2022-01-06 05:30:16 -06:00
|
|
|
}));
|
2019-11-14 01:04:39 -06:00
|
|
|
}
|
2020-11-30 13:26:35 -06:00
|
|
|
};
|
|
|
|
|
2023-02-08 05:11:54 -06:00
|
|
|
ExpandResult { value: Some((file_id, raw_node)), err }
|
2019-11-14 01:04:39 -06:00
|
|
|
}
|
|
|
|
|
2021-04-18 12:56:13 -05:00
|
|
|
pub fn exit(&mut self, db: &dyn DefDatabase, mut mark: Mark) {
|
2020-04-11 10:52:26 -05:00
|
|
|
self.cfg_expander.hygiene = Hygiene::new(db.upcast(), mark.file_id);
|
2019-11-14 00:52:03 -06:00
|
|
|
self.current_file_id = mark.file_id;
|
2023-02-08 05:11:54 -06:00
|
|
|
if self.recursion_depth == usize::MAX {
|
|
|
|
// Recursion limit has been reached somewhere in the macro expansion tree. Reset the
|
|
|
|
// depth only when we get out of the tree.
|
|
|
|
if !self.current_file_id.is_macro() {
|
|
|
|
self.recursion_depth = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.recursion_depth -= 1;
|
|
|
|
}
|
2019-12-20 04:19:09 -06:00
|
|
|
mark.bomb.defuse();
|
2019-11-14 00:41:46 -06:00
|
|
|
}
|
|
|
|
|
2019-12-23 07:47:11 -06:00
|
|
|
pub(crate) fn to_source<T>(&self, value: T) -> InFile<T> {
|
2019-11-28 03:50:26 -06:00
|
|
|
InFile { file_id: self.current_file_id, value }
|
2019-11-14 00:43:59 -06:00
|
|
|
}
|
|
|
|
|
2021-09-27 05:54:24 -05:00
|
|
|
pub(crate) fn parse_attrs(&self, db: &dyn DefDatabase, owner: &dyn ast::HasAttrs) -> Attrs {
|
2020-12-17 17:23:46 -06:00
|
|
|
self.cfg_expander.parse_attrs(db, owner)
|
2020-10-23 12:27:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn cfg_options(&self) -> &CfgOptions {
|
|
|
|
&self.cfg_expander.cfg_options
|
2020-04-11 10:09:50 -05:00
|
|
|
}
|
|
|
|
|
2021-04-18 12:56:13 -05:00
|
|
|
pub fn current_file_id(&self) -> HirFileId {
|
2021-04-17 10:38:38 -05:00
|
|
|
self.current_file_id
|
|
|
|
}
|
|
|
|
|
2021-05-06 12:59:54 -05:00
|
|
|
fn parse_path(&mut self, db: &dyn DefDatabase, path: ast::Path) -> Option<Path> {
|
|
|
|
let ctx = LowerCtx::with_hygiene(db, &self.cfg_expander.hygiene);
|
2021-05-06 16:23:50 -05:00
|
|
|
Path::from_src(path, &ctx)
|
2019-11-14 00:58:39 -06:00
|
|
|
}
|
|
|
|
|
2022-03-08 14:41:19 -06:00
|
|
|
fn resolve_path_as_macro(&self, db: &dyn DefDatabase, path: &ModPath) -> Option<MacroId> {
|
2021-02-04 08:04:21 -06:00
|
|
|
self.def_map.resolve_path(db, self.module, path, BuiltinShadowMode::Other).0.take_macros()
|
2019-11-12 09:46:57 -06:00
|
|
|
}
|
2019-12-20 04:19:09 -06:00
|
|
|
|
2022-01-27 14:53:49 -06:00
|
|
|
fn recursion_limit(&self, db: &dyn DefDatabase) -> Limit {
|
|
|
|
let limit = db.crate_limits(self.cfg_expander.krate).recursion_limit as _;
|
|
|
|
|
|
|
|
#[cfg(not(test))]
|
|
|
|
return Limit::new(limit);
|
|
|
|
|
|
|
|
// Without this, `body::tests::your_stack_belongs_to_me` stack-overflows in debug
|
|
|
|
#[cfg(test)]
|
|
|
|
return Limit::new(std::cmp::min(32, limit));
|
|
|
|
}
|
2023-02-08 05:11:54 -06:00
|
|
|
|
|
|
|
fn within_limit<F, T: ast::AstNode>(
|
|
|
|
&mut self,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
op: F,
|
|
|
|
) -> ExpandResult<Option<(Mark, T)>>
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> ExpandResult<Option<MacroCallId>>,
|
|
|
|
{
|
|
|
|
if self.recursion_depth == usize::MAX {
|
|
|
|
// Recursion limit has been reached somewhere in the macro expansion tree. We should
|
|
|
|
// stop expanding other macro calls in this tree, or else this may result in
|
|
|
|
// exponential number of macro expansions, leading to a hang.
|
|
|
|
//
|
|
|
|
// The overflow error should have been reported when it occurred (see the next branch),
|
|
|
|
// so don't return overflow error here to avoid diagnostics duplication.
|
|
|
|
cov_mark::hit!(overflow_but_not_me);
|
|
|
|
return ExpandResult::only_err(ExpandError::RecursionOverflowPosioned);
|
|
|
|
} else if self.recursion_limit(db).check(self.recursion_depth + 1).is_err() {
|
|
|
|
self.recursion_depth = usize::MAX;
|
|
|
|
cov_mark::hit!(your_stack_belongs_to_me);
|
|
|
|
return ExpandResult::only_err(ExpandError::Other(
|
|
|
|
"reached recursion limit during macro expansion".into(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let ExpandResult { value, err } = op(self);
|
|
|
|
let Some(call_id) = value else {
|
|
|
|
return ExpandResult { value: None, err };
|
|
|
|
};
|
|
|
|
|
|
|
|
Self::enter_expand_inner(db, call_id, err).map(|value| {
|
|
|
|
value.and_then(|(new_file_id, node)| {
|
|
|
|
let node = T::cast(node)?;
|
|
|
|
|
|
|
|
self.recursion_depth += 1;
|
|
|
|
self.cfg_expander.hygiene = Hygiene::new(db.upcast(), new_file_id);
|
|
|
|
let old_file_id = std::mem::replace(&mut self.current_file_id, new_file_id);
|
|
|
|
let mark =
|
|
|
|
Mark { file_id: old_file_id, bomb: DropBomb::new("expansion mark dropped") };
|
|
|
|
Some((mark, node))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2019-11-12 09:46:57 -06:00
|
|
|
}
|
|
|
|
|
2021-04-10 10:49:12 -05:00
|
|
|
#[derive(Debug)]
|
2021-04-18 12:56:13 -05:00
|
|
|
pub struct Mark {
|
2019-11-14 00:52:03 -06:00
|
|
|
file_id: HirFileId,
|
2019-12-20 04:19:09 -06:00
|
|
|
bomb: DropBomb,
|
2019-11-14 00:52:03 -06:00
|
|
|
}
|
|
|
|
|
2019-11-12 09:46:57 -06:00
|
|
|
/// The body of an item (function, const etc.).
|
2022-07-17 10:22:11 -05:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2019-11-12 09:46:57 -06:00
|
|
|
pub struct Body {
|
2020-03-19 10:00:11 -05:00
|
|
|
pub exprs: Arena<Expr>,
|
|
|
|
pub pats: Arena<Pat>,
|
2023-02-18 14:32:55 -06:00
|
|
|
pub bindings: Arena<Binding>,
|
2020-12-23 09:34:30 -06:00
|
|
|
pub labels: Arena<Label>,
|
2019-11-12 09:46:57 -06:00
|
|
|
/// The patterns for the function's parameters. While the parameter types are
|
|
|
|
/// part of the function signature, the patterns are not (they don't change
|
|
|
|
/// the external type of the function).
|
|
|
|
///
|
|
|
|
/// If this `Body` is for the body of a constant, this will just be
|
|
|
|
/// empty.
|
2019-11-24 09:48:29 -06:00
|
|
|
pub params: Vec<PatId>,
|
2019-11-12 09:46:57 -06:00
|
|
|
/// The `ExprId` of the actual body expression.
|
2019-11-24 09:48:29 -06:00
|
|
|
pub body_expr: ExprId,
|
2021-03-05 07:08:23 -06:00
|
|
|
/// Block expressions in this body that may contain inner items.
|
2021-04-03 20:03:18 -05:00
|
|
|
block_scopes: Vec<BlockId>,
|
2021-01-27 03:16:24 -06:00
|
|
|
_c: Count<Self>,
|
2019-11-12 09:46:57 -06:00
|
|
|
}
|
|
|
|
|
2020-04-11 12:25:33 -05:00
|
|
|
pub type ExprPtr = AstPtr<ast::Expr>;
|
2019-11-28 03:50:26 -06:00
|
|
|
pub type ExprSource = InFile<ExprPtr>;
|
2019-11-12 09:46:57 -06:00
|
|
|
|
|
|
|
pub type PatPtr = Either<AstPtr<ast::Pat>, AstPtr<ast::SelfParam>>;
|
2019-11-28 03:50:26 -06:00
|
|
|
pub type PatSource = InFile<PatPtr>;
|
2019-11-12 09:46:57 -06:00
|
|
|
|
2020-12-23 09:34:30 -06:00
|
|
|
pub type LabelPtr = AstPtr<ast::Label>;
|
|
|
|
pub type LabelSource = InFile<LabelPtr>;
|
2022-09-02 09:57:31 -05:00
|
|
|
|
|
|
|
pub type FieldPtr = AstPtr<ast::RecordExprField>;
|
|
|
|
pub type FieldSource = InFile<FieldPtr>;
|
|
|
|
|
2019-11-12 09:46:57 -06:00
|
|
|
/// An item body together with the mapping from syntax nodes to HIR expression
|
|
|
|
/// IDs. This is needed to go from e.g. a position in a file to the HIR
|
|
|
|
/// expression containing it; but for type inference etc., we want to operate on
|
|
|
|
/// a structure that is agnostic to the actual positions of expressions in the
|
|
|
|
/// file, so that we don't recompute types whenever some whitespace is typed.
|
|
|
|
///
|
|
|
|
/// One complication here is that, due to macro expansion, a single `Body` might
|
|
|
|
/// be spread across several files. So, for each ExprId and PatId, we record
|
|
|
|
/// both the HirFileId and the position inside the file. However, we only store
|
|
|
|
/// AST -> ExprId mapping for non-macro files, as it is not clear how to handle
|
|
|
|
/// this properly for macros.
|
|
|
|
#[derive(Default, Debug, Eq, PartialEq)]
|
|
|
|
pub struct BodySourceMap {
|
2019-11-14 01:30:30 -06:00
|
|
|
expr_map: FxHashMap<ExprSource, ExprId>,
|
2022-09-02 08:08:48 -05:00
|
|
|
expr_map_back: ArenaMap<ExprId, ExprSource>,
|
2021-03-15 07:38:50 -05:00
|
|
|
|
2019-11-14 01:30:30 -06:00
|
|
|
pat_map: FxHashMap<PatSource, PatId>,
|
2022-09-02 08:08:48 -05:00
|
|
|
pat_map_back: ArenaMap<PatId, PatSource>,
|
2021-03-15 07:38:50 -05:00
|
|
|
|
2020-12-23 09:34:30 -06:00
|
|
|
label_map: FxHashMap<LabelSource, LabelId>,
|
|
|
|
label_map_back: ArenaMap<LabelId, LabelSource>,
|
2021-03-15 07:38:50 -05:00
|
|
|
|
|
|
|
/// We don't create explicit nodes for record fields (`S { record_field: 92 }`).
|
|
|
|
/// Instead, we use id of expression (`92`) to identify the field.
|
2022-09-02 09:57:31 -05:00
|
|
|
field_map: FxHashMap<FieldSource, ExprId>,
|
|
|
|
field_map_back: FxHashMap<ExprId, FieldSource>,
|
2021-03-15 07:38:50 -05:00
|
|
|
|
2019-12-23 07:47:11 -06:00
|
|
|
expansions: FxHashMap<InFile<AstPtr<ast::MacroCall>>, HirFileId>,
|
2020-10-23 12:27:04 -05:00
|
|
|
|
|
|
|
/// Diagnostics accumulated during body lowering. These contain `AstPtr`s and so are stored in
|
|
|
|
/// the source map (since they're just as volatile).
|
internal: move diagnostics to hir
The idea here is to eventually get rid of `dyn Diagnostic` and
`DiagnosticSink` infrastructure altogether, and just have a `enum
hir::Diagnostic` instead.
The problem with `dyn Diagnostic` is that it is defined in the lowest
level of the stack (hir_expand), but is used by the highest level (ide).
As a first step, we free hir_expand and hir_def from `dyn Diagnostic`
and kick the can up to `hir_ty`, as an intermediate state. The plan is
then to move DiagnosticSink similarly to the hir crate, and, as final
third step, remove its usage from the ide.
One currently unsolved problem is testing. You can notice that the test
which checks precise diagnostic ranges, unresolved_import_in_use_tree,
was moved to the ide layer. Logically, only IDE should have the infra to
render a specific range.
At the same time, the range is determined with the data produced in
hir_def and hir crates, so this layering is rather unfortunate. Working
on hir_def shouldn't require compiling `ide` for testing.
2021-05-23 15:31:59 -05:00
|
|
|
diagnostics: Vec<BodyDiagnostic>,
|
2019-11-12 09:46:57 -06:00
|
|
|
}
|
|
|
|
|
2020-03-06 08:11:05 -06:00
|
|
|
#[derive(Default, Debug, Eq, PartialEq, Clone, Copy)]
|
2020-03-06 07:44:44 -06:00
|
|
|
pub struct SyntheticSyntax;
|
|
|
|
|
internal: move diagnostics to hir
The idea here is to eventually get rid of `dyn Diagnostic` and
`DiagnosticSink` infrastructure altogether, and just have a `enum
hir::Diagnostic` instead.
The problem with `dyn Diagnostic` is that it is defined in the lowest
level of the stack (hir_expand), but is used by the highest level (ide).
As a first step, we free hir_expand and hir_def from `dyn Diagnostic`
and kick the can up to `hir_ty`, as an intermediate state. The plan is
then to move DiagnosticSink similarly to the hir crate, and, as final
third step, remove its usage from the ide.
One currently unsolved problem is testing. You can notice that the test
which checks precise diagnostic ranges, unresolved_import_in_use_tree,
was moved to the ide layer. Logically, only IDE should have the infra to
render a specific range.
At the same time, the range is determined with the data produced in
hir_def and hir crates, so this layering is rather unfortunate. Working
on hir_def shouldn't require compiling `ide` for testing.
2021-05-23 15:31:59 -05:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
pub enum BodyDiagnostic {
|
|
|
|
InactiveCode { node: InFile<SyntaxNodePtr>, cfg: CfgExpr, opts: CfgOptions },
|
|
|
|
MacroError { node: InFile<AstPtr<ast::MacroCall>>, message: String },
|
2022-06-28 03:41:10 -05:00
|
|
|
UnresolvedProcMacro { node: InFile<AstPtr<ast::MacroCall>>, krate: CrateId },
|
internal: move diagnostics to hir
The idea here is to eventually get rid of `dyn Diagnostic` and
`DiagnosticSink` infrastructure altogether, and just have a `enum
hir::Diagnostic` instead.
The problem with `dyn Diagnostic` is that it is defined in the lowest
level of the stack (hir_expand), but is used by the highest level (ide).
As a first step, we free hir_expand and hir_def from `dyn Diagnostic`
and kick the can up to `hir_ty`, as an intermediate state. The plan is
then to move DiagnosticSink similarly to the hir crate, and, as final
third step, remove its usage from the ide.
One currently unsolved problem is testing. You can notice that the test
which checks precise diagnostic ranges, unresolved_import_in_use_tree,
was moved to the ide layer. Logically, only IDE should have the infra to
render a specific range.
At the same time, the range is determined with the data produced in
hir_def and hir crates, so this layering is rather unfortunate. Working
on hir_def shouldn't require compiling `ide` for testing.
2021-05-23 15:31:59 -05:00
|
|
|
UnresolvedMacroCall { node: InFile<AstPtr<ast::MacroCall>>, path: ModPath },
|
|
|
|
}
|
|
|
|
|
2019-11-12 09:46:57 -06:00
|
|
|
impl Body {
|
2019-11-14 08:37:22 -06:00
|
|
|
pub(crate) fn body_with_source_map_query(
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-11-14 08:37:22 -06:00
|
|
|
def: DefWithBodyId,
|
|
|
|
) -> (Arc<Body>, Arc<BodySourceMap>) {
|
2020-08-12 09:32:36 -05:00
|
|
|
let _p = profile::span("body_with_source_map_query");
|
2019-11-14 08:37:22 -06:00
|
|
|
let mut params = None;
|
|
|
|
|
2023-04-04 14:37:38 -05:00
|
|
|
let (file_id, module, body, is_async_fn) = match def {
|
2019-11-14 08:37:22 -06:00
|
|
|
DefWithBodyId::FunctionId(f) => {
|
2023-04-04 14:37:38 -05:00
|
|
|
let data = db.function_data(f);
|
2019-11-20 07:03:59 -06:00
|
|
|
let f = f.lookup(db);
|
2019-11-14 08:37:22 -06:00
|
|
|
let src = f.source(db);
|
2022-10-10 02:47:09 -05:00
|
|
|
params = src.value.param_list().map(|param_list| {
|
|
|
|
let item_tree = f.id.item_tree(db);
|
|
|
|
let func = &item_tree[f.id.value];
|
|
|
|
let krate = f.container.module(db).krate;
|
|
|
|
let crate_graph = db.crate_graph();
|
|
|
|
(
|
|
|
|
param_list,
|
|
|
|
func.params.clone().map(move |param| {
|
|
|
|
item_tree
|
|
|
|
.attrs(db, krate, param.into())
|
|
|
|
.is_cfg_enabled(&crate_graph[krate].cfg_options)
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
});
|
2023-04-04 14:37:38 -05:00
|
|
|
(
|
|
|
|
src.file_id,
|
|
|
|
f.module(db),
|
|
|
|
src.value.body().map(ast::Expr::from),
|
|
|
|
data.has_async_kw(),
|
|
|
|
)
|
2019-11-14 08:37:22 -06:00
|
|
|
}
|
|
|
|
DefWithBodyId::ConstId(c) => {
|
2019-11-20 09:00:01 -06:00
|
|
|
let c = c.lookup(db);
|
2019-11-14 08:37:22 -06:00
|
|
|
let src = c.source(db);
|
2023-04-04 14:37:38 -05:00
|
|
|
(src.file_id, c.module(db), src.value.body(), false)
|
2019-11-14 08:37:22 -06:00
|
|
|
}
|
|
|
|
DefWithBodyId::StaticId(s) => {
|
2019-11-24 06:13:56 -06:00
|
|
|
let s = s.lookup(db);
|
2019-11-14 08:37:22 -06:00
|
|
|
let src = s.source(db);
|
2023-04-04 14:37:38 -05:00
|
|
|
(src.file_id, s.module(db), src.value.body(), false)
|
2019-11-14 08:37:22 -06:00
|
|
|
}
|
2022-08-06 11:50:21 -05:00
|
|
|
DefWithBodyId::VariantId(v) => {
|
|
|
|
let e = v.parent.lookup(db);
|
|
|
|
let src = v.parent.child_source(db);
|
|
|
|
let variant = &src.value[v.local_id];
|
2023-04-04 14:37:38 -05:00
|
|
|
(src.file_id, e.container, variant.expr(), false)
|
2022-08-06 11:50:21 -05:00
|
|
|
}
|
2019-11-14 08:37:22 -06:00
|
|
|
};
|
|
|
|
let expander = Expander::new(db, file_id, module);
|
2023-04-04 14:37:38 -05:00
|
|
|
let (mut body, source_map) =
|
|
|
|
Body::new(db, expander, params, body, module.krate, is_async_fn);
|
2021-04-03 20:26:16 -05:00
|
|
|
body.shrink_to_fit();
|
2022-10-10 02:47:09 -05:00
|
|
|
|
2019-11-14 08:37:22 -06:00
|
|
|
(Arc::new(body), Arc::new(source_map))
|
|
|
|
}
|
|
|
|
|
2020-03-13 10:05:46 -05:00
|
|
|
pub(crate) fn body_query(db: &dyn DefDatabase, def: DefWithBodyId) -> Arc<Body> {
|
2019-11-14 08:37:22 -06:00
|
|
|
db.body_with_source_map(def).0
|
|
|
|
}
|
|
|
|
|
2021-04-03 20:03:18 -05:00
|
|
|
/// Returns an iterator over all block expressions in this body that define inner items.
|
|
|
|
pub fn blocks<'a>(
|
|
|
|
&'a self,
|
|
|
|
db: &'a dyn DefDatabase,
|
|
|
|
) -> impl Iterator<Item = (BlockId, Arc<DefMap>)> + '_ {
|
|
|
|
self.block_scopes
|
|
|
|
.iter()
|
2022-03-04 13:09:32 -06:00
|
|
|
.map(move |&block| (block, db.block_def_map(block).expect("block ID without DefMap")))
|
2021-04-03 20:03:18 -05:00
|
|
|
}
|
|
|
|
|
2022-08-15 06:51:45 -05:00
|
|
|
pub fn pretty_print(&self, db: &dyn DefDatabase, owner: DefWithBodyId) -> String {
|
|
|
|
pretty::print_body_hir(db, self, owner)
|
|
|
|
}
|
|
|
|
|
2019-11-14 08:37:22 -06:00
|
|
|
fn new(
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-11-14 00:38:25 -06:00
|
|
|
expander: Expander,
|
2022-10-10 02:47:09 -05:00
|
|
|
params: Option<(ast::ParamList, impl Iterator<Item = bool>)>,
|
2019-11-12 09:46:57 -06:00
|
|
|
body: Option<ast::Expr>,
|
2023-03-08 11:28:52 -06:00
|
|
|
krate: CrateId,
|
2023-04-04 14:37:38 -05:00
|
|
|
is_async_fn: bool,
|
2019-11-12 09:46:57 -06:00
|
|
|
) -> (Body, BodySourceMap) {
|
2023-04-04 14:37:38 -05:00
|
|
|
lower::lower(db, expander, params, body, krate, is_async_fn)
|
2019-11-12 09:46:57 -06:00
|
|
|
}
|
2021-04-03 20:26:16 -05:00
|
|
|
|
|
|
|
fn shrink_to_fit(&mut self) {
|
2023-03-09 08:40:51 -06:00
|
|
|
let Self { _c: _, body_expr: _, block_scopes, exprs, labels, params, pats, bindings } =
|
|
|
|
self;
|
2021-04-03 20:26:16 -05:00
|
|
|
block_scopes.shrink_to_fit();
|
|
|
|
exprs.shrink_to_fit();
|
|
|
|
labels.shrink_to_fit();
|
|
|
|
params.shrink_to_fit();
|
|
|
|
pats.shrink_to_fit();
|
2023-02-18 14:32:55 -06:00
|
|
|
bindings.shrink_to_fit();
|
2021-04-03 20:26:16 -05:00
|
|
|
}
|
2023-03-14 03:45:16 -05:00
|
|
|
|
|
|
|
pub fn walk_bindings_in_pat(&self, pat_id: PatId, mut f: impl FnMut(BindingId)) {
|
|
|
|
self.walk_pats(pat_id, &mut |pat| {
|
|
|
|
if let Pat::Bind { id, .. } = pat {
|
|
|
|
f(*id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn walk_pats(&self, pat_id: PatId, f: &mut impl FnMut(&Pat)) {
|
|
|
|
let pat = &self[pat_id];
|
|
|
|
f(pat);
|
|
|
|
match pat {
|
|
|
|
Pat::Range { .. }
|
|
|
|
| Pat::Lit(..)
|
|
|
|
| Pat::Path(..)
|
|
|
|
| Pat::ConstBlock(..)
|
|
|
|
| Pat::Wild
|
|
|
|
| Pat::Missing => {}
|
|
|
|
&Pat::Bind { subpat, .. } => {
|
|
|
|
if let Some(subpat) = subpat {
|
|
|
|
self.walk_pats(subpat, f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Pat::Or(args) | Pat::Tuple { args, .. } | Pat::TupleStruct { args, .. } => {
|
|
|
|
args.iter().copied().for_each(|p| self.walk_pats(p, f));
|
|
|
|
}
|
|
|
|
Pat::Ref { pat, .. } => self.walk_pats(*pat, f),
|
|
|
|
Pat::Slice { prefix, slice, suffix } => {
|
|
|
|
let total_iter = prefix.iter().chain(slice.iter()).chain(suffix.iter());
|
|
|
|
total_iter.copied().for_each(|p| self.walk_pats(p, f));
|
|
|
|
}
|
|
|
|
Pat::Record { args, .. } => {
|
|
|
|
args.iter().for_each(|RecordFieldPat { pat, .. }| self.walk_pats(*pat, f));
|
|
|
|
}
|
|
|
|
Pat::Box { inner } => self.walk_pats(*inner, f),
|
|
|
|
}
|
|
|
|
}
|
2019-11-12 09:46:57 -06:00
|
|
|
}
|
|
|
|
|
2022-07-17 10:22:11 -05:00
|
|
|
impl Default for Body {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
body_expr: dummy_expr_id(),
|
|
|
|
exprs: Default::default(),
|
|
|
|
pats: Default::default(),
|
2023-02-18 14:32:55 -06:00
|
|
|
bindings: Default::default(),
|
2022-07-17 10:22:11 -05:00
|
|
|
labels: Default::default(),
|
|
|
|
params: Default::default(),
|
|
|
|
block_scopes: Default::default(),
|
|
|
|
_c: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 09:46:57 -06:00
|
|
|
impl Index<ExprId> for Body {
|
|
|
|
type Output = Expr;
|
|
|
|
|
|
|
|
fn index(&self, expr: ExprId) -> &Expr {
|
|
|
|
&self.exprs[expr]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Index<PatId> for Body {
|
|
|
|
type Output = Pat;
|
|
|
|
|
|
|
|
fn index(&self, pat: PatId) -> &Pat {
|
|
|
|
&self.pats[pat]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 09:34:30 -06:00
|
|
|
impl Index<LabelId> for Body {
|
|
|
|
type Output = Label;
|
|
|
|
|
|
|
|
fn index(&self, label: LabelId) -> &Label {
|
|
|
|
&self.labels[label]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-18 14:32:55 -06:00
|
|
|
impl Index<BindingId> for Body {
|
|
|
|
type Output = Binding;
|
|
|
|
|
|
|
|
fn index(&self, b: BindingId) -> &Binding {
|
|
|
|
&self.bindings[b]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 07:38:50 -05:00
|
|
|
// FIXME: Change `node_` prefix to something more reasonable.
|
|
|
|
// Perhaps `expr_syntax` and `expr_id`?
|
2019-11-12 09:46:57 -06:00
|
|
|
impl BodySourceMap {
|
2020-03-06 07:44:44 -06:00
|
|
|
pub fn expr_syntax(&self, expr: ExprId) -> Result<ExprSource, SyntheticSyntax> {
|
2022-09-02 08:08:48 -05:00
|
|
|
self.expr_map_back.get(expr).cloned().ok_or(SyntheticSyntax)
|
2019-11-12 09:46:57 -06:00
|
|
|
}
|
|
|
|
|
2019-11-28 03:50:26 -06:00
|
|
|
pub fn node_expr(&self, node: InFile<&ast::Expr>) -> Option<ExprId> {
|
2022-03-12 06:35:31 -06:00
|
|
|
let src = node.map(AstPtr::new);
|
2019-11-14 01:30:30 -06:00
|
|
|
self.expr_map.get(&src).cloned()
|
2019-11-12 09:46:57 -06:00
|
|
|
}
|
|
|
|
|
2019-12-23 07:47:11 -06:00
|
|
|
pub fn node_macro_file(&self, node: InFile<&ast::MacroCall>) -> Option<HirFileId> {
|
2022-03-12 06:35:31 -06:00
|
|
|
let src = node.map(AstPtr::new);
|
2019-12-23 07:47:11 -06:00
|
|
|
self.expansions.get(&src).cloned()
|
|
|
|
}
|
|
|
|
|
2020-03-06 07:44:44 -06:00
|
|
|
pub fn pat_syntax(&self, pat: PatId) -> Result<PatSource, SyntheticSyntax> {
|
2022-09-02 08:08:48 -05:00
|
|
|
self.pat_map_back.get(pat).cloned().ok_or(SyntheticSyntax)
|
2019-11-12 09:46:57 -06:00
|
|
|
}
|
|
|
|
|
2019-11-28 03:50:26 -06:00
|
|
|
pub fn node_pat(&self, node: InFile<&ast::Pat>) -> Option<PatId> {
|
2019-12-03 10:07:56 -06:00
|
|
|
let src = node.map(|it| Either::Left(AstPtr::new(it)));
|
2019-11-14 01:30:30 -06:00
|
|
|
self.pat_map.get(&src).cloned()
|
2019-11-12 09:46:57 -06:00
|
|
|
}
|
|
|
|
|
2020-07-10 07:08:35 -05:00
|
|
|
pub fn node_self_param(&self, node: InFile<&ast::SelfParam>) -> Option<PatId> {
|
|
|
|
let src = node.map(|it| Either::Right(AstPtr::new(it)));
|
|
|
|
self.pat_map.get(&src).cloned()
|
|
|
|
}
|
|
|
|
|
2020-12-23 09:34:30 -06:00
|
|
|
pub fn label_syntax(&self, label: LabelId) -> LabelSource {
|
|
|
|
self.label_map_back[label].clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn node_label(&self, node: InFile<&ast::Label>) -> Option<LabelId> {
|
2022-03-12 06:35:31 -06:00
|
|
|
let src = node.map(AstPtr::new);
|
2020-12-23 09:34:30 -06:00
|
|
|
self.label_map.get(&src).cloned()
|
|
|
|
}
|
|
|
|
|
2022-09-02 09:57:31 -05:00
|
|
|
pub fn field_syntax(&self, expr: ExprId) -> FieldSource {
|
2021-03-15 07:38:50 -05:00
|
|
|
self.field_map_back[&expr].clone()
|
|
|
|
}
|
2022-09-02 09:57:31 -05:00
|
|
|
|
2021-03-15 07:38:50 -05:00
|
|
|
pub fn node_field(&self, node: InFile<&ast::RecordExprField>) -> Option<ExprId> {
|
2022-03-12 06:35:31 -06:00
|
|
|
let src = node.map(AstPtr::new);
|
2021-03-15 07:38:50 -05:00
|
|
|
self.field_map.get(&src).cloned()
|
2019-11-12 09:46:57 -06:00
|
|
|
}
|
2020-07-11 11:35:35 -05:00
|
|
|
|
2022-07-01 07:43:57 -05:00
|
|
|
pub fn macro_expansion_expr(&self, node: InFile<&ast::MacroExpr>) -> Option<ExprId> {
|
|
|
|
let src = node.map(AstPtr::new).map(AstPtr::upcast::<ast::MacroExpr>).map(AstPtr::upcast);
|
|
|
|
self.expr_map.get(&src).copied()
|
2022-03-20 13:07:44 -05:00
|
|
|
}
|
|
|
|
|
internal: move diagnostics to hir
The idea here is to eventually get rid of `dyn Diagnostic` and
`DiagnosticSink` infrastructure altogether, and just have a `enum
hir::Diagnostic` instead.
The problem with `dyn Diagnostic` is that it is defined in the lowest
level of the stack (hir_expand), but is used by the highest level (ide).
As a first step, we free hir_expand and hir_def from `dyn Diagnostic`
and kick the can up to `hir_ty`, as an intermediate state. The plan is
then to move DiagnosticSink similarly to the hir crate, and, as final
third step, remove its usage from the ide.
One currently unsolved problem is testing. You can notice that the test
which checks precise diagnostic ranges, unresolved_import_in_use_tree,
was moved to the ide layer. Logically, only IDE should have the infra to
render a specific range.
At the same time, the range is determined with the data produced in
hir_def and hir crates, so this layering is rather unfortunate. Working
on hir_def shouldn't require compiling `ide` for testing.
2021-05-23 15:31:59 -05:00
|
|
|
/// Get a reference to the body source map's diagnostics.
|
|
|
|
pub fn diagnostics(&self) -> &[BodyDiagnostic] {
|
|
|
|
&self.diagnostics
|
2020-07-11 11:35:35 -05:00
|
|
|
}
|
|
|
|
}
|