Skip to content

Commit

Permalink
BE: add InFunctionClauseBody::ast_fun_decl helper function
Browse files Browse the repository at this point in the history
Summary: Add a helper function to return the AST that was the lowered to generate this function clause.

Reviewed By: jcpetruzza

Differential Revision: D52164542

fbshipit-source-id: 8a4793bdcb7efd63ae638419a1f7f4fc08ffb452
  • Loading branch information
alanz authored and facebook-github-bot committed Dec 14, 2023
1 parent 4fc1cbd commit 02e99be
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions crates/hir/src/sema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,15 @@ impl<T> InFunctionClauseBody<T> {
self.function_id.file_id
}

pub fn ast_fun_decl(&self, db: &dyn MinDefDatabase) -> ast::FunDecl {
let form_list = db.file_form_list(self.function_id.file_id);
let function = &form_list[self.function_id.value];
let function_ast = function
.form_id
.get(&self.function_id.file_syntax(db.upcast()));
function_ast
}

pub fn body_exprs(&self) -> impl Iterator<Item = (ExprId, &Expr)> {
self.body.body.exprs.iter()
}
Expand Down Expand Up @@ -1466,6 +1475,7 @@ mod tests {
use crate::test_db::TestDB;
use crate::AnyExprId;
use crate::InFile;
use crate::InFunctionClauseBody;
use crate::Semantic;

#[track_caller]
Expand Down Expand Up @@ -1671,4 +1681,86 @@ mod tests {
"#,
)
}

#[track_caller]
fn check_in_clause_ast(fixture_before: &str, expect: Expect) {
let (db, position) = TestDB::with_position(fixture_before);
let sema = Semantic::new(&db);

let file_syntax = db.parse(position.file_id).syntax_node();
let token = file_syntax
.token_at_offset(position.offset)
.right_biased()
.unwrap();
let node = token.parent().unwrap();
let (_clause_id, clause) = sema
.to_clause_body(InFile::new(position.file_id, &node))
.unwrap();
let function_id = sema
.find_enclosing_function_id(position.file_id, &node)
.unwrap();
let in_clause =
InFunctionClauseBody::new(clause, InFile::new(position.file_id, function_id), None, ());
let ast = in_clause.ast_fun_decl(&db);
expect.assert_debug_eq(&ast);
}

#[test]
fn infunctionclausebody_get_ast() {
check_in_clause_ast(
r#"foo(0) -> ok;
foo(~A) -> this.
"#,
expect![[r#"
FunDecl {
syntax: [email protected]
[email protected]
[email protected]
[email protected] "foo"
[email protected]
[email protected] "("
[email protected]
[email protected] "A"
[email protected] ")"
[email protected] " "
[email protected]
[email protected] "->"
[email protected] " "
[email protected]
[email protected] "this"
[email protected] "."
,
}
"#]],
);
}

#[test]
fn infunctionclausebody_get_ast_from_macro() {
check_in_clause_ast(
r#"
-define(CLAUSE(Res), foo(_) -> Res).
foo() -> 1;
?CLAUSE(~V).
"#,
expect![[r#"
FunDecl {
syntax: [email protected]
[email protected]
[email protected] "?"
[email protected]
[email protected] "CLAUSE"
[email protected]
[email protected] "("
[email protected]
[email protected]
[email protected] "V"
[email protected] ")"
[email protected] "."
,
}
"#]],
);
}
}

0 comments on commit 02e99be

Please sign in to comment.