From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by sourceware.org (Postfix) with ESMTPS id 68B5B395A447 for ; Tue, 31 Jan 2023 13:22:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 68B5B395A447 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-x42d.google.com with SMTP id m14so13720567wrg.13 for ; Tue, 31 Jan 2023 05:22:29 -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=ZU+4+cazKcDiXPQW/ElyNsbAMl1uEJ33R/cB3FOE9Sc6A7ORE/QMMm7rXx2MDKytSk gZs6/hWTC1OPG0g+6Ek8rA5mpZYM3qXgMd1Frasx0EdAgFZvIhD98T9KwQlCtoeWVuKJ GkoHxj2tGexbAkGzmJgnMfr7G3rzxulvIUhnsjieyxHeIZx9T5uM7U1RhVpvk1QC54IO 0gTFqhzDyQHRqg8Jnij8QtHP8Bp6qmFcpKuQPY04siVddk97UwZ6DQBGFrLLd4HHKkHh MezG2E8PTm5Oh1WlIo+zzc/J7NpBu/QLXAr+P5NcdRDjCoDAQgksP5KkjA5rBJVhJyY1 YYbw== X-Gm-Message-State: AO0yUKWxUS4ze4DB9Zo9lv4cAbqHpvAMb9VX8BpbFIn+eKsJQk/KMch8 8mNdw5uFYCYDRczwnCiXVUwqBuQl2W3gIvvwNQ== X-Google-Smtp-Source: AK7set+gVLQ7OTLoQuWnBJX2DqNA4++mqTZ70i+Wyv1DVRmNWW2+aoxUAEj6ZcV3lyOIl4bdtJVGjQ== 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 Content-Transfer-Encoding: 8bit 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=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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" } -- 2.39.1