public inbox for gcc-rust@gcc.gnu.org
 help / color / mirror / Atom feed
* Modifying self
@ 2021-08-04 23:58 Mark Wielaard
  2021-08-06 14:56 ` Philip Herron
  0 siblings, 1 reply; 2+ messages in thread
From: Mark Wielaard @ 2021-08-04 23:58 UTC (permalink / raw)
  To: gcc-rust

Hi,

I am trying to get this program working:

extern "C" { fn abort (); }

pub struct H
{
  l: u32,
}

impl H
{
  fn p (&mut self) -> u32
  {
    self.l -= 1;
    self.l
  }
}

fn main ()
{
  let mut h = H { l: 11 }; 
  let eleven = h.l; 
  let ten = h.p (); 
  if ten + 1 != eleven { unsafe { abort (); } } 
  let h2 = H { l: ten }; 
  if h.l != h2.l { unsafe { abort (); } } 
}

This doesn't currently compile:

$ gcc/gccrs -Bgcc -g p.rs 
p.rs:12:5: error: invalid left-hand side of assignment
   12 |     self.l -= 1;
      |     ^

But this isn't too hard to solve:

diff --git a/gcc/rust/resolve/rust-ast-verify-assignee.h b/gcc/rust/resolve/rust-ast-verify-assignee.h
index aed01196f81..1e8988d47df 100644
--- a/gcc/rust/resolve/rust-ast-verify-assignee.h
+++ b/gcc/rust/resolve/rust-ast-verify-assignee.h
@@ -75,6 +75,13 @@ public:
       }
   }
 
+  void visit (AST::PathInExpression &path) override
+  {
+    /* XXX do we need to check self is mutable? How?  */
+    if (path.as_string () == "self")
+      ok = true;
+  }
+
 private:
   VerifyAsignee (NodeId parent) : ResolverBase (parent), ok (false) {}
 
I am not sure whether this is a good implementation of the
VerifyAsignee visitor for a PathInExpression. What exactly is the goal
of this visitor?

But with the above simple fix, it compiles! And it actually seems to
mostly work. The implementation method is called, it gets its own
field, substracts the value and correctly returns it!

But... then it still aborts on the second check.  The method was
supposed to adjust the given self (H), but it is not given a mutable
reference, it gets a copy...

On irc Philip suggested this is probably
https://github.com/Rust-GCC/gccrs/issues/241

But I must admit I don't fully understand what really needs to be done
here or if the fact that this is a &mut self makes it different from a
&self argument.

Cheers,

Mark


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Modifying self
  2021-08-04 23:58 Modifying self Mark Wielaard
@ 2021-08-06 14:56 ` Philip Herron
  0 siblings, 0 replies; 2+ messages in thread
From: Philip Herron @ 2021-08-06 14:56 UTC (permalink / raw)
  To: gcc-rust


[-- Attachment #1.1: Type: text/plain, Size: 2992 bytes --]


On 05/08/2021 00:58, Mark Wielaard wrote:
> Hi,
>
> I am trying to get this program working:
>
> extern "C" { fn abort (); }
>
> pub struct H
> {
>   l: u32,
> }
>
> impl H
> {
>   fn p (&mut self) -> u32
>   {
>     self.l -= 1;
>     self.l
>   }
> }
>
> fn main ()
> {
>   let mut h = H { l: 11 }; 
>   let eleven = h.l; 
>   let ten = h.p (); 
>   if ten + 1 != eleven { unsafe { abort (); } } 
>   let h2 = H { l: ten }; 
>   if h.l != h2.l { unsafe { abort (); } } 
> }
>
> This doesn't currently compile:
>
> $ gcc/gccrs -Bgcc -g p.rs 
> p.rs:12:5: error: invalid left-hand side of assignment
>    12 |     self.l -= 1;
>       |     ^
>
> But this isn't too hard to solve:
>
> diff --git a/gcc/rust/resolve/rust-ast-verify-assignee.h b/gcc/rust/resolve/rust-ast-verify-assignee.h
> index aed01196f81..1e8988d47df 100644
> --- a/gcc/rust/resolve/rust-ast-verify-assignee.h
> +++ b/gcc/rust/resolve/rust-ast-verify-assignee.h
> @@ -75,6 +75,13 @@ public:
>        }
>    }
>  
> +  void visit (AST::PathInExpression &path) override
> +  {
> +    /* XXX do we need to check self is mutable? How?  */
> +    if (path.as_string () == "self")
> +      ok = true;
> +  }
> +
>  private:
>    VerifyAsignee (NodeId parent) : ResolverBase (parent), ok (false) {}
>  
> I am not sure whether this is a good implementation of the
> VerifyAsignee visitor for a PathInExpression. What exactly is the goal
> of this visitor?
>
> But with the above simple fix, it compiles! And it actually seems to
> mostly work. The implementation method is called, it gets its own
> field, substracts the value and correctly returns it!
>
> But... then it still aborts on the second check.  The method was
> supposed to adjust the given self (H), but it is not given a mutable
> reference, it gets a copy...
>
> On irc Philip suggested this is probably
> https://github.com/Rust-GCC/gccrs/issues/241
>
> But I must admit I don't fully understand what really needs to be done
> here or if the fact that this is a &mut self makes it different from a
> &self argument.
>
> Cheers,
>
> Mark
>
This is a fantastic test case. I think we should add this to the xfail
section. In terms of getting the mutable self working on methods, its a
little more complex since the compiler is very permissive here, where
the type system is meant to be able to coerce the reciever with a system
called autoderef which is not documented very well. This is something
that must be fixed as part of traits since you can end up with many
candidates for a path or method call and choosing the correct one is
very important to get right.

This missing feature is causing a deficiency in constant folding from
this issue: https://github.com/Rust-GCC/gccrs/issues/547

The verify lvalue looks good, this validation could really do with some
work down the line. I will reply to this thread when some movement is
made here on this.

--Phil



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 665 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-08-06 14:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-04 23:58 Modifying self Mark Wielaard
2021-08-06 14:56 ` Philip Herron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).