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

Create const blocks on the fly during typeck instead of waiting until writeback. #125806

Closed
wants to merge 2 commits into from

Conversation

oli-obk
Copy link
Contributor

@oli-obk oli-obk commented May 31, 2024

This allows us to spawn a nested FnCtxt, creating a barrier that control flow statements cannot cross.

fixes #125670

The issues were caused by #124650 where I failed to account for the lack of changing scopes for const blocks

… writeback.

This allows us to spawn a nested FnCtxt, creating a barrier that control flow statements cannot cross.
@rustbot
Copy link
Collaborator

rustbot commented May 31, 2024

r? @fee1-dead

rustbot has assigned @fee1-dead.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 31, 2024
Comment on lines -906 to +912
let encl_body_owner_id = self.tcx.hir().enclosing_body_owner(expr.hir_id);

// If this didn't hold, we would not have to report an error in
// the first place.
assert_ne!(encl_item_id.def_id, encl_body_owner_id);
assert_ne!(encl_item_id.def_id, self.body_id);

let encl_body = self.tcx.hir().body_owned_by(encl_body_owner_id);

err.encl_body_span = Some(encl_body.value.span);
err.encl_body_span = Some(self.tcx.def_span(self.body_id));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change is because enclosing_body_owner does not yield const blocks anymore. I'm addressing this separately to remove this footgun, but the code here could have been written simpler anyway, so I just did that.

Copy link
Member

Choose a reason for hiding this comment

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

enclosing_body_owner being out of sync is somewhat sketch 💀 why doesn't it? it should be as much of a body as a closure is, for example.

Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

I think this change definitely improves things, but I'm still somewhat skeptical that we can't improve it more... Thoughts?

