feat: add is_mutable_raw_ptr and as_raw_ptr to hir::Type#21835
Open
mehmet-ylcnky wants to merge 1 commit intorust-lang:masterfrom
Open
feat: add is_mutable_raw_ptr and as_raw_ptr to hir::Type#21835mehmet-ylcnky wants to merge 1 commit intorust-lang:masterfrom
mehmet-ylcnky wants to merge 1 commit intorust-lang:masterfrom
Conversation
b3ada01 to
afaf10e
Compare
ChayimFriedman2
requested changes
Mar 18, 2026
Author
|
Thanks for the review! Removed the tests and added comments noting these methods are used outside rust-analyzer. |
ChayimFriedman2
approved these changes
Mar 18, 2026
94a2f32 to
30d15d8
Compare
Contributor
|
You need to rebase so CI can pass, then I'll merge this. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add
is_mutable_raw_ptr()andas_raw_ptr()methods tohir::Type, closing an API gap where raw pointer mutability (*mutvs*const) was not queryable through the public API.Problem
hir::Typeprovidesis_raw_ptr()to check if a type is a raw pointer, but there is no way to distinguish*mut Tfrom*const T. This is asymmetric with references, where bothis_mutable_reference()andas_reference() -> Option<(Type, Mutability)>exist:The internal representation (
TyKind::RawPtr(Ty, Mutability)) already carries the mutability, butType.tyispub(crate)so external consumers cannot access it. The only workaround was parsing the display string:This feature was asked about on StackOverflow (How to determine raw pointer mutability from ra_ap_hir::Type?), where a rust-analyzer team member confirmed this is a gap in the API and encouraged contributing the fix.
Solution Proposal
Add two methods to
hir::Type, following the exact same pattern as their reference counterparts:Test
Snapshot test covers all five type categories side by side, showing the symmetry between the raw pointer and reference APIs:
AI tools were used in the development of this change.