From 4924160c275d8fafab2dbd4257fb7473fbac5ceb Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Wed, 24 Nov 2021 15:15:40 +0000 Subject: Stop copying HIR::FunctionParams when during lowering Some HIR::TypeNoBounds have a gcc_unreachable() unpon usage of their respective copy constructors this change moves the instances of FunctionParams to avoid the copy. --- gcc/rust/hir/rust-ast-lower-implitem.h | 6 +++--- gcc/rust/hir/rust-ast-lower-item.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/hir/rust-ast-lower-implitem.h b/gcc/rust/hir/rust-ast-lower-implitem.h index 8f9c5d5..3613df5 100644 --- a/gcc/rust/hir/rust-ast-lower-implitem.h +++ b/gcc/rust/hir/rust-ast-lower-implitem.h @@ -153,7 +153,7 @@ public: = HIR::FunctionParam (mapping, std::move (translated_pattern), std::move (translated_type), param.get_locus ()); - function_params.push_back (hir_param); + function_params.push_back (std::move (hir_param)); } bool terminated = false; @@ -239,7 +239,7 @@ public: = HIR::FunctionParam (mapping, std::move (translated_pattern), std::move (translated_type), param.get_locus ()); - function_params.push_back (hir_param); + function_params.push_back (std::move (hir_param)); } bool terminated = false; @@ -345,7 +345,7 @@ public: = HIR::FunctionParam (mapping, std::move (translated_pattern), std::move (translated_type), param.get_locus ()); - function_params.push_back (hir_param); + function_params.push_back (std::move (hir_param)); } HIR::TraitFunctionDecl decl (ref.get_identifier (), std::move (qualifiers), diff --git a/gcc/rust/hir/rust-ast-lower-item.h b/gcc/rust/hir/rust-ast-lower-item.h index 65a4921..697e98a 100644 --- a/gcc/rust/hir/rust-ast-lower-item.h +++ b/gcc/rust/hir/rust-ast-lower-item.h @@ -465,7 +465,7 @@ public: = HIR::FunctionParam (mapping, std::move (translated_pattern), std::move (translated_type), param.get_locus ()); - function_params.push_back (hir_param); + function_params.push_back (std::move (hir_param)); } bool terminated = false; -- cgit v1.1 From f967dd27790a16837b55e6d2c5ee01dcba096f53 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Wed, 24 Nov 2021 16:06:55 +0000 Subject: Remove bad mutability lint This was an initial pass to try and ensure all assignments were valid with respect to the binding mutability. This pass cannot be done at the name resolution level and in rustc is achieved on mir as part of the borrow checker. This patch removes this pass and associated test cases. We will be able to do this type of validation on the GCC tree's such as the C/C++ family const checks we may be able to reuse. Fixes #815 --- gcc/rust/resolve/rust-ast-verify-assignee.h | 8 -------- gcc/testsuite/rust/compile/immutable1.rs | 5 ----- gcc/testsuite/rust/compile/immutable2.rs | 5 ----- gcc/testsuite/rust/compile/immutable3.rs | 4 ---- gcc/testsuite/rust/compile/immutable4.rs | 4 ---- gcc/testsuite/rust/compile/immutable5.rs | 6 ------ 6 files changed, 32 deletions(-) delete mode 100644 gcc/testsuite/rust/compile/immutable1.rs delete mode 100644 gcc/testsuite/rust/compile/immutable2.rs delete mode 100644 gcc/testsuite/rust/compile/immutable3.rs delete mode 100644 gcc/testsuite/rust/compile/immutable4.rs delete mode 100644 gcc/testsuite/rust/compile/immutable5.rs (limited to 'gcc') diff --git a/gcc/rust/resolve/rust-ast-verify-assignee.h b/gcc/rust/resolve/rust-ast-verify-assignee.h index d362fe4..e9f6f23 100644 --- a/gcc/rust/resolve/rust-ast-verify-assignee.h +++ b/gcc/rust/resolve/rust-ast-verify-assignee.h @@ -65,14 +65,6 @@ public: ok = true; // mark the assignment to the name resolver->mark_assignment_to_decl (resolved_node, parent); - - // check is mutable - if (!resolver->decl_is_mutable (resolved_node)) - { - // we only allow a single assignment to immutable decls - if (resolver->get_num_assignments_to_decl (resolved_node) > 1) - rust_error_at (expr.get_locus (), "cannot assign to immutable"); - } } void visit (AST::DereferenceExpr &expr) override diff --git a/gcc/testsuite/rust/compile/immutable1.rs b/gcc/testsuite/rust/compile/immutable1.rs deleted file mode 100644 index d78c1b7..0000000 --- a/gcc/testsuite/rust/compile/immutable1.rs +++ /dev/null @@ -1,5 +0,0 @@ -static x: i32 = 3; - -fn main() { - x = 1; /* { dg-error "cannot assign to immutable" } */ -} diff --git a/gcc/testsuite/rust/compile/immutable2.rs b/gcc/testsuite/rust/compile/immutable2.rs deleted file mode 100644 index 86e70fe..0000000 --- a/gcc/testsuite/rust/compile/immutable2.rs +++ /dev/null @@ -1,5 +0,0 @@ -const TEST_CONST: i32 = 10; - -fn main() { - TEST_CONST = 1; // { dg-error "cannot assign to immutable" } -} diff --git a/gcc/testsuite/rust/compile/immutable3.rs b/gcc/testsuite/rust/compile/immutable3.rs deleted file mode 100644 index fb60280..0000000 --- a/gcc/testsuite/rust/compile/immutable3.rs +++ /dev/null @@ -1,4 +0,0 @@ -fn main() { - let a = 1; - a += 2; // { dg-error "cannot assign to immutable" } -} diff --git a/gcc/testsuite/rust/compile/immutable4.rs b/gcc/testsuite/rust/compile/immutable4.rs deleted file mode 100644 index fb14554..0000000 --- a/gcc/testsuite/rust/compile/immutable4.rs +++ /dev/null @@ -1,4 +0,0 @@ -fn main() { - let array: [i32; 3] = [0; 3]; - array[0] = 1; // { dg-error "cannot assign to immutable" } -} diff --git a/gcc/testsuite/rust/compile/immutable5.rs b/gcc/testsuite/rust/compile/immutable5.rs deleted file mode 100644 index db23609..0000000 --- a/gcc/testsuite/rust/compile/immutable5.rs +++ /dev/null @@ -1,6 +0,0 @@ -struct Foo(f32, i32); - -fn main() { - let a = Foo(1, 2); - a.0 = 22; // { dg-error "cannot assign to immutable" } -} -- cgit v1.1 From e7e65bbdf3a06a7d00ed7da4ff8ebb16031ed6c3 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Wed, 24 Nov 2021 16:31:49 +0000 Subject: Allow references to arrays for ArrayIndexExpr accessor's When we have an array-index expr rust allows the array reference to be a reference and the compiler is meant to add in the required implicit indirection. This checks for this senario and injects the indirection during code-generation. Fixes #815 --- gcc/rust/backend/rust-compile-expr.h | 26 ++++++++++++++++++++-- gcc/rust/typecheck/rust-hir-type-check-expr.h | 32 +++++++++++++++------------ gcc/testsuite/rust/compile/array3.rs | 5 +++++ 3 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 gcc/testsuite/rust/compile/array3.rs (limited to 'gcc') diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h index 2bf969b..46d501a 100644 --- a/gcc/rust/backend/rust-compile-expr.h +++ b/gcc/rust/backend/rust-compile-expr.h @@ -390,10 +390,32 @@ public: void visit (HIR::ArrayIndexExpr &expr) override { - tree array = CompileExpr::Compile (expr.get_array_expr (), ctx); + tree array_reference = CompileExpr::Compile (expr.get_array_expr (), ctx); tree index = CompileExpr::Compile (expr.get_index_expr (), ctx); + + // lets check if the array is a reference type then we can add an + // indirection if required + TyTy::BaseType *array_expr_ty = nullptr; + bool ok = ctx->get_tyctx ()->lookup_type ( + expr.get_array_expr ()->get_mappings ().get_hirid (), &array_expr_ty); + rust_assert (ok); + + // do we need to add an indirect reference + if (array_expr_ty->get_kind () == TyTy::TypeKind::REF) + { + TyTy::ReferenceType *r + = static_cast (array_expr_ty); + TyTy::BaseType *tuple_type = r->get_base (); + tree array_tyty = TyTyResolveCompile::compile (ctx, tuple_type); + + array_reference + = ctx->get_backend ()->indirect_expression (array_tyty, + array_reference, true, + expr.get_locus ()); + } + translated - = ctx->get_backend ()->array_index_expression (array, index, + = ctx->get_backend ()->array_index_expression (array_reference, index, expr.get_locus ()); } diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h index fb1cd97..8c34b6c 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.h +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h @@ -929,24 +929,27 @@ public: auto resolved_index_expr = size_ty->unify (TypeCheckExpr::Resolve (expr.get_index_expr (), false)); - if (resolved_index_expr == nullptr) + if (resolved_index_expr->get_kind () != TyTy::TypeKind::ERROR) { - rust_error_at (expr.get_index_expr ()->get_locus (), - "Type Resolver failure in Index for ArrayIndexExpr"); - return; + // allow the index expr to fail lets just continue on + context->insert_type (expr.get_index_expr ()->get_mappings (), + resolved_index_expr); } - context->insert_type (expr.get_index_expr ()->get_mappings (), - resolved_index_expr); - // resolve the array reference - expr.get_array_expr ()->accept_vis (*this); - if (infered == nullptr) + auto array_expr_ty + = TypeCheckExpr::Resolve (expr.get_array_expr (), inside_loop); + if (array_expr_ty->get_kind () == TyTy::TypeKind::ERROR) + return; + else if (array_expr_ty->get_kind () == TyTy::TypeKind::REF) { - rust_error_at (expr.get_index_expr ()->get_locus (), - "failed to resolve array reference expression"); - return; + // lets try and deref it since rust allows this + auto ref = static_cast (array_expr_ty); + auto base = ref->get_base (); + if (base->get_kind () == TyTy::TypeKind::ARRAY) + array_expr_ty = base; } - else if (infered->get_kind () != TyTy::TypeKind::ARRAY) + + if (array_expr_ty->get_kind () != TyTy::TypeKind::ARRAY) { rust_error_at (expr.get_index_expr ()->get_locus (), "expected an ArrayType got [%s]", @@ -955,7 +958,8 @@ public: return; } - TyTy::ArrayType *array_type = static_cast (infered); + TyTy::ArrayType *array_type + = static_cast (array_expr_ty); infered = array_type->get_element_type ()->clone (); } diff --git a/gcc/testsuite/rust/compile/array3.rs b/gcc/testsuite/rust/compile/array3.rs new file mode 100644 index 0000000..a62c6ca --- /dev/null +++ b/gcc/testsuite/rust/compile/array3.rs @@ -0,0 +1,5 @@ +fn foo(state: &mut [u32; 16], a: usize) { + // { dg-warning "function is never used: .foo." "" { target *-*-* } .-1 } + // { dg-warning "unused name .foo." "" { target *-*-* } .-2 } + state[a] = 1; +} -- cgit v1.1