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

fix: Lower const params with a bad id #14932

Merged
merged 5 commits into from Jun 12, 2023
Merged

fix: Lower const params with a bad id #14932

merged 5 commits into from Jun 12, 2023

Conversation

HKalbasi
Copy link
Member

cc #7434

This PR adds an InTypeConstId which is a DefWithBodyId and lower const generic parameters into bodies using it, and evaluate them with the mir interpreter. I think this is the last unimplemented const generic feature relative to rustc stable.

But there is a problem: The id used in the InTypeConstId is the raw FileAstId, which changes frequently. So these ids and their bodies will be invalidated very frequently, which is bad for incremental analysis.

Due this problem, I disabled lowering for local crates (in library crate the id is stable since files won't be changed). This might be overreacting (const generic expressions are usually small, maybe it would be better enabled with bad performance than disabled) but it makes motivation for doing it in the correct way, and it splits the potential panic and breakages that usually comes with const generic PRs in two steps.

Other than the id, I think (at least I hope) other parts are in the right direction.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 30, 2023
@HKalbasi HKalbasi force-pushed the dev branch 2 times, most recently from c0ddcc9 to e238389 Compare May 31, 2023 22:33
crates/hir-def/src/hir/type_ref.rs Outdated Show resolved Hide resolved
crates/hir-def/src/hir/type_ref.rs Outdated Show resolved Hide resolved
crates/hir-def/src/lib.rs Outdated Show resolved Hide resolved
crates/hir-def/src/lib.rs Show resolved Hide resolved
crates/hir-expand/src/ast_id_map.rs Outdated Show resolved Hide resolved
@Veykril
Copy link
Member

Veykril commented Jun 1, 2023

I think before we try to tackle this issue we should take a step back and consider the possible ways to implement it with advantages and disadvantages in mind (maybe as a zulip discussion would make sense). This looks like it will have some architecture implications so it feels like we wanna get this right (or close to right) in an initial implementation. If I recall, this approach here is a different from the one that was mentioned in the issue itself right?

@HKalbasi HKalbasi force-pushed the dev branch 3 times, most recently from bee5565 to f6b57e1 Compare June 5, 2023 11:27
Copy link
Member

@flodiebold flodiebold left a comment

Choose a reason for hiding this comment

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

some drive-by comments

crates/hir-def/src/hir/type_ref.rs Outdated Show resolved Hide resolved
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct AnonymousConstId(InternId);
impl_intern_key!(AnonymousConstId);

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum TypeOwnerId {
ModuleId(ModuleId),
Copy link
Member

Choose a reason for hiding this comment

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

how can a module be a TypeOwner?

crates/hir-expand/src/ast_id_map.rs Outdated Show resolved Hide resolved
@@ -102,6 +102,10 @@ pub(crate) fn infer_query(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<Infer
},
});
}
DefWithBodyId::InTypeConstId(_) => {
// FIXME: We should know the expected type here.
Copy link
Member

Choose a reason for hiding this comment

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

yeah, but this is one of the hard parts, isn't it... IIRC there's a pretty hacky hack for this in rustc

Copy link
Member Author

Choose a reason for hiding this comment

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

🤔 My plan was to make a query on TypeOwnerId to lower all types that it owns, and record the types and other data for these consts in the result of that query. But it would be a massive change and can go wrong in multiple ways.

Another approach that just came to my mind is encoding the output type inside InTypeConstId. It is a bit weird but would work with a minimal change and wouldn't affect the stability of id very much.

crates/hir-def/src/lib.rs Show resolved Hide resolved
@HKalbasi
Copy link
Member Author

@bors r+

@bors
Copy link
Collaborator

bors commented Jun 12, 2023

📌 Commit a469578 has been approved by HKalbasi

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Jun 12, 2023

⌛ Testing commit a469578 with merge dcd3155...

@bors
Copy link
Collaborator

bors commented Jun 12, 2023

☀️ Test successful - checks-actions
Approved by: HKalbasi
Pushing dcd3155 to master...

@bors bors merged commit dcd3155 into rust-lang:master Jun 12, 2023
10 checks passed
@lnicola lnicola changed the title Lower const params with a bad id fix: Lower const params with a bad id Jun 12, 2023
bors added a commit that referenced this pull request Jun 12, 2023
internal: Give ConstBlockId and InTypeConstId named Location types

cc #14932
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants