tree
HIRCompileBase::compile_constant_item (
- TyTy::BaseType *resolved_type, const Resolver::CanonicalPath &canonical_path,
- HIR::Expr &const_value_expr, location_t locus)
+ HirId coercion_id, TyTy::BaseType *resolved_type,
+ TyTy::BaseType *expected_type, const Resolver::CanonicalPath &canonical_path,
+ HIR::Expr &const_value_expr, location_t locus, location_t expr_locus)
{
const std::string &ident = canonical_path.get ();
tree type = TyTyResolveCompile::compile (ctx, resolved_type);
tree const_type = build_qualified_type (type, TYPE_QUAL_CONST);
+
+ tree actual_type = TyTyResolveCompile::compile (ctx, expected_type);
+ tree actual_const_type = build_qualified_type (actual_type, TYPE_QUAL_CONST);
+
bool is_block_expr
= const_value_expr.get_expression_type () == HIR::Expr::ExprType::Block;
tree call = build_call_array_loc (locus, const_type, fndecl, 0, NULL);
tree folded_expr = fold_expr (call);
- return named_constant_expression (const_type, ident, folded_expr, locus);
+ // coercion site
+ tree coerced = coercion_site (coercion_id, folded_expr, resolved_type,
+ expected_type, locus, expr_locus);
+
+ return named_constant_expression (actual_const_type, ident, coerced, locus);
}
tree
void compile_function_body (tree fndecl, HIR::BlockExpr &function_body,
TyTy::BaseType *fn_return_ty);
- tree compile_constant_item (TyTy::BaseType *resolved_type,
+ tree compile_constant_item (HirId coercion_id, TyTy::BaseType *resolved_type,
+ TyTy::BaseType *expected_type,
const Resolver::CanonicalPath &canonical_path,
- HIR::Expr &const_value_expr, location_t locus);
+ HIR::Expr &const_value_expr, location_t locus,
+ location_t expr_locus);
tree compile_function (const std::string &fn_name, HIR::SelfParam &self_param,
std::vector<HIR::FunctionParam> &function_params,
rust_assert (canonical_path);
HIR::Expr &const_value_expr = constant.get_expr ();
+ TyTy::BaseType *expr_type = nullptr;
+ bool ok = ctx->get_tyctx ()->lookup_type (
+ const_value_expr.get_mappings ().get_hirid (), &expr_type);
+ rust_assert (ok);
+
tree const_expr
- = compile_constant_item (resolved_type, *canonical_path, const_value_expr,
- constant.get_locus ());
+ = compile_constant_item (constant.get_mappings ().get_hirid (), expr_type,
+ resolved_type, *canonical_path, const_value_expr,
+ constant.get_locus (),
+ const_value_expr.get_locus ());
ctx->push_const (const_expr);
ctx->insert_const_decl (constant.get_mappings ().get_hirid (), const_expr);
return;
}
+ HIR::Expr &const_value_expr = var.get_expr ();
+
TyTy::BaseType *resolved_type = nullptr;
+ TyTy::BaseType *expr_type = nullptr;
bool ok = ctx->get_tyctx ()->lookup_type (var.get_mappings ().get_hirid (),
&resolved_type);
rust_assert (ok);
+ ok = ctx->get_tyctx ()->lookup_type (
+ const_value_expr.get_mappings ().get_hirid (), &expr_type);
+ rust_assert (ok);
tree type = TyTyResolveCompile::compile (ctx, resolved_type);
rust_assert (canonical_path.has_value ());
- HIR::Expr &const_value_expr = var.get_expr ();
ctx->push_const_context ();
- tree value = compile_constant_item (resolved_type, *canonical_path,
- const_value_expr, var.get_locus ());
+ tree value
+ = compile_constant_item (var.get_mappings ().get_hirid (), expr_type,
+ resolved_type, *canonical_path, const_value_expr,
+ var.get_locus (), const_value_expr.get_locus ());
ctx->pop_const_context ();
std::string name = canonical_path->get ();
void
CompileItem::visit (HIR::ConstantItem &constant)
{
+ HIR::Expr &const_value_expr = constant.get_expr ();
auto &mappings = constant.get_mappings ();
if (ctx->lookup_const_decl (mappings.get_hirid (), &reference))
return;
// resolve the type
- TyTy::BaseType *resolved_type = nullptr;
+ TyTy::BaseType *constant_type = nullptr;
+ TyTy::BaseType *expr_type = nullptr;
bool ok
- = ctx->get_tyctx ()->lookup_type (mappings.get_hirid (), &resolved_type);
+ = ctx->get_tyctx ()->lookup_type (mappings.get_hirid (), &constant_type);
+ rust_assert (ok);
+ ok = ctx->get_tyctx ()->lookup_type (
+ const_value_expr.get_mappings ().get_hirid (), &expr_type);
rust_assert (ok);
// canonical path
.value ();
}
- HIR::Expr &const_value_expr = constant.get_expr ();
ctx->push_const_context ();
tree const_expr
- = compile_constant_item (resolved_type, canonical_path, const_value_expr,
- constant.get_locus ());
+ = compile_constant_item (mappings.get_hirid (), expr_type, constant_type,
+ canonical_path, const_value_expr,
+ constant.get_locus (),
+ const_value_expr.get_locus ());
ctx->pop_const_context ();
ctx->push_const (const_expr);
--- /dev/null
+fn main() {
+ const slice: &[i32] = &[1, 2, 3];
+ let _slice2: &[i32] = slice;
+}