Skip to content
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

handle modules better #157

Open
mimoo opened this issue Jul 31, 2024 · 0 comments
Open

handle modules better #157

mimoo opened this issue Jul 31, 2024 · 0 comments
Labels

Comments

@mimoo
Copy link
Contributor

mimoo commented Jul 31, 2024

This is a main issue for a number of issues we need to fix.

Enforcing allowed imports

First, I can import anything I want here:

use something::that_doesnt_exist;

and it will work. This is because the name resolution pass doesn't seem to be aware of dependencies that are allowed. The Noname.toml file related to the code being compiled should dictate which dependencies are accessible. Some standard modules should be accessible as well (not std::builtins which is included by default, but std::crypto or std::bits)

Scope of imports

Second, it seems to me that we aren't really keeping track of what dependencies a function has access to as we traverse the AST. This is because we build one main TAST with all the fully-qualified functions (or exported structs or constants) in its functions: HashMap<FullyQualified, FnInfo<B>> field when we compile all dependencies in cmd_build_and_check.rs:produce_all_asts():

for dep in dep_graph.from_leaves_to_roots() {
        let path = path_to_package(&dep);

        let lib_file = path.join("src").join("lib.no");
        let code = std::fs::read_to_string(&lib_file)
            .into_diagnostic()
            .wrap_err_with(|| format!("could not read file `{path}`"))?;

        let lib_file_str = lib_file.to_string();
        node_id = typecheck_next_file(
            &mut tast,
            Some(dep),
            &mut sources,
            lib_file_str.clone(),
            code,
            node_id,
        )?;
    }

This means that if we have the following chain of imports a -> b -> c then a will be able to call c's functions. Worst, even parts of the dependency graph might have access to their neighboring parts if they are parsed after. I think we should keep a dependency graph on the side AND go through it as we go through different modules, so that we can keep track of what other modules are accessible to us.

I'm wondering if this could be done in the name resolution phase as explained in the previous section (so basically I might be repeating the same stuff here).

Move to declaring stdlib modules in noname files!

#155

@mimoo mimoo added the medium label Jul 31, 2024
@mimoo mimoo mentioned this issue Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant