Skip to content

Simplify lowering #602

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
merged 18 commits into from
Sep 7, 2020
Merged

Simplify lowering #602

merged 18 commits into from
Sep 7, 2020

Conversation

detrumi
Copy link
Member

@detrumi detrumi commented Sep 4, 2020

  • Moved lowering functions that just take Env to a single Lower trait with associated type Lowered, to get rid of all the single-use traits. This retains .lower(env) chaining for the most important types.
  • Changed lowering functions with extra params to freestanding functions. These could in principle be handled by the same trait, by making the argument generic, but that would make it less clear, and having lowering functions that take extra parameters named differenlty is a good thing, so they stand out.
  • Same for lowering functions that don't take an environment. These often aren't chained anyways, and having names like lower_adt_repr makes the code clearer IMO.
  • Use macros to reduce boilerplate around LowerParameterMap (also removing some unused functions there) and lower_type_kind (renamed OpaqueTyDefn.identifier to OpaqueTyDefn.name to line things up better)
  • Re-use lookup_apply_type in ApplyTypeLookup by changing it slightly, to avoid duplicating map lookup code

@detrumi detrumi force-pushed the simplify-lowering branch 2 times, most recently from 7a30599 to 7c21b0b Compare September 5, 2020 10:37
@detrumi detrumi changed the title [WIP] Simplify lowering Simplify lowering Sep 5, 2020
@detrumi detrumi marked this pull request as ready for review September 5, 2020 15:41
Copy link
Member

@jackh726 jackh726 left a comment

Choose a reason for hiding this comment

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

Some nits. But I think I still would prefer the lower_* functions to just implement Lower but not use the environment.

@@ -86,7 +86,8 @@ struct AssociatedTyLookup {
addl_variable_kinds: Vec<chalk_ir::VariableKind<ChalkIr>>,
}

enum ApplyTypeLookup {
enum ApplyTypeLookup<'k> {
Param(&'k chalk_ir::WithKind<ChalkIr, BoundVar>),
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, so. I'm not super sure about this name, because I don't want it to be confused with the rustc Param type.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, not sure what else to go with, since BoundVar would be confusing as well.

Copy link
Member

Choose a reason for hiding this comment

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

I guess it ends up serving a similar purpose to rustc's Param, so if we can't think of a better name, Param is probably acceptable. Another option: Parameter. Feel free to take it or leave it.

Comment on lines +723 to +736
lower_param_map!(AdtDefn, None);
lower_param_map!(FnDefn, None);
lower_param_map!(ClosureDefn, None);
lower_param_map!(Impl, None);
lower_param_map!(AssocTyDefn, None);
lower_param_map!(AssocTyValue, None);
lower_param_map!(Clause, None);
lower_param_map!(
TraitDefn,
Some(chalk_ir::WithKind::new(
chalk_ir::VariableKind::Ty(TyKind::General),
Atom::from(SELF),
))
);
Copy link
Member

Choose a reason for hiding this comment

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

Beautiful.

@detrumi
Copy link
Member Author

detrumi commented Sep 7, 2020

But I think I still would prefer the lower_* functions to just implement Lower but not use the environment.

The problem is that some functions, like lower_fn_abi, are called before we even construct the empty environment.

I've tried having 2 Lower traits, one with environment and one without, but the one without is just less consistent, since it doesn't always return a LowerResult<_>, and I think mixing .lower() and .lower(env)? would be confusing.

@jackh726
Copy link
Member

jackh726 commented Sep 7, 2020

I don't think having two separate traits is all that bad. We're looking for the solution that best conveys what's going on in the lowering as a whole. Having one or two traits implemented for most things makes more sense that one trait for some and a bunch of functions for everything else. (I'm curious if we could refactor away the arguments in all of the functions, but I'm purposely that out from this PR, because I would like to land us somewhere that is definitely better than get us stuck on what ifs).

I think we can always just return a LowerResult, even if it's always Ok wrapped. For this bit of Chalk, readability is more important that really anything else. If lower() and lower(env) is too confusing (I don't think so), then we could name them differently.

Another thing worth thinking about is which of the functions must be called before the environment is constructed. Then, these could be special and everything else takes an env. (This somewhat depends on how much there are and such)

@detrumi
Copy link
Member Author

detrumi commented Sep 7, 2020

I don't think having two separate traits is all that bad

👍

I think we can always just return a LowerResult, even if it's always Ok wrapped

This is awkward when lowering vecs, so let's go with this instead, even if it's a bit inconsistent:

trait Lower {
    type Lowered;

    fn lower(&self) -> Self::Lowered;
}

Another thing worth thinking about is which of the functions must be called before the environment is constructed. Then, these could be special and everything else takes an env. (This somewhat depends on how much there are and such)

Turns out that the fn_def_abis part wasn't even needed, so this isn't a problem after all. Entry points like lower_goal are a bit trickier (because it ends with goal.lower(&env)), but maybe that's something to fix in another PR.

Copy link
Member

@jackh726 jackh726 left a comment

Choose a reason for hiding this comment

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

Just a couple more minor comments. But this looks great. Feel free to merge with/without comments addressed.

@@ -86,7 +86,8 @@ struct AssociatedTyLookup {
addl_variable_kinds: Vec<chalk_ir::VariableKind<ChalkIr>>,
}

enum ApplyTypeLookup {
enum ApplyTypeLookup<'k> {
Param(&'k chalk_ir::WithKind<ChalkIr, BoundVar>),
Copy link
Member

Choose a reason for hiding this comment

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

I guess it ends up serving a similar purpose to rustc's Param, so if we can't think of a better name, Param is probably acceptable. Another option: Parameter. Feel free to take it or leave it.

return Err(RustIrError::NotStruct(name.clone()));
.cast(interner)),
Err(_) => {
if let Some(id) = self.foreign_ty_ids.get(&name.str) {
Copy link
Member

Choose a reason for hiding this comment

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

So, how did this end up on this path (I would expect it to be returned in lookup_apply_type)? Is it just because these can't have generics?

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess so, since there's no foreign_ty_kinds. Possibly lookup_apply_type and lookup_generic_arg should just use their own lookups, but it's kind of awkward to have all these separate maps.

@detrumi
Copy link
Member Author

detrumi commented Sep 7, 2020

@bors r+

@jackh726
Copy link
Member

jackh726 commented Sep 7, 2020

@bors r=jackh726

@bors
Copy link
Contributor

bors commented Sep 7, 2020

📌 Commit b1c6c4e has been approved by jackh726

@detrumi
Copy link
Member Author

detrumi commented Sep 7, 2020

Wait, that didn't merge it? Huh, I don't really get how bors works

@detrumi detrumi merged commit 99ea62d into rust-lang:master Sep 7, 2020
@detrumi detrumi deleted the simplify-lowering branch September 7, 2020 19:23
@jackh726
Copy link
Member

jackh726 commented Sep 8, 2020

Wait, that didn't merge it? Huh, I don't really get how bors works

I think there's some bors weirdness happening right now.

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.

3 participants