@@ -76,7 +76,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
ExprKind::ConstBlock(c) => {
self.has_inline_consts = true;
hir::ExprKind::ConstBlock(self.lower_expr(c))
self.with_new_scopes(e.span, |this| {
let coroutine_kind = this.coroutine_kind.take();
Copy link
Member

Choose a reason for hiding this comment

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

Kind of strange that coroutine_kind is not handled in with_new_scopes. Is there some other with_body_*-like function that does the coroutine kind handling too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have been looking into this in a branch, but didn't want to add more stuff to this PR.

we can get it all working by merging with_new_scopes into lower_body, but it needs some managing of the right spans to maintain diagnostic quality.

Comment on lines -906 to +912
let encl_body_owner_id = self.tcx.hir().enclosing_body_owner(expr.hir_id);

// If this didn't hold, we would not have to report an error in
// the first place.
assert_ne!(encl_item_id.def_id, encl_body_owner_id);
assert_ne!(encl_item_id.def_id, self.body_id);

let encl_body = self.tcx.hir().body_owned_by(encl_body_owner_id);

err.encl_body_span = Some(encl_body.value.span);
err.encl_body_span = Some(self.tcx.def_span(self.body_id));
Copy link
Member

Choose a reason for hiding this comment

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

enclosing_body_owner being out of sync is somewhat sketch 💀 why doesn't it? it should be as much of a body as a closure is, for example.

Comment on lines +1466 to +1468
let feed = self.tcx().create_def(self.body_id, kw::Empty, DefKind::InlineConst);
feed.def_span(span);
feed.local_def_id_to_hir_id(hir_id);
Copy link
Member

Choose a reason for hiding this comment

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

I feel like const blocks should be first-class nested bodies (liek closures) all the way from the AST-level (i.e. having a def id, being treated as a hir owner, etc) instead of having to synthesize a def-id here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that's what they used to be (before #124650), but it meant we couldn't feed the type_of query. Turns out I couldn't do that anyway, so now I think I should probably just revert #124650 and try again from scratch

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps yeah

Copy link
Contributor Author

Choose a reason for hiding this comment

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

though we'll def need to lazily create the DefId of anon consts of method calls' const generic params, so that we can feed type_of. That's why I'm trying to figure out how to do this better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

right now we are breaking query system invariants and are feeding anon const type_of from typeck, even though typeck did not create the DefId.

Copy link
Member

Choose a reason for hiding this comment

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

Also, why do we need to feed the type_of query at all?

The type_of for a const block should operate like a type_of for a closure -- for closures, it calls typeck_results on the parent and extracts it from the expr_ty:

Node::Expr(&Expr { kind: ExprKind::Closure { .. }, .. }) => {
tcx.typeck(def_id).node_type(hir_id)
}

Copy link
Member

Choose a reason for hiding this comment

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

though we'll def need to lazily create the DefId of anon consts of method calls' const generic params, so that we can feed type_of. That's why I'm trying to figure out how to do this better.

I don't understand why this needs to be entangled with inline const exprs. Perhaps we should just distinguish these two rather than treating them the same.

@oli-obk
Copy link
Contributor Author

oli-obk commented May 31, 2024

Just gonna revert #124650 and start over

@oli-obk oli-obk closed this May 31, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jun 3, 2024
…r-errors

Revert: create const block bodies in typeck via query feeding

as per the discussion in rust-lang#125806 (comment)

It was a mistake to try to shoehorn const blocks and some specific anon consts into the same box and feed them during typeck. It turned out not simplifying anything (my hope was that we could feed `type_of` to start avoiding the huge HIR matcher, but that didn't work out), but instead making a few things more fragile.

reverts the const-block-specific parts of rust-lang#124650

`@bors` rollup=never had a small perf impact previously

fixes rust-lang#125846

r? `@compiler-errors`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jun 3, 2024
…r-errors

Revert: create const block bodies in typeck via query feeding

as per the discussion in rust-lang#125806 (comment)

It was a mistake to try to shoehorn const blocks and some specific anon consts into the same box and feed them during typeck. It turned out not simplifying anything (my hope was that we could feed `type_of` to start avoiding the huge HIR matcher, but that didn't work out), but instead making a few things more fragile.

reverts the const-block-specific parts of rust-lang#124650

``@bors`` rollup=never had a small perf impact previously

fixes rust-lang#125846

r? ``@compiler-errors``
fmease added a commit to fmease/rust that referenced this pull request Jun 4, 2024
…r-errors

Revert: create const block bodies in typeck via query feeding

as per the discussion in rust-lang#125806 (comment)

It was a mistake to try to shoehorn const blocks and some specific anon consts into the same box and feed them during typeck. It turned out not simplifying anything (my hope was that we could feed `type_of` to start avoiding the huge HIR matcher, but that didn't work out), but instead making a few things more fragile.

reverts the const-block-specific parts of rust-lang#124650

```@bors``` rollup=never had a small perf impact previously

fixes rust-lang#125846

r? ```@compiler-errors```
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 6, 2024
…errors

Revert: create const block bodies in typeck via query feeding

as per the discussion in rust-lang#125806 (comment)

It was a mistake to try to shoehorn const blocks and some specific anon consts into the same box and feed them during typeck. It turned out not simplifying anything (my hope was that we could feed `type_of` to start avoiding the huge HIR matcher, but that didn't work out), but instead making a few things more fragile.

reverts the const-block-specific parts of rust-lang#124650

`@bors` rollup=never had a small perf impact previously

fixes rust-lang#125846

r? `@compiler-errors`
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 7, 2024
…errors

Revert: create const block bodies in typeck via query feeding

as per the discussion in rust-lang#125806 (comment)

It was a mistake to try to shoehorn const blocks and some specific anon consts into the same box and feed them during typeck. It turned out not simplifying anything (my hope was that we could feed `type_of` to start avoiding the huge HIR matcher, but that didn't work out), but instead making a few things more fragile.

reverts the const-block-specific parts of rust-lang#124650

`@bors` rollup=never had a small perf impact previously

fixes rust-lang#125846

r? `@compiler-errors`
flip1995 pushed a commit to flip1995/rust-clippy that referenced this pull request Jun 13, 2024
Revert: create const block bodies in typeck via query feeding

as per the discussion in rust-lang/rust#125806 (comment)

It was a mistake to try to shoehorn const blocks and some specific anon consts into the same box and feed them during typeck. It turned out not simplifying anything (my hope was that we could feed `type_of` to start avoiding the huge HIR matcher, but that didn't work out), but instead making a few things more fragile.

reverts the const-block-specific parts of rust-lang/rust#124650

`@bors` rollup=never had a small perf impact previously

fixes rust-lang/rust#125846

r? `@compiler-errors`
flip1995 pushed a commit to flip1995/rust-clippy that referenced this pull request Jun 28, 2024
Revert: create const block bodies in typeck via query feeding

as per the discussion in rust-lang/rust#125806 (comment)

It was a mistake to try to shoehorn const blocks and some specific anon consts into the same box and feed them during typeck. It turned out not simplifying anything (my hope was that we could feed `type_of` to start avoiding the huge HIR matcher, but that didn't work out), but instead making a few things more fragile.

reverts the const-block-specific parts of rust-lang/rust#124650

`@bors` rollup=never had a small perf impact previously

fixes rust-lang/rust#125846

r? `@compiler-errors`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
Status: Done
Status: Done
Development

Successfully merging this pull request may close these issues.

ICE: mir build: scope: index out of bounds: the len is 0 but the index is 0
4 participants