diff --git a/compiler/cl-typeck/examples/typeck.rs b/compiler/cl-typeck/examples/typeck.rs index 65ec481..08b6cc9 100644 --- a/compiler/cl-typeck/examples/typeck.rs +++ b/compiler/cl-typeck/examples/typeck.rs @@ -1,11 +1,11 @@ use cl_ast::{ - ast_visitor::fold::Fold, + ast_visitor::{Fold, Visit}, desugar::{squash_groups::SquashGroups, while_else::WhileElseDesugar}, }; use cl_lexer::Lexer; use cl_parser::{inliner::ModuleInliner, Parser}; use cl_typeck::{ - definition::Def, name_collector::NameCollectable, project::Project, type_resolver::resolve, + definition::Def, name_collector::NameCollector, project::Project, type_resolver::resolve, }; use repline::{error::Error as RlError, prebaked::*}; use std::{error::Error, path}; @@ -38,8 +38,7 @@ fn main() -> Result<(), Box> { } }; let code = inline_modules(code, concat!(env!("PWD"), "/stdlib")); - - unsafe { TREES.push(code) }.collect_in_root(&mut prj)?; + NameCollector::new(&mut prj).visit_file(unsafe { TREES.push(code) }); main_menu(&mut prj)?; Ok(()) @@ -84,7 +83,7 @@ fn enter_code(prj: &mut Project) -> Result<(), RlError> { let code = inline_modules(code, ""); let code = WhileElseDesugar.fold_file(code); // Safety: this is totally unsafe - unsafe { TREES.push(code) }.collect_in_root(prj)?; + NameCollector::new(prj).visit_file(unsafe { TREES.push(code) }); Ok(Response::Accept) }) diff --git a/compiler/cl-typeck/src/name_collector.rs b/compiler/cl-typeck/src/name_collector.rs index 67cae54..8726358 100644 --- a/compiler/cl-typeck/src/name_collector.rs +++ b/compiler/cl-typeck/src/name_collector.rs @@ -8,24 +8,6 @@ use crate::{ use cl_ast::{ast_visitor::Visit, *}; use std::mem; -pub trait NameCollectable<'a> { - /// Collects the identifiers within this node, - /// returning a new [DefID] if any were allocated, - /// else returning the parent [DefID] - fn collect(&'a self, c: &mut Prj<'a>, parent: DefID) -> Result; - - fn collect_in_root(&'a self, c: &mut Prj<'a>) -> Result { - self.collect(c, c.root) - } -} - -impl<'a> NameCollectable<'a> for File { - fn collect(&'a self, prj: &mut Prj<'a>, parent: DefID) -> Result { - NameCollector::with_root(prj, parent).visit_file(self); - Ok(parent) - } -} - #[derive(Debug)] pub struct NameCollector<'prj, 'a> { prj: &'prj mut Prj<'a>, @@ -34,9 +16,11 @@ pub struct NameCollector<'prj, 'a> { } impl<'prj, 'a> NameCollector<'prj, 'a> { + /// Constructs a new [NameCollector] out of a [Project](Prj) pub fn new(prj: &'prj mut Prj<'a>) -> Self { Self { parent: prj.root, prj, retval: None } } + /// Constructs a new [NameCollector] out of a [Project](Prj) and a parent [DefID] pub fn with_root(prj: &'prj mut Prj<'a>, parent: DefID) -> Self { Self { prj, parent, retval: None } } @@ -56,7 +40,7 @@ impl<'prj, 'a> NameCollector<'prj, 'a> { } } -impl<'prj, 'a> ast_visitor::Visit<'a> for NameCollector<'prj, 'a> { +impl<'prj, 'a> Visit<'a> for NameCollector<'prj, 'a> { fn visit_item(&mut self, i: &'a Item) { let Item { extents: _, attrs, vis, kind } = i; if let Some(def) = self.returns(kind, Self::visit_item_kind) {