diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-07-29 16:27:22 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-29 16:27:22 +0000 |
commit | 8809ee8c6a5621e830f3cfe66c381f986e63c7f2 (patch) | |
tree | e2faec377641b1cadb98f4941339a6d6c38a4c17 | |
parent | 2d824b796cde571d270f4c20af8183dbd93614d5 (diff) | |
parent | f742538d13375aa90ccaa787b06e07835bba5887 (diff) | |
download | gcc-8809ee8c6a5621e830f3cfe66c381f986e63c7f2.zip gcc-8809ee8c6a5621e830f3cfe66c381f986e63c7f2.tar.gz gcc-8809ee8c6a5621e830f3cfe66c381f986e63c7f2.tar.bz2 |
Merge #1427
1427: unsafe: Check for unsafe function/method calls r=CohenArthur a=CohenArthur
Addresses #1411
Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
39 files changed, 460 insertions, 190 deletions
diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc index 95d8841..b5bfa3c 100644 --- a/gcc/rust/backend/rust-compile-resolve-path.cc +++ b/gcc/rust/backend/rust-compile-resolve-path.cc @@ -159,8 +159,9 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup, Location expr_locus, bool is_qualified_path) { HIR::Item *resolved_item = ctx->get_mappings ()->lookup_hir_item (ref); + HirId parent_block; HIR::ExternalItem *resolved_extern_item - = ctx->get_mappings ()->lookup_hir_extern_item (ref); + = ctx->get_mappings ()->lookup_hir_extern_item (ref, &parent_block); bool is_hir_item = resolved_item != nullptr; bool is_hir_extern_item = resolved_extern_item != nullptr; if (is_hir_item) diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc b/gcc/rust/checks/errors/rust-unsafe-checker.cc index 0d1e0e9..174901f0 100644 --- a/gcc/rust/checks/errors/rust-unsafe-checker.cc +++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc @@ -69,7 +69,10 @@ UnsafeChecker::check_use_of_static (HirId node_id, Location locus) return; auto maybe_static_mut = mappings.lookup_hir_item (node_id); - auto maybe_extern_static = mappings.lookup_hir_extern_item (node_id); + + HirId extern_block; + auto maybe_extern_static + = mappings.lookup_hir_extern_item (node_id, &extern_block); if (maybe_static_mut) check_static_mut (maybe_static_mut, locus); @@ -79,6 +82,52 @@ UnsafeChecker::check_use_of_static (HirId node_id, Location locus) locus); } +static void +check_unsafe_call (HIR::Function *fn, Location locus, const std::string &kind) +{ + if (fn->get_qualifiers ().is_unsafe ()) + rust_error_at (locus, "call to unsafe %s requires unsafe function or block", + kind.c_str ()); +} + +static void +check_extern_call (HIR::ExternalItem *maybe_fn, HIR::ExternBlock *parent_block, + Location locus) +{ + // We have multiple operations to perform here + // 1. Is the item an actual function we're calling + // 2. Is the block it's defined in an FFI block or an `extern crate` block + // + // It is not unsafe to call into other crates, so items defined in an `extern + // crate` must be callable without being in an unsafe context. On the other + // hand, any function defined in a block with a specific ABI (even `extern + // "Rust"` blocks) is unsafe to call + + if (maybe_fn->get_extern_kind () == ExternalItem::ExternKind::Function) + rust_error_at (locus, + "call to extern function requires unsafe function or block"); +} + +void +UnsafeChecker::check_function_call (HirId node_id, Location locus) +{ + if (unsafe_context.is_in_context ()) + return; + + HirId parent_extern_block; + auto maybe_fn = mappings.lookup_hir_item (node_id); + auto maybe_extern + = mappings.lookup_hir_extern_item (node_id, &parent_extern_block); + + if (maybe_fn && maybe_fn->get_item_kind () == Item::ItemKind::Function) + check_unsafe_call (static_cast<Function *> (maybe_fn), locus, "function"); + + if (maybe_extern) + check_extern_call (static_cast<ExternalItem *> (maybe_extern), + mappings.lookup_hir_extern_block (parent_extern_block), + locus); +} + void UnsafeChecker::visit (IdentifierExpr &ident_expr) { @@ -297,6 +346,28 @@ UnsafeChecker::visit (StructExprStructBase &expr) void UnsafeChecker::visit (CallExpr &expr) { + auto fn = expr.get_fnexpr (); + if (!fn) + return; + + NodeId ast_node_id = fn->get_mappings ().get_nodeid (); + NodeId ref_node_id; + HirId definition_id; + + // There are no unsafe types, and functions are defined in the name resolver. + // If we can't find the name, then we're dealing with a type and should return + // early. + if (!resolver.lookup_resolved_name (ast_node_id, &ref_node_id)) + return; + + 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 + // must perform: + // 1. The function is an unsafe one + // 2. The function is an extern one + check_function_call (definition_id, expr.get_locus ()); + if (expr.has_params ()) for (auto &arg : expr.get_arguments ()) arg->accept_vis (*this); @@ -304,7 +375,23 @@ UnsafeChecker::visit (CallExpr &expr) void UnsafeChecker::visit (MethodCallExpr &expr) -{} +{ + TyTy::BaseType *method_type; + context.lookup_type (expr.get_method_name ().get_mappings ().get_hirid (), + &method_type); + + auto fn = *static_cast<TyTy::FnType *> (method_type); + auto method = mappings.lookup_hir_implitem (fn.get_ref (), nullptr); + + if (!unsafe_context.is_in_context () && method) + check_unsafe_call (static_cast<Function *> (method), expr.get_locus (), + "method"); + + expr.get_receiver ()->accept_vis (*this); + + for (auto &arg : expr.get_arguments ()) + arg->accept_vis (*this); +} void UnsafeChecker::visit (FieldAccessExpr &expr) diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.h b/gcc/rust/checks/errors/rust-unsafe-checker.h index b9d06ef..087bdb7 100644 --- a/gcc/rust/checks/errors/rust-unsafe-checker.h +++ b/gcc/rust/checks/errors/rust-unsafe-checker.h @@ -40,7 +40,14 @@ private: */ void check_use_of_static (HirId node_id, Location locus); + /** + * Check if a call to an unsafe or external function is outside of an unsafe + * context + */ + void check_function_call (HirId node_id, Location locus); + StackedContexts<HirId> unsafe_context; + Resolver::TypeCheckContext &context; Resolver::Resolver &resolver; Analysis::Mappings &mappings; diff --git a/gcc/rust/hir/rust-ast-lower-base.cc b/gcc/rust/hir/rust-ast-lower-base.cc index cae4428..a674617 100644 --- a/gcc/rust/hir/rust-ast-lower-base.cc +++ b/gcc/rust/hir/rust-ast-lower-base.cc @@ -1038,6 +1038,11 @@ ASTLoweringBase::lower_extern_block (AST::ExternBlock &extern_block) { HIR::Visibility vis = translate_visibility (extern_block.get_visibility ()); + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, extern_block.get_node_id (), + mappings->get_next_hir_id (crate_num), + mappings->get_next_localdef_id (crate_num)); + std::vector<std::unique_ptr<HIR::ExternalItem>> extern_items; for (auto &item : extern_block.get_extern_items ()) { @@ -1045,7 +1050,7 @@ ASTLoweringBase::lower_extern_block (AST::ExternBlock &extern_block) continue; HIR::ExternalItem *lowered - = ASTLoweringExternItem::translate (item.get ()); + = ASTLoweringExternItem::translate (item.get (), mapping.get_hirid ()); extern_items.push_back (std::unique_ptr<HIR::ExternalItem> (lowered)); } @@ -1058,17 +1063,14 @@ ASTLoweringBase::lower_extern_block (AST::ExternBlock &extern_block) rust_error_at (extern_block.get_locus (), "unknown ABI option"); } - auto crate_num = mappings->get_current_crate (); - Analysis::NodeMapping mapping (crate_num, extern_block.get_node_id (), - mappings->get_next_hir_id (crate_num), - mappings->get_next_localdef_id (crate_num)); - HIR::ExternBlock *hir_extern_block = new HIR::ExternBlock (mapping, abi, std::move (extern_items), std::move (vis), extern_block.get_inner_attrs (), extern_block.get_outer_attrs (), extern_block.get_locus ()); + mappings->insert_hir_extern_block (hir_extern_block); + return hir_extern_block; } diff --git a/gcc/rust/hir/rust-ast-lower-extern.h b/gcc/rust/hir/rust-ast-lower-extern.h index 28d160b..eeb59c9 100644 --- a/gcc/rust/hir/rust-ast-lower-extern.h +++ b/gcc/rust/hir/rust-ast-lower-extern.h @@ -31,13 +31,15 @@ class ASTLoweringExternItem : public ASTLoweringBase using Rust::HIR::ASTLoweringBase::visit; public: - static HIR::ExternalItem *translate (AST::ExternalItem *item) + static HIR::ExternalItem *translate (AST::ExternalItem *item, + HirId parent_hirid) { ASTLoweringExternItem resolver; item->accept_vis (resolver); rust_assert (resolver.translated != nullptr); - resolver.mappings->insert_hir_extern_item (resolver.translated); + resolver.mappings->insert_hir_extern_item (resolver.translated, + parent_hirid); resolver.mappings->insert_location ( resolver.translated->get_mappings ().get_hirid (), resolver.translated->get_locus ()); diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index c7bf182..6a6deeb 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -371,23 +371,45 @@ Mappings::lookup_hir_trait_item (HirId id) } void -Mappings::insert_hir_extern_item (HIR::ExternalItem *item) +Mappings::insert_hir_extern_block (HIR::ExternBlock *block) +{ + auto id = block->get_mappings ().get_hirid (); + rust_assert (lookup_hir_extern_block (id) == nullptr); + + hirExternBlockMappings[id] = block; + insert_node_to_hir (block->get_mappings ().get_nodeid (), id); +} + +HIR::ExternBlock * +Mappings::lookup_hir_extern_block (HirId id) +{ + auto it = hirExternBlockMappings.find (id); + if (it == hirExternBlockMappings.end ()) + return nullptr; + + return it->second; +} + +void +Mappings::insert_hir_extern_item (HIR::ExternalItem *item, HirId parent_block) { auto id = item->get_mappings ().get_hirid (); - rust_assert (lookup_hir_extern_item (id) == nullptr); + rust_assert (lookup_hir_extern_item (id, nullptr) == nullptr); - hirExternItemMappings[id] = item; + hirExternItemMappings[id] = {item, parent_block}; insert_node_to_hir (item->get_mappings ().get_nodeid (), id); } HIR::ExternalItem * -Mappings::lookup_hir_extern_item (HirId id) +Mappings::lookup_hir_extern_item (HirId id, HirId *parent_block) { auto it = hirExternItemMappings.find (id); if (it == hirExternItemMappings.end ()) return nullptr; - return it->second; + *parent_block = it->second.second; + + return it->second.first; } void diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h index c8cebef..98fcfe6 100644 --- a/gcc/rust/util/rust-hir-map.h +++ b/gcc/rust/util/rust-hir-map.h @@ -115,8 +115,11 @@ public: void insert_hir_trait_item (HIR::TraitItem *item); HIR::TraitItem *lookup_hir_trait_item (HirId id); - void insert_hir_extern_item (HIR::ExternalItem *item); - HIR::ExternalItem *lookup_hir_extern_item (HirId id); + void insert_hir_extern_block (HIR::ExternBlock *block); + HIR::ExternBlock *lookup_hir_extern_block (HirId id); + + void insert_hir_extern_item (HIR::ExternalItem *item, HirId parent_block); + HIR::ExternalItem *lookup_hir_extern_item (HirId id, HirId *parent_block); void insert_hir_impl_block (HIR::ImplBlock *item); HIR::ImplBlock *lookup_hir_impl_block (HirId id); @@ -312,7 +315,8 @@ private: std::map<HirId, HIR::ImplBlock *> hirImplItemsToImplMappings; std::map<HirId, HIR::ImplBlock *> hirImplBlockMappings; std::map<HirId, HIR::TraitItem *> hirTraitItemMappings; - std::map<HirId, HIR::ExternalItem *> hirExternItemMappings; + std::map<HirId, HIR::ExternBlock *> hirExternBlockMappings; + std::map<HirId, std::pair<HIR::ExternalItem *, HirId>> hirExternItemMappings; std::map<HirId, HIR::PathExprSegment *> hirPathSegMappings; std::map<HirId, HIR::GenericParam *> hirGenericParamMappings; std::map<HirId, HIR::Trait *> hirTraitItemsToTraitMappings; diff --git a/gcc/testsuite/rust/compile/issue-1173.rs b/gcc/testsuite/rust/compile/issue-1173.rs index b08d720..5c2a917 100644 --- a/gcc/testsuite/rust/compile/issue-1173.rs +++ b/gcc/testsuite/rust/compile/issue-1173.rs @@ -1,14 +1,17 @@ // { dg-additional-options "-w" } + +#![feature(intrinsics)] + mod mem { extern "rust-intrinsic" { - fn transmute<U, V>(_: U) -> V; + pub fn transmute<U, V>(_: U) -> V; } } pub trait Hasher { fn write(&mut self, bytes: &[u8]); fn write_u16(&mut self, i: u16) { - self.write(&mem::transmute::<_, [u8; 2]>(i)) + self.write(unsafe { &mem::transmute::<_, [u8; 2]>(i) }) } } diff --git a/gcc/testsuite/rust/compile/torture/intrinsics-1.rs b/gcc/testsuite/rust/compile/torture/intrinsics-1.rs index 3c604df..6704c02 100644 --- a/gcc/testsuite/rust/compile/torture/intrinsics-1.rs +++ b/gcc/testsuite/rust/compile/torture/intrinsics-1.rs @@ -1,16 +1,22 @@ // { dg-additional-options -fdump-tree-original } +#![feature(intrinsics)] + extern "rust-intrinsic" { pub fn sqrtf32(x: f32) -> f32; pub fn sinf32(x: f32) -> f32; } fn main() { - let mut f32; + unsafe fn foo() { + let mut f32; + + f32 = sqrtf32(5f32); + // { dg-final { scan-tree-dump-times {(?n)f32 = __builtin_sqrtf \(5\.0e\+0\);$} 1 original } } - f32 = sqrtf32(5f32); - // { dg-final { scan-tree-dump-times {(?n)f32 = __builtin_sqrtf \(5\.0e\+0\);$} 1 original } } + f32 = sinf32(39f32); + // { dg-final { scan-tree-dump-times {(?n)f32 = __builtin_sinf \(3\.9e\+1\);$} 1 original } } + } - f32 = sinf32(39f32); - // { dg-final { scan-tree-dump-times {(?n)f32 = __builtin_sinf \(3\.9e\+1\);$} 1 original } } + unsafe { foo() }; } diff --git a/gcc/testsuite/rust/compile/torture/transmute-size-check-1.rs b/gcc/testsuite/rust/compile/torture/transmute-size-check-1.rs index 1aef4d8..461a35d 100644 --- a/gcc/testsuite/rust/compile/torture/transmute-size-check-1.rs +++ b/gcc/testsuite/rust/compile/torture/transmute-size-check-1.rs @@ -7,5 +7,5 @@ mod mem { fn main() { let a = 123; - let _b: [u32; mem::size_of::<i32>()] = mem::transmute(a); + let _b: [u32; mem::size_of::<i32>()] = unsafe { mem::transmute(a) }; } diff --git a/gcc/testsuite/rust/compile/torture/transmute1.rs b/gcc/testsuite/rust/compile/torture/transmute1.rs index 333fffe..af9a55d 100644 --- a/gcc/testsuite/rust/compile/torture/transmute1.rs +++ b/gcc/testsuite/rust/compile/torture/transmute1.rs @@ -7,5 +7,5 @@ mod mem { fn main() { let a = 123; - let _b: [u8; mem::size_of::<i32>()] = mem::transmute(a); + let _b: [u8; mem::size_of::<i32>()] = unsafe { mem::transmute(a) }; } diff --git a/gcc/testsuite/rust/compile/unsafe6.rs b/gcc/testsuite/rust/compile/unsafe6.rs new file mode 100644 index 0000000..cf4b754 --- /dev/null +++ b/gcc/testsuite/rust/compile/unsafe6.rs @@ -0,0 +1,14 @@ +unsafe fn foo() {} +unsafe fn bar() { + foo(); +} + +fn main() { + foo(); // { dg-error "call to unsafe function" } + bar(); // { dg-error "call to unsafe function" } + + unsafe { + foo(); + bar(); + } +} diff --git a/gcc/testsuite/rust/compile/unsafe7.rs b/gcc/testsuite/rust/compile/unsafe7.rs new file mode 100644 index 0000000..a6b69e1 --- /dev/null +++ b/gcc/testsuite/rust/compile/unsafe7.rs @@ -0,0 +1,9 @@ +extern "C" { + fn printf(fmt: *const i8, ...); +} + +fn main() { + let s = "hey\0"; + + printf(s as *const str as *const i8); // { dg-error "call to extern function" } +} diff --git a/gcc/testsuite/rust/compile/unsafe8.rs b/gcc/testsuite/rust/compile/unsafe8.rs new file mode 100644 index 0000000..03fe491 --- /dev/null +++ b/gcc/testsuite/rust/compile/unsafe8.rs @@ -0,0 +1,14 @@ +struct S; + +impl S { + unsafe fn foo(self) {} +} + +fn main() { + let s = S; + s.foo(); // { dg-error "call to unsafe method" } + + unsafe { + s.foo(); + } +} diff --git a/gcc/testsuite/rust/execute/torture/builtin_macro_cfg.rs b/gcc/testsuite/rust/execute/torture/builtin_macro_cfg.rs index ac24791..9fa5222 100644 --- a/gcc/testsuite/rust/execute/torture/builtin_macro_cfg.rs +++ b/gcc/testsuite/rust/execute/torture/builtin_macro_cfg.rs @@ -9,10 +9,14 @@ extern "C" { } fn print(s: &str) { - printf("%s\n" as *const str as *const i8, s as *const str as *const i8); + unsafe { + printf( + "%s\n" as *const str as *const i8, + s as *const str as *const i8, + ); + } } - fn main() -> i32 { let cfg = cfg!(A); if cfg { diff --git a/gcc/testsuite/rust/execute/torture/builtin_macro_concat.rs b/gcc/testsuite/rust/execute/torture/builtin_macro_concat.rs index ca40585..555d49c 100644 --- a/gcc/testsuite/rust/execute/torture/builtin_macro_concat.rs +++ b/gcc/testsuite/rust/execute/torture/builtin_macro_concat.rs @@ -8,7 +8,12 @@ extern "C" { } fn print(s: &str) { - printf("%s\n" as *const str as *const i8, s as *const str as *const i8); + unsafe { + printf( + "%s\n" as *const str as *const i8, + s as *const str as *const i8, + ); + } } fn main() -> i32 { diff --git a/gcc/testsuite/rust/execute/torture/builtin_macro_env.rs b/gcc/testsuite/rust/execute/torture/builtin_macro_env.rs index ab6f139..211ddfc 100644 --- a/gcc/testsuite/rust/execute/torture/builtin_macro_env.rs +++ b/gcc/testsuite/rust/execute/torture/builtin_macro_env.rs @@ -10,7 +10,12 @@ extern "C" { } fn print(s: &str) { - printf("%s\n" as *const str as *const i8, s as *const str as *const i8); + unsafe { + printf( + "%s\n" as *const str as *const i8, + s as *const str as *const i8, + ); + } } fn main() -> i32 { diff --git a/gcc/testsuite/rust/execute/torture/builtin_macro_include_bytes.rs b/gcc/testsuite/rust/execute/torture/builtin_macro_include_bytes.rs index 3f7ebd2..49da093 100644 --- a/gcc/testsuite/rust/execute/torture/builtin_macro_include_bytes.rs +++ b/gcc/testsuite/rust/execute/torture/builtin_macro_include_bytes.rs @@ -1,7 +1,7 @@ // { dg-output "104\n33\n1\n" } macro_rules! include_bytes { - () => {{}}; + () => {{}}; } extern "C" { @@ -10,35 +10,37 @@ extern "C" { fn print_int(value: i32) { let s = "%d\n\0" as *const str as *const i8; - printf(s, value); + unsafe { + printf(s, value); + } } fn main() -> i32 { - let bytes = include_bytes! ("include.txt"); - - print_int (bytes[0] as i32); - print_int (bytes[14] as i32); - - let the_bytes = b"hello, include!\n"; - - let x = bytes[0] == the_bytes[0] - && bytes[1] == the_bytes [1] - && bytes[2] == the_bytes [2] - && bytes[3] == the_bytes [3] - && bytes[4] == the_bytes [4] - && bytes[5] == the_bytes [5] - && bytes[6] == the_bytes [6] - && bytes[7] == the_bytes [7] - && bytes[8] == the_bytes [8] - && bytes[9] == the_bytes [9] - && bytes[10] == the_bytes [10] - && bytes[11] == the_bytes [11] - && bytes[12] == the_bytes [12] - && bytes[13] == the_bytes [13] - && bytes[14] == the_bytes [14] - && bytes[15] == the_bytes [15]; - - print_int (x as i32); - - 0 + let bytes = include_bytes!("include.txt"); + + print_int(bytes[0] as i32); + print_int(bytes[14] as i32); + + let the_bytes = b"hello, include!\n"; + + let x = bytes[0] == the_bytes[0] + && bytes[1] == the_bytes[1] + && bytes[2] == the_bytes[2] + && bytes[3] == the_bytes[3] + && bytes[4] == the_bytes[4] + && bytes[5] == the_bytes[5] + && bytes[6] == the_bytes[6] + && bytes[7] == the_bytes[7] + && bytes[8] == the_bytes[8] + && bytes[9] == the_bytes[9] + && bytes[10] == the_bytes[10] + && bytes[11] == the_bytes[11] + && bytes[12] == the_bytes[12] + && bytes[13] == the_bytes[13] + && bytes[14] == the_bytes[14] + && bytes[15] == the_bytes[15]; + + print_int(x as i32); + + 0 } diff --git a/gcc/testsuite/rust/execute/torture/builtin_macro_include_str.rs b/gcc/testsuite/rust/execute/torture/builtin_macro_include_str.rs index 095d7cb..334b9c6 100644 --- a/gcc/testsuite/rust/execute/torture/builtin_macro_include_str.rs +++ b/gcc/testsuite/rust/execute/torture/builtin_macro_include_str.rs @@ -1,7 +1,7 @@ // { dg-output "hello, include!\n" } macro_rules! include_str { - () => {{}}; + () => {{}}; } extern "C" { @@ -9,15 +9,19 @@ extern "C" { } fn print(s: &str) { - printf("%s" as *const str as *const i8, s as *const str as *const i8); + unsafe { + printf( + "%s" as *const str as *const i8, + s as *const str as *const i8, + ); + } } - fn main() -> i32 { - // include_str! (and include_bytes!) allow for an optional trailing comma. - let my_str = include_str! ("include.txt",); + // include_str! (and include_bytes!) allow for an optional trailing comma. + let my_str = include_str!("include.txt",); - print (my_str); + print(my_str); - 0 + 0 } diff --git a/gcc/testsuite/rust/execute/torture/builtin_macro_line.rs b/gcc/testsuite/rust/execute/torture/builtin_macro_line.rs index 873054d..6153bf5 100644 --- a/gcc/testsuite/rust/execute/torture/builtin_macro_line.rs +++ b/gcc/testsuite/rust/execute/torture/builtin_macro_line.rs @@ -1,10 +1,12 @@ -// { dg-output "15\n18\n" } +// { dg-output "17\n20\n" } extern "C" { fn printf(fmt: *const i8, ...); } fn print(s: u32) { - printf("%u\n\0" as *const str as *const i8, s); + unsafe { + printf("%u\n\0" as *const str as *const i8, s); + } } macro_rules! line { diff --git a/gcc/testsuite/rust/execute/torture/builtin_macros1.rs b/gcc/testsuite/rust/execute/torture/builtin_macros1.rs index d68f62f..6f52c37 100644 --- a/gcc/testsuite/rust/execute/torture/builtin_macros1.rs +++ b/gcc/testsuite/rust/execute/torture/builtin_macros1.rs @@ -8,7 +8,9 @@ extern "C" { } fn print(s: &str) { - printf("%s\n\0" as *const str as *const i8, s); + unsafe { + printf("%s\n\0" as *const str as *const i8, s); + } } fn main() -> i32 { diff --git a/gcc/testsuite/rust/execute/torture/builtin_macros3.rs b/gcc/testsuite/rust/execute/torture/builtin_macros3.rs index ed11780..6c0facb 100644 --- a/gcc/testsuite/rust/execute/torture/builtin_macros3.rs +++ b/gcc/testsuite/rust/execute/torture/builtin_macros3.rs @@ -8,7 +8,9 @@ extern "C" { } fn print(s: u32) { - printf("%u\n\0" as *const str as *const i8, s); + unsafe { + printf("%u\n\0" as *const str as *const i8, s); + } } fn main() -> i32 { @@ -21,4 +23,5 @@ fn main() -> i32 { print(c1); 0 -}
\ No newline at end of file +} + diff --git a/gcc/testsuite/rust/execute/torture/macros10.rs b/gcc/testsuite/rust/execute/torture/macros10.rs index f1fc34e..155a440 100644 --- a/gcc/testsuite/rust/execute/torture/macros10.rs +++ b/gcc/testsuite/rust/execute/torture/macros10.rs @@ -5,7 +5,9 @@ extern "C" { fn print_int(value: i32) { let s = "%d\n\0" as *const str as *const i8; - printf(s, value); + unsafe { + printf(s, value); + } } macro_rules! add_exprs { diff --git a/gcc/testsuite/rust/execute/torture/macros12.rs b/gcc/testsuite/rust/execute/torture/macros12.rs index ff4a862..d310dff 100644 --- a/gcc/testsuite/rust/execute/torture/macros12.rs +++ b/gcc/testsuite/rust/execute/torture/macros12.rs @@ -5,7 +5,9 @@ extern "C" { fn print_int(value: i32) { let s = "%d\n\0" as *const str as *const i8; - printf(s, value); + unsafe { + printf(s, value); + } } macro_rules! add_exprs { diff --git a/gcc/testsuite/rust/execute/torture/macros13.rs b/gcc/testsuite/rust/execute/torture/macros13.rs index af5dfe8..afb20264 100644 --- a/gcc/testsuite/rust/execute/torture/macros13.rs +++ b/gcc/testsuite/rust/execute/torture/macros13.rs @@ -5,7 +5,9 @@ extern "C" { fn print_int(value: i32) { let s = "%d\n\0" as *const str as *const i8; - printf(s, value); + unsafe { + printf(s, value); + } } macro_rules! add_exprs { diff --git a/gcc/testsuite/rust/execute/torture/macros14.rs b/gcc/testsuite/rust/execute/torture/macros14.rs index 2dc95e3..0065654 100644 --- a/gcc/testsuite/rust/execute/torture/macros14.rs +++ b/gcc/testsuite/rust/execute/torture/macros14.rs @@ -5,7 +5,9 @@ extern "C" { fn print_int(value: i32) { let s = "%d\n\0" as *const str as *const i8; - printf(s, value); + unsafe { + printf(s, value); + } } macro_rules! add_exprs { diff --git a/gcc/testsuite/rust/execute/torture/macros22.rs b/gcc/testsuite/rust/execute/torture/macros22.rs index 973af23..3f291ac 100644 --- a/gcc/testsuite/rust/execute/torture/macros22.rs +++ b/gcc/testsuite/rust/execute/torture/macros22.rs @@ -1,9 +1,11 @@ // { dg-output "1\n2\nNaN\n3\n" } macro_rules! print_num { - ($l:literal) => { - printf("%d\n\0" as *const str as *const i8, $l); - }; + ($l:literal) => {{ + unsafe { + printf("%d\n\0" as *const str as *const i8, $l); + } + }}; } extern "C" { @@ -15,7 +17,9 @@ fn main() -> i32 { print_num!(1); print_num!(2); - printf("NaN\n\0" as *const str as *const i8); + unsafe { + printf("NaN\n\0" as *const str as *const i8); + } print_num!(3); diff --git a/gcc/testsuite/rust/execute/torture/macros29.rs b/gcc/testsuite/rust/execute/torture/macros29.rs index 7bce29b..506d660 100644 --- a/gcc/testsuite/rust/execute/torture/macros29.rs +++ b/gcc/testsuite/rust/execute/torture/macros29.rs @@ -1,21 +1,23 @@ // { dg-output "1\n" } macro_rules! concat { - () => {{}}; + () => {{}}; } extern "C" { - fn printf(fmt: *const i8, ...); + fn printf(fmt: *const i8, ...); } fn print(s: u32) { - printf("%u\n\0" as *const str as *const i8, s); + unsafe { + printf("%u\n\0" as *const str as *const i8, s); + } } -fn main () -> i32 { - let res = concat!("test2") == "test3"; - if !res { - print(1); - } +fn main() -> i32 { + let res = concat!("test2") == "test3"; + if !res { + print(1); + } - 0 + 0 } diff --git a/gcc/testsuite/rust/execute/torture/macros30.rs b/gcc/testsuite/rust/execute/torture/macros30.rs index 09247e6..8f54b05 100644 --- a/gcc/testsuite/rust/execute/torture/macros30.rs +++ b/gcc/testsuite/rust/execute/torture/macros30.rs @@ -1,22 +1,24 @@ // { dg-output "1\n" } macro_rules! concat { - () => {{}}; + () => {{}}; } extern "C" { - fn printf(fmt: *const i8, ...); + fn printf(fmt: *const i8, ...); } fn print(s: u32) { - printf("%u\n\0" as *const str as *const i8, s); + unsafe { + printf("%u\n\0" as *const str as *const i8, s); + } } -fn main () -> i32 { - let mut x = concat!("x"); - x = concat!("y"); - if x == "y" { - print(1); - } +fn main() -> i32 { + let mut x = concat!("x"); + x = concat!("y"); + if x == "y" { + print(1); + } - 0 + 0 } diff --git a/gcc/testsuite/rust/execute/torture/macros31.rs b/gcc/testsuite/rust/execute/torture/macros31.rs index 1a67c47..6ad6d7e 100644 --- a/gcc/testsuite/rust/execute/torture/macros31.rs +++ b/gcc/testsuite/rust/execute/torture/macros31.rs @@ -9,10 +9,14 @@ extern "C" { } fn print(s: &str) { - printf("%s\n" as *const str as *const i8, s as *const str as *const i8); + unsafe { + printf( + "%s\n" as *const str as *const i8, + s as *const str as *const i8, + ); + } } - fn main() -> i32 { let cfg = cfg!(A) || cfg!(B); if cfg { diff --git a/gcc/testsuite/rust/execute/torture/match_bool1.rs b/gcc/testsuite/rust/execute/torture/match_bool1.rs index 45900b8..101dbb5 100644 --- a/gcc/testsuite/rust/execute/torture/match_bool1.rs +++ b/gcc/testsuite/rust/execute/torture/match_bool1.rs @@ -4,41 +4,46 @@ extern "C" { fn printf(s: *const i8, ...); } -fn foo (x: bool) -> i32 { +fn foo(x: bool) -> i32 { match x { - true => { return 182; }, - false => { return 55; }, + true => { + return 182; + } + false => { + return 55; + } } } -fn bar (y: i32) { - +fn bar(y: i32) { match y < 100 { true => { let a = "%i is less than 100\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c, y); + unsafe { + printf(c, y); + } } _ => { let a = "%i is more than 100\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c, y); + unsafe { + printf(c, y); + } } } } +fn main() -> i32 { + let a = foo(true); + let b = foo(false); -fn main () -> i32 { - - let a = foo (true); - let b = foo (false); - - bar (a); - bar (b); + bar(a); + bar(b); 0 } diff --git a/gcc/testsuite/rust/execute/torture/match_byte1.rs b/gcc/testsuite/rust/execute/torture/match_byte1.rs index 3d09a33..3546cfb 100644 --- a/gcc/testsuite/rust/execute/torture/match_byte1.rs +++ b/gcc/testsuite/rust/execute/torture/match_byte1.rs @@ -4,46 +4,53 @@ extern "C" { fn printf(s: *const i8, ...); } -fn foo (x: u8) { +fn foo(x: u8) { match x { b'a' => { let a = "a\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } } b'\x07' => { let a = "seven\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } } b'\'' => { let a = "quote\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } } _ => { let a = "else\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } } } } -fn main () -> i32 { - +fn main() -> i32 { let x: u8 = 7; - foo (b'a'); - foo (x); - foo (b'\''); - foo (b'\\'); + foo(b'a'); + foo(x); + foo(b'\''); + foo(b'\\'); 0 } diff --git a/gcc/testsuite/rust/execute/torture/match_char1.rs b/gcc/testsuite/rust/execute/torture/match_char1.rs index e9da8ff..fa65876a 100644 --- a/gcc/testsuite/rust/execute/torture/match_char1.rs +++ b/gcc/testsuite/rust/execute/torture/match_char1.rs @@ -4,46 +4,53 @@ extern "C" { fn printf(s: *const i8, ...); } -fn foo (x: char) { +fn foo(x: char) { match x { 'a' => { let a = "amazing\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } } 'c' => { let a = "compiler\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } } 'p' => { let a = "productivity\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } } _ => { let a = "wildcard\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } } } } -fn main () -> i32 { - +fn main() -> i32 { let p = 'p'; - foo ('a'); - foo ('b'); - foo ('c'); - foo (p); + foo('a'); + foo('b'); + foo('c'); + foo(p); 0 } diff --git a/gcc/testsuite/rust/execute/torture/match_int1.rs b/gcc/testsuite/rust/execute/torture/match_int1.rs index b1373bf..209429a 100644 --- a/gcc/testsuite/rust/execute/torture/match_int1.rs +++ b/gcc/testsuite/rust/execute/torture/match_int1.rs @@ -4,91 +4,106 @@ extern "C" { fn printf(s: *const i8, ...); } -fn foo_i32 (x: i32) { +fn foo_i32(x: i32) { match x { 15 => { let a = "fifteen!\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } } _ => { let a = "other!\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } } } } -fn foo_isize (x: isize) { +fn foo_isize(x: isize) { match x { 15 => { let a = "fifteen!\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } } _ => { let a = "other!\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } } } } -fn foo_u32 (x: u32) { +fn foo_u32(x: u32) { match x { 15 => { let a = "fifteen!\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } } _ => { let a = "other!\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } } } } -fn foo_usize (x: usize) { +fn foo_usize(x: usize) { match x { 15 => { let a = "fifteen!\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } } _ => { let a = "other!\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } } } } - -fn main () -> i32 { +fn main() -> i32 { let x = -2; - foo_i32 (x); - foo_i32 (334); - foo_isize (-4768); - foo_isize (15); + foo_i32(x); + foo_i32(334); + foo_isize(-4768); + foo_isize(15); let y = 127; - foo_u32 (15); - foo_u32 (y); - foo_usize (2394); - foo_usize (15); + foo_u32(15); + foo_u32(y); + foo_usize(2394); + foo_usize(15); 0 } diff --git a/gcc/testsuite/rust/execute/torture/match_loop1.rs b/gcc/testsuite/rust/execute/torture/match_loop1.rs index d3aab6b..bb6aee9 100644 --- a/gcc/testsuite/rust/execute/torture/match_loop1.rs +++ b/gcc/testsuite/rust/execute/torture/match_loop1.rs @@ -7,10 +7,10 @@ extern "C" { enum E { One, Two, - Other + Other, } -fn foo () { +fn foo() { let mut x = E::One; loop { @@ -19,7 +19,9 @@ fn foo () { let a = "E::One\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } x = E::Two; } @@ -27,7 +29,9 @@ fn foo () { let a = "E::Two\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } x = E::Other; } @@ -35,7 +39,9 @@ fn foo () { let a = "break!\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } break; } @@ -43,9 +49,8 @@ fn foo () { } } - -fn main () -> i32 { - foo (); +fn main() -> i32 { + foo(); 0 } diff --git a/gcc/testsuite/rust/execute/torture/match_range1.rs b/gcc/testsuite/rust/execute/torture/match_range1.rs index 8fe8f4c..82e9e34 100644 --- a/gcc/testsuite/rust/execute/torture/match_range1.rs +++ b/gcc/testsuite/rust/execute/torture/match_range1.rs @@ -6,29 +6,32 @@ extern "C" { const END_RANGE: i32 = 15; -fn foo (x: i32) { +fn foo(x: i32) { match x { 0..=END_RANGE => { let a = "zero to END_RANGE\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } } _ => { let a = "else\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } } } } -fn main () -> i32 { - - foo (11); - foo (15); - foo (21); +fn main() -> i32 { + foo(11); + foo(15); + foo(21); 0 } diff --git a/gcc/testsuite/rust/execute/torture/match_range2.rs b/gcc/testsuite/rust/execute/torture/match_range2.rs index 82980c2..8153f9e 100644 --- a/gcc/testsuite/rust/execute/torture/match_range2.rs +++ b/gcc/testsuite/rust/execute/torture/match_range2.rs @@ -7,34 +7,39 @@ extern "C" { const BIG_A: char = 'A'; const BIG_Z: char = 'Z'; -fn bar (x: char) { +fn bar(x: char) { match x { - 'a'..='z' => { let a = "lowercase\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } } BIG_A..=BIG_Z => { let a = "uppercase\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } } _ => { let a = "other\n\0"; let b = a as *const str; let c = b as *const i8; - printf (c); + unsafe { + printf(c); + } } } } -fn main () -> i32 { - bar ('b'); - bar ('X'); - bar ('!'); +fn main() -> i32 { + bar('b'); + bar('X'); + bar('!'); 0 } diff --git a/gcc/testsuite/rust/execute/torture/match_tuple1.rs b/gcc/testsuite/rust/execute/torture/match_tuple1.rs index 1874062..cb61cc0 100644 --- a/gcc/testsuite/rust/execute/torture/match_tuple1.rs +++ b/gcc/testsuite/rust/execute/torture/match_tuple1.rs @@ -30,12 +30,16 @@ fn inspect(f: Foo, g: u8) -> i32 { return 25; } -fn main () -> i32 { - let x = inspect (Foo::B, 2); - let y = inspect (Foo::B, 1); +fn main() -> i32 { + let x = inspect(Foo::B, 2); + let y = inspect(Foo::B, 1); - printf ("x:%d\n" as *const str as *const i8, x); - printf ("y:%d\n" as *const str as *const i8, y); + unsafe { + printf("x:%d\n" as *const str as *const i8, x); + } + unsafe { + printf("y:%d\n" as *const str as *const i8, y); + } y - x - 5 } diff --git a/gcc/testsuite/rust/link/simple_function_0.rs b/gcc/testsuite/rust/link/simple_function_0.rs index 5bd3238..5bd4926 100644 --- a/gcc/testsuite/rust/link/simple_function_0.rs +++ b/gcc/testsuite/rust/link/simple_function_0.rs @@ -3,5 +3,6 @@ use simple_function_1::test_func; fn main() -> i32 { let a = test_func(123); + // { dg-bogus "call to extern function" "" { xfail *-*-* } .-1 } a - 124 } |