Skip to content
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
28 changes: 21 additions & 7 deletions gcc/rust/backend/rust-compile-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2010,11 +2010,25 @@ CompileExpr::array_copied_expr (location_t expr_locus,
return error_mark_node;
}

auto capacity_tyty = array_tyty.get_capacity ();
tree capacity_expr = capacity_tyty->get_value ();
if (!TREE_CONSTANT (capacity_expr))
auto capacity_ty = array_tyty.get_capacity ();

// Check if capacity is a const type
if (capacity_ty->get_kind () != TyTy::TypeKind::CONST)
{
rust_error_at (array_tyty.get_locus (),
"array capacity is not a const type");
return error_mark_node;
}

auto *capacity_const = capacity_ty->as_const_type ();

rust_assert (capacity_const->const_kind ()
== TyTy::BaseConstType::ConstKind::Value);
auto &capacity_value = *static_cast<TyTy::ConstValueType *> (capacity_const);
auto cap_tree = capacity_value.get_value ();
if (error_operand_p (cap_tree) || !TREE_CONSTANT (cap_tree))
{
rust_error_at (expr_locus, "non const num copies %qT", capacity_expr);
rust_error_at (expr_locus, "non const num copies %qT", cap_tree);
return error_mark_node;
}

Expand Down Expand Up @@ -2067,9 +2081,9 @@ CompileExpr::array_copied_expr (location_t expr_locus,
ctx->push_block (init_block);

tree tmp;
tree stmts = Backend::array_initializer (fndecl, init_block, array_type,
capacity_expr, translated_expr,
&tmp, expr_locus);
tree stmts
= Backend::array_initializer (fndecl, init_block, array_type, cap_tree,
translated_expr, &tmp, expr_locus);
ctx->add_statement (stmts);

tree block = ctx->pop_block ();
Expand Down
27 changes: 25 additions & 2 deletions gcc/rust/backend/rust-compile-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "rust-hir-pattern.h"
#include "rust-system.h"
#include "rust-tyty.h"
#include "tree.h"

namespace Rust {
namespace Compile {
Expand Down Expand Up @@ -676,7 +677,18 @@ CompilePatternCheckExpr::visit (HIR::SlicePattern &pattern)
// for array type scrutinee, we can simply get the capacity as a
// const and calculate how many elements to skip
auto array_ty = static_cast<TyTy::ArrayType *> (lookup);
auto cap_tree = array_ty->get_capacity ()->get_value ();
auto capacity_ty = array_ty->get_capacity ();

rust_assert (capacity_ty->get_kind () == TyTy::TypeKind::CONST);
auto *capacity_const = capacity_ty->as_const_type ();
rust_assert (capacity_const->const_kind ()
== TyTy::BaseConstType::ConstKind::Value);
auto &capacity_value
= *static_cast<TyTy::ConstValueType *> (capacity_const);
auto cap_tree = capacity_value.get_value ();

rust_assert (!error_operand_p (cap_tree));

size_t cap_wi = (size_t) wi::to_wide (cap_tree).to_uhwi ();
element_index = cap_wi - items.get_upper_patterns ().size ();
for (auto &pattern_member : items.get_upper_patterns ())
Expand Down Expand Up @@ -1164,7 +1176,18 @@ CompilePatternBindings::visit (HIR::SlicePattern &pattern)
case TyTy::TypeKind::ARRAY:
{
auto array_ty = static_cast<TyTy::ArrayType *> (lookup);
auto cap_tree = array_ty->get_capacity ()->get_value ();
auto capacity_ty = array_ty->get_capacity ();

rust_assert (capacity_ty->get_kind () == TyTy::TypeKind::CONST);
auto *capacity_const = capacity_ty->as_const_type ();
rust_assert (capacity_const->const_kind ()
== TyTy::BaseConstType::ConstKind::Value);
auto &capacity_value
= *static_cast<TyTy::ConstValueType *> (capacity_const);
auto cap_tree = capacity_value.get_value ();

rust_assert (!error_operand_p (cap_tree));

size_t cap_wi = (size_t) wi::to_wide (cap_tree).to_uhwi ();
element_index = cap_wi - items.get_upper_patterns ().size ();
for (auto &pattern_member : items.get_upper_patterns ())
Expand Down
40 changes: 36 additions & 4 deletions gcc/rust/backend/rust-compile-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,31 @@ TyTyResolveCompile::visit (const TyTy::InferType &type)
}

void
TyTyResolveCompile::visit (const TyTy::ParamType &)
TyTyResolveCompile::visit (const TyTy::ParamType &type)
{
translated = error_mark_node;
}

void
TyTyResolveCompile::visit (const TyTy::ConstType &)
TyTyResolveCompile::visit (const TyTy::ConstParamType &type)
{
translated = error_mark_node;
}

void
TyTyResolveCompile::visit (const TyTy::ConstValueType &type)
{
translated = error_mark_node;
}

void
TyTyResolveCompile::visit (const TyTy::ConstInferType &type)
{
translated = error_mark_node;
}

void
TyTyResolveCompile::visit (const TyTy::ConstErrorType &type)
{
translated = error_mark_node;
}
Expand Down Expand Up @@ -470,8 +488,22 @@ TyTyResolveCompile::visit (const TyTy::ArrayType &type)
{
tree element_type
= TyTyResolveCompile::compile (ctx, type.get_element_type ());
TyTy::ConstType *const_capacity = type.get_capacity ();
tree folded_capacity_expr = const_capacity->get_value ();
auto const_capacity = type.get_capacity ();

// Check if capacity is a const type
if (const_capacity->get_kind () != TyTy::TypeKind::CONST)
{
rust_error_at (type.get_locus (), "array capacity is not a const type");
translated = error_mark_node;
return;
}

auto *capacity_const = const_capacity->as_const_type ();

rust_assert (capacity_const->const_kind ()
== TyTy::BaseConstType::ConstKind::Value);
auto &capacity_value = *static_cast<TyTy::ConstValueType *> (capacity_const);
auto folded_capacity_expr = capacity_value.get_value ();

// build_index_type takes the maximum index, which is one less than
// the length.
Expand Down
5 changes: 4 additions & 1 deletion gcc/rust/backend/rust-compile-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ class TyTyResolveCompile : protected TyTy::TyConstVisitor
void visit (const TyTy::ReferenceType &) override;
void visit (const TyTy::PointerType &) override;
void visit (const TyTy::ParamType &) override;
void visit (const TyTy::ConstType &) override;
void visit (const TyTy::ConstParamType &) override;
void visit (const TyTy::ConstValueType &) override;
void visit (const TyTy::ConstInferType &) override;
void visit (const TyTy::ConstErrorType &) override;
void visit (const TyTy::StrType &) override;
void visit (const TyTy::NeverType &) override;
void visit (const TyTy::PlaceholderType &) override;
Expand Down
45 changes: 22 additions & 23 deletions gcc/rust/typecheck/rust-hir-type-check-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -367,17 +367,17 @@ TypeCheckBase::resolve_literal (const Analysis::NodeMapping &expr_mappings,
tree capacity = Compile::HIRCompileBase::query_compile_const_expr (
ctx, expected_ty, *literal_capacity);

TyTy::ConstType *capacity_expr
= new TyTy::ConstType (TyTy::ConstType::ConstKind::Value, "",
expected_ty, capacity, {},
literal_capacity->get_locus (),
literal_capacity->get_mappings ().get_hirid (),
literal_capacity->get_mappings ().get_hirid (),
{});

TyTy::ArrayType *array
= new TyTy::ArrayType (array_mapping.get_hirid (), locus,
capacity_expr, TyTy::TyVar (u8->get_ref ()));
HirId capacity_expr_id = literal_capacity->get_mappings ().get_hirid ();
auto capacity_expr
= new TyTy::ConstValueType (capacity, expected_ty, capacity_expr_id,
capacity_expr_id);
context->insert_type (literal_capacity->get_mappings (),
capacity_expr->as_base_type ());

TyTy::ArrayType *array = new TyTy::ArrayType (
array_mapping.get_hirid (), locus,
TyTy::TyVar (capacity_expr->as_base_type ()->get_ty_ref ()),
TyTy::TyVar (u8->get_ref ()));
context->insert_type (array_mapping, array);

infered = new TyTy::ReferenceType (expr_mappings.get_hirid (),
Expand Down Expand Up @@ -597,22 +597,21 @@ TypeCheckBase::resolve_generic_params (
= Compile::HIRCompileBase::query_compile_const_expr (
ctx, specified_type, expr);

TyTy::ConstType *default_const_decl
= new TyTy::ConstType (TyTy::ConstType::ConstKind::Value,
param.get_name (), specified_type,
default_value, {}, param.get_locus (),
expr.get_mappings ().get_hirid (),
expr.get_mappings ().get_hirid (), {});
auto default_const_decl
= new TyTy::ConstValueType (default_value, specified_type,
expr.get_mappings ().get_hirid (),
expr.get_mappings ().get_hirid (),
{});

context->insert_type (expr.get_mappings (), default_const_decl);
}

TyTy::ConstType *const_decl
= new TyTy::ConstType (TyTy::ConstType::ConstKind::Decl,
param.get_name (), specified_type,
error_mark_node, {}, param.get_locus (),
param.get_mappings ().get_hirid (),
param.get_mappings ().get_hirid (), {});
TyTy::BaseGeneric *const_decl
= new TyTy::ConstParamType (param.get_name (), param.get_locus (),
specified_type,
param.get_mappings ().get_hirid (),
param.get_mappings ().get_hirid (),
{});

context->insert_type (generic_param->get_mappings (), const_decl);
TyTy::SubstitutionParamMapping p (*generic_param, const_decl);
Expand Down
35 changes: 19 additions & 16 deletions gcc/rust/typecheck/rust-hir-type-check-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "rust-immutable-name-resolution-context.h"
#include "rust-compile-base.h"
#include "rust-tyty-util.h"
#include "rust-tyty.h"
#include "tree.h"

namespace Rust {
Expand Down Expand Up @@ -668,16 +669,9 @@ TypeCheckExpr::visit (HIR::AnonConst &expr)
return;
}

auto locus = expr.get_locus ();
auto infer_ty_var = TyTy::TyVar::get_implicit_infer_var (locus);

HirId next = mappings.get_next_hir_id ();
infered = new TyTy::ConstType (TyTy::ConstType::ConstKind::Infer, "",
infer_ty_var.get_tyty (), error_mark_node, {},
locus, next, next, {});

context->insert_implicit_type (infered->get_ref (), infered);
mappings.insert_location (infered->get_ref (), locus);
TyTy::TyVar var
= TyTy::TyVar::get_implicit_const_infer_var (expr.get_locus ());
infered = var.get_tyty ();
}

void
Expand Down Expand Up @@ -1155,14 +1149,23 @@ TypeCheckExpr::visit (HIR::ArrayExpr &expr)
tree capacity_value
= Compile::HIRCompileBase::query_compile_const_expr (ctx, capacity_type,
*capacity_expr);
HirId size_id = capacity_expr->get_mappings ().get_hirid ();
TyTy::ConstType *const_type
= new TyTy::ConstType (TyTy::ConstType::ConstKind::Value, "", expected_ty,
capacity_value, {}, capacity_expr->get_locus (),
size_id, size_id);

// Create ConstValueType with ref == ty_ref (both pointing to capacity_expr)
// ty_ref gets updated during substitution via set_ty_ref()
HirId capacity_expr_id = capacity_expr->get_mappings ().get_hirid ();
auto const_type
= new TyTy::ConstValueType (capacity_value, expected_ty, capacity_expr_id,
capacity_expr_id);

// Insert the ConstValueType at its ref
context->insert_type (capacity_expr->get_mappings (),
const_type->as_base_type ());

infered
= new TyTy::ArrayType (expr.get_mappings ().get_hirid (), expr.get_locus (),
const_type, TyTy::TyVar (element_type->get_ref ()));
TyTy::TyVar (
const_type->as_base_type ()->get_ty_ref ()),
TyTy::TyVar (element_type->get_ref ()));
}

// empty struct
Expand Down
29 changes: 28 additions & 1 deletion gcc/rust/typecheck/rust-hir-type-check-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "rust-hir-type-check-expr.h"
#include "rust-type-util.h"
#include "rust-immutable-name-resolution-context.h"
#include "rust-tyty.h"
#include "tree.h"

namespace Rust {
namespace Resolver {
Expand Down Expand Up @@ -778,7 +780,32 @@ TypeCheckPattern::visit (HIR::SlicePattern &pattern)
auto &array_ty_ty = static_cast<TyTy::ArrayType &> (*parent);
parent_element_ty = array_ty_ty.get_element_type ();
auto capacity = array_ty_ty.get_capacity ();
tree cap = capacity->get_value ();

tree cap = error_mark_node;
if (capacity->get_kind () != TyTy::TypeKind::CONST)
{
// Error case - capacity is not a const type
break;
}

auto *capacity_const = capacity->as_const_type ();
switch (capacity_const->const_kind ())
{
case TyTy::BaseConstType::ConstKind::Value:
{
const auto &const_value
= *static_cast<TyTy::ConstValueType *> (capacity);
cap = const_value.get_value ();
}
break;

case TyTy::BaseConstType::ConstKind::Decl:
case TyTy::BaseConstType::ConstKind::Infer:
case TyTy::BaseConstType::ConstKind::Error:
cap = error_mark_node;
break;
}

if (error_operand_p (cap))
{
rust_error_at (parent->get_locus (),
Expand Down
26 changes: 11 additions & 15 deletions gcc/rust/typecheck/rust-hir-type-check-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -706,14 +706,14 @@ TypeCheckType::visit (HIR::ArrayType &type)
rust_assert (ok);
context->insert_type (type.get_size_expr ().get_mappings (), expected_ty);

TyTy::ConstType *const_type = nullptr;
TyTy::BaseConstType *const_type = nullptr;
if (capacity_type->get_kind () == TyTy::TypeKind::CONST)
{
const_type = static_cast<TyTy::ConstType *> (capacity_type);
const_type = capacity_type->as_const_type ();

unify_site (type.get_size_expr ().get_mappings ().get_hirid (),
TyTy::TyWithLocation (expected_ty),
TyTy::TyWithLocation (const_type->get_ty (),
TyTy::TyWithLocation (const_type->get_specified_type (),
type.get_size_expr ().get_locus ()),
type.get_size_expr ().get_locus ());
}
Expand All @@ -727,30 +727,26 @@ TypeCheckType::visit (HIR::ArrayType &type)
type.get_size_expr ().get_locus ());

if (result->is<TyTy::ErrorType> ())
{
const_type
= new TyTy::ConstType (TyTy::ConstType::ConstKind::Error, "",
expected_ty, error_mark_node, {},
type.get_size_expr ().get_locus (), size_id,
size_id);
}
const_type = new TyTy::ConstErrorType (expected_ty, size_id, size_id);
else
{
auto ctx = Compile::Context::get ();
tree capacity_expr
= Compile::HIRCompileBase::query_compile_const_expr (
ctx, capacity_type, type.get_size_expr ());

const_type = new TyTy::ConstType (TyTy::ConstType::ConstKind::Value,
"", expected_ty, capacity_expr, {},
type.get_size_expr ().get_locus (),
size_id, size_id);
const_type = new TyTy::ConstValueType (capacity_expr, expected_ty,
size_id, size_id);
context->insert_type (type.get_size_expr ().get_mappings (),
const_type->as_base_type ());
}
}

translated
= new TyTy::ArrayType (type.get_mappings ().get_hirid (), type.get_locus (),
const_type, TyTy::TyVar (element_type->get_ref ()));
TyTy::TyVar (
const_type->as_base_type ()->get_ty_ref ()),
TyTy::TyVar (element_type->get_ref ()));
}

void
Expand Down
Loading
Loading