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

Implement Entity HIR structure #134

Merged
merged 66 commits into from
May 5, 2024
Merged

Implement Entity HIR structure #134

merged 66 commits into from
May 5, 2024

Conversation

DropDemBits
Copy link
Owner

Inspired by rust-analyzer's current entity HIR structure and its effort for a HIR 2.0, this reifies HIR items and bodies as distinct entity handles. This leverages salsa-2022's tracked entity infrastructure so that we don't have to intern everything like RA currently does.

The important thing about this implementation is that it prefers to be lazy (i.e. we only do what we need to do right now). While this does negatively affect the one-shot case of compiling straight to bytecode, we're preferentially improving the IDE experience since it's a nice-to-have for one-shot compilation being fast, while the IDE side of things needs to be fast in order to be responsive and have a nice experience.

However, laziness does not necessarily imply dropping all but the most relevant information (i.e. preferentially recomputing), since RA has had issues with biasing towards recomputing instead of recording results (for example, while trying to replace macros with their expansions). We should bias towards recording and memoizing results so that later micro-passes can leverage these results.

Main entry point is via hir::def::root_module(Package), which gives a module to start discovering the rest of the HIR structure.

Notes on salsa::tracked entities vs. arena allocations

HIR items are tracked entities whilst body expressions and statements are backed by arena allocations as opposed to both being either arena-backed or tracked entities since:

  • Bodies are more likely to be changed than items, so will have to be recomputed often
  • Tracking still has a cost, and tracking individual expressions and statements is too much overhead since again, they change often

@DropDemBits DropDemBits force-pushed the hir-entities branch 2 times, most recently from 3816f36 to 9918dc1 Compare June 12, 2023 08:11
toc-hir rexports toc-source-graph types, so don't need to directly
depend on that
BindDecl is just a bundle of BindItems, and it's not really important to know
where the original BindDecl is beyond the validation stage.
Only deals with modules right now, since they're the top level item.
`UnstableSemanticLoc` is useful for referring to nodes that aren't in
`AstLocations` (e.g. `ast::Expr` and `ast::Stmt`, and `SemanticNodePtr`
for just referring to a syntax range in a semantic file)
It's really just a kind of topological ordering,
since all of the parents come before all of the children
Will probably be turned into proper lowering structure later
Makes it easier to refer to those locations stably, for better
incrementality
While this means that we may drop type-check errors inside of modules,
this comes at the advantage of being able to move items while still
maintaining id stability
Better to be able to refer to things by just like `hir::Item`
instead of `hir::item::Item`
Is atually in normal topological order
For tracking what tracked functions get re-executed.
Currently we don't have any tracked functions downstream of item lookup,
which matters since initial item lookup is relatively unstable as it's
closest to the AST layer. So for now, logging is unused.

Also fixed a typo and updated a comment.
Wanting to display what item a body is associated with,
and we might as well use the same format.
Safe to send around the location itself, since it only manifests
the actual syntax node (which is `!Send + !Sync`) when calling `to_node`
It also impls `Copy`, so we can just do `*self`
We aren't going to be reusing the existing HIR structs and
instead have copied the structs into `toc-hir-def`.
- Put stmt
- Assign stmt
- Name expr
- Literal expr
`ast::ConstVarDecl` unfortunately isn't specific enough of an `ast::Item`,
since each `ast::ConstVarDeclName` is considered an item in the entity HIR.
Since the underlying erased location doesn't change when going to a
more general syntax node repr (e.g. `ast::TypeDecl` into `ast::Item`),
it's fine to just change the target wrapper node type.

We use associated functions instead of implementing `From` directly
since that overlaps the `impl From<T> for T` in `core`.
This points to a specific instance of a `SourceFile` which is part
of a given `Package`, as a `SourceFile` may be part of certain
packages.

This permits parent item lookups since they eventually need to know
what package the item is a part of in order to find the starting
place to find nodes from.
Allows for fallible projections of syntax nodes, useful for when
the target node might not always be present.
Is not what we're looking for when we are considering if another file is reachable
Allows for doing parent lookup stuff
Allows deriving `PartialEq` and `Eq` on `expr::Literal`
@DropDemBits DropDemBits marked this pull request as ready for review May 5, 2024 22:05
@DropDemBits DropDemBits merged commit d580175 into main May 5, 2024
@DropDemBits DropDemBits deleted the hir-entities branch May 5, 2024 22:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant