From patchwork Thu Aug 1 14:57:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 1967862 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=embecosm.com header.i=@embecosm.com header.a=rsa-sha256 header.s=google header.b=WTdoYe03; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WZY8M0mjdz1ybV for ; Fri, 2 Aug 2024 01:40:51 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5C2FA3864827 for ; Thu, 1 Aug 2024 15:40:49 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by sourceware.org (Postfix) with ESMTPS id 307633865C29 for ; Thu, 1 Aug 2024 15:00:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 307633865C29 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 307633865C29 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::635 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524480; cv=none; b=IzIaZPsxfvt9zqmA5t5kAEwLNqvOFFOYjQHZYnrzxoPxyBgpOMIV2BHT68ChOPwHi1zUtx4/djAnr2rGDjImDnk5S4KoHOOSQH7eh5uY80F4F8o65sCQwEO5AD833mmpfOP7sP5mfYJeWWNcskFmGMvDapwuuBwsOXq+ww9GuI4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524480; c=relaxed/simple; bh=jOpwqp53QR0qQj6U+Kwfo5H3X5Slj1QfLpY8la1/CEw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=koEZpEukxxRwTePUaXstVj9YU8pEb9RO5oAjLgYnsfloCinajve8xVDwm2bRcOuJGM8GsngC4r+iOV7AAEXdUAppMuuGq1oXDWv6bXdZN8Meh9O1bBfMEX/S0isJb3ZKgGFWGJxyPAQ16xOPO1mnrstauCpl39HrbnYbs1a8kXg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ej1-x635.google.com with SMTP id a640c23a62f3a-a7d638a1f27so373123266b.2 for ; Thu, 01 Aug 2024 08:00:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1722524433; x=1723129233; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=L4xZT9oXHJCzvBy5vwbKzdOSnS/0JH1Y+dRETZbj7Gs=; b=WTdoYe03dPto06HxJXOytkd6/0WL7ei7IKJtyi6AgIstR7Nchjbcg0Mgx7OeA2JKES rebnQP8swJvkB1FkSeHsRO9Cm8iw3haYpFux6IcsGXcBASYC6y6TOJNVQgNCmjIS9WAJ HxiJfq9bzLlsFDvLklZ59NsoMoiyQjk+Zb+FBClWbnjNpmmzK4ZvscWe9rDEsvwRyf1I NQxUJdClygMvNmmE6zadzQvA1Uldo6tn/Crx20T0m6dwxcJpRK5lQ03HcOQuB4udOJWX +PD1qhyMH23TEjrwlnMzmZtFyxHSKv5aK0WuHXuCnvKUxSAlZWARFopfF/o2LTaX3Q3H zQqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722524433; x=1723129233; h=content-transfer-encoding:mime-version: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=L4xZT9oXHJCzvBy5vwbKzdOSnS/0JH1Y+dRETZbj7Gs=; b=f2lAnB/WpE8oG6xPkbZkizjJkybwPZ/KzeIXOtlIN1ZID4tH/jHE0Vti7RGrve8xPR GqeYg++8oiNUa7KpwOT+PNXOa3O+mKGAJY7bT5bsgdSOhFyowgjr7X53h3lMgxER/TiX J9PVxVHF8dsCI47Y+tjUMp1qABj1SAlJI/euO9nEjNhliPdTwQwD0AEE1adGhbbx1sU6 sGJTUd7RPvouNRBsJxwXuGTFFFHiH6sSxASnWsAPfEW0Bk4rcFlmhyBkPtWlKXYy8kU2 GoNEC/zbBk7N4cfD+bFLeEd1w7uShF++tbJ5u4ggoZYT8TAjuFkiqL9rBKBWgc+JAj86 Q6Nw== X-Gm-Message-State: AOJu0YzZV1W085TFeOEwZz+8w1hxjMSBFtvV/tUSfNHzMYrpKjpHjmZt HWR6Fj4olV8h7tbHTXpzU8GxmiH/LfQPWndLNPKTNR4KZ0lR3wIPDRJ2ahkH7V8LMKZeQqp+C4Z BOEwX X-Google-Smtp-Source: AGHT+IEtAzYVh38CCe48AYdz6lgXNQg+lZFaXQZ/7A5/02QWfTlwZT3WluARJmmWDnAZNzYsbQCFTg== X-Received: by 2002:aa7:d755:0:b0:5a1:b6d8:b561 with SMTP id 4fb4d7f45d1cf-5b7f39e0ab6mr525091a12.9.1722524432978; Thu, 01 Aug 2024 08:00:32 -0700 (PDT) Received: from platypus.lan ([2a04:cec2:9:dc84:3622:6733:ff49:ee91]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5ac63590592sm10252456a12.25.2024.08.01.08.00.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Aug 2024 08:00:32 -0700 (PDT) From: Arthur Cohen To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Arthur Cohen Subject: [PATCH 109/125] gccrs: unify: Always coerce `!` to the target type. Date: Thu, 1 Aug 2024 16:57:45 +0200 Message-ID: <20240801145809.366388-111-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240801145809.366388-2-arthur.cohen@embecosm.com> References: <20240801145809.366388-2-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, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Never can... never... exist, so it should always be coerced to the type it is being matched against. This is useful for breaking off of a loop from inside a match, or an if condition, for example. gcc/rust/ChangeLog: * typecheck/rust-unify.cc (UnifyRules::go): Always unify to `ltype` if we are matching against a `Never` in `rtype`. (UnifyRules::expect_never): Always unify to the expected type. gcc/testsuite/ChangeLog: * rust/compile/match-never-ltype.rs: New test. * rust/compile/match-never-rtype.rs: New test. --- gcc/rust/typecheck/rust-unify.cc | 36 ++++++------------- .../rust/compile/match-never-ltype.rs | 17 +++++++++ .../rust/compile/match-never-rtype.rs | 17 +++++++++ 3 files changed, 45 insertions(+), 25 deletions(-) create mode 100644 gcc/testsuite/rust/compile/match-never-ltype.rs create mode 100644 gcc/testsuite/rust/compile/match-never-rtype.rs diff --git a/gcc/rust/typecheck/rust-unify.cc b/gcc/rust/typecheck/rust-unify.cc index 8b43380cc59..7d1042d8e63 100644 --- a/gcc/rust/typecheck/rust-unify.cc +++ b/gcc/rust/typecheck/rust-unify.cc @@ -17,6 +17,7 @@ // . #include "rust-unify.h" +#include "rust-tyty.h" namespace Rust { namespace Resolver { @@ -237,6 +238,15 @@ UnifyRules::go () } } + // The never type should always get coerced to the type it's being matched + // against, so in that case, ltype. This avoids doing the same check in all + // the `expect_*` functions. + // However, this does not work if we have an annoying ltype - like INFER. + // TODO: Is ltype == Infer the only special case here? What about projections? + // references? + if (rtype->get_kind () == TyTy::NEVER && ltype->get_kind () != TyTy::INFER) + return ltype->clone (); + switch (ltype->get_kind ()) { case TyTy::INFER: @@ -1536,32 +1546,8 @@ UnifyRules::expect_never (TyTy::NeverType *ltype, TyTy::BaseType *rtype) } break; - case TyTy::NEVER: + default: return rtype->clone (); - - case TyTy::PLACEHOLDER: - case TyTy::PROJECTION: - case TyTy::DYNAMIC: - case TyTy::CLOSURE: - case TyTy::SLICE: - case TyTy::PARAM: - case TyTy::POINTER: - case TyTy::STR: - case TyTy::ADT: - case TyTy::REF: - case TyTy::ARRAY: - case TyTy::FNDEF: - case TyTy::FNPTR: - case TyTy::TUPLE: - case TyTy::BOOL: - case TyTy::CHAR: - case TyTy::INT: - case TyTy::UINT: - case TyTy::FLOAT: - case TyTy::USIZE: - case TyTy::ISIZE: - case TyTy::ERROR: - return new TyTy::ErrorType (0); } return new TyTy::ErrorType (0); } diff --git a/gcc/testsuite/rust/compile/match-never-ltype.rs b/gcc/testsuite/rust/compile/match-never-ltype.rs new file mode 100644 index 00000000000..6516ab3c1ea --- /dev/null +++ b/gcc/testsuite/rust/compile/match-never-ltype.rs @@ -0,0 +1,17 @@ +fn foo() {} + +enum Foo { + A, + B, +} + +fn main() { + let a = Foo::A; + + loop { + match a { + Foo::A => break, + Foo::B => foo(), + } + } +} diff --git a/gcc/testsuite/rust/compile/match-never-rtype.rs b/gcc/testsuite/rust/compile/match-never-rtype.rs new file mode 100644 index 00000000000..6e4e7638f67 --- /dev/null +++ b/gcc/testsuite/rust/compile/match-never-rtype.rs @@ -0,0 +1,17 @@ +fn foo() {} + +enum Foo { + A, + B, +} + +fn main() { + let a = Foo::A; + + loop { + match a { + Foo::B => foo(), + Foo::A => break, + } + } +}