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

Use nr2.0 in typechecker #3253

Merged
merged 1 commit into from
Nov 21, 2024
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
45 changes: 40 additions & 5 deletions gcc/rust/typecheck/rust-hir-type-check-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#include "rust-hir-type-check-stmt.h"
#include "rust-hir-type-check-item.h"
#include "rust-type-util.h"
#include "rust-immutable-name-resolution-context.h"

// for flag_name_resolution_2_0
#include "options.h"

namespace Rust {
namespace Resolver {
Expand Down Expand Up @@ -1238,8 +1242,17 @@ TypeCheckExpr::visit (HIR::MethodCallExpr &expr)
// store the expected fntype
context->insert_type (expr.get_method_name ().get_mappings (), lookup);

if (flag_name_resolution_2_0)
{
auto &nr_ctx = const_cast<Resolver2_0::NameResolutionContext &> (
Resolver2_0::ImmutableNameResolutionContext::get ().resolver ());

nr_ctx.map_usage (Resolver2_0::Usage (expr.get_mappings ().get_nodeid ()),
Resolver2_0::Definition (resolved_node_id));
}
// set up the resolved name on the path
if (resolver->get_name_scope ().decl_was_declared_here (resolved_node_id))
else if (resolver->get_name_scope ().decl_was_declared_here (
resolved_node_id))
{
resolver->insert_resolved_name (expr.get_mappings ().get_nodeid (),
resolved_node_id);
Expand Down Expand Up @@ -1821,8 +1834,19 @@ TypeCheckExpr::resolve_operator_overload (LangItem::Kind lang_item_type,
context->insert_operator_overload (expr.get_mappings ().get_hirid (), type);

// set up the resolved name on the path
resolver->insert_resolved_name (expr.get_mappings ().get_nodeid (),
resolved_node_id);
if (flag_name_resolution_2_0)
{
auto &nr_ctx = const_cast<Resolver2_0::NameResolutionContext &> (
Resolver2_0::ImmutableNameResolutionContext::get ().resolver ());

nr_ctx.map_usage (Resolver2_0::Usage (expr.get_mappings ().get_nodeid ()),
Resolver2_0::Definition (resolved_node_id));
}
else
{
resolver->insert_resolved_name (expr.get_mappings ().get_nodeid (),
resolved_node_id);
}

// return the result of the function back
infered = function_ret_tyty;
Expand Down Expand Up @@ -1991,8 +2015,19 @@ TypeCheckExpr::resolve_fn_trait_call (HIR::CallExpr &expr,
context->insert_operator_overload (expr.get_mappings ().get_hirid (), fn);

// set up the resolved name on the path
resolver->insert_resolved_name (expr.get_mappings ().get_nodeid (),
resolved_node_id);
if (flag_name_resolution_2_0)
{
auto &nr_ctx = const_cast<Resolver2_0::NameResolutionContext &> (
Resolver2_0::ImmutableNameResolutionContext::get ().resolver ());

nr_ctx.map_usage (Resolver2_0::Usage (expr.get_mappings ().get_nodeid ()),
Resolver2_0::Definition (resolved_node_id));
}
else
{
resolver->insert_resolved_name (expr.get_mappings ().get_nodeid (),
resolved_node_id);
}

// return the result of the function back
*result = function_ret_tyty;
Expand Down
27 changes: 24 additions & 3 deletions gcc/rust/typecheck/rust-hir-type-check-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,20 @@ TypeCheckExpr::visit (HIR::QualifiedPathInExpression &expr)
bool fully_resolved = expr.get_segments ().size () <= 1;
if (fully_resolved)
{
resolver->insert_resolved_name (expr.get_mappings ().get_nodeid (),
root_resolved_node_id);
if (flag_name_resolution_2_0)
{
auto &nr_ctx = const_cast<Resolver2_0::NameResolutionContext &> (
Resolver2_0::ImmutableNameResolutionContext::get ().resolver ());

nr_ctx.map_usage (Resolver2_0::Usage (
expr.get_mappings ().get_nodeid ()),
Resolver2_0::Definition (root_resolved_node_id));
}
else
{
resolver->insert_resolved_name (expr.get_mappings ().get_nodeid (),
root_resolved_node_id);
}
context->insert_receiver (expr.get_mappings ().get_hirid (), root);
return;
}
Expand Down Expand Up @@ -491,8 +503,17 @@ TypeCheckExpr::resolve_segments (NodeId root_resolved_node_id,
rust_assert (resolved_node_id != UNKNOWN_NODEID);
context->insert_receiver (expr_mappings.get_hirid (), prev_segment);

if (flag_name_resolution_2_0)
{
auto &nr_ctx = const_cast<Resolver2_0::NameResolutionContext &> (
Resolver2_0::ImmutableNameResolutionContext::get ().resolver ());

nr_ctx.map_usage (Resolver2_0::Usage (expr_mappings.get_nodeid ()),
Resolver2_0::Definition (resolved_node_id));
}
// name scope first
if (resolver->get_name_scope ().decl_was_declared_here (resolved_node_id))
else if (resolver->get_name_scope ().decl_was_declared_here (
resolved_node_id))
{
resolver->insert_resolved_name (expr_mappings.get_nodeid (),
resolved_node_id);
Expand Down
122 changes: 91 additions & 31 deletions gcc/rust/typecheck/rust-hir-type-check-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,7 @@ TypeCheckType::visit (HIR::TupleType &tuple)
{
if (tuple.is_unit_type ())
{
auto unit_node_id = resolver->get_unit_type_node_id ();
if (!context->lookup_builtin (unit_node_id, &translated))
{
rust_error_at (tuple.get_locus (),
"failed to lookup builtin unit type");
}
translated = TyTy::TupleType::get_unit_type ();
return;
}

Expand Down Expand Up @@ -176,9 +171,25 @@ TypeCheckType::visit (HIR::QualifiedPathInType &path)
{
// then this is just a normal path-in-expression
NodeId root_resolved_node_id = UNKNOWN_NODEID;
bool ok = resolver->lookup_resolved_type (
qual_path_type.get_type ().get_mappings ().get_nodeid (),
&root_resolved_node_id);
bool ok = false;
if (flag_name_resolution_2_0)
{
auto &nr_ctx
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();

if (auto id = nr_ctx.lookup (
qual_path_type.get_type ().get_mappings ().get_nodeid ()))
{
root_resolved_node_id = *id;
ok = true;
}
}
else
{
ok = resolver->lookup_resolved_type (
qual_path_type.get_type ().get_mappings ().get_nodeid (),
&root_resolved_node_id);
}
rust_assert (ok);

translated = resolve_segments (root_resolved_node_id,
Expand Down Expand Up @@ -323,8 +334,21 @@ TypeCheckType::visit (HIR::QualifiedPathInType &path)
bool fully_resolved = path.get_segments ().empty ();
if (fully_resolved)
{
resolver->insert_resolved_type (path.get_mappings ().get_nodeid (),
root_resolved_node_id);
if (flag_name_resolution_2_0)
{
auto &nr_ctx = const_cast<Resolver2_0::NameResolutionContext &> (
Resolver2_0::ImmutableNameResolutionContext::get ().resolver ());

nr_ctx.map_usage (Resolver2_0::Usage (
path.get_mappings ().get_nodeid ()),
Resolver2_0::Definition (root_resolved_node_id));
}
else
{
resolver->insert_resolved_type (path.get_mappings ().get_nodeid (),
root_resolved_node_id);
}

context->insert_receiver (path.get_mappings ().get_hirid (), root);
return;
}
Expand Down Expand Up @@ -595,33 +619,53 @@ TypeCheckType::resolve_segments (
context->insert_receiver (expr_mappings.get_hirid (), prev_segment);
rust_assert (resolved_node_id != UNKNOWN_NODEID);

// lookup if the name resolver was able to canonically resolve this or not
NodeId path_resolved_id = UNKNOWN_NODEID;
if (resolver->lookup_resolved_name (expr_mappings.get_nodeid (),
&path_resolved_id))
{
rust_assert (path_resolved_id == resolved_node_id);
}
// check the type scope
else if (resolver->lookup_resolved_type (expr_mappings.get_nodeid (),
&path_resolved_id))
if (flag_name_resolution_2_0)
{
rust_assert (path_resolved_id == resolved_node_id);
auto &nr_ctx = const_cast<Resolver2_0::NameResolutionContext &> (
Resolver2_0::ImmutableNameResolutionContext::get ().resolver ());

auto old = nr_ctx.lookup (expr_mappings.get_nodeid ());
if (old.has_value ())
{
rust_assert (*old == resolved_node_id);
}
else
{
nr_ctx.map_usage (Resolver2_0::Usage (expr_mappings.get_nodeid ()),
Resolver2_0::Definition (resolved_node_id));
}
}
else
{
// name scope first
if (resolver->get_name_scope ().decl_was_declared_here (resolved_node_id))
// lookup if the name resolver was able to canonically resolve this or not
NodeId path_resolved_id = UNKNOWN_NODEID;
if (resolver->lookup_resolved_name (expr_mappings.get_nodeid (),
&path_resolved_id))
{
resolver->insert_resolved_name (expr_mappings.get_nodeid (),
resolved_node_id);
rust_assert (path_resolved_id == resolved_node_id);
}
// check the type scope
else if (resolver->get_type_scope ().decl_was_declared_here (
resolved_node_id))
else if (resolver->lookup_resolved_type (expr_mappings.get_nodeid (),
&path_resolved_id))
{
resolver->insert_resolved_type (expr_mappings.get_nodeid (),
resolved_node_id);
rust_assert (path_resolved_id == resolved_node_id);
}
else
{
// name scope first
if (resolver->get_name_scope ().decl_was_declared_here (
resolved_node_id))
{
resolver->insert_resolved_name (expr_mappings.get_nodeid (),
resolved_node_id);
}
// check the type scope
else if (resolver->get_type_scope ().decl_was_declared_here (
resolved_node_id))
{
resolver->insert_resolved_type (expr_mappings.get_nodeid (),
resolved_node_id);
}
}
}

Expand Down Expand Up @@ -990,7 +1034,23 @@ ResolveWhereClauseItem::visit (HIR::TypeBoundWhereClauseItem &item)

// then lookup the reference_node_id
NodeId ref_node_id = UNKNOWN_NODEID;
if (!resolver->lookup_resolved_type (ast_node_id, &ref_node_id))
if (flag_name_resolution_2_0)
{
auto &nr_ctx
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();

if (auto id = nr_ctx.lookup (ast_node_id))
ref_node_id = *id;
}
else
{
NodeId id = UNKNOWN_NODEID;

if (resolver->lookup_resolved_type (ast_node_id, &id))
ref_node_id = id;
}

if (ref_node_id == UNKNOWN_NODEID)
{
// FIXME
rust_error_at (UNDEF_LOCATION,
Expand Down
4 changes: 0 additions & 4 deletions gcc/testsuite/rust/compile/nr2/exclude
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ issue-2037.rs
issue-2043.rs
issue-2070.rs
issue-2105.rs
issue-2106.rs
issue-2135.rs
issue-2136-1.rs
issue-2136-2.rs
Expand All @@ -88,10 +87,8 @@ issue-2330.rs
issue-2375.rs
issue-2478.rs
issue-2479.rs
issue-2514.rs
issue-2723-1.rs
issue-2723-2.rs
issue-2772-1.rs
issue-2772-2.rs
issue-2775.rs
issue-2747.rs
Expand Down Expand Up @@ -194,7 +191,6 @@ issue-3046.rs
unknown-associated-item.rs
issue-3030.rs
issue-3035.rs
issue-3082.rs
issue-3139-1.rs
issue-3139-2.rs
issue-3139-3.rs
Expand Down
Loading