aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/backend/rust-compile-expr.h42
-rw-r--r--gcc/rust/backend/rust-compile.cc142
-rw-r--r--gcc/rust/hir/tree/rust-hir-expr.h62
-rw-r--r--gcc/rust/hir/tree/rust-hir-item.h18
-rw-r--r--gcc/rust/lint/rust-lint-marklive.cc6
-rw-r--r--gcc/rust/lint/rust-lint-marklive.h30
-rw-r--r--gcc/rust/lint/rust-lint-scan-deadcode.h20
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-expr.h9
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-stmt.h44
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-toplevel.h45
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check.cc33
-rw-r--r--gcc/rust/typecheck/rust-tycheck-dump.h10
-rw-r--r--gcc/rust/typecheck/rust-tyty.cc174
-rw-r--r--gcc/testsuite/rust/compile/func3.rs6
-rw-r--r--gcc/testsuite/rust/compile/tuple_struct3.rs5
-rw-r--r--gcc/testsuite/rust/execute/torture/coercion1.rs43
-rw-r--r--gcc/testsuite/rust/execute/torture/coercion2.rs41
17 files changed, 430 insertions, 300 deletions
diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h
index eb245dc..170580b 100644
--- a/gcc/rust/backend/rust-compile-expr.h
+++ b/gcc/rust/backend/rust-compile-expr.h
@@ -363,12 +363,28 @@ public:
void visit (HIR::AssignmentExpr &expr) override
{
fncontext fn = ctx->peek_fn ();
- auto lhs = CompileExpr::Compile (expr.get_lhs (), ctx);
- auto rhs = CompileExpr::Compile (expr.get_rhs (), ctx);
+ auto lvalue = CompileExpr::Compile (expr.get_lhs (), ctx);
+ auto rvalue = CompileExpr::Compile (expr.get_rhs (), ctx);
+
+ // assignments are coercion sites so lets convert the rvalue if necessary
+ TyTy::BaseType *expected = nullptr;
+ TyTy::BaseType *actual = nullptr;
+
+ bool ok;
+ ok = ctx->get_tyctx ()->lookup_type (
+ expr.get_lhs ()->get_mappings ().get_hirid (), &expected);
+ rust_assert (ok);
+
+ ok = ctx->get_tyctx ()->lookup_type (
+ expr.get_rhs ()->get_mappings ().get_hirid (), &actual);
+ rust_assert (ok);
+
+ rvalue = coercion_site (rvalue, actual, expected, expr.get_locus ());
Bstatement *assignment
- = ctx->get_backend ()->assignment_statement (fn.fndecl, lhs, rhs,
+ = ctx->get_backend ()->assignment_statement (fn.fndecl, lvalue, rvalue,
expr.get_locus ());
+
ctx->add_statement (assignment);
}
@@ -412,11 +428,11 @@ public:
void visit (HIR::ArrayElemsValues &elems) override
{
- elems.iterate ([&] (HIR::Expr *e) mutable -> bool {
- Bexpression *translated_expr = CompileExpr::Compile (e, ctx);
- constructor.push_back (translated_expr);
- return true;
- });
+ for (auto &elem : elems.get_values ())
+ {
+ Bexpression *translated_expr = CompileExpr::Compile (elem.get (), ctx);
+ constructor.push_back (translated_expr);
+ }
}
void visit (HIR::ArrayElemsCopied &elems) override
@@ -646,11 +662,11 @@ public:
// this assumes all fields are in order from type resolution and if a base
// struct was specified those fields are filed via accesors
std::vector<Bexpression *> vals;
- struct_expr.iterate ([&] (HIR::StructExprField *field) mutable -> bool {
- Bexpression *expr = CompileStructExprField::Compile (field, ctx);
- vals.push_back (expr);
- return true;
- });
+ for (auto &field : struct_expr.get_fields ())
+ {
+ Bexpression *expr = CompileStructExprField::Compile (field.get (), ctx);
+ vals.push_back (expr);
+ }
translated
= ctx->get_backend ()->constructor_expression (type, vals,
diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc
index 58c679f..23a035f 100644
--- a/gcc/rust/backend/rust-compile.cc
+++ b/gcc/rust/backend/rust-compile.cc
@@ -69,39 +69,120 @@ CompileExpr::visit (HIR::CallExpr &expr)
|| tyty->get_kind () == TyTy::TypeKind::FNPTR;
if (!is_fn)
{
- Btype *type = TyTyResolveCompile::compile (ctx, tyty);
+ rust_assert (tyty->get_kind () == TyTy::TypeKind::ADT);
+ TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (tyty);
+ Btype *compiled_adt_type = TyTyResolveCompile::compile (ctx, tyty);
// this assumes all fields are in order from type resolution and if a
// base struct was specified those fields are filed via accesors
std::vector<Bexpression *> vals;
- expr.iterate_params ([&] (HIR::Expr *argument) mutable -> bool {
- Bexpression *e = CompileExpr::Compile (argument, ctx);
- vals.push_back (e);
- return true;
- });
+ for (size_t i = 0; i < expr.get_arguments ().size (); i++)
+ {
+ auto &argument = expr.get_arguments ().at (i);
+ auto rvalue = CompileExpr::Compile (argument.get (), ctx);
+
+ // assignments are coercion sites so lets convert the rvalue if
+ // necessary
+ auto respective_field = adt->get_field (i);
+ auto expected = respective_field->get_field_type ();
+
+ TyTy::BaseType *actual = nullptr;
+ bool ok = ctx->get_tyctx ()->lookup_type (
+ argument->get_mappings ().get_hirid (), &actual);
+ rust_assert (ok);
+
+ // coerce it if required
+ rvalue = coercion_site (rvalue, actual, expected, expr.get_locus ());
+
+ // add it to the list
+ vals.push_back (rvalue);
+ }
translated
- = ctx->get_backend ()->constructor_expression (type, vals, -1,
- expr.get_locus ());
+ = ctx->get_backend ()->constructor_expression (compiled_adt_type, vals,
+ -1, expr.get_locus ());
}
else
{
- // must be a call to a function
- Bexpression *fn = CompileExpr::Compile (expr.get_fnexpr (), ctx);
- rust_assert (fn != nullptr);
+ auto get_parameter_tyty_at_index
+ = [] (const TyTy::BaseType *base, size_t index,
+ TyTy::BaseType **result) -> bool {
+ bool is_fn = base->get_kind () == TyTy::TypeKind::FNDEF
+ || base->get_kind () == TyTy::TypeKind::FNPTR;
+ rust_assert (is_fn);
+
+ if (base->get_kind () == TyTy::TypeKind::FNPTR)
+ {
+ const TyTy::FnPtr *fn = static_cast<const TyTy::FnPtr *> (base);
+ *result = fn->param_at (index);
+
+ return true;
+ }
+
+ const TyTy::FnType *fn = static_cast<const TyTy::FnType *> (base);
+ auto param = fn->param_at (index);
+ *result = param.second;
- std::vector<Bexpression *> args;
- expr.iterate_params ([&] (HIR::Expr *p) mutable -> bool {
- Bexpression *compiled_expr = CompileExpr::Compile (p, ctx);
- rust_assert (compiled_expr != nullptr);
- args.push_back (compiled_expr);
return true;
- });
+ };
+
+ bool is_varadic = false;
+ if (tyty->get_kind () == TyTy::TypeKind::FNDEF)
+ {
+ const TyTy::FnType *fn = static_cast<const TyTy::FnType *> (tyty);
+ is_varadic = fn->is_varadic ();
+ }
+
+ size_t required_num_args;
+ if (tyty->get_kind () == TyTy::TypeKind::FNDEF)
+ {
+ const TyTy::FnType *fn = static_cast<const TyTy::FnType *> (tyty);
+ required_num_args = fn->num_params ();
+ }
+ else
+ {
+ const TyTy::FnPtr *fn = static_cast<const TyTy::FnPtr *> (tyty);
+ required_num_args = fn->num_params ();
+ }
+ std::vector<Bexpression *> args;
+ for (size_t i = 0; i < expr.get_arguments ().size (); i++)
+ {
+ auto &argument = expr.get_arguments ().at (i);
+ auto rvalue = CompileExpr::Compile (argument.get (), ctx);
+
+ if (is_varadic && i >= required_num_args)
+ {
+ args.push_back (rvalue);
+ continue;
+ }
+
+ // assignments are coercion sites so lets convert the rvalue if
+ // necessary
+ bool ok;
+ TyTy::BaseType *expected = nullptr;
+ ok = get_parameter_tyty_at_index (tyty, i, &expected);
+ rust_assert (ok);
+
+ TyTy::BaseType *actual = nullptr;
+ ok = ctx->get_tyctx ()->lookup_type (
+ argument->get_mappings ().get_hirid (), &actual);
+ rust_assert (ok);
+
+ // coerce it if required
+ rvalue = coercion_site (rvalue, actual, expected, expr.get_locus ());
+
+ // add it to the list
+ args.push_back (rvalue);
+ }
+
+ // must be a call to a function
+ auto fn_address = CompileExpr::Compile (expr.get_fnexpr (), ctx);
auto fncontext = ctx->peek_fn ();
translated
- = ctx->get_backend ()->call_expression (fncontext.fndecl, fn, args,
- nullptr, expr.get_locus ());
+ = ctx->get_backend ()->call_expression (fncontext.fndecl, fn_address,
+ args, nullptr,
+ expr.get_locus ());
}
}
@@ -224,12 +305,12 @@ CompileExpr::visit (HIR::MethodCallExpr &expr)
std::vector<Bexpression *> args;
args.push_back (self_argument);
- expr.iterate_params ([&] (HIR::Expr *p) mutable -> bool {
- Bexpression *compiled_expr = CompileExpr::Compile (p, ctx);
- rust_assert (compiled_expr != nullptr);
- args.push_back (compiled_expr);
- return true;
- });
+ for (auto &argument : expr.get_arguments ())
+ {
+ Bexpression *compiled_expr
+ = CompileExpr::Compile (argument.get (), ctx);
+ args.push_back (compiled_expr);
+ }
Bexpression *fn_expr
= ctx->get_backend ()->var_expression (fn_convert_expr_tmp,
@@ -414,12 +495,11 @@ CompileExpr::visit (HIR::MethodCallExpr &expr)
args.push_back (self);
// normal args
- expr.iterate_params ([&] (HIR::Expr *p) mutable -> bool {
- Bexpression *compiled_expr = CompileExpr::Compile (p, ctx);
- rust_assert (compiled_expr != nullptr);
- args.push_back (compiled_expr);
- return true;
- });
+ for (auto &argument : expr.get_arguments ())
+ {
+ Bexpression *compiled_expr = CompileExpr::Compile (argument.get (), ctx);
+ args.push_back (compiled_expr);
+ }
auto fncontext = ctx->peek_fn ();
translated
diff --git a/gcc/rust/hir/tree/rust-hir-expr.h b/gcc/rust/hir/tree/rust-hir-expr.h
index 05bc1f9..66de17d 100644
--- a/gcc/rust/hir/tree/rust-hir-expr.h
+++ b/gcc/rust/hir/tree/rust-hir-expr.h
@@ -796,14 +796,7 @@ public:
size_t get_num_elements () const { return values.size (); }
- void iterate (std::function<bool (Expr *)> cb)
- {
- for (auto it = values.begin (); it != values.end (); it++)
- {
- if (!cb ((*it).get ()))
- return;
- }
- }
+ std::vector<std::unique_ptr<Expr> > &get_values () { return values; }
protected:
ArrayElemsValues *clone_array_elems_impl () const override
@@ -1070,15 +1063,6 @@ public:
bool is_unit () const { return tuple_elems.size () == 0; }
- void iterate (std::function<bool (Expr *)> cb)
- {
- for (auto &tuple_elem : tuple_elems)
- {
- if (!cb (tuple_elem.get ()))
- return;
- }
- }
-
protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
@@ -1491,15 +1475,6 @@ public:
void accept_vis (HIRVisitor &vis) override;
- void iterate (std::function<bool (StructExprField *)> cb)
- {
- for (auto &field : fields)
- {
- if (!cb (field.get ()))
- return;
- }
- }
-
std::vector<std::unique_ptr<StructExprField> > &get_fields ()
{
return fields;
@@ -1510,11 +1485,6 @@ public:
return fields;
};
- std::vector<std::unique_ptr<StructExprField> > get_fields_as_owner ()
- {
- return std::move (fields);
- };
-
void set_fields_as_owner (
std::vector<std::unique_ptr<StructExprField> > new_fields)
{
@@ -1578,14 +1548,10 @@ protected:
}
};
-// Forward decl for Function - used in CallExpr
-class Function;
-
// Function call expression HIR node
class CallExpr : public ExprWithoutBlock
{
std::unique_ptr<Expr> function;
- // inlined form of CallParams
std::vector<std::unique_ptr<Expr> > params;
Location locus;
@@ -1642,13 +1608,11 @@ public:
size_t num_params () const { return params.size (); }
- void iterate_params (std::function<bool (Expr *)> cb)
+ std::vector<std::unique_ptr<Expr> > &get_arguments () { return params; }
+
+ const std::vector<std::unique_ptr<Expr> > &get_arguments () const
{
- for (auto &param : params)
- {
- if (!cb (param.get ()))
- return;
- }
+ return params;
}
protected:
@@ -1731,21 +1695,13 @@ public:
PathExprSegment get_method_name () const { return method_name; };
- std::vector<std::unique_ptr<Expr> > &get_params () { return params; }
- const std::vector<std::unique_ptr<Expr> > &get_params () const
- {
- return params;
- }
-
size_t num_params () const { return params.size (); }
- void iterate_params (std::function<bool (Expr *)> cb)
+ std::vector<std::unique_ptr<Expr> > &get_arguments () { return params; }
+
+ const std::vector<std::unique_ptr<Expr> > &get_arguments () const
{
- for (auto &param : params)
- {
- if (!cb (param.get ()))
- return;
- }
+ return params;
}
protected:
diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h
index 8cd7a01..54e32f7 100644
--- a/gcc/rust/hir/tree/rust-hir-item.h
+++ b/gcc/rust/hir/tree/rust-hir-item.h
@@ -1492,14 +1492,7 @@ public:
void accept_vis (HIRVisitor &vis) override;
- void iterate (std::function<bool (StructField &)> cb)
- {
- for (auto &field : fields)
- {
- if (!cb (field))
- return;
- }
- }
+ std::vector<StructField> &get_fields () { return fields; }
protected:
/* Use covariance to implement clone function as returning this object
@@ -1610,15 +1603,6 @@ public:
std::vector<TupleField> &get_fields () { return fields; }
const std::vector<TupleField> &get_fields () const { return fields; }
- void iterate (std::function<bool (TupleField &)> cb)
- {
- for (auto &field : fields)
- {
- if (!cb (field))
- return;
- }
- }
-
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
diff --git a/gcc/rust/lint/rust-lint-marklive.cc b/gcc/rust/lint/rust-lint-marklive.cc
index 0871291..4b095ab4 100644
--- a/gcc/rust/lint/rust-lint-marklive.cc
+++ b/gcc/rust/lint/rust-lint-marklive.cc
@@ -149,10 +149,8 @@ MarkLive::visit (HIR::MethodCallExpr &expr)
{
expr.get_receiver ()->accept_vis (*this);
visit_path_segment (expr.get_method_name ());
- expr.iterate_params ([&] (HIR::Expr *param) -> bool {
- param->accept_vis (*this);
- return true;
- });
+ for (auto &argument : expr.get_arguments ())
+ argument->accept_vis (*this);
// Trying to find the method definition and mark it alive.
NodeId ast_node_id = expr.get_mappings ().get_nodeid ();
diff --git a/gcc/rust/lint/rust-lint-marklive.h b/gcc/rust/lint/rust-lint-marklive.h
index 062bb96..7b7b68f 100644
--- a/gcc/rust/lint/rust-lint-marklive.h
+++ b/gcc/rust/lint/rust-lint-marklive.h
@@ -81,18 +81,18 @@ public:
void visit (HIR::ArrayElemsValues &expr) override
{
- expr.iterate ([&] (HIR::Expr *expr) mutable -> bool {
- expr->accept_vis (*this);
- return true;
- });
+ for (auto &elem : expr.get_values ())
+ {
+ elem->accept_vis (*this);
+ }
}
void visit (HIR::TupleExpr &expr) override
{
- expr.iterate ([&] (HIR::Expr *expr) mutable -> bool {
- expr->accept_vis (*this);
- return true;
- });
+ for (auto &elem : expr.get_tuple_elems ())
+ {
+ elem->accept_vis (*this);
+ }
}
void visit (HIR::BlockExpr &expr) override
@@ -165,10 +165,8 @@ public:
void visit (HIR::CallExpr &expr) override
{
expr.get_fnexpr ()->accept_vis (*this);
- expr.iterate_params ([&] (HIR::Expr *expr) -> bool {
- expr->accept_vis (*this);
- return true;
- });
+ for (auto &argument : expr.get_arguments ())
+ argument->accept_vis (*this);
}
void visit (HIR::ArithmeticOrLogicalExpr &expr) override
@@ -236,10 +234,10 @@ public:
void visit (HIR::StructExprStructFields &stct) override
{
- stct.iterate ([&] (HIR::StructExprField *field) -> bool {
- field->accept_vis (*this);
- return true;
- });
+ for (auto &field : stct.get_fields ())
+ {
+ field->accept_vis (*this);
+ }
stct.get_struct_name ().accept_vis (*this);
if (stct.has_struct_base ())
diff --git a/gcc/rust/lint/rust-lint-scan-deadcode.h b/gcc/rust/lint/rust-lint-scan-deadcode.h
index 464852a..152858a 100644
--- a/gcc/rust/lint/rust-lint-scan-deadcode.h
+++ b/gcc/rust/lint/rust-lint-scan-deadcode.h
@@ -88,16 +88,16 @@ public:
else
{
// only warn the unused fields when in unwarned struct.
- stct.iterate ([&] (HIR::StructField &field) -> bool {
- HirId field_hir_id = field.get_mappings ().get_hirid ();
- if (should_warn (field_hir_id))
- {
- rust_warning_at (field.get_locus (), 0,
- "field is never read: %<%s%>",
- field.get_field_name ().c_str ());
- }
- return true;
- });
+ for (auto &field : stct.get_fields ())
+ {
+ HirId field_hir_id = field.get_mappings ().get_hirid ();
+ if (should_warn (field_hir_id))
+ {
+ rust_warning_at (field.get_locus (), 0,
+ "field is never read: %<%s%>",
+ field.get_field_name ().c_str ());
+ }
+ }
}
}
diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h
index ae1ac29..aa23e64 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h
@@ -880,10 +880,11 @@ public:
void visit (HIR::ArrayElemsValues &elems) override
{
std::vector<TyTy::BaseType *> types;
- elems.iterate ([&] (HIR::Expr *e) mutable -> bool {
- types.push_back (TypeCheckExpr::Resolve (e, false));
- return true;
- });
+
+ for (auto &elem : elems.get_values ())
+ {
+ types.push_back (TypeCheckExpr::Resolve (elem.get (), false));
+ }
infered_array_elems
= TyTy::TyVar::get_implicit_infer_var (root_array_expr_locus).get_tyty ();
diff --git a/gcc/rust/typecheck/rust-hir-type-check-stmt.h b/gcc/rust/typecheck/rust-hir-type-check-stmt.h
index 5f4721b..3f8d17e 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-stmt.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-stmt.h
@@ -144,17 +144,18 @@ public:
std::vector<TyTy::StructFieldType *> fields;
size_t idx = 0;
- struct_decl.iterate ([&] (HIR::TupleField &field) mutable -> bool {
- TyTy::BaseType *field_type
- = TypeCheckType::Resolve (field.get_field_type ().get ());
- TyTy::StructFieldType *ty_field
- = new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
- std::to_string (idx), field_type);
- fields.push_back (ty_field);
- context->insert_type (field.get_mappings (), ty_field->get_field_type ());
- idx++;
- return true;
- });
+ for (auto &field : struct_decl.get_fields ())
+ {
+ TyTy::BaseType *field_type
+ = TypeCheckType::Resolve (field.get_field_type ().get ());
+ TyTy::StructFieldType *ty_field
+ = new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
+ std::to_string (idx), field_type);
+ fields.push_back (ty_field);
+ context->insert_type (field.get_mappings (),
+ ty_field->get_field_type ());
+ idx++;
+ }
TyTy::BaseType *type
= new TyTy::ADTType (struct_decl.get_mappings ().get_hirid (),
@@ -196,16 +197,17 @@ public:
}
std::vector<TyTy::StructFieldType *> fields;
- struct_decl.iterate ([&] (HIR::StructField &field) mutable -> bool {
- TyTy::BaseType *field_type
- = TypeCheckType::Resolve (field.get_field_type ().get ());
- TyTy::StructFieldType *ty_field
- = new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
- field.get_field_name (), field_type);
- fields.push_back (ty_field);
- context->insert_type (field.get_mappings (), ty_field->get_field_type ());
- return true;
- });
+ for (auto &field : struct_decl.get_fields ())
+ {
+ TyTy::BaseType *field_type
+ = TypeCheckType::Resolve (field.get_field_type ().get ());
+ TyTy::StructFieldType *ty_field
+ = new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
+ field.get_field_name (), field_type);
+ fields.push_back (ty_field);
+ context->insert_type (field.get_mappings (),
+ ty_field->get_field_type ());
+ }
TyTy::BaseType *type
= new TyTy::ADTType (struct_decl.get_mappings ().get_hirid (),
diff --git a/gcc/rust/typecheck/rust-hir-type-check-toplevel.h b/gcc/rust/typecheck/rust-hir-type-check-toplevel.h
index 9fac813..131149f 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-toplevel.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-toplevel.h
@@ -79,17 +79,18 @@ public:
std::vector<TyTy::StructFieldType *> fields;
size_t idx = 0;
- struct_decl.iterate ([&] (HIR::TupleField &field) mutable -> bool {
- TyTy::BaseType *field_type
- = TypeCheckType::Resolve (field.get_field_type ().get ());
- TyTy::StructFieldType *ty_field
- = new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
- std::to_string (idx), field_type);
- fields.push_back (ty_field);
- context->insert_type (field.get_mappings (), ty_field->get_field_type ());
- idx++;
- return true;
- });
+ for (auto &field : struct_decl.get_fields ())
+ {
+ TyTy::BaseType *field_type
+ = TypeCheckType::Resolve (field.get_field_type ().get ());
+ TyTy::StructFieldType *ty_field
+ = new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
+ std::to_string (idx), field_type);
+ fields.push_back (ty_field);
+ context->insert_type (field.get_mappings (),
+ ty_field->get_field_type ());
+ idx++;
+ }
TyTy::BaseType *type
= new TyTy::ADTType (struct_decl.get_mappings ().get_hirid (),
@@ -136,16 +137,18 @@ public:
}
std::vector<TyTy::StructFieldType *> fields;
- struct_decl.iterate ([&] (HIR::StructField &field) mutable -> bool {
- TyTy::BaseType *field_type
- = TypeCheckType::Resolve (field.get_field_type ().get ());
- TyTy::StructFieldType *ty_field
- = new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
- field.get_field_name (), field_type);
- fields.push_back (ty_field);
- context->insert_type (field.get_mappings (), ty_field->get_field_type ());
- return true;
- });
+
+ for (auto &field : struct_decl.get_fields ())
+ {
+ TyTy::BaseType *field_type
+ = TypeCheckType::Resolve (field.get_field_type ().get ());
+ TyTy::StructFieldType *ty_field
+ = new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
+ field.get_field_name (), field_type);
+ fields.push_back (ty_field);
+ context->insert_type (field.get_mappings (),
+ ty_field->get_field_type ());
+ }
TyTy::BaseType *type
= new TyTy::ADTType (struct_decl.get_mappings ().get_hirid (),
diff --git a/gcc/rust/typecheck/rust-hir-type-check.cc b/gcc/rust/typecheck/rust-hir-type-check.cc
index 548b5c2..ae5b2a0 100644
--- a/gcc/rust/typecheck/rust-hir-type-check.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check.cc
@@ -153,20 +153,21 @@ TypeCheckStructExpr::visit (HIR::StructExprStructFields &struct_expr)
std::vector<TyTy::StructFieldType *> infered_fields;
bool ok = true;
- struct_expr.iterate ([&] (HIR::StructExprField *field) mutable -> bool {
- resolved_field_value_expr = nullptr;
- field->accept_vis (*this);
- if (resolved_field_value_expr == nullptr)
- {
- rust_fatal_error (field->get_locus (),
- "failed to resolve type for field");
- ok = false;
- return false;
- }
- context->insert_type (field->get_mappings (), resolved_field_value_expr);
- return true;
- });
+ for (auto &field : struct_expr.get_fields ())
+ {
+ resolved_field_value_expr = nullptr;
+ field->accept_vis (*this);
+ if (resolved_field_value_expr == nullptr)
+ {
+ rust_fatal_error (field->get_locus (),
+ "failed to resolve type for field");
+ ok = false;
+ break;
+ }
+
+ context->insert_type (field->get_mappings (), resolved_field_value_expr);
+ }
// something failed setting up the fields
if (!ok)
@@ -266,10 +267,8 @@ TypeCheckStructExpr::visit (HIR::StructExprStructFields &struct_expr)
// correctly. The GIMPLE backend uses a simple algorithm that assumes each
// assigned field in the constructor is in the same order as the field in
// the type
- std::vector<std::unique_ptr<HIR::StructExprField> > expr_fields
- = struct_expr.get_fields_as_owner ();
- for (auto &f : expr_fields)
- f.release ();
+ for (auto &field : struct_expr.get_fields ())
+ field.release ();
std::vector<std::unique_ptr<HIR::StructExprField> > ordered_fields;
for (size_t i = 0; i < adtFieldIndexToField.size (); i++)
diff --git a/gcc/rust/typecheck/rust-tycheck-dump.h b/gcc/rust/typecheck/rust-tycheck-dump.h
index cc2e3c0..6856d05 100644
--- a/gcc/rust/typecheck/rust-tycheck-dump.h
+++ b/gcc/rust/typecheck/rust-tycheck-dump.h
@@ -162,11 +162,11 @@ public:
void visit (HIR::ArrayElemsValues &elems) override
{
- elems.iterate ([&] (HIR::Expr *e) mutable -> bool {
- e->accept_vis (*this);
- dump += ",";
- return true;
- });
+ for (auto &elem : elems.get_values ())
+ {
+ elem->accept_vis (*this);
+ dump += ",";
+ }
}
void visit (HIR::GroupedExpr &expr) override
diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc
index 481185f..3f0d8a3 100644
--- a/gcc/rust/typecheck/rust-tyty.cc
+++ b/gcc/rust/typecheck/rust-tyty.cc
@@ -2383,27 +2383,28 @@ TypeCheckCallExpr::visit (ADTType &type)
}
size_t i = 0;
- call.iterate_params ([&] (HIR::Expr *p) mutable -> bool {
- StructFieldType *field = type.get_field (i);
- BaseType *field_tyty = field->get_field_type ();
+ for (auto &argument : call.get_arguments ())
+ {
+ StructFieldType *field = type.get_field (i);
+ BaseType *field_tyty = field->get_field_type ();
- BaseType *arg = Resolver::TypeCheckExpr::Resolve (p, false);
- if (arg->get_kind () == TyTy::TypeKind::ERROR)
- {
- rust_error_at (p->get_locus (), "failed to resolve argument type");
- return false;
- }
+ BaseType *arg = Resolver::TypeCheckExpr::Resolve (argument.get (), false);
+ if (arg->get_kind () == TyTy::TypeKind::ERROR)
+ {
+ rust_error_at (argument->get_locus (),
+ "failed to resolve argument type");
+ return;
+ }
- auto res = field_tyty->coerce (arg);
- if (res->get_kind () == TyTy::TypeKind::ERROR)
- {
- return false;
- }
+ auto res = field_tyty->coerce (arg);
+ if (res->get_kind () == TyTy::TypeKind::ERROR)
+ {
+ return;
+ }
- delete res;
- i++;
- return true;
- });
+ delete res;
+ i++;
+ }
if (i != call.num_params ())
{
@@ -2441,35 +2442,36 @@ TypeCheckCallExpr::visit (FnType &type)
}
size_t i = 0;
- call.iterate_params ([&] (HIR::Expr *param) mutable -> bool {
- auto argument_expr_tyty = Resolver::TypeCheckExpr::Resolve (param, false);
- if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR)
- {
- rust_error_at (param->get_locus (),
- "failed to resolve type for argument expr in CallExpr");
- return false;
- }
-
- auto resolved_argument_type = argument_expr_tyty;
+ for (auto &argument : call.get_arguments ())
+ {
+ auto argument_expr_tyty
+ = Resolver::TypeCheckExpr::Resolve (argument.get (), false);
+ if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR)
+ {
+ rust_error_at (
+ argument->get_locus (),
+ "failed to resolve type for argument expr in CallExpr");
+ return;
+ }
- // it might be a varadic function
- if (i < type.num_params ())
- {
- auto fnparam = type.param_at (i);
- resolved_argument_type = fnparam.second->coerce (argument_expr_tyty);
- if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR)
- {
- rust_error_at (param->get_locus (),
- "Type Resolution failure on parameter");
- return false;
- }
- }
+ // it might be a varadic function
+ if (i < type.num_params ())
+ {
+ auto fnparam = type.param_at (i);
+ auto resolved_argument_type
+ = fnparam.second->coerce (argument_expr_tyty);
+ if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR)
+ {
+ rust_error_at (argument->get_locus (),
+ "Type Resolution failure on parameter");
+ return;
+ }
+ }
- context->insert_type (param->get_mappings (), resolved_argument_type);
+ context->insert_type (argument->get_mappings (), argument_expr_tyty);
- i++;
- return true;
- });
+ i++;
+ }
if (i < call.num_params ())
{
@@ -2505,29 +2507,31 @@ TypeCheckCallExpr::visit (FnPtr &type)
}
size_t i = 0;
- call.iterate_params ([&] (HIR::Expr *param) mutable -> bool {
- auto fnparam = type.param_at (i);
- auto argument_expr_tyty = Resolver::TypeCheckExpr::Resolve (param, false);
- if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR)
- {
- rust_error_at (param->get_locus (),
- "failed to resolve type for argument expr in CallExpr");
- return false;
- }
+ for (auto &argument : call.get_arguments ())
+ {
+ auto fnparam = type.param_at (i);
+ auto argument_expr_tyty
+ = Resolver::TypeCheckExpr::Resolve (argument.get (), false);
+ if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR)
+ {
+ rust_error_at (
+ argument->get_locus (),
+ "failed to resolve type for argument expr in CallExpr");
+ return;
+ }
- auto resolved_argument_type = fnparam->coerce (argument_expr_tyty);
- if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR)
- {
- rust_error_at (param->get_locus (),
- "Type Resolution failure on parameter");
- return false;
- }
+ auto resolved_argument_type = fnparam->coerce (argument_expr_tyty);
+ if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR)
+ {
+ rust_error_at (argument->get_locus (),
+ "Type Resolution failure on parameter");
+ return;
+ }
- context->insert_type (param->get_mappings (), resolved_argument_type);
+ context->insert_type (argument->get_mappings (), argument_expr_tyty);
- i++;
- return true;
- });
+ i++;
+ }
if (i != call.num_params ())
{
@@ -2556,29 +2560,31 @@ TypeCheckMethodCallExpr::visit (FnType &type)
}
size_t i = 1;
- call.iterate_params ([&] (HIR::Expr *param) mutable -> bool {
- auto fnparam = type.param_at (i);
- auto argument_expr_tyty = Resolver::TypeCheckExpr::Resolve (param, false);
- if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR)
- {
- rust_error_at (param->get_locus (),
- "failed to resolve type for argument expr in CallExpr");
- return false;
- }
+ for (auto &argument : call.get_arguments ())
+ {
+ auto fnparam = type.param_at (i);
+ auto argument_expr_tyty
+ = Resolver::TypeCheckExpr::Resolve (argument.get (), false);
+ if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR)
+ {
+ rust_error_at (
+ argument->get_locus (),
+ "failed to resolve type for argument expr in CallExpr");
+ return;
+ }
- auto resolved_argument_type = fnparam.second->coerce (argument_expr_tyty);
- if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR)
- {
- rust_error_at (param->get_locus (),
- "Type Resolution failure on parameter");
- return false;
- }
+ auto resolved_argument_type = fnparam.second->coerce (argument_expr_tyty);
+ if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR)
+ {
+ rust_error_at (argument->get_locus (),
+ "Type Resolution failure on parameter");
+ return;
+ }
- context->insert_type (param->get_mappings (), resolved_argument_type);
+ context->insert_type (argument->get_mappings (), argument_expr_tyty);
- i++;
- return true;
- });
+ i++;
+ }
if (i != num_args_to_call)
{
diff --git a/gcc/testsuite/rust/compile/func3.rs b/gcc/testsuite/rust/compile/func3.rs
index 6cedf8e..3ab374a 100644
--- a/gcc/testsuite/rust/compile/func3.rs
+++ b/gcc/testsuite/rust/compile/func3.rs
@@ -3,5 +3,9 @@ fn test(a: i32, b: i32) -> i32 {
}
fn main() {
- let a = test(1, true); // { dg-error "expected .i32. got .bool." }
+ let a = test(1, true);
+ // { dg-error "expected .i32. got .bool." "" { target *-*-* } .-1 }
+ // { dg-error "Type Resolution failure on parameter" "" { target *-*-* } .-2 }
+ // { dg-error "failed to lookup type to CallExpr" "" { target *-*-* } .-3 }
+ // { dg-error "failed to type resolve expression" "" { target *-*-* } .-4 }
}
diff --git a/gcc/testsuite/rust/compile/tuple_struct3.rs b/gcc/testsuite/rust/compile/tuple_struct3.rs
index 1af72a2..a70306d 100644
--- a/gcc/testsuite/rust/compile/tuple_struct3.rs
+++ b/gcc/testsuite/rust/compile/tuple_struct3.rs
@@ -3,7 +3,6 @@ struct Foo(i32, i32, bool);
fn main() {
let c = Foo(1, 2f32, true);
// { dg-error "expected .i32. got .f32." "" { target *-*-* } .-1 }
- // { dg-error "unexpected number of arguments 1 expected 3" "" { target *-*-* } .-2 }
- // { dg-error "failed to lookup type to CallExpr" "" { target *-*-* } .-3 }
- // { dg-error "failed to type resolve expression" "" { target *-*-* } .-4 }
+ // { dg-error "failed to lookup type to CallExpr" "" { target *-*-* } .-2 }
+ // { dg-error "failed to type resolve expression" "" { target *-*-* } .-3 }
}
diff --git a/gcc/testsuite/rust/execute/torture/coercion1.rs b/gcc/testsuite/rust/execute/torture/coercion1.rs
new file mode 100644
index 0000000..dff9809
--- /dev/null
+++ b/gcc/testsuite/rust/execute/torture/coercion1.rs
@@ -0,0 +1,43 @@
+/* { dg-output "123\n123\n" } */
+extern "C" {
+ fn printf(s: *const i8, ...);
+}
+
+struct Foo(i32);
+trait Bar {
+ fn baz(&self);
+ // { dg-warning "unused name" "" { target *-*-* } .-1 }
+}
+
+impl Bar for Foo {
+ fn baz(&self) {
+ // { dg-warning "unused name" "" { target *-*-* } .-1 }
+ unsafe {
+ let a = "%i\n\0";
+ let b = a as *const str;
+ let c = b as *const i8;
+
+ printf(c, self.0);
+ }
+ }
+}
+
+fn static_dispatch<T: Bar>(t: &T) {
+ t.baz();
+}
+
+fn dynamic_dispatch(t: &dyn Bar) {
+ t.baz();
+}
+
+fn main() -> i32 {
+ let a;
+ a = Foo(123);
+ static_dispatch(&a);
+
+ let b: &dyn Bar;
+ b = &a;
+ dynamic_dispatch(b);
+
+ 0
+}
diff --git a/gcc/testsuite/rust/execute/torture/coercion2.rs b/gcc/testsuite/rust/execute/torture/coercion2.rs
new file mode 100644
index 0000000..e404954
--- /dev/null
+++ b/gcc/testsuite/rust/execute/torture/coercion2.rs
@@ -0,0 +1,41 @@
+/* { dg-output "123\n123\n" } */
+extern "C" {
+ fn printf(s: *const i8, ...);
+}
+
+struct Foo(i32);
+trait Bar {
+ fn baz(&self);
+ // { dg-warning "unused name" "" { target *-*-* } .-1 }
+}
+
+impl Bar for Foo {
+ fn baz(&self) {
+ // { dg-warning "unused name" "" { target *-*-* } .-1 }
+ unsafe {
+ let a = "%i\n\0";
+ let b = a as *const str;
+ let c = b as *const i8;
+
+ printf(c, self.0);
+ }
+ }
+}
+
+fn static_dispatch<T: Bar>(t: &T) {
+ t.baz();
+}
+
+fn dynamic_dispatch(t: &dyn Bar) {
+ t.baz();
+}
+
+fn main() -> i32 {
+ let a;
+ a = &Foo(123);
+
+ static_dispatch(a);
+ dynamic_dispatch(a);
+
+ 0
+}