Skip to content

Commit

Permalink
this causes more regressions but lets see if it fixes the generics29,…
Browse files Browse the repository at this point in the history
…30,31 issue on macros by using a safer optional on passing the HIR::GenericArgs
  • Loading branch information
philberty committed Nov 7, 2022
1 parent e63de69 commit 135f54f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 27 deletions.
5 changes: 3 additions & 2 deletions gcc/rust/typecheck/rust-hir-type-check-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1152,10 +1152,11 @@ TypeCheckExpr::visit (HIR::MethodCallExpr &expr)
rust_debug_loc (expr.get_method_name ().get_generic_args ().get_locus (),
"applying generic arguments to method_call: {%s}",
lookup->debug_str ().c_str ());
HIR::GenericArgs &args = expr.get_method_name ().get_generic_args ();
auto wrapped_args = Optional<HIR::GenericArgs &>::some (
expr.get_method_name ().get_generic_args ());
lookup
= SubstMapper::Resolve (lookup, expr.get_method_name ().get_locus (),
&args);
wrapped_args);
if (lookup->get_kind () == TyTy::TypeKind::ERROR)
return;
}
Expand Down
16 changes: 10 additions & 6 deletions gcc/rust/typecheck/rust-hir-type-check-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ TypeCheckExpr::visit (HIR::QualifiedPathInExpression &expr)
infered = new TyTy::ErrorType (expr.get_mappings ().get_hirid ());
return;
}
infered = SubstMapper::Resolve (infered, expr.get_locus (),
&item_seg.get_generic_args ());
auto wrapped_args
= Optional<HIR::GenericArgs &>::some (item_seg.get_generic_args ());
infered = SubstMapper::Resolve (infered, expr.get_locus (), wrapped_args);
}

// continue on as a path-in-expression
Expand Down Expand Up @@ -277,8 +278,10 @@ TypeCheckExpr::resolve_root_path (HIR::PathInExpression &expr, size_t *offset,
return new TyTy::ErrorType (expr.get_mappings ().get_hirid ());
}

lookup = SubstMapper::Resolve (lookup, expr.get_locus (),
&seg.get_generic_args ());
auto wrapped_args
= Optional<HIR::GenericArgs &>::some (seg.get_generic_args ());
lookup
= SubstMapper::Resolve (lookup, expr.get_locus (), wrapped_args);
if (lookup->get_kind () == TyTy::TypeKind::ERROR)
return new TyTy::ErrorType (expr.get_mappings ().get_hirid ());
}
Expand Down Expand Up @@ -424,8 +427,9 @@ TypeCheckExpr::resolve_segments (NodeId root_resolved_node_id,
return;
}

tyseg = SubstMapper::Resolve (tyseg, expr_locus,
&seg.get_generic_args ());
auto wrapped_args
= Optional<HIR::GenericArgs &>::some (seg.get_generic_args ());
tyseg = SubstMapper::Resolve (tyseg, expr_locus, wrapped_args);
if (tyseg->get_kind () == TyTy::TypeKind::ERROR)
return;
}
Expand Down
9 changes: 9 additions & 0 deletions gcc/rust/typecheck/rust-hir-type-check-stmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ TypeCheckStmt::visit (HIR::LetStmt &stmt)
TyTy::TyWithLocation (init_expr_ty, init_expr_locus),
stmt.get_locus ());
context->insert_type (stmt_pattern.get_pattern_mappings (), specified_ty);
rust_debug_loc (stmt.get_locus (),
"resolved stmt with coercion-site to: %s",
specified_ty->debug_str ().c_str ());
}
else
{
Expand All @@ -120,12 +123,18 @@ TypeCheckStmt::visit (HIR::LetStmt &stmt)
{
context->insert_type (stmt_pattern.get_pattern_mappings (),
specified_ty);
rust_debug_loc (stmt.get_locus (),
"resolved stmt of specified ty to: %s",
specified_ty->debug_str ().c_str ());
}
// let x = 123;
else if (init_expr_ty != nullptr)
{
context->insert_type (stmt_pattern.get_pattern_mappings (),
init_expr_ty);
rust_debug_loc (stmt.get_locus (),
"resolved stmt with initilizer to: %s",
init_expr_ty->debug_str ().c_str ());
}
// let x;
else
Expand Down
21 changes: 13 additions & 8 deletions gcc/rust/typecheck/rust-hir-type-check-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,11 @@ TypeCheckType::visit (HIR::QualifiedPathInType &path)
= new TyTy::ErrorType (path.get_mappings ().get_hirid ());
return;
}

auto wrapped_args = Optional<HIR::GenericArgs &>::some (
generic_seg.get_generic_args ());
translated = SubstMapper::Resolve (translated, path.get_locus (),
&generic_seg.get_generic_args ());
wrapped_args);
}
}

