Skip to content

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
mehmet-ylcnky:feat/raw-ptr-mutability-api
Open

feat: add is_mutable_raw_ptr and as_raw_ptr to hir::Type#21835
mehmet-ylcnky wants to merge 1 commit intorust-lang:masterfrom
mehmet-ylcnky:feat/raw-ptr-mutability-api

Conversation

@mehmet-ylcnky
Copy link

Summary

Add is_mutable_raw_ptr() and as_raw_ptr() methods to hir::Type, closing an API gap where raw pointer mutability (*mut vs *const) was not queryable through the public API.

Problem

hir::Type provides is_raw_ptr() to check if a type is a raw pointer, but there is no way to distinguish *mut T from *const T. This is asymmetric with references, where both is_mutable_reference() and as_reference() -> Option<(Type, Mutability)> exist:

// References — full mutability API:
pub fn is_reference(&self) -> bool;
pub fn is_mutable_reference(&self) -> bool;
pub fn as_reference(&self) -> Option<(Type, Mutability)>;

// Raw pointers — mutability NOT queryable before this PR:
pub fn is_raw_ptr(&self) -> bool;
pub fn remove_raw_ptr(&self) -> Option<Type>;  // discards mutability

The internal representation (TyKind::RawPtr(Ty, Mutability)) already carries the mutability, but Type.ty is pub(crate) so external consumers cannot access it. The only workaround was parsing the display string:

let is_mut = ty.is_raw_ptr() && ty.display(db).to_string().starts_with("*mut ");

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:

pub fn is_mutable_raw_ptr(&self) -> bool {
    matches!(self.ty.kind(), TyKind::RawPtr(.., Mutability::Mut))
}

pub fn as_raw_ptr(&self) -> Option<(Type<'db>, Mutability)> {
    let TyKind::RawPtr(ty, m) = self.ty.kind() else { return None };
    let m = Mutability::from_mutable(matches!(m, Mutability::Mut));
    Some((self.derived(ty), m))
}

Test

Snapshot test covers all five type categories side by side, showing the symmetry between the raw pointer and reference APIs:

a (*const u32): is_mutable_raw_ptr: false, as_raw_ptr: Some(Shared)
b (*mut u32):   is_mutable_raw_ptr: true,  as_raw_ptr: Some(Mut)
c (&u32):       is_mutable_reference: false, as_reference: Some(Shared)
d (&mut u32):   is_mutable_reference: true,  as_reference: Some(Mut)
e (u32):        all false/None

AI tools were used in the development of this change.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 17, 2026
@mehmet-ylcnky mehmet-ylcnky force-pushed the feat/raw-ptr-mutability-api branch from b3ada01 to afaf10e Compare March 17, 2026 21:52
Copy link
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

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

We don't add tests for API methods.

Also, please add a comment that these methods are used outside r-a, to prevent removing them.

View changes since this review

@mehmet-ylcnky
Copy link
Author

Thanks for the review! Removed the tests and added comments noting these methods are used outside rust-analyzer.

Copy link
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

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

@mehmet-ylcnky mehmet-ylcnky force-pushed the feat/raw-ptr-mutability-api branch from 94a2f32 to 30d15d8 Compare March 18, 2026 15:31
@ChayimFriedman2
Copy link
Contributor

You need to rebase so CI can pass, then I'll merge this.

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.

3 participants