From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 14CED3857C44; Thu, 12 Jan 2023 07:43:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 14CED3857C44 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1673509439; bh=dhZIC0jqa+5fZOXIrp/eb3UOdMH+8WWWbJCOxhuR3k0=; h=From:To:Subject:Date:From; b=KXFzlqWZ/yvI25hL1Sxm3+Kd8beOYagV6xxDAUOb/0QwY1gYqRCfuqIOtkR0iiFU2 v5XLFiHdclIznrcDpj138ijEwXobr099HheT663AOZR3EgKloB8Gzl3G5AuHGoYrY+ 6s449q2c/UiXSVtII8El8zCileffwnpVOK4Lfzfo= 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] unsafe: check use of `target_feature` attribute X-Act-Checkin: gcc X-Git-Author: Prajwal S N X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 0152926ab36ba52153f3f457f6f3bb02bb274073 X-Git-Newrev: aa7698dca97505737ff092c6b9758d08f03daf2a Message-Id: <20230112074359.14CED3857C44@sourceware.org> Date: Thu, 12 Jan 2023 07:43:59 +0000 (GMT) List-Id: https://gcc.gnu.org/g:aa7698dca97505737ff092c6b9758d08f03daf2a commit aa7698dca97505737ff092c6b9758d08f03daf2a Author: Prajwal S N Date: Sat Dec 31 12:49:02 2022 +0530 unsafe: check use of `target_feature` attribute The `target_feature` attribute is for conditional compilation and may or may not compile on all platforms. Using it requires an unsafe function or block. Signed-off-by: Prajwal S N Diff: --- gcc/rust/checks/errors/rust-unsafe-checker.cc | 29 ++++++++++++++++++++++++++- gcc/rust/checks/errors/rust-unsafe-checker.h | 5 +++++ gcc/rust/util/rust-attributes.cc | 3 +++ gcc/testsuite/rust/compile/unsafe11.rs | 8 ++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc b/gcc/rust/checks/errors/rust-unsafe-checker.cc index 82bde0ecb33..f68243020b1 100644 --- a/gcc/rust/checks/errors/rust-unsafe-checker.cc +++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc @@ -179,6 +179,31 @@ UnsafeChecker::check_function_call (HirId node_id, Location locus) locus); } +static void +check_target_attr (HIR::Function *fn, Location locus) +{ + if (std::any_of (fn->get_outer_attrs ().begin (), + fn->get_outer_attrs ().end (), + [] (const AST::Attribute &attr) { + return attr.get_path ().as_string () == "target_feature"; + })) + rust_error_at (locus, + "call to function with %<#[target_feature]%> requires " + "unsafe function or block"); +} + +void +UnsafeChecker::check_function_attr (HirId node_id, Location locus) +{ + if (unsafe_context.is_in_context ()) + return; + + auto maybe_fn = mappings.lookup_hir_item (node_id); + + if (maybe_fn && maybe_fn->get_item_kind () == Item::ItemKind::Function) + check_target_attr (static_cast (maybe_fn), locus); +} + void UnsafeChecker::visit (Lifetime &) {} @@ -398,11 +423,13 @@ UnsafeChecker::visit (CallExpr &expr) rust_assert (mappings.lookup_node_to_hir (ref_node_id, &definition_id)); - // At this point we have the function's HIR Id. There are two checks we + // At this point we have the function's HIR Id. There are three checks we // must perform: // 1. The function is an unsafe one // 2. The function is an extern one + // 3. The function is marked with a target_feature attribute check_function_call (definition_id, expr.get_locus ()); + check_function_attr (definition_id, expr.get_locus ()); if (expr.has_params ()) for (auto &arg : expr.get_arguments ()) diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.h b/gcc/rust/checks/errors/rust-unsafe-checker.h index fe564a2088e..540e5c33f6d 100644 --- a/gcc/rust/checks/errors/rust-unsafe-checker.h +++ b/gcc/rust/checks/errors/rust-unsafe-checker.h @@ -46,6 +46,11 @@ private: */ void check_function_call (HirId node_id, Location locus); + /** + * Check if any unsafe attributes are present on a function + */ + void check_function_attr (HirId node_id, Location locus); + StackedContexts unsafe_context; Resolver::TypeCheckContext &context; diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc index 1cdc122177c..9de86db7cc1 100644 --- a/gcc/rust/util/rust-attributes.cc +++ b/gcc/rust/util/rust-attributes.cc @@ -41,6 +41,9 @@ static const BuiltinAttrDefinition __definitions[] {"repr", CODE_GENERATION}, {"path", EXPANSION}, {"macro_use", NAME_RESOLUTION}, + // FIXME: This is not implemented yet, see + // https://github.com/Rust-GCC/gccrs/issues/1475 + {"target_feature", CODE_GENERATION}, // From now on, these are reserved by the compiler and gated through // #![feature(rustc_attrs)] {"rustc_inherit_overflow_checks", CODE_GENERATION}}; diff --git a/gcc/testsuite/rust/compile/unsafe11.rs b/gcc/testsuite/rust/compile/unsafe11.rs new file mode 100644 index 00000000000..c87902fcd5f --- /dev/null +++ b/gcc/testsuite/rust/compile/unsafe11.rs @@ -0,0 +1,8 @@ +#[target_feature(sse)] +fn foo() { + let a: usize = 0; +} + +fn main() { + foo() // { dg-error "requires unsafe function or block" } +}