Expand Down Expand Up @@ -395,14 +398,15 @@ TypeCheckType::resolve_root_path (HIR::TypePath &path, size_t *offset,
lookup->as_string ().c_str ());
return new TyTy::ErrorType (lookup->get_ref ());
}
lookup = SubstMapper::Resolve (lookup, path.get_locus (),
&generic_segment->get_generic_args ());
auto wrapped_args = Optional<HIR::GenericArgs &>::some (
generic_segment->get_generic_args ());
lookup
= SubstMapper::Resolve (lookup, path.get_locus (), wrapped_args);
}
else if (lookup->needs_generic_substitutions ())
{
HIR::GenericArgs empty
= HIR::GenericArgs::create_empty (path.get_locus ());
lookup = SubstMapper::Resolve (lookup, path.get_locus (), &empty);
auto empty = Optional<HIR::GenericArgs &>::none ();
lookup = SubstMapper::Resolve (lookup, path.get_locus (), empty);
}

*root_resolved_node_id = ref_node_id;
Expand Down Expand Up @@ -489,8 +493,9 @@ TypeCheckType::resolve_segments (
return new TyTy::ErrorType (expr_id);
}

tyseg = SubstMapper::Resolve (tyseg, expr_locus,
&generic_segment->get_generic_args ());
auto wrapped_args = Optional<HIR::GenericArgs &>::some (
generic_segment->get_generic_args ());
tyseg = SubstMapper::Resolve (tyseg, expr_locus, wrapped_args);
if (tyseg->get_kind () == TyTy::TypeKind::ERROR)
return new TyTy::ErrorType (expr_id);
}
Expand Down
26 changes: 15 additions & 11 deletions gcc/rust/typecheck/rust-substitution-mapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "rust-tyty.h"
#include "rust-tyty-visitor.h"
#include "rust-optional.h"

namespace Rust {
namespace Resolver {
Expand All @@ -29,20 +30,21 @@ class SubstMapper : public TyTy::TyVisitor
{
public:
static TyTy::BaseType *Resolve (TyTy::BaseType *base, Location locus,
HIR::GenericArgs *generics = nullptr)
Optional<HIR::GenericArgs &> &specified_args)
{
SubstMapper mapper (base->get_ref (), generics, locus);
SubstMapper mapper (base->get_ref (), specified_args, locus);
base->accept_vis (mapper);
rust_assert (mapper.resolved != nullptr);
return mapper.resolved;
}

static TyTy::BaseType *InferSubst (TyTy::BaseType *base, Location locus)
{
return SubstMapper::Resolve (base, locus, nullptr);
auto empty = Optional<HIR::GenericArgs &>::none ();
return SubstMapper::Resolve (base, locus, empty);
}

bool have_generic_args () const { return generics != nullptr; }
bool have_generic_args () const { return specified_args.is_some (); }

void visit (TyTy::FnType &type) override
{
Expand All @@ -56,7 +58,7 @@ class SubstMapper : public TyTy::TyVisitor
else
{
TyTy::SubstitutionArgumentMappings mappings
= type.get_mappings_from_generic_args (*generics);
= type.get_mappings_from_generic_args (specified_args.get ());
if (mappings.is_error ())
return;

Expand All @@ -79,7 +81,7 @@ class SubstMapper : public TyTy::TyVisitor
else
{
TyTy::SubstitutionArgumentMappings mappings
= type.get_mappings_from_generic_args (*generics);
= type.get_mappings_from_generic_args (specified_args.get ());
if (mappings.is_error ())
return;

Expand All @@ -93,7 +95,7 @@ class SubstMapper : public TyTy::TyVisitor
void visit (TyTy::PlaceholderType &type) override
{
rust_assert (type.can_resolve ());
resolved = SubstMapper::Resolve (type.resolve (), locus, generics);
resolved = SubstMapper::Resolve (type.resolve (), locus, specified_args);
}

void visit (TyTy::ProjectionType &type) override
Expand All @@ -108,7 +110,7 @@ class SubstMapper : public TyTy::TyVisitor
else
{
TyTy::SubstitutionArgumentMappings mappings
= type.get_mappings_from_generic_args (*generics);
= type.get_mappings_from_generic_args (specified_args.get ());
if (mappings.is_error ())
return;

Expand Down Expand Up @@ -142,12 +144,14 @@ class SubstMapper : public TyTy::TyVisitor
void visit (TyTy::ClosureType &) override { gcc_unreachable (); }

private:
SubstMapper (HirId ref, HIR::GenericArgs *generics, Location locus)
: resolved (new TyTy::ErrorType (ref)), generics (generics), locus (locus)
SubstMapper (HirId ref, Optional<HIR::GenericArgs &> &specified_args,
Location locus)
: resolved (new TyTy::ErrorType (ref)), specified_args (specified_args),
locus (locus)
{}

TyTy::BaseType *resolved;
HIR::GenericArgs *generics;
Optional<HIR::GenericArgs &> &specified_args;
Location locus;
};

Expand Down

0 comments on commit 135f54f

Please sign in to comment.