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

feat: Display the value of enum variant on hover #12966

Merged
merged 10 commits into from
Sep 20, 2022
Merged

Conversation

OleStrohm
Copy link
Contributor

fixes #12955

This PR adds const eval support for enums, as well as showing their value on hover, just as consts currently have.

I developed these two things at the same time, but I've realized now that they are separate. However since the hover is just a 10 line change (not including tests), I figured I may as well put them in the same PR. Though if you want them split up into "enum const eval support" and "show enum variant value on hover", I think that's reasonable too.

Since this adds const eval support for enums this also allows consts that reference enums to have their values computed now too.

The const evaluation itself is quite rudimentary, it doesn't keep track of the actual type of the enum, but it turns out that Rust doesn't actually either, and E::A as u8 is valid regardless of the repr on E.

It also doesn't really care about what expression the enum variant contains, it could for example be a string, despite that not being allowed, but I guess it's up to the cargo check diagnostics to inform of such issues anyway?

crates/hir-ty/src/infer.rs Show resolved Hide resolved
crates/hir/src/lib.rs Show resolved Hide resolved
crates/hir/src/lib.rs Outdated Show resolved Hide resolved
crates/hir-ty/src/consteval.rs Outdated Show resolved Hide resolved
@Veykril
Copy link
Member

Veykril commented Sep 20, 2022

Thanks!
@bors r+

@bors
Copy link
Contributor

bors commented Sep 20, 2022

📌 Commit f87ad8d has been approved by Veykril

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Sep 20, 2022

⌛ Testing commit f87ad8d with merge 817a6a8...

@bors
Copy link
Contributor

bors commented Sep 20, 2022

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing 817a6a8 to master...

@bors bors merged commit 817a6a8 into rust-lang:master Sep 20, 2022
@Veykril
Copy link
Member

Veykril commented Sep 20, 2022

Note for the changelog, this PR is two features really, const eval support for enum variants (enabling hover to work better for enum variant constants), but imo more important, we now do inference in enum variant bodies

bors added a commit that referenced this pull request Sep 20, 2022
Properly set the enum variant body type from the repr attribute

Follow up for #12966, fixes some type inference problems
bors added a commit that referenced this pull request Sep 24, 2022
Fix diagnostics not working in enum variant bodies

cc #12966
@lnicola
Copy link
Member

lnicola commented Sep 26, 2022

image

Did I break this? 😅

image

@Veykril
Copy link
Member

Veykril commented Sep 26, 2022

That is not a valid expression, the variant is missing a cast. Though clearly we shouldn't leak the HIR representation on hover if we fail computation 😬

@lnicola
Copy link
Member

lnicola commented Sep 26, 2022

Weird, I actually checked that it compiled, but now it doesn't. Maybe I didn't save..

And you're right, of course, it does need a cast.

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.

Display the value of enum variants on hover
4 participants