-
Notifications
You must be signed in to change notification settings - Fork 0
fix(lambda): use scope graph env for edit capture checks #420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,187 @@ fn collect_var_usages( | |
| } | ||
| } | ||
|
|
||
| ///| | ||
| fn name_captured_at_node( | ||
| g : @scope.ScopeGraph, | ||
| node_id : NodeId, | ||
| name : String, | ||
| ) -> Bool { | ||
| @scope.enclosing_env(g, node_id).contains(name) | ||
| } | ||
|
|
||
| ///| | ||
| fn free_names_captured_at_use_sites( | ||
| g : @scope.ScopeGraph, | ||
| node : ProjNode[@ast.Term], | ||
| free_names : @immut/hashset.HashSet[String], | ||
| ) -> Bool { | ||
| let mut captured = false | ||
| free_names.each(fn(v) { | ||
| let var_ids : Array[NodeId] = [] | ||
| collect_var_usages(node, v, var_ids) | ||
| if var_ids.any(fn(vid) { name_captured_at_node(g, vid, v) }) { | ||
| captured = true | ||
| } | ||
| }) | ||
| captured | ||
| } | ||
|
|
||
| ///| | ||
| fn def_cutoff_at_node( | ||
| flat_proj : FlatProj, | ||
| source_map : SourceMap, | ||
| node_id : NodeId, | ||
| ) -> Int { | ||
| guard source_map.get_range(node_id) is Some(r) else { | ||
| return flat_proj.defs.length() | ||
| } | ||
| flat_proj.defs | ||
| .search_by(fn(def) { | ||
| source_map.get_range(def.1.id()) is Some(dr) && | ||
| r.start >= dr.start && | ||
| r.start < dr.end | ||
| }) | ||
| .unwrap_or(flat_proj.defs.length()) | ||
| } | ||
|
|
||
| ///| | ||
| fn scope_id_for_node( | ||
| g : @scope.ScopeGraph, | ||
| node_id : NodeId, | ||
| ) -> @scope.ScopeId? { | ||
| match g.refs.iter().find_first(fn(r) { r.node_id == node_id }) { | ||
| Some(r) => Some(r.scope) | ||
| None => | ||
| g.decls | ||
| .iter() | ||
| .find_first(fn(d) { d.node_id == node_id }) | ||
| .map(fn(d) { d.scope }) | ||
| } | ||
| } | ||
|
|
||
| ///| | ||
| fn root_scope_id(g : @scope.ScopeGraph) -> @scope.ScopeId? { | ||
| g.scopes.iter().find_first(fn(s) { s.parent is None }).map(fn(s) { s.id }) | ||
| } | ||
|
|
||
| ///| | ||
| fn declaration_id_for_name_from_scope( | ||
| g : @scope.ScopeGraph, | ||
| start_scope : @scope.ScopeId, | ||
| cutoff : Int, | ||
| name : String, | ||
| ) -> @scope.DeclId? { | ||
| let mut cur : @scope.ScopeId? = Some(start_scope) | ||
| while cur is Some(sid) { | ||
| let @scope.ScopeId(si) = sid | ||
| let scope = g.scopes[si] | ||
| let mut hit : @scope.DeclId? = None | ||
| for did in scope.decl_ids { | ||
| let @scope.DeclId(di) = did | ||
| let decl = g.decls[di] | ||
| if decl.name == name { | ||
| match decl.kind { | ||
| @scope.DeclKind::ModuleDef(def_index~) => | ||
| if def_index < cutoff { | ||
| hit = Some(did) | ||
| } | ||
| @scope.DeclKind::LamParam(_) => hit = Some(did) | ||
| } | ||
| } | ||
| } | ||
| if hit is Some(_) { | ||
| return hit | ||
| } | ||
| cur = scope.parent | ||
| } | ||
| None | ||
| } | ||
|
|
||
| ///| | ||
| fn declaration_id_for_name_at_node( | ||
| g : @scope.ScopeGraph, | ||
| flat_proj : FlatProj, | ||
| source_map : SourceMap, | ||
| node_id : NodeId, | ||
| name : String, | ||
| ) -> @scope.DeclId? { | ||
| guard scope_id_for_node(g, node_id) is Some(start_scope) else { return None } | ||
| declaration_id_for_name_from_scope( | ||
| g, | ||
| start_scope, | ||
| def_cutoff_at_node(flat_proj, source_map, node_id), | ||
| name, | ||
| ) | ||
| } | ||
|
|
||
| ///| | ||
| fn declaration_id_for_name_at_module_end( | ||
| g : @scope.ScopeGraph, | ||
| flat_proj : FlatProj, | ||
| name : String, | ||
| ) -> @scope.DeclId? { | ||
| guard root_scope_id(g) is Some(root_scope) else { return None } | ||
| declaration_id_for_name_from_scope( | ||
| g, | ||
| root_scope, | ||
| flat_proj.defs.length(), | ||
| name, | ||
| ) | ||
| } | ||
|
|
||
| ///| | ||
| fn free_names_would_rebind_at_node( | ||
| g : @scope.ScopeGraph, | ||
| flat_proj : FlatProj, | ||
| source_map : SourceMap, | ||
| source_node : ProjNode[@ast.Term], | ||
| target_node_id : NodeId, | ||
| free_names : @immut/hashset.HashSet[String], | ||
| ) -> Bool { | ||
| let mut rebound = false | ||
| free_names.each(fn(v) { | ||
| let target_decl = declaration_id_for_name_at_node( | ||
| g, flat_proj, source_map, target_node_id, v, | ||
| ) | ||
| if free_name_would_rebind_to(g, source_node, v, target_decl) { | ||
| rebound = true | ||
| } | ||
| }) | ||
| rebound | ||
| } | ||
|
|
||
| ///| | ||
| fn free_names_would_rebind_at_module_end( | ||
| g : @scope.ScopeGraph, | ||
| flat_proj : FlatProj, | ||
| source_node : ProjNode[@ast.Term], | ||
| free_names : @immut/hashset.HashSet[String], | ||
| ) -> Bool { | ||
| let mut rebound = false | ||
| free_names.each(fn(v) { | ||
| let target_decl = declaration_id_for_name_at_module_end(g, flat_proj, v) | ||
| if free_name_would_rebind_to(g, source_node, v, target_decl) { | ||
| rebound = true | ||
| } | ||
| }) | ||
| rebound | ||
| } | ||
|
|
||
| ///| | ||
| fn free_name_would_rebind_to( | ||
| g : @scope.ScopeGraph, | ||
| source_node : ProjNode[@ast.Term], | ||
| name : String, | ||
| target_decl : @scope.DeclId?, | ||
| ) -> Bool { | ||
| let var_ids : Array[NodeId] = [] | ||
| collect_var_usages(source_node, name, var_ids) | ||
| var_ids.any(fn(vid) { | ||
| @scope.declaration(g, vid).map(fn(d) { d.id }) != target_decl | ||
|
Comment on lines
+218
to
+220
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When the initializer has both a free occurrence and a nested binder with the same name, this collects every Useful? React with 👍 / 👎. |
||
| }) | ||
| } | ||
|
|
||
| ///| | ||
| /// Look up the FlatProj binding NodeId for a given init expression NodeId. | ||
| /// The UI selects init expression ProjNodes (children of Module); this function | ||
|
|
@@ -55,27 +236,3 @@ pub fn find_binding_for_init( | |
| } | ||
| None | ||
| } | ||
|
|
||
| ///| | ||
| /// Collect Lam parameter names in scope at a node position (walking up the tree). | ||
| pub fn collect_lam_env( | ||
| node_id : NodeId, | ||
| registry : Map[NodeId, ProjNode[@ast.Term]], | ||
| ) -> @immut/hashset.HashSet[String] { | ||
| let mut env : @immut/hashset.HashSet[String] = @immut/hashset.new() | ||
| fn walk_up(nid : NodeId) { | ||
| for pid, pnode in registry { | ||
| for child in pnode.children { | ||
| if child.id() == nid { | ||
| if pnode.kind is @ast.Term::Lam(param, _) { | ||
| env = env.add(param) | ||
| } | ||
| walk_up(pid) | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } | ||
| walk_up(node_id) | ||
| env | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the initializer has a free occurrence with the same name as the binding being inlined, e.g.
let x = x\nx, this lookup resolvestarget_declto the current module declaration because the graph is built before the inline deletes that declaration. The source occurrence is correctly unresolved in its own initializer, so the comparison below reports a rebind and rejects the inline, even though after deleting the binding the insertedxwould also be unresolved and semantics are preserved. Treat the module decl being inlined as absent when computing the target declaration.Useful? React with 👍 / 👎.