From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 61057385AC3C; Tue, 9 Aug 2022 20:39:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 61057385AC3C Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] const-checker: Add `is_const_extern_fn` helper function X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 084f959076c10d54bdb5c80cad10a0aac5756ea4 X-Git-Newrev: 4ffd884a69396d828049d4a14d17e6d3f6c8d61f Message-Id: <20220809203930.61057385AC3C@sourceware.org> Date: Tue, 9 Aug 2022 20:39:30 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 09 Aug 2022 20:39:30 -0000 https://gcc.gnu.org/g:4ffd884a69396d828049d4a14d17e6d3f6c8d61f commit 4ffd884a69396d828049d4a14d17e6d3f6c8d61f Author: Arthur Cohen Date: Tue Aug 9 09:37:41 2022 +0200 const-checker: Add `is_const_extern_fn` helper function Co-authored-by: philberty Diff: --- gcc/rust/checks/errors/rust-const-checker.cc | 27 ++++++++++++++++----------- gcc/rust/checks/errors/rust-const-checker.h | 7 +++++++ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/gcc/rust/checks/errors/rust-const-checker.cc b/gcc/rust/checks/errors/rust-const-checker.cc index bd4c7f19906..ad0a2cfc5c5 100644 --- a/gcc/rust/checks/errors/rust-const-checker.cc +++ b/gcc/rust/checks/errors/rust-const-checker.cc @@ -37,6 +37,21 @@ ConstChecker::go (HIR::Crate &crate) item->accept_vis (*this); } +bool +ConstChecker::is_const_extern_fn (HIR::ExternalFunctionItem &fn) +{ + // FIXME: Is it really how we want to handle `rustc_const_stable` + // and `rustc_const_unstable`? + // TODO: Add these attributes to the attribute check and handle + // `stable` and `unstable` as well + return std::any_of ( + fn.get_outer_attrs ().begin (), fn.get_outer_attrs ().end (), + [] (const AST::Attribute &attr) { + // `starts_with` in C++11... + return attr.get_path ().as_string ().rfind ("rustc_const_", 0) == 0; + }); +} + void ConstChecker::visit (IdentifierExpr &ident_expr) {} @@ -261,17 +276,7 @@ ConstChecker::check_function_call (HirId fn_id, Location locus) { { auto fn = static_cast (maybe_extern_item); - auto is_const_extern = std::any_of ( - fn->get_outer_attrs ().begin (), fn->get_outer_attrs ().end (), - [] (const AST::Attribute &attr) { - // `starts_with` in C++11... - // FIXME: Is it really how we want to handle `rustc_const_stable` - // and `rustc_const_unstable`? - // TODO: Add these attributes to the attribute check and handle - // `stable` and `unstable` as well - return attr.get_path ().as_string ().rfind ("rustc_const_", 0) == 0; - }); - if (!is_const_extern) + if (!is_const_extern_fn (*fn)) is_error = true; } } diff --git a/gcc/rust/checks/errors/rust-const-checker.h b/gcc/rust/checks/errors/rust-const-checker.h index a474fc83d10..608ea3e0750 100644 --- a/gcc/rust/checks/errors/rust-const-checker.h +++ b/gcc/rust/checks/errors/rust-const-checker.h @@ -33,6 +33,13 @@ public: void go (HIR::Crate &crate); + /** + * Check if an item is a const extern item or not + * TODO: Move this to a const compilation context class or an attribute + * checking class + */ + static bool is_const_extern_fn (HIR::ExternalFunctionItem &fn); + private: /** * Check that only const functions are called in const contexts