From patchwork Wed Apr 5 14:03:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 79716 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp362126vqo; Wed, 5 Apr 2023 07:46:20 -0700 (PDT) X-Google-Smtp-Source: AKy350aCEHxA5wOf45NN3UxRlsa5Kmikdg8UyPL1Kxs70ByqlmnnNrQfM2M9PStblhCGHir1pk53 X-Received: by 2002:a50:ed16:0:b0:4ac:b6b2:1233 with SMTP id j22-20020a50ed16000000b004acb6b21233mr2044575eds.30.1680705980532; Wed, 05 Apr 2023 07:46:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680705980; cv=none; d=google.com; s=arc-20160816; b=UQ/pGc+P7NhvFVSi3GghYGELYuNj3rds3RddEzCDP6SHidUd/1OqhsSi2JnioL9+AC lBgevmnBDM7+omKkhc0tP4lFGok63VMwU/F8LuTfWwQlKvd1hRFACmpvgZUT38thbfPZ tP4P4NLk7JZqZ9Y6yvKDuo9Flix7Rbm0A97qKEdxyM5ZGNJmb9suqPXWcB6qccOvoSFj 6ZMFcOyS2UVi5iMObGgmqDVPsVwf3R16JKBDH6y99GGm+JmvmnUG62ekyJve84JuK3vl RxhZO6NgMrqNW4aMs1fmPCryZR6+jVovBcnixR3x/4kps/RH9ZGZgLzkRJ4VWgCSVTDh A7Sw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature:dmarc-filter :delivered-to; bh=1eRh257QnJUVYyQs1yxXT1PKV2j4Po5H5hnErHdy/Dg=; b=MUaQAoiFYAf9oth/SK2jpovQn3E84+1Z1YOqI1PCwOwAEI3JMGGqzsobWGs7KuHk+T Lnj1wfhpNALOOlMqZH89zww5dIb1svd+r8A9NgsZwhV4imQpatZFVcsDniiojFyNomV/ /9iNboVtvhh1EkCgTOBQhREUB9XxqIP3YCT2B7PKvYM5eE0XnleGpuo1bJNziXW7A/07 wmZbI5GGFkqCH8OL4ZqxFqZRzpUjcAh0tkls6vquHrOhrDDJfISmXdtaxGGz7AfCSR2J ZzU2fjqWKkRM+aWXpP/n8Nc3wPgHd4T2JUTtgRHPrmVSMs9m440SbzsmQzKDGMhOYcg1 jK/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=NxFIaQFD; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id d15-20020a50fb0f000000b005021f0d574bsi2723123edq.658.2023.04.05.07.46.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 07:46:20 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=NxFIaQFD; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8AF5A386E550 for ; Wed, 5 Apr 2023 14:16:38 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by sourceware.org (Postfix) with ESMTPS id A29633870896 for ; Wed, 5 Apr 2023 14:06:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A29633870896 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-wm1-x32c.google.com with SMTP id m8so10053028wmq.5 for ; Wed, 05 Apr 2023 07:06:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1680703567; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=1eRh257QnJUVYyQs1yxXT1PKV2j4Po5H5hnErHdy/Dg=; b=NxFIaQFDPjtiXZwwRuGVKVuw9f2FPS7w/16xoIv1M2OLNNK39sKj3ZQ/8UQ3o3G4tY UVwB9cMR6zAqKUo/AX/IFNpx0q8HUc9vCZjklxItzQLfs2PI4bcMxjxJqQZU5AbV1RnP FXumMk/S10+LH+Wl2aia6TgT01imMRe0JttQWHL4/YpuRnmT792SrV20sVElk6yQp2O3 EJktjtuXId4ULk7Cn+PVCynR14oRVYNb6XZXle+xDVAnEv2d/8rx7vFW8L0Ujrp2s2jP e8P8Xl3XkYfc4SCFbnrcC9mafMW9euTYP3wvvgga1bouQhx8L1+kgsJlb9FmPUr1VuMF 0h/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680703567; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=1eRh257QnJUVYyQs1yxXT1PKV2j4Po5H5hnErHdy/Dg=; b=4P1tCCfqmxVNOcrGDgs1yW2MQ2zzqt7cPfA4CP+9qtYRLh7/0FJBDu7t/xC1pgu5LG urqKKbLTjpiz34v/G0zEfw0zE11eLkNCHVt4lRqES0QoqOWAz9Y7SSsA/LJ74WttteL1 GrPtS4P6DtEetRdmq36sgZooy/zyOMru0iaNAQVLEbmcW6OUAh22n4XOQTUiAhkmlK34 z8fDJijxCEP18CwEYmw43bZkKK+uFJ0jByDrVDMfWiBmfPhsVn3XhR26kKGBSigSskXk xyHlRZBO1qIMfa5EmbLTphv0tFZsMF2EBS/qksPGMmsTQ0JPmw8jNu2ynsixeUc7MBFr kEqw== X-Gm-Message-State: AAQBX9cRUOS+aG/d4s+f0RfNWX51WKllbBuWUowvCNTvrCmMQgMwGSU6 bx0nb3Qo47ujsXPX+w6l7pU+4m3G+upac4DU7g== X-Received: by 2002:a05:600c:211a:b0:3ea:f73e:9d8a with SMTP id u26-20020a05600c211a00b003eaf73e9d8amr4915202wml.30.1680703566968; Wed, 05 Apr 2023 07:06:06 -0700 (PDT) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id ay8-20020a05600c1e0800b003edddae1068sm2330150wmb.9.2023.04.05.07.06.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 07:06:06 -0700 (PDT) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Philip Herron Subject: [committed 55/88] gccrs: Add general TypeBounds checks Date: Wed, 5 Apr 2023 16:03:39 +0200 Message-Id: <20230405140411.3016563-56-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230405140411.3016563-1-arthur.cohen@embecosm.com> References: <20230405140411.3016563-1-arthur.cohen@embecosm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-14.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: arthur.cohen@embecosm.com Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762347954613817358?= X-GMAIL-MSGID: =?utf-8?q?1762347954613817358?= From: Philip Herron Existing tests are updated to use libcore copy and clone implementation. Addresses #1725 Signed-off-by: Philip Herron gcc/rust/ChangeLog: * typecheck/rust-unify.cc (UnifyRules::go): ensure the bounds are checked gcc/testsuite/ChangeLog: * rust/compile/torture/intrinsics-4.rs: implement Copy trait * rust/compile/torture/intrinsics-5.rs: likewise * rust/execute/torture/atomic_load.rs: likewise * rust/execute/torture/atomic_store.rs: likewise * rust/bounds1.rs: New test. --- gcc/rust/typecheck/rust-unify.cc | 11 ++++ gcc/testsuite/rust/bounds1.rs | 19 ++++++ .../rust/compile/torture/intrinsics-4.rs | 63 +++++++++++++++++- .../rust/compile/torture/intrinsics-5.rs | 66 ++++++++++++++++++- .../rust/execute/torture/atomic_load.rs | 59 ++++++++++++++++- .../rust/execute/torture/atomic_store.rs | 59 ++++++++++++++++- 6 files changed, 271 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/rust/bounds1.rs diff --git a/gcc/rust/typecheck/rust-unify.cc b/gcc/rust/typecheck/rust-unify.cc index 072f76133bc..415ffcdc6ea 100644 --- a/gcc/rust/typecheck/rust-unify.cc +++ b/gcc/rust/typecheck/rust-unify.cc @@ -124,6 +124,17 @@ UnifyRules::go () rust_debug ("unify::go ltype={%s} rtype={%s}", ltype->debug_str ().c_str (), rtype->debug_str ().c_str ()); + // check bounds + if (ltype->num_specified_bounds () > 0) + { + if (!ltype->bounds_compatible (*rtype, locus, true)) + { + // already emitted an error + emit_error = false; + return new TyTy::ErrorType (0); + } + } + switch (ltype->get_kind ()) { case TyTy::INFER: diff --git a/gcc/testsuite/rust/bounds1.rs b/gcc/testsuite/rust/bounds1.rs new file mode 100644 index 00000000000..665836088be --- /dev/null +++ b/gcc/testsuite/rust/bounds1.rs @@ -0,0 +1,19 @@ +mod core { + mod ops { + #[lang = "add"] + pub trait Add { + type Output; + + fn add(self, rhs: Rhs) -> Self::Output; + } + } +} + +pub fn foo>(a: T) -> i32 { + // { dg-error "bounds not satisfied for f32 .Add. is not satisfied" "" { target *-*-* } .-1 } + a + a +} + +pub fn main() { + foo(123f32); +} diff --git a/gcc/testsuite/rust/compile/torture/intrinsics-4.rs b/gcc/testsuite/rust/compile/torture/intrinsics-4.rs index 243d4460089..4e09f1089b0 100644 --- a/gcc/testsuite/rust/compile/torture/intrinsics-4.rs +++ b/gcc/testsuite/rust/compile/torture/intrinsics-4.rs @@ -1,10 +1,67 @@ -trait Copy {} +#[lang = "sized"] +pub trait Sized {} + +#[lang = "clone"] +pub trait Clone: Sized { + fn clone(&self) -> Self; + + fn clone_from(&mut self, source: &Self) { + *self = source.clone() + } +} + +mod impls { + use super::Clone; + + macro_rules! impl_clone { + ($($t:ty)*) => { + $( + impl Clone for $t { + fn clone(&self) -> Self { + *self + } + } + )* + } + } + + impl_clone! { + usize u8 u16 u32 u64 // u128 + isize i8 i16 i32 i64 // i128 + f32 f64 + bool char + } +} + +#[lang = "copy"] +pub trait Copy: Clone { + // Empty. +} + +mod copy_impls { + use super::Copy; + + macro_rules! impl_copy { + ($($t:ty)*) => { + $( + impl Copy for $t {} + )* + } + } + + impl_copy! { + usize u8 u16 u32 u64 // u128 + isize i8 i16 i32 i64 // i128 + f32 f64 + bool char + } +} extern "rust-intrinsic" { pub fn atomic_store_seqcst(dst: *mut T, val: T); pub fn atomic_store_release(dst: *mut T, val: T); pub fn atomic_store_relaxed(dst: *mut T, val: T); - // pub fn atomic_store_unordered(dst: *mut T, val: T); + pub fn atomic_store_unordered(dst: *mut T, val: T); } fn main() { @@ -15,6 +72,6 @@ fn main() { atomic_store_seqcst(&mut dst, new_value); atomic_store_release(&mut dst, new_value); atomic_store_relaxed(&mut dst, new_value); - // atomic_store_unordered(&mut dst, new_value); + atomic_store_unordered(&mut dst, new_value); } } diff --git a/gcc/testsuite/rust/compile/torture/intrinsics-5.rs b/gcc/testsuite/rust/compile/torture/intrinsics-5.rs index 7fd84dcd635..ffad0bd3a85 100644 --- a/gcc/testsuite/rust/compile/torture/intrinsics-5.rs +++ b/gcc/testsuite/rust/compile/torture/intrinsics-5.rs @@ -1,4 +1,61 @@ -trait Copy {} +#[lang = "sized"] +pub trait Sized {} + +#[lang = "clone"] +pub trait Clone: Sized { + fn clone(&self) -> Self; + + fn clone_from(&mut self, source: &Self) { + *self = source.clone() + } +} + +mod impls { + use super::Clone; + + macro_rules! impl_clone { + ($($t:ty)*) => { + $( + impl Clone for $t { + fn clone(&self) -> Self { + *self + } + } + )* + } + } + + impl_clone! { + usize u8 u16 u32 u64 // u128 + isize i8 i16 i32 i64 // i128 + f32 f64 + bool char + } +} + +#[lang = "copy"] +pub trait Copy: Clone { + // Empty. +} + +mod copy_impls { + use super::Copy; + + macro_rules! impl_copy { + ($($t:ty)*) => { + $( + impl Copy for $t {} + )* + } + } + + impl_copy! { + usize u8 u16 u32 u64 // u128 + isize i8 i16 i32 i64 // i128 + f32 f64 + bool char + } +} extern "rust-intrinsic" { pub fn atomic_store_seqcst(dst: *mut T, value: T); @@ -24,6 +81,13 @@ impl VeryLargeType { } } +impl Clone for VeryLargeType { + fn clone(&self) -> Self { + *self + } +} +impl Copy for VeryLargeType {} + fn main() { let mut dst = VeryLargeType::new(0); let mut b = false; diff --git a/gcc/testsuite/rust/execute/torture/atomic_load.rs b/gcc/testsuite/rust/execute/torture/atomic_load.rs index 28ed8ae78f1..6e7383aa7e9 100644 --- a/gcc/testsuite/rust/execute/torture/atomic_load.rs +++ b/gcc/testsuite/rust/execute/torture/atomic_load.rs @@ -1,4 +1,61 @@ -trait Copy {} +#[lang = "sized"] +pub trait Sized {} + +#[lang = "clone"] +pub trait Clone: Sized { + fn clone(&self) -> Self; + + fn clone_from(&mut self, source: &Self) { + *self = source.clone() + } +} + +mod impls { + use super::Clone; + + macro_rules! impl_clone { + ($($t:ty)*) => { + $( + impl Clone for $t { + fn clone(&self) -> Self { + *self + } + } + )* + } + } + + impl_clone! { + usize u8 u16 u32 u64 // u128 + isize i8 i16 i32 i64 // i128 + f32 f64 + bool char + } +} + +#[lang = "copy"] +pub trait Copy: Clone { + // Empty. +} + +mod copy_impls { + use super::Copy; + + macro_rules! impl_copy { + ($($t:ty)*) => { + $( + impl Copy for $t {} + )* + } + } + + impl_copy! { + usize u8 u16 u32 u64 // u128 + isize i8 i16 i32 i64 // i128 + f32 f64 + bool char + } +} extern "rust-intrinsic" { pub fn atomic_load_seqcst(src: *const T) -> T; diff --git a/gcc/testsuite/rust/execute/torture/atomic_store.rs b/gcc/testsuite/rust/execute/torture/atomic_store.rs index 9f248b4f823..46960a7a186 100644 --- a/gcc/testsuite/rust/execute/torture/atomic_store.rs +++ b/gcc/testsuite/rust/execute/torture/atomic_store.rs @@ -1,4 +1,61 @@ -trait Copy {} +#[lang = "sized"] +pub trait Sized {} + +#[lang = "clone"] +pub trait Clone: Sized { + fn clone(&self) -> Self; + + fn clone_from(&mut self, source: &Self) { + *self = source.clone() + } +} + +mod impls { + use super::Clone; + + macro_rules! impl_clone { + ($($t:ty)*) => { + $( + impl Clone for $t { + fn clone(&self) -> Self { + *self + } + } + )* + } + } + + impl_clone! { + usize u8 u16 u32 u64 // u128 + isize i8 i16 i32 i64 // i128 + f32 f64 + bool char + } +} + +#[lang = "copy"] +pub trait Copy: Clone { + // Empty. +} + +mod copy_impls { + use super::Copy; + + macro_rules! impl_copy { + ($($t:ty)*) => { + $( + impl Copy for $t {} + )* + } + } + + impl_copy! { + usize u8 u16 u32 u64 // u128 + isize i8 i16 i32 i64 // i128 + f32 f64 + bool char + } +} extern "rust-intrinsic" { pub fn atomic_store_seqcst(dst: *mut T, val: T);