Skip to content

Commit

Permalink
fold_file wild attribute
Browse files Browse the repository at this point in the history
Summary: Fold over wild attributes

Reviewed By: perehonchuk

Differential Revision: D49821521

fbshipit-source-id: 3a83ab84f272352838feb1c096950b80f6e2806c
  • Loading branch information
alanz authored and facebook-github-bot committed Oct 3, 2023
1 parent 08c811e commit bd5f4e8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
22 changes: 20 additions & 2 deletions crates/hir/src/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ pub fn fold_file<'a, T>(
callback(acc, ctx)
})
}
FormIdx::Attribute(_attribute_id) => {
todo!()
FormIdx::Attribute(attribute_id) => {
sema.fold_attribute(InFile::new(file_id, attribute_id), r, &mut |acc, ctx| {
callback(acc, ctx)
})
}
FormIdx::CompileOption(_attribute_id) => {
todo!()
Expand Down Expand Up @@ -1578,6 +1580,13 @@ bar() ->
acc
}
}
AnyExpr::Term(Term::Literal(Literal::Atom(atom))) => {
if atom == hir_atom {
acc + 1
} else {
acc
}
}
_ => acc,
});

Expand Down Expand Up @@ -1638,4 +1647,13 @@ bar() ->
// Note: fold does not look into field names
count_atom_foo(fixture_str, 1);
}

#[test]
fn traverse_attribute() {
let fixture_str = r#"
-module(foo).
-wild(r1, {f1, f~oo}).
"#;
count_atom_foo(fixture_str, 1);
}
}
18 changes: 18 additions & 0 deletions crates/hir/src/sema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub use crate::intern::MinInternDatabaseStorage;
use crate::resolver::Resolution;
use crate::resolver::Resolver;
use crate::AnyExprId;
use crate::AttributeId;
use crate::Body;
use crate::BodySourceMap;
use crate::CRClause;
Expand Down Expand Up @@ -637,6 +638,23 @@ impl<'db> Semantic<'db> {
})
}

pub fn fold_attribute<'a, T>(
&self,
attribute_id: InFile<AttributeId>,
initial: T,
callback: AnyCallBack<'a, T>,
) -> T {
let body = self.db.attribute_body(attribute_id);
FoldCtx::fold_term(
&body.body,
Strategy::TopDown,
FormIdx::Attribute(attribute_id.value),
body.value,
initial,
callback,
)
}

pub fn bound_vars_in_pattern_diagnostic(
&self,
file_id: FileId,
Expand Down

0 comments on commit bd5f4e8

Please sign in to comment.