Skip to content
Merged
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: 4 additions & 0 deletions crates/hir-expand/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ impl Name {
pub fn symbol(&self) -> &Symbol {
&self.symbol
}

pub fn is_generated(&self) -> bool {
self.as_str().starts_with("<ra@gennew>")
}
}

struct Display<'a> {
Expand Down
103 changes: 102 additions & 1 deletion crates/hir/src/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ use std::{
ops::{self, ControlFlow, Not},
};

use base_db::FxIndexSet;
use either::Either;
use hir_def::{
DefWithBodyId, FunctionId, MacroId, StructId, TraitId, VariantId,
expr_store::{Body, ExprOrPatSource, HygieneId, path::Path},
hir::{BindingId, Expr, ExprId, ExprOrPatId, Pat},
nameres::{ModuleOrigin, crate_def_map},
resolver::{self, HasResolver, Resolver, TypeNs},
resolver::{self, HasResolver, Resolver, TypeNs, ValueNs},
type_ref::Mutability,
};
use hir_expand::{
Expand Down Expand Up @@ -2192,6 +2193,106 @@ impl<'db> SemanticsImpl<'db> {
self.cache(adt_source.value.syntax().ancestors().last().unwrap(), adt_source.file_id);
ToDef::to_def(self, adt_source.as_ref())
}

pub fn locals_used(
&self,
element: Either<&ast::Expr, &ast::StmtList>,
text_range: TextRange,
) -> Option<FxIndexSet<Local>> {
let sa = self.analyze(element.either(|e| e.syntax(), |s| s.syntax()))?;
let store = sa.store()?;
let mut resolver = sa.resolver.clone();
let def = resolver.body_owner()?;

let is_not_generated = |path: &Path| {
!path.mod_path().and_then(|path| path.as_ident()).is_some_and(Name::is_generated)
};

let exprs = element.either(
|e| vec![e.clone()],
|stmts| {
let mut exprs: Vec<_> = stmts
.statements()
.filter(|stmt| text_range.contains_range(stmt.syntax().text_range()))
.filter_map(|stmt| match stmt {
ast::Stmt::ExprStmt(expr_stmt) => expr_stmt.expr().map(|e| vec![e]),
ast::Stmt::Item(_) => None,
ast::Stmt::LetStmt(let_stmt) => {
let init = let_stmt.initializer();
let let_else = let_stmt
.let_else()
.and_then(|le| le.block_expr())
.map(ast::Expr::BlockExpr);

match (init, let_else) {
(Some(i), Some(le)) => Some(vec![i, le]),
(Some(i), _) => Some(vec![i]),
(_, Some(le)) => Some(vec![le]),
_ => None,
}
}
})
.flatten()
.collect();

if let Some(tail_expr) = stmts.tail_expr()
&& text_range.contains_range(tail_expr.syntax().text_range())
{
exprs.push(tail_expr);
}
exprs
},
);
let mut exprs: Vec<_> =
exprs.into_iter().filter_map(|e| sa.expr_id(e).and_then(|e| e.as_expr())).collect();

let mut locals: FxIndexSet<Local> = FxIndexSet::default();
let mut add_to_locals_used = |id, parent_expr| {
let path = match id {
ExprOrPatId::ExprId(expr_id) => {
if let Expr::Path(path) = &store[expr_id] {
Some(path)
} else {
None
}
}
ExprOrPatId::PatId(pat_id) => {
if let Pat::Path(path) = &store[pat_id] {
Some(path)
} else {
None
}
}
};

if let Some(path) = path
&& is_not_generated(path)
{
let _ = resolver.update_to_inner_scope(self.db, def, parent_expr);
let hygiene = store.expr_or_pat_path_hygiene(id);
resolver.resolve_path_in_value_ns_fully(self.db, path, hygiene).inspect(|value| {
if let ValueNs::LocalBinding(id) = value {
locals.insert((def, *id).into());
}
});
}
};

while let Some(expr_id) = exprs.pop() {
if let Expr::Assignment { target, .. } = store[expr_id] {
store.walk_pats(target, &mut |id| {
add_to_locals_used(ExprOrPatId::PatId(id), expr_id)
});
};
store.walk_child_exprs(expr_id, |id| {
exprs.push(id);
});

add_to_locals_used(ExprOrPatId::ExprId(expr_id), expr_id)
}

Some(locals)
}
}

// FIXME This can't be the best way to do this
Expand Down
2 changes: 1 addition & 1 deletion crates/hir/src/source_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl<'db> SourceAnalyzer<'db> {
)
}

fn expr_id(&self, expr: ast::Expr) -> Option<ExprOrPatId> {
pub(crate) fn expr_id(&self, expr: ast::Expr) -> Option<ExprOrPatId> {
let src = InFile { file_id: self.file_id, value: expr };
self.store_sm()?.node_expr(src.as_ref())
}
Expand Down
Loading