From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by sourceware.org (Postfix) with ESMTPS id 20635383B42B for ; Fri, 6 Aug 2021 14:56:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 20635383B42B Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-wr1-x429.google.com with SMTP id m12so11433344wru.12 for ; Fri, 06 Aug 2021 07:56:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=to:references:from:subject:message-id:date:user-agent:mime-version :in-reply-to; bh=+j46KUgm2zhvhbvcFAfLg4pm4bVFFD7+0f+CHgu8p1A=; b=ZulF/LpEC0KK3D8/pvRQieZ2FdIIfTQSt87E/ek+iw+8Aue0qoVoGjCqU6SfPZAw/v kvdCCajkBCNR/HSlXyzh7xuc9Zd33ACZZp+mLl/7kQYgMDTJFLeQQ9vYzPoFB4rrd6rN /HTGL0fyfGKzpTluPaQd++bZn7NyzYWKYSQnRytkz3dDSbkFCgcKIRbCrLzVzyPc6Dke 9ttinG7RINlhMhsxHX1RczidXaXHbl5FprMdLWIV4OyStMdJxX29aKSHnX6TqygRTjEY qiGaaJ2CTJBWVsaPTH9rr0cfP4BUEkyCJY67OpkC0vKSBqoUctSecsxj11WeSrmPXbSC 85qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:references:from:subject:message-id:date :user-agent:mime-version:in-reply-to; bh=+j46KUgm2zhvhbvcFAfLg4pm4bVFFD7+0f+CHgu8p1A=; b=Qr0WA3KJI/nmPm7DQ3mdFG/ISpuAI5wnDscWvlH6qSZbDIncGwDMaPZxKVaYNn4exj USVbGQoKtqlg4OmpsmCWzvQhZMELz2wkC/w6gDiKuf2NW6oMsiCBdPR87e3LyrBbUZ99 NwUF5JjXxVtfjZ54CMAYzSwpHD0AIKqT7M0uPZK+ZGu5VUG83OYbkfZYLAAZIEw8ak69 s/pm5c6NoKl1rAJv3wriB+WKnnMI0qKMgLDmn2QFqPJvEWuCJ6QynkkZQS2gLNT+QIXq BTh76P+l7E1O3A+MNF+5RaT6/k/uiFyVjS3SbGC+rlFoq517bKmF0ksph/2kNdQNkprN uHXg== X-Gm-Message-State: AOAM5307e4kEYu0yygd9g3kMp3Yx0wSBeMtCjmlEx5HN3dNnnbcFYVnX iPDNt4rP+9a5SZpiGR6NpuAHA1dcYlUyOg== X-Google-Smtp-Source: ABdhPJz7hMu9UqGjHGjP7sLchYJNow0mNXwCgu6avnZsmegur4aKPHMwno4dC/NcFXgnFwu9KK1e3A== X-Received: by 2002:adf:fb8d:: with SMTP id a13mr11663062wrr.164.1628261813971; Fri, 06 Aug 2021 07:56:53 -0700 (PDT) Received: from [192.168.0.40] ([86.14.124.218]) by smtp.gmail.com with ESMTPSA id u23sm12043591wmc.24.2021.08.06.07.56.53 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 06 Aug 2021 07:56:53 -0700 (PDT) To: gcc-rust@gcc.gnu.org References: From: Philip Herron Subject: Re: Modifying self Message-ID: <8b7b4048-d5e3-8fce-c9ba-149994edbe78@embecosm.com> Date: Fri, 6 Aug 2021 15:56:52 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="dsuUBtxn2zZjRYA6gytR0WbxEAMFT0SKv" X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, 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: Fri, 06 Aug 2021 14:56:56 -0000 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --dsuUBtxn2zZjRYA6gytR0WbxEAMFT0SKv Content-Type: multipart/mixed; boundary="V9Id43Rfgtb33UqXtLkHJ2DIsNdVsh7uZ"; protected-headers="v1" From: Philip Herron To: gcc-rust@gcc.gnu.org Message-ID: <8b7b4048-d5e3-8fce-c9ba-149994edbe78@embecosm.com> Subject: Re: Modifying self References: In-Reply-To: --V9Id43Rfgtb33UqXtLkHJ2DIsNdVsh7uZ Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Language: en-US 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 -=3D 1; > self.l > } > } > > fn main () > { > let mut h =3D H { l: 11 };=20 > let eleven =3D h.l;=20 > let ten =3D h.p ();=20 > if ten + 1 !=3D eleven { unsafe { abort (); } }=20 > let h2 =3D H { l: ten };=20 > if h.l !=3D h2.l { unsafe { abort (); } }=20 > } > > This doesn't currently compile: > > $ gcc/gccrs -Bgcc -g p.rs=20 > p.rs:12:5: error: invalid left-hand side of assignment > 12 | self.l -=3D 1; > | ^ > > But this isn't too hard to solve: > > diff --git a/gcc/rust/resolve/rust-ast-verify-assignee.h b/gcc/rust/res= olve/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: > } > } > =20 > + void visit (AST::PathInExpression &path) override > + { > + /* XXX do we need to check self is mutable? How? */ > + if (path.as_string () =3D=3D "self") > + ok =3D true; > + } > + > private: > VerifyAsignee (NodeId parent) : ResolverBase (parent), ok (false) {}= > =20 > 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 --V9Id43Rfgtb33UqXtLkHJ2DIsNdVsh7uZ-- --dsuUBtxn2zZjRYA6gytR0WbxEAMFT0SKv Content-Type: application/pgp-signature; name="OpenPGP_signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="OpenPGP_signature" -----BEGIN PGP SIGNATURE----- wsD5BAABCAAjFiEET83ATZOayqRjyL0Cr7gxHEFOdpkFAmENTbQFAwAAAAAACgkQr7gxHEFOdpkt sgv9G/zwuTo6HAd84kDBQOFbq9Eyi/GDC+BqsTa+A3xPQn9G/dMyLl2VqZEMVfpvoPGFSHcSXES4 bFZt4tzk59j14ig4wm6f8WwkDgKp6+ktUYiB11fWKHUSf20xnP4XSAk/KCgRVhIBMa+nQxrz1jvy q1GE7ANQ81LbxTaMx36XXETSm/J5qMMrUlcBFt1BnkTbY82HQzpgBSRbrRzkVOXu6lZiR0ZV4cj8 rOuxU3guAjFdyqixd95DvRhH5yhoR3EAP2Je37r3lNPWmYttIFb+ai78kZoMzAVmuueSg6SdGhED Ivlr4NXONNjnhM6Scoqvwq6B2B/RBs1AoNRAjdiDJrV9/sp9kZBLWtwiQAv2t4839K1FaH0iFeVY wcBLitqwGgTnEG0XjoskJQ7SLhZNqt9rh6uKZQTDNxd0b8wF2ab8Gw9Wo3Tt+N5x1jzeSdRBrjn7 d1LmU2DogPEtElN3PHG11ZoowxpaWL9A3nO2k94q2kJq1kkPCJgu0dE6N0jx =XOT0 -----END PGP SIGNATURE----- --dsuUBtxn2zZjRYA6gytR0WbxEAMFT0SKv--