Skip to content

Refactor Persistent_env to separate imports from bound names #1764

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

Merged

Conversation

lukemaurer
Copy link
Contributor

@lukemaurer lukemaurer commented Aug 23, 2023

Depends on #1753.

With the advent of parameters, we have reasons to refer to global module names besides those names being runtime global variables, or addressable values at all. In addition, we're going to want to load a .cmi but then parameterise it in different ways to get different signatures, each of which will be bound to a separate value.

In the end, then, instead of just pers_struct, we'll want three different record types, each with an associated cache:

  • import, corresponding directly to a .cmi file, keyed by Compilation_unit.Name.t
  • pers_name, corresponding to an import with parameters applied to it, keyed by a Compilation_unit.Name.t and some parameters
  • pers_struct, corresponding to a persistent name that is actually bound in the environment, with the same key as pers_name

For this PR, I'm leaving out the second cache as nothing about it is relevant yet, but the split is disruptive enough that even the two are worthwhile. Note that pers_struct is currently still keyed by Compilation_unit.Name.t since we don't yet have the datatypes for parameterised names.

Besides the internal reorganisation of Persistent_env, this also changes the API between it and Env, offloading some of the work done by the callback, read_sign_of_cmi. Significantly, read_sign_of_cmi is no longer called at all until something is going to be bound (i.e., become a pers_struct). This keeps the Env.t completely free of anything it shouldn't know about.

With the advent of parameters, we have reasons to refer to global module names
besides those names being runtime global variables, or addressable values at
all. In addition, we're going to want to load a .cmi but then parameterise it
in different ways to get different signatures, each of which will be bound to
a separate value.

In the end, then, instead of just `pers_struct`, we'll want three different
record types, each with an associated cache:

* `import`, corresponding directly to a .cmi file, keyed by
  `Compilation_unit.Name.t`
* `pers_name`, corresponding to an `import` with parameters applied to it, keyed
  by a `Compilation_unit.Name.t` and some parameters
* `pers_struct`, corresponding to a persistent name that is actually bound in
  the environment, with the same key as `pers_name`

For this PR, I'm leaving out the second cache as nothing about it is relevant
yet, but the split is disruptive enough that even the two are worthwhile. Note
that `pers_struct` is currently still keyed by `Compilation_unit.Name.t` since
we don't yet have the datatypes for parameterised names.

Besides the internal reorganisation of `Persistent_env`, this also changes the
API between it and `Env`, offloading some of the work done by the callback,
`read_sign_of_cmi`. Significantly, `read_sign_of_cmi` is no longer called _at
all_ until something is going to be bound (i.e., become a `pers_struct`). This
keeps the `Env.t` completely free of anything it shouldn't know about.
lukemaurer added a commit to lukemaurer/flambda-backend that referenced this pull request Aug 31, 2023
I've disabled the check on the output, since currently we get the wrong error
message (and the one we get is confusing). This will be much easier to fix when
PR ocaml-flambda#1764 is fixed to avoid unhelpful checks on `.mli` files that are loaded
directly rather than as part of name resolution.
lukemaurer added a commit to lukemaurer/flambda-backend that referenced this pull request Aug 31, 2023
I've disabled the check on the output, since currently we get the wrong error
message (and the one we get is confusing). This will be much easier to fix when
PR ocaml-flambda#1764 is fixed to avoid unhelpful checks on `.mli` files that are loaded
directly rather than as part of name resolution.
This eliminates a silly bit of back-and-forth, and `Persistent_env` is
perfectly capable of knowing the shape.
This lets us fulfil the intent that we don't call the callback for
things that aren't actually bound in the environment. That way we don't
do awkward things like calculate a binding for something that doesn't
actually have a binding.
@lukemaurer lukemaurer added the parameterized-libs PRs needed for parameterized libraries label Sep 19, 2023
lukemaurer added a commit to lukemaurer/flambda-backend that referenced this pull request Sep 27, 2023
I've disabled the check on the output, since currently we get the wrong error
message (and the one we get is confusing). This will be much easier to fix when
PR ocaml-flambda#1764 is fixed to avoid unhelpful checks on `.mli` files that are loaded
directly rather than as part of name resolution.
lukemaurer added a commit to lukemaurer/flambda-backend that referenced this pull request Oct 19, 2023
Displaying the correct error message will be easier again after ocaml-flambda#1764
@lukemaurer lukemaurer changed the base branch from main to lmaurer/cu-name-is-cmi October 19, 2023 16:47
@lukemaurer lukemaurer marked this pull request as ready for review November 17, 2023 15:59
Copy link
Collaborator

@lpw25 lpw25 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small suggestions, but this looks good to me.

lukemaurer added a commit that referenced this pull request Dec 8, 2023
* Add `-as-parameter` option

This allows compiling an .mli as a _parameter module_ rather than a normal
compilation unit. A parameter module defines a module _type_ rather than a
module, so it cannot be referred to directly from another module. A forthcoming
PR will add the `-parameter P` option which adds the parameter module `P` as a
parameter to the current module, which then allows references to `P` in the
module. Further PRs will deal with how to actually use a module that takes
parameters.

For the moment, `-parameter` is unimplemented, so any reference to a parameter
module is an error.

* Add checks for misuse of `-as-parameter`

* Raise error on combination of `-as-parameter` and `-for-pack`

* Add test for check for compiling `.ml` of parameter `.mli`

I've disabled the check on the output, since currently we get the wrong error
message (and the one we get is confusing). This will be much easier to fix when
PR #1764 is fixed to avoid unhelpful checks on `.mli` files that are loaded
directly rather than as part of name resolution.

* Add test of check for `-as-parameter` on implementation

* Implement `register_parameter_import` and `is_registered_parameter_import`

* Code review

* Code review

* Fix error message and re-enable test
Copy link
Collaborator

@lpw25 lpw25 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small additional point

lukemaurer added 2 commits May 1, 2024 18:22
With module information cached at multiple levels, we need to make sure that the
visibility check happens no matter which cache is hit.
@lpw25 lpw25 merged commit b641a35 into ocaml-flambda:lmaurer/cu-name-is-cmi May 15, 2024
17 checks passed
lukemaurer added a commit to lukemaurer/flambda-backend that referenced this pull request Oct 23, 2024
* Add `-as-parameter` option

This allows compiling an .mli as a _parameter module_ rather than a normal
compilation unit. A parameter module defines a module _type_ rather than a
module, so it cannot be referred to directly from another module. A forthcoming
PR will add the `-parameter P` option which adds the parameter module `P` as a
parameter to the current module, which then allows references to `P` in the
module. Further PRs will deal with how to actually use a module that takes
parameters.

For the moment, `-parameter` is unimplemented, so any reference to a parameter
module is an error.

* Add checks for misuse of `-as-parameter`

* Raise error on combination of `-as-parameter` and `-for-pack`

* Add test for check for compiling `.ml` of parameter `.mli`

I've disabled the check on the output, since currently we get the wrong error
message (and the one we get is confusing). This will be much easier to fix when
PR ocaml-flambda#1764 is fixed to avoid unhelpful checks on `.mli` files that are loaded
directly rather than as part of name resolution.

* Add test of check for `-as-parameter` on implementation

* Implement `register_parameter_import` and `is_registered_parameter_import`

* Code review

* Code review

* Fix error message and re-enable test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parameterized-libs PRs needed for parameterized libraries
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants