Skip to content

feat: add is_mutable_raw_ptr and as_raw_ptr to hir::Type#21835

Merged
ChayimFriedman2 merged 1 commit intorust-lang:masterfrom
mehmet-ylcnky:feat/raw-ptr-mutability-api
Apr 13, 2026
Merged

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

Conversation

@mehmet-ylcnky
Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
Author

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

Copy link
Copy Markdown
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
Copy Markdown
Contributor

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

@ChayimFriedman2
Copy link
Copy Markdown
Contributor

@mehmet-ylcnky Do you want to rebase or do you want me to rebase for you (or maybe close the PR)?

@ChayimFriedman2 ChayimFriedman2 force-pushed the feat/raw-ptr-mutability-api branch from 30d15d8 to 1fd8d47 Compare April 13, 2026 21:16
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 13, 2026

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@ChayimFriedman2
Copy link
Copy Markdown
Contributor

@mehmet-ylcnky Given that you haven't answered, I rebased myself and merged this.

Thank you for your contribution!

@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue Apr 13, 2026
Merged via the queue into rust-lang:master with commit 5205b52 Apr 13, 2026
18 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 13, 2026
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.

3 participants