Skip to content
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

WIP typecheck issues with Try #3394

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 2 additions & 2 deletions gcc/rust/backend/rust-compile-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ HIRCompileBase::compile_function (

ctx->add_statement (ret_var_stmt);

ctx->push_fn (fndecl, return_address, tyret);
ctx->push_fn (fndecl, return_address, tyret, fntype);
compile_function_body (fndecl, *function_body, tyret);
tree bind_tree = ctx->pop_block ();

Expand Down Expand Up @@ -825,7 +825,7 @@ HIRCompileBase::compile_constant_item (
address_is_taken, locus, &ret_var_stmt);

ctx->add_statement (ret_var_stmt);
ctx->push_fn (fndecl, return_address, resolved_type);
ctx->push_fn (fndecl, return_address, resolved_type, resolved_type);

if (is_block_expr)
{
Expand Down
6 changes: 4 additions & 2 deletions gcc/rust/backend/rust-compile-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct fncontext
tree fndecl;
::Bvariable *ret_addr;
TyTy::BaseType *retty;
TyTy::BaseType *fnty;
};

struct CustomDeriveInfo
Expand Down Expand Up @@ -275,9 +276,10 @@ class Context
return true;
}

void push_fn (tree fn, ::Bvariable *ret_addr, TyTy::BaseType *retty)
void push_fn (tree fn, ::Bvariable *ret_addr, TyTy::BaseType *retty,
TyTy::BaseType *fntype)
{
fn_stack.push_back (fncontext{fn, ret_addr, retty});
fn_stack.push_back (fncontext{fn, ret_addr, retty, fntype});
}
void pop_fn () { fn_stack.pop_back (); }

Expand Down
16 changes: 15 additions & 1 deletion gcc/rust/backend/rust-compile-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,8 @@ CompileExpr::visit (HIR::CallExpr &expr)
{
rust_assert (tyty->get_kind () == TyTy::TypeKind::ADT);
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (tyty);
rust_debug ("111 XXXXXXXXXXXXXXXXXXXXXXX");
adt->debug ();
tree compiled_adt_type = TyTyResolveCompile::compile (ctx, tyty);

// what variant is it?
Expand Down Expand Up @@ -1273,11 +1275,23 @@ CompileExpr::visit (HIR::CallExpr &expr)
return;
}

rust_debug_loc (expr.get_locus (), "XXXXXXXXXX ADT CTOR");
debug_tree (compiled_adt_type);

const auto &c = ctx->peek_fn ();
c.fnty->debug ();

HIR::Expr &discrim_expr = variant->get_discriminant ();
tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx);
tree folded_discrim_expr = fold_expr (discrim_expr_node);
tree qualifier = folded_discrim_expr;

if (compiled_adt_type == error_mark_node)
{
rust_debug_loc (expr.get_fnexpr ().get_locus (),
"XXX this is broken!!");
}

tree enum_root_files = TYPE_FIELDS (compiled_adt_type);
tree payload_root = DECL_CHAIN (enum_root_files);

