From patchwork Tue Jan 31 13:26:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 50932 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2816388wrn; Tue, 31 Jan 2023 07:31:22 -0800 (PST) X-Google-Smtp-Source: AK7set9CzfKn22O88O/ivfxDPm0wVs23cAWX+Xd5E8N7X80KbZALr3koiOmmwuCs9nm1viMMwmDc X-Received: by 2002:a17:907:cc9c:b0:87b:d636:c26d with SMTP id up28-20020a170907cc9c00b0087bd636c26dmr12724571ejc.58.1675179082100; Tue, 31 Jan 2023 07:31:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675179082; cv=none; d=google.com; s=arc-20160816; b=Mrm3WGhP5bQazT0tejgQ33WYK797B2QS0is7G95gmunSRLK6H0NjuhWZT6lZL0pj/l zESfEQHNqGGbvoZWPSU39et0JG0D6JV1cYEla+BRCDpybFIxrJKvVMrQOY8BBvFqCjnc jAreNi++DITgb0T1F605WM2PrObxJzfvFeNSc+Jmc4mQ3OBgI7Mx8cEgYBOPYC9lsaLF nHpp/viL2v+/AF/7eeXI3ztDtlVsk1m6HXrxPsEQJiXkMbAg/dzecjvhjr1Z6mudOmwe xJ4ElsV1WmSeo1gRZO9rlzsHnbHOQhk9Jd8uhgNyBl4rgijgTQ4ZxUHLnfn2GGp2hhIc 8OcA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dmarc-filter:delivered-to; bh=upTGiSSx/MCgAI9gG2vImzUaqISMZfCpZeN0EWlLKn0=; b=zkIqZ7p5S3cGKg8WnCtnY1LSpep2Xbb/qfEtG4niH9eny3f/ZAWyd7CTNR3pEY5cPh NNuNRI3dLB3mPociLU8xAtiwBjExcrF+N5NzSkFEqjzWfvCMtjeTgDL/S1zt77h0gH0n c4DKY6LVf8BIA5oOoImURnpM2e3qYLelNLjolbvJ0kHH4ELkHfrRsbNBX7QrMvI4f3p3 3QLzz5E6Kch5iY2d6ItVv+P0kuE8drfTYZn3FBSCjWuJmHw3RVHSqQfdgMh1xQgf9WOg 6lMQKGZDdM0dcQJxeWcWiCrIIEIXWhl2FSJhQK51R8EtNIo8NaLjFJnZlnpVR0hXq+ys fL6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=UO3sufQR; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id 17-20020a17090602d100b00888161349d7si7805219ejk.662.2023.01.31.07.31.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Jan 2023 07:31:22 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=UO3sufQR; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 82DB3396D80E for ; Tue, 31 Jan 2023 13:25:56 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by sourceware.org (Postfix) with ESMTPS id DD9C73895FDF for ; Tue, 31 Jan 2023 13:22:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DD9C73895FDF 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-x42c.google.com with SMTP id o18so4715438wrj.3 for ; Tue, 31 Jan 2023 05:22:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=upTGiSSx/MCgAI9gG2vImzUaqISMZfCpZeN0EWlLKn0=; b=UO3sufQREyiP2hGhGCJ9ZNXN7Rq9Ri6MrHbVNl4qiOrYBlkS2lIB/9a8u+WORmAWoT gc/xqIUFMMwg1ckCou5F3FgVnXahgwqxp4YuxgYnIgyAAag2JWcnT8TH4nA6ndTD9C+q gRCwqjAixj3Y3FDpqourjQbM2qCKg6jfvlCmfnvRF/JH4hynuMcwBZ4STnHarceRnXFJ RDlGIjactBuv8AyvIWYJkFCm7hl7C2HblmKYGdy0EI+2TszKemfXm7/Mtv/GacPdSp/f VCva58iBy9b/c9OxKmn8N4A/EyJrrsCog7/kwYmNNrBe2ortLgClxP1oHQMiu4M1+Ql/ gttQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=upTGiSSx/MCgAI9gG2vImzUaqISMZfCpZeN0EWlLKn0=; b=BLjz/mAR0bC3UWHkOONa10VzowEm3x/Sh5Wu5j3dmv2N0f6Cywq0MalOftRsCE2cn7 6Y5UbfNMDQmGNDMKbBaplXCFF7yyRrP5adVAmvx0GxlpMhEhO91cdOGuqDlo/H9EQrwf 87DX13OdLd5n7wzglAA0qneCkxED5wM/c8hkUiw8SCNTDVHAflUj88BwIBtP7srwE5kP djIHXOD6Sj4s3Dzjt1C1RRc2gcGQkkaG4lkSaktzqrwS8WxMl/M1EmiJHWf50xGpkUb4 fb1eV4wGQfVHf4zsRkxLf019y8JndrSbh6fmFg+WWTqs4qu0BFQi70TiPHkriny7CDxp 5vMA== X-Gm-Message-State: AO0yUKVC8dHvNXSLH9cHiNsbfKNhnwHa1BlEyOEw7aMKsDHYviwPZ3Z2 PuxKKUrhSvvJNHnCUgTNNQLWO/Qh0rxXG2T68w== X-Received: by 2002:a5d:6505:0:b0:2c2:6498:1688 with SMTP id x5-20020a5d6505000000b002c264981688mr2113572wru.58.1675171348208; Tue, 31 Jan 2023 05:22:28 -0800 (PST) Received: from platypus.lan ([2001:861:5e4c:3bb0:6424:328a:1734:3249]) by smtp.gmail.com with ESMTPSA id r25-20020adfa159000000b00297dcfdc90fsm15111859wrr.24.2023.01.31.05.22.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Jan 2023 05:22:27 -0800 (PST) From: Arthur Cohen To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Arthur Cohen Subject: [COMMITTED] gccrs: const generics: Forbid default values in Functions, Traits and Impls Date: Tue, 31 Jan 2023 14:26:13 +0100 Message-Id: <20230131132613.663060-1-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 X-Spam-Status: No, score=-15.4 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=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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , 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?1756552581244233795?= X-GMAIL-MSGID: =?utf-8?q?1756552581244233795?= gcc/rust/ChangeLog: * checks/errors/rust-const-checker.cc (ConstChecker::ctx_to_str): Allow getting an error string from a specific constant context. (ConstChecker::ctx_allows_default): New function, check if a context allows default values for Const generics. (ConstChecker::visit): Call into `ctx_allows_default`. * checks/errors/rust-const-checker.h: Declare `ctx_allows_default`. gcc/testsuite/ChangeLog: * rust/compile/const_generics_8.rs: New test. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/rust/checks/errors/rust-const-checker.cc | 97 +++++++++++++++++-- gcc/rust/checks/errors/rust-const-checker.h | 25 +++++ .../rust/compile/const_generics_8.rs | 12 +++ 3 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/rust/compile/const_generics_8.rs diff --git a/gcc/rust/checks/errors/rust-const-checker.cc b/gcc/rust/checks/errors/rust-const-checker.cc index 0c1b743cfaf..2fa9614abf1 100644 --- a/gcc/rust/checks/errors/rust-const-checker.cc +++ b/gcc/rust/checks/errors/rust-const-checker.cc @@ -52,6 +52,67 @@ ConstChecker::is_const_extern_fn (HIR::ExternalFunctionItem &fn) }); } +const char * +ConstChecker::ctx_to_str (ConstGenericCtx ctx) +{ + switch (ctx) + { + case ConstGenericCtx::Function: + return "function"; + case ConstGenericCtx::TypeAlias: + return "type alias"; + case ConstGenericCtx::Struct: + return "struct"; + case ConstGenericCtx::Enum: + return "enum"; + case ConstGenericCtx::Union: + return "union"; + case ConstGenericCtx::Trait: + return "trait"; + case ConstGenericCtx::Impl: + return "impl"; + default: + gcc_unreachable (); + } +} + +bool +ConstChecker::ctx_allows_default (ConstGenericCtx ctx) +{ + switch (ctx) + { + case ConstGenericCtx::TypeAlias: + case ConstGenericCtx::Struct: + case ConstGenericCtx::Enum: + case ConstGenericCtx::Trait: + return true; + default: + return false; + } +} + +void +ConstChecker::check_default_const_generics ( + std::vector> ¶ms, ConstGenericCtx context) +{ + if (ctx_allows_default (context)) + return; + + for (auto ¶m : params) + { + if (param->get_kind () == GenericParam::GenericKind::CONST) + { + auto const_param = static_cast (param.get ()); + if (const_param->has_default_expression ()) + rust_error_at ( + param->get_locus (), + "default values for const generic parameters are not " + "allowed in %qs items", + ctx_to_str (context)); + } + } +} + void ConstChecker::visit (Lifetime &lifetime) {} @@ -560,6 +621,9 @@ ConstChecker::visit (Function &function) if (const_fn) const_context.enter (function.get_mappings ().get_hirid ()); + check_default_const_generics (function.get_generic_params (), + ConstGenericCtx::Function); + for (auto ¶m : function.get_function_params ()) param.get_type ()->accept_vis (*this); @@ -571,18 +635,27 @@ ConstChecker::visit (Function &function) void ConstChecker::visit (TypeAlias &type_alias) -{} +{ + check_default_const_generics (type_alias.get_generic_params (), + ConstGenericCtx::TypeAlias); +} void ConstChecker::visit (StructStruct &struct_item) -{} +{ + check_default_const_generics (struct_item.get_generic_params (), + ConstGenericCtx::Struct); +} void ConstChecker::visit (TupleStruct &tuple_struct) -{} +{ + check_default_const_generics (tuple_struct.get_generic_params (), + ConstGenericCtx::Struct); +} void -ConstChecker::visit (EnumItem &item) +ConstChecker::visit (EnumItem &enum_item) {} void @@ -605,11 +678,17 @@ ConstChecker::visit (EnumItemDiscriminant &item) void ConstChecker::visit (Enum &enum_item) -{} +{ + check_default_const_generics (enum_item.get_generic_params (), + ConstGenericCtx::Enum); +} void ConstChecker::visit (Union &union_item) -{} +{ + check_default_const_generics (union_item.get_generic_params (), + ConstGenericCtx::Union); +} void ConstChecker::visit (ConstantItem &const_item) @@ -652,6 +731,9 @@ ConstChecker::visit (TraitItemType &item) void ConstChecker::visit (Trait &trait) { + check_default_const_generics (trait.get_generic_params (), + ConstGenericCtx::Trait); + for (auto &item : trait.get_trait_items ()) item->accept_vis (*this); } @@ -659,6 +741,9 @@ ConstChecker::visit (Trait &trait) void ConstChecker::visit (ImplBlock &impl) { + check_default_const_generics (impl.get_generic_params (), + ConstGenericCtx::Impl); + for (auto &item : impl.get_impl_items ()) item->accept_vis (*this); } diff --git a/gcc/rust/checks/errors/rust-const-checker.h b/gcc/rust/checks/errors/rust-const-checker.h index ca86b490130..13cf44b1273 100644 --- a/gcc/rust/checks/errors/rust-const-checker.h +++ b/gcc/rust/checks/errors/rust-const-checker.h @@ -46,6 +46,31 @@ private: */ void check_function_call (HirId fn_id, Location locus); + /* All possible const contexts */ + enum class ConstGenericCtx + { + Function, + TypeAlias, + Struct, + Enum, + Union, + Trait, + Impl + }; + + /* Get the string representation of a const context */ + const char *ctx_to_str (ConstGenericCtx ctx); + + /* Check if a const context allows default values */ + bool ctx_allows_default (ConstGenericCtx ctx); + + /** + * Check that const generic parameters only contains defaults in allowed + * contexts + */ + void check_default_const_generics ( + std::vector> ¶m, ConstGenericCtx context); + StackedContexts const_context; Resolver::Resolver &resolver; Analysis::Mappings &mappings; diff --git a/gcc/testsuite/rust/compile/const_generics_8.rs b/gcc/testsuite/rust/compile/const_generics_8.rs new file mode 100644 index 00000000000..b0bb2624a2c --- /dev/null +++ b/gcc/testsuite/rust/compile/const_generics_8.rs @@ -0,0 +1,12 @@ +struct Bidule {} +enum Bidoule {} + +// Note - missing generic parameter - needs name resolution on const generics +type Bipboupe = Bidule; +trait Fooable {} + +union Bidoulepe {} // { dg-error "default values for const generic parameters are not allowed in .union. items" } +fn const_default() {} // { dg-error "default values for const generic parameters are not allowed in .function. items" } + +// Note - missing generic parameter - needs name resolution on const generics +impl Bidule {} // { dg-error "default values for const generic parameters are not allowed in .impl. items" }