public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] Allow references to arrays for ArrayIndexExpr accessor's Date: Wed, 8 Jun 2022 11:54:17 +0000 (GMT) [thread overview] Message-ID: <20220608115417.2FA6939960EA@sourceware.org> (raw) https://gcc.gnu.org/g:e7e65bbdf3a06a7d00ed7da4ff8ebb16031ed6c3 commit e7e65bbdf3a06a7d00ed7da4ff8ebb16031ed6c3 Author: Philip Herron <philip.herron@embecosm.com> Date: Wed Nov 24 16:31:49 2021 +0000 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 Diff: --- 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(-) diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h index 2bf969beef9..46d501a5859 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<TyTy::ReferenceType *> (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 fb1cd972293..8c34b6c776a 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<TyTy::ReferenceType *> (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<TyTy::ArrayType *> (infered); + TyTy::ArrayType *array_type + = static_cast<TyTy::ArrayType *> (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 00000000000..a62c6ca69c7 --- /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; +}
reply other threads:[~2022-06-08 11:54 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20220608115417.2FA6939960EA@sourceware.org \ --to=tschwinge@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).