Skip to content

Fix bad method resolution and add move_val_init #1999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion gcc/rust/backend/rust-compile-intrinsic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ static tree
op_with_overflow_inner (Context *ctx, TyTy::FnType *fntype, tree_code op);
static tree
uninit_handler (Context *ctx, TyTy::FnType *fntype);
static tree
move_val_init_handler (Context *ctx, TyTy::FnType *fntype);

enum class Prefetch
{
Expand Down Expand Up @@ -205,6 +207,7 @@ static const std::map<std::string,
{"unchecked_shl", unchecked_op_handler (LSHIFT_EXPR)},
{"unchecked_shr", unchecked_op_handler (RSHIFT_EXPR)},
{"uninit", uninit_handler},
{"move_val_init", move_val_init_handler},
};

Intrinsics::Intrinsics (Context *ctx) : ctx (ctx) {}
Expand Down Expand Up @@ -1001,7 +1004,7 @@ uninit_handler (Context *ctx, TyTy::FnType *fntype)

auto fndecl = compile_intrinsic_function (ctx, fntype);

// get the template parameter type tree fn size_of<T>();
// get the template parameter type tree fn uninit<T>();
rust_assert (fntype->get_num_substitutions () == 1);
auto &param_mapping = fntype->get_substs ().at (0);
const TyTy::ParamType *param_tyty = param_mapping.get_param_ty ();
Expand Down Expand Up @@ -1042,5 +1045,56 @@ uninit_handler (Context *ctx, TyTy::FnType *fntype)
return fndecl;
}

static tree
move_val_init_handler (Context *ctx, TyTy::FnType *fntype)
{
rust_assert (fntype->get_params ().size () == 2);

tree lookup = NULL_TREE;
if (check_for_cached_intrinsic (ctx, fntype, &lookup))
return lookup;

auto fndecl = compile_intrinsic_function (ctx, fntype);

// get the template parameter type tree fn size_of<T>();
rust_assert (fntype->get_num_substitutions () == 1);
auto &param_mapping = fntype->get_substs ().at (0);
const TyTy::ParamType *param_tyty = param_mapping.get_param_ty ();
TyTy::BaseType *resolved_tyty = param_tyty->resolve ();
tree template_parameter_type
= TyTyResolveCompile::compile (ctx, resolved_tyty);

std::vector<Bvariable *> param_vars;
compile_fn_params (ctx, fntype, fndecl, &param_vars);

if (!ctx->get_backend ()->function_set_parameters (fndecl, param_vars))
return error_mark_node;

enter_intrinsic_block (ctx, fndecl);

// BUILTIN size_of FN BODY BEGIN

tree dst = ctx->get_backend ()->var_expression (param_vars[0], Location ());
tree src = ctx->get_backend ()->var_expression (param_vars[1], Location ());
tree size = TYPE_SIZE_UNIT (template_parameter_type);

tree memcpy_builtin = error_mark_node;
BuiltinsContext::get ().lookup_simple_builtin ("memcpy", &memcpy_builtin);
rust_assert (memcpy_builtin != error_mark_node);

src = build_fold_addr_expr_loc (BUILTINS_LOCATION, src);
tree memset_call = build_call_expr_loc (BUILTINS_LOCATION, memcpy_builtin, 3,
dst, src, size);
TREE_READONLY (memset_call) = 0;
TREE_SIDE_EFFECTS (memset_call) = 1;

ctx->add_statement (memset_call);
// BUILTIN size_of FN BODY END

finalize_intrinsic_block (ctx, fndecl);

return fndecl;
}

} // namespace Compile
} // namespace Rust
8 changes: 4 additions & 4 deletions gcc/rust/typecheck/rust-coercion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,9 @@ TypeCoercionRules::coerce_unsized (TyTy::BaseType *source,
bool
TypeCoercionRules::select (TyTy::BaseType &autoderefed)
{
rust_debug (
"autoderef type-coercion select autoderefed={%s} can_eq expected={%s}",
autoderefed.debug_str ().c_str (), expected->debug_str ().c_str ());
rust_debug ("TypeCoercionRules::select autoderefed={%s} can_eq expected={%s}",
autoderefed.debug_str ().c_str (),
expected->debug_str ().c_str ());

TyTy::BaseType *result
= unify_site_and (autoderefed.get_ref (), TyTy::TyWithLocation (expected),
Expand All @@ -426,7 +426,7 @@ TypeCoercionRules::select (TyTy::BaseType &autoderefed)
if (!ok)
return false;

try_result = CoercionResult{adjustments, autoderefed.clone ()};
try_result = CoercionResult{adjustments, result};
return true;
}

Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/typecheck/rust-hir-type-check-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ TypeCheckBase::resolve_literal (const Analysis::NodeMapping &expr_mappings,
infered
= new TyTy::InferType (expr_mappings.get_hirid (),
TyTy::InferType::InferTypeKind::INTEGRAL,
TyTy::InferType::TypeHint::Default (),
locus);
break;
}
Expand All @@ -189,6 +190,7 @@ TypeCheckBase::resolve_literal (const Analysis::NodeMapping &expr_mappings,
infered
= new TyTy::InferType (expr_mappings.get_hirid (),
TyTy::InferType::InferTypeKind::FLOAT,
TyTy::InferType::TypeHint::Default (),
locus);
break;
}
Expand Down
10 changes: 5 additions & 5 deletions gcc/rust/typecheck/rust-hir-type-check-stmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ TypeCheckStmt::visit (HIR::LetStmt &stmt)
// let x;
else
{
TypeCheckPattern::Resolve (
&stmt_pattern,
new TyTy::InferType (
stmt_pattern.get_pattern_mappings ().get_hirid (),
TyTy::InferType::InferTypeKind::GENERAL, stmt.get_locus ()));
auto infer = new TyTy::InferType (
stmt_pattern.get_pattern_mappings ().get_hirid (),
TyTy::InferType::InferTypeKind::GENERAL,
TyTy::InferType::TypeHint::Default (), stmt.get_locus ());
TypeCheckPattern::Resolve (&stmt_pattern, infer);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/typecheck/rust-hir-type-check-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ TypeCheckType::visit (HIR::InferredType &type)
{
translated = new TyTy::InferType (type.get_mappings ().get_hirid (),
TyTy::InferType::InferTypeKind::GENERAL,
TyTy::InferType::TypeHint::Default (),
type.get_locus ());
}

Expand Down
3 changes: 2 additions & 1 deletion gcc/rust/typecheck/rust-typecheck-context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ void
TypeCheckContext::push_new_loop_context (HirId id, Location locus)
{
TyTy::BaseType *infer_var
= new TyTy::InferType (id, TyTy::InferType::InferTypeKind::GENERAL, locus);
= new TyTy::InferType (id, TyTy::InferType::InferTypeKind::GENERAL,
TyTy::InferType::TypeHint::Default (), locus);
loop_type_stack.push_back (infer_var);
}

Expand Down
3 changes: 2 additions & 1 deletion gcc/rust/typecheck/rust-tyty-util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ TyVar::get_implicit_infer_var (Location locus)
auto context = Resolver::TypeCheckContext::get ();

InferType *infer = new InferType (mappings->get_next_hir_id (),
InferType::InferTypeKind::GENERAL, locus);
InferType::InferTypeKind::GENERAL,
InferType::TypeHint::Default (), locus);
context->insert_type (Analysis::NodeMapping (mappings->get_current_crate (),
UNKNOWN_NODEID,
infer->get_ref (),
Expand Down
Loading