public inbox for gcc-rust@gcc.gnu.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: gcc-rust@gcc.gnu.org
Subject: Modifying self
Date: Thu, 5 Aug 2021 01:58:32 +0200	[thread overview]
Message-ID: <YQspqOnQwv8dSw1C@wildebeest.org> (raw)

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


             reply	other threads:[~2021-08-04 23:58 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-04 23:58 Mark Wielaard [this message]
2021-08-06 14:56 ` Philip Herron

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YQspqOnQwv8dSw1C@wildebeest.org \
    --to=mark@klomp.org \
    --cc=gcc-rust@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).