From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gnu.wildebeest.org (wildebeest.demon.nl [212.238.236.112]) by sourceware.org (Postfix) with ESMTPS id 8ED3D385382B for ; Wed, 4 Aug 2021 23:58:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8ED3D385382B Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from reform (deer0x02.wildebeest.org [172.31.17.132]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 0037630291A9 for ; Thu, 5 Aug 2021 01:58:32 +0200 (CEST) Received: by reform (Postfix, from userid 1000) id 45C6F2E80F32; Thu, 5 Aug 2021 01:58:32 +0200 (CEST) Date: Thu, 5 Aug 2021 01:58:32 +0200 From: Mark Wielaard To: gcc-rust@gcc.gnu.org Subject: Modifying self Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-rust@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: gcc-rust mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 04 Aug 2021 23:58:38 -0000 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