0

I have gotten a PatType from an FnArg::Typed(my_pat_type) which I got by extracting the inputs from a function signature in a procedural macro.

My problem is that I need a concise way to check if my PatType is in any way mutable or not, without going into the specific information.

I tried checking the documentation for a method, or something to tell me if a PatType is mutable, but I instead found the pat and ty fields, which are themselves enum's that I don't really want to match against to get the specific information, and check mutability there.

I tried asking ChatGPT but it gave me a huge mess of spaghetti code that does match against those enum's

whyrgola
  • 11
  • 1
  • 4
  • You cannot do that in general, because mutable references can be hidden inside types, and interior mutability also cannot be observed by macros. – Chayim Friedman Mar 28 '23 at 13:42
  • It's completely unclear to me what you want to check for. Would you consider any of these mutable? `mut x: i32`, `x: &mut i32`, `(x, mut y): (i32, i32)` – Sven Marnach Mar 28 '23 at 13:42
  • Yes, I would consider those mutable. – whyrgola Mar 28 '23 at 13:47
  • I want to check mutability to decide between generating either an `Fn` or `FnMut`. In other words if the argument wouldn't work in an `Fn` and would need an `FnMut`, I would consider it mutable. I don't care about interior mutability. – whyrgola Mar 28 '23 at 13:48
  • You still need to know when there is a mutable reference hidden in the type, so you cannot do that. – Chayim Friedman Mar 28 '23 at 13:54
  • Although how would a mutable reference be hidden in the type? Do you mean hard to find or not in the signature at all? – whyrgola Mar 28 '23 at 13:57
  • 1
    E.g. `struct Ty<'a> { v: &'a mut String }` Then you have a parameter of type `Ty` and you cannot know that it hides a mutable reference. – Chayim Friedman Mar 28 '23 at 14:05

0 Answers0