Expand Down Expand Up @@ -2498,7 +2512,7 @@ CompileExpr::generate_closure_function (HIR::ClosureExpr &expr,

ctx->add_statement (ret_var_stmt);

ctx->push_fn (fndecl, return_address, tyret);
ctx->push_fn (fndecl, return_address, tyret, fn_tyty);

if (is_block_expr)
{
Expand Down
27 changes: 26 additions & 1 deletion gcc/rust/backend/rust-compile-item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "rust-compile-item.h"
#include "rust-compile-implitem.h"
#include "rust-compile-extern.h"
#include "rust-substitution-mapper.h"
#include "rust-type-util.h"
#include "rust-immutable-name-resolution-context.h"

namespace Rust {
Expand Down Expand Up @@ -159,6 +161,17 @@ CompileItem::visit (HIR::Function &function)

rust_assert (fntype_tyty->get_kind () == TyTy::TypeKind::FNDEF);
TyTy::FnType *fntype = static_cast<TyTy::FnType *> (fntype_tyty);

if (concrete && fntype->has_substitutions_defined ())
{
rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
TyTy::FnType *concrete_fnty = static_cast<TyTy::FnType *> (fntype_tyty);

rust_debug ("XPPPPPPPPPPPPPPPPPPPPPPPPPPPPP");
fntype->debug ();
concrete_fnty->debug ();
}

if (fntype->has_substitutions_defined ())
{
// we cant do anything for this only when it is used and a concrete type
Expand All @@ -168,7 +181,19 @@ CompileItem::visit (HIR::Function &function)
else
{
rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
fntype = static_cast<TyTy::FnType *> (concrete);
TyTy::BaseType *infer
= Resolver::SubstMapper::InferSubst (fntype, function.get_locus ());
TyTy::BaseType *resolved
= Resolver::unify_site (function.get_mappings ().get_hirid (),
TyTy::TyWithLocation (infer),
TyTy::TyWithLocation (concrete),
function.get_locus ());

rust_debug ("UUUUUUUUUUUUU");
resolved->debug ();

rust_assert (resolved->is<TyTy::FnType> ());
fntype = resolved->as<TyTy::FnType> ();
fntype->monomorphize ();
}
}
Expand Down
4 changes: 2 additions & 2 deletions gcc/rust/backend/rust-compile-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class CompileItem : private HIRCompileBase, protected HIR::HIRStmtVisitor
CompileItem compiler (ctx, concrete, ref_locus);
item->accept_vis (compiler);

if (compiler.reference == error_mark_node)
rust_debug ("failed to compile item: %s", item->as_string ().c_str ());
// if (compiler.reference == error_mark_node)
// rust_debug ("failed to compile item: %s", item->as_string ().c_str ());

return compiler.reference;
}
Expand Down
38 changes: 27 additions & 11 deletions gcc/rust/backend/rust-compile-resolve-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,25 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup,
trait->get_mappings ().get_defid (), &trait_ref);
rust_assert (ok);

TyTy::BaseType *receiver = nullptr;
ok = ctx->get_tyctx ()->lookup_receiver (mappings.get_hirid (),
&receiver);
rust_assert (ok);
receiver = receiver->destructure ();

// the type resolver can only resolve type bounds to their trait
// item so its up to us to figure out if this path should resolve
// to an trait-impl-block-item or if it can be defaulted to the
// trait-impl-item's definition
//
// because we know this is resolved to a trait item we can actually
// just grab the Self type parameter here for the receiver to match
// the appropriate impl block

rust_assert (lookup->is<TyTy::FnType> ());
auto fn = lookup->as<TyTy::FnType> ();
rust_assert (fn->get_num_type_params () > 0);
auto &self = fn->get_substs ().at (0);
auto receiver = self.get_param_ty ();

rust_debug ("XXXXXXXXXX");
lookup->debug ();
receiver->debug ();

auto candidates
= Resolver::PathProbeImplTrait::Probe (receiver, final_segment,
trait_ref);
Expand Down Expand Up @@ -340,12 +349,19 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup,
rust_assert (ok);

if (!lookup->has_substitutions_defined ())
return CompileInherentImplItem::Compile (impl_item, ctx,
nullptr, true,
expr_locus);
{
rust_debug ("XXXXXXXXX YYY 1");
return CompileInherentImplItem::Compile (impl_item, ctx,
nullptr, true,
expr_locus);
}
else
return CompileInherentImplItem::Compile (impl_item, ctx, lookup,
true, expr_locus);
{
rust_debug ("XXXXXXXXX YYY 2");
return CompileInherentImplItem::Compile (impl_item, ctx,
lookup, true,
expr_locus);
}
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,12 @@ PrivacyReporter::check_base_type_privacy (Analysis::NodeMapping &node_mappings,
static_cast<const TyTy::TupleType *> (ty)->get_fields ())
recursive_check (param.get_tyty ());
return;
case TyTy::PLACEHOLDER:
return recursive_check (
// FIXME: Can we use `resolve` here? Is that what we should do?
static_cast<const TyTy::PlaceholderType *> (ty)->resolve ());
case TyTy::PLACEHOLDER: {
const auto p = static_cast<const TyTy::PlaceholderType *> (ty);
if (!p->can_resolve ())
return;
return recursive_check (p->resolve ());
}
case TyTy::PROJECTION:
return recursive_check (
static_cast<const TyTy::ProjectionType *> (ty)->get ());
Expand Down
12 changes: 6 additions & 6 deletions gcc/rust/typecheck/rust-hir-path-probe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ PathProbeType::visit (HIR::TypeAlias &alias)
{
HirId tyid = alias.get_mappings ().get_hirid ();
TyTy::BaseType *ty = nullptr;
bool ok = query_type (tyid, &ty);
rust_assert (ok);
if (!query_type (tyid, &ty))
return;

PathProbeCandidate::ImplItemCandidate impl_item_candidate{&alias,
current_impl};
Expand All @@ -232,8 +232,8 @@ PathProbeType::visit (HIR::ConstantItem &constant)
{
HirId tyid = constant.get_mappings ().get_hirid ();
TyTy::BaseType *ty = nullptr;
bool ok = query_type (tyid, &ty);
rust_assert (ok);
if (!query_type (tyid, &ty))
return;

PathProbeCandidate::ImplItemCandidate impl_item_candidate{&constant,
current_impl};
Expand All @@ -252,8 +252,8 @@ PathProbeType::visit (HIR::Function &function)
{
HirId tyid = function.get_mappings ().get_hirid ();
TyTy::BaseType *ty = nullptr;
bool ok = query_type (tyid, &ty);
rust_assert (ok);
if (!query_type (tyid, &ty))
return;

PathProbeCandidate::ImplItemCandidate impl_item_candidate{&function,
current_impl};
Expand Down
81 changes: 65 additions & 16 deletions gcc/rust/typecheck/rust-hir-type-check-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ TypeCheckExpr::visit (HIR::QualifiedPathInExpression &expr)

// lookup the associated item from the specified bound
HIR::PathExprSegment &item_seg = expr.get_segments ().at (0);
HIR::PathIdentSegment item_seg_identifier = item_seg.get_segment ();
HIR::PathIdentSegment &item_seg_identifier = item_seg.get_segment ();
TyTy::TypeBoundPredicateItem item
= specified_bound.lookup_associated_item (item_seg_identifier.as_string ());
if (item.is_error ())
Expand Down Expand Up @@ -416,28 +416,77 @@ TypeCheckExpr::resolve_segments (NodeId root_resolved_node_id,
for (size_t i = offset; i < segments.size (); i++)
{
HIR::PathExprSegment &seg = segments.at (i);
HIR::PathIdentSegment &item_seg_identifier = seg.get_segment ();
bool probe_impls = !receiver_is_generic;

// probe the path is done in two parts one where we search impls if no
// candidate is found then we search extensions from traits
auto candidates
= PathProbeType::Probe (prev_segment, seg.get_segment (), probe_impls,
false /*probe_bounds*/,
true /*ignore_mandatory_trait_items*/);
std::set<PathProbeCandidate> candidates = {};

// // is this segment a Trait TODO
// if (prev_segment->is<TyTy::DynamicObjectType> ()
// && prev_segment->num_specified_bounds () == 1)
// {
// rust_debug ("attempting trait lookup magic man ");
// TyTy::TyVar infer_var_tyvar
// = TyTy::TyVar::get_implicit_infer_var (seg.get_locus ());
// TyTy::BaseType *new_seg = infer_var_tyvar.get_tyty ();
// new_seg->inherit_bounds (*prev_segment);

// auto &pred = new_seg->get_specified_bounds ().at (0);
// TyTy::TypeBoundPredicateItem item
// = pred.lookup_associated_item (item_seg_identifier.as_string ());
// if (!item.is_error ())
// {
// PathProbeCandidate::CandidateType candidate_type;
// switch (item.get_raw_item ()->get_trait_item_type ())
// {
// case TraitItemReference::TraitItemType::FN:
// candidate_type
// = PathProbeCandidate::CandidateType::TRAIT_FUNC;
// break;
// case TraitItemReference::TraitItemType::CONST:
// candidate_type
// = PathProbeCandidate::CandidateType::TRAIT_ITEM_CONST;
// break;
// case TraitItemReference::TraitItemType::TYPE:
// candidate_type
// = PathProbeCandidate::CandidateType::TRAIT_TYPE_ALIAS;
// break;

// case TraitItemReference::TraitItemType::ERROR:
// default:
// rust_unreachable ();
// break;
// }

// auto item_ty = item.get_tyty_for_receiver (new_seg);
// auto trait_item_candidate
// = PathProbeCandidate::TraitItemCandidate{
// item.get_parent ()->get (), item.get_raw_item (), nullptr};
// auto candidate
// = PathProbeCandidate{candidate_type, item_ty, item.get_locus (),
// trait_item_candidate};
// candidates = {std::move (candidate)};
// }
// else

// probe the path is done in two parts one where we search impls
// if no candidate is found then we search extensions from traits
candidates = PathProbeType::Probe (prev_segment, seg.get_segment (),
probe_impls, false /*probe_bounds*/,
true /*ignore_mandatory_trait_items*/);
if (candidates.size () == 0)
{
candidates
= PathProbeType::Probe (prev_segment, seg.get_segment (), false,
= PathProbeType::Probe (prev_segment, seg.get_segment (),
false /*probe_impls*/,
true /*probe_bounds*/,
false /*ignore_mandatory_trait_items*/);
}

if (candidates.size () == 0)
{
rust_error_at (
seg.get_locus (),
"failed to resolve path segment using an impl Probe");
return;
}
if (candidates.size () == 0)
{
rust_error_at (seg.get_locus (),
"failed to resolve path segment using an impl Probe");
return;
}

if (candidates.size () > 1)
Expand Down
8 changes: 6 additions & 2 deletions gcc/rust/typecheck/rust-substitution-mapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,10 @@ SubstMapperInternal::visit (TyTy::FnType &type)
{
TyTy::SubstitutionArgumentMappings adjusted
= type.adjust_mappings_for_this (mappings);
if (adjusted.is_error ())
if (adjusted.is_error () && !mappings.trait_item_mode ())
return;
if (adjusted.is_error () && mappings.trait_item_mode ())
adjusted = mappings;

TyTy::BaseType *concrete = type.handle_substitions (adjusted);
if (concrete != nullptr)
Expand All @@ -205,8 +207,10 @@ SubstMapperInternal::visit (TyTy::ADTType &type)
{
TyTy::SubstitutionArgumentMappings adjusted
= type.adjust_mappings_for_this (mappings);
if (adjusted.is_error ())
if (adjusted.is_error () && !mappings.trait_item_mode ())
return;
if (adjusted.is_error () && mappings.trait_item_mode ())
adjusted = mappings;

TyTy::BaseType *concrete = type.handle_substitions (adjusted);
if (concrete != nullptr)
Expand Down
6 changes: 4 additions & 2 deletions gcc/rust/typecheck/rust-type-util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,10 @@ coercion_site (HirId id, TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs,
rust_debug ("coerce_default_unify(a={%s}, b={%s})",
receiver->debug_str ().c_str (), expected->debug_str ().c_str ());
TyTy::BaseType *coerced
= unify_site (id, lhs, TyTy::TyWithLocation (receiver, rhs.get_locus ()),
locus);
= unify_site_and (id, lhs,
TyTy::TyWithLocation (receiver, rhs.get_locus ()), locus,
true /*emit_error*/, true /*commit*/, true /*infer*/,
true /*cleanup*/);
context->insert_autoderef_mappings (id, std::move (result.adjustments));
return coerced;
}
Expand Down
Loading
Loading