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

Borrowck: variance analysis #2823

Merged
merged 3 commits into from
Mar 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
1 change: 1 addition & 0 deletions gcc/rust/Make-lang.in
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ GRS_OBJS = \
rust/rust-tyty-util.o \
rust/rust-tyty-call.o \
rust/rust-tyty-subst.o \
rust/rust-tyty-variance-analysis.o \
rust/rust-typecheck-context.o \
rust/rust-tyty-bounds.o \
rust/rust-hir-trait-resolve.o \
Expand Down
3 changes: 3 additions & 0 deletions gcc/rust/rust-session-manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "rust-attribute-values.h"
#include "rust-borrow-checker.h"
#include "rust-ast-validation.h"
#include "rust-tyty-variance-analysis.h"

#include "input.h"
#include "selftest.h"
Expand Down Expand Up @@ -652,6 +653,8 @@ Session::compile_crate (const char *filename)
// type resolve
Resolver::TypeResolution::Resolve (hir);

Resolver::TypeCheckContext::get ()->get_variance_analysis_ctx ().solve ();

if (saw_errors ())
return;

Expand Down
17 changes: 13 additions & 4 deletions gcc/rust/typecheck/rust-hir-type-check-item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "rust-hir-trait-resolve.h"
#include "rust-substitution-mapper.h"
#include "rust-type-util.h"
#include "rust-tyty-variance-analysis.h"

namespace Rust {
namespace Resolver {
Expand Down Expand Up @@ -204,7 +205,7 @@ TypeCheckItem::visit (HIR::TupleStruct &struct_decl)
TyTy::ADTType::ReprOptions repr
= parse_repr_options (attrs, struct_decl.get_locus ());

TyTy::BaseType *type = new TyTy::ADTType (
auto *type = new TyTy::ADTType (
struct_decl.get_mappings ().get_hirid (), mappings->get_next_hir_id (),
struct_decl.get_identifier ().as_string (), ident,
TyTy::ADTType::ADTKind::TUPLE_STRUCT, std::move (variants),
Expand All @@ -215,6 +216,8 @@ TypeCheckItem::visit (HIR::TupleStruct &struct_decl)

context->insert_type (struct_decl.get_mappings (), type);
infered = type;

context->get_variance_analysis_ctx ().add_type_constraints (*type);
}

void
Expand Down Expand Up @@ -266,7 +269,7 @@ TypeCheckItem::visit (HIR::StructStruct &struct_decl)
TyTy::ADTType::ReprOptions repr
= parse_repr_options (attrs, struct_decl.get_locus ());

TyTy::BaseType *type = new TyTy::ADTType (
auto *type = new TyTy::ADTType (
struct_decl.get_mappings ().get_hirid (), mappings->get_next_hir_id (),
struct_decl.get_identifier ().as_string (), ident,
TyTy::ADTType::ADTKind::STRUCT_STRUCT, std::move (variants),
Expand All @@ -277,6 +280,8 @@ TypeCheckItem::visit (HIR::StructStruct &struct_decl)

context->insert_type (struct_decl.get_mappings (), type);
infered = type;

context->get_variance_analysis_ctx ().add_type_constraints (*type);
}

void
Expand Down Expand Up @@ -307,7 +312,7 @@ TypeCheckItem::visit (HIR::Enum &enum_decl)
RustIdent ident{*canonical_path, enum_decl.get_locus ()};

// multi variant ADT
TyTy::BaseType *type
auto *type
= new TyTy::ADTType (enum_decl.get_mappings ().get_hirid (),
mappings->get_next_hir_id (),
enum_decl.get_identifier ().as_string (), ident,
Expand All @@ -316,6 +321,8 @@ TypeCheckItem::visit (HIR::Enum &enum_decl)

context->insert_type (enum_decl.get_mappings (), type);
infered = type;

context->get_variance_analysis_ctx ().add_type_constraints (*type);
}

void
Expand Down Expand Up @@ -363,7 +370,7 @@ TypeCheckItem::visit (HIR::Union &union_decl)
TyTy::VariantDef::VariantType::STRUCT, nullptr,
std::move (fields)));

TyTy::BaseType *type
auto *type
= new TyTy::ADTType (union_decl.get_mappings ().get_hirid (),
mappings->get_next_hir_id (),
union_decl.get_identifier ().as_string (), ident,
Expand All @@ -372,6 +379,8 @@ TypeCheckItem::visit (HIR::Union &union_decl)

context->insert_type (union_decl.get_mappings (), type);
infered = type;

context->get_variance_analysis_ctx ().add_type_constraints (*type);
}

void
Expand Down
6 changes: 6 additions & 0 deletions gcc/rust/typecheck/rust-hir-type-check.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "rust-hir-trait-reference.h"
#include "rust-autoderef.h"
#include "rust-tyty-region.h"
#include "rust-tyty-variance-analysis.h"

#include <stack>

Expand Down Expand Up @@ -233,6 +234,8 @@ class TypeCheckContext

void compute_inference_variables (bool error);

TyTy::VarianceAnalysis::CrateCtx &get_variance_analysis_ctx ();

private:
TypeCheckContext ();

Expand Down Expand Up @@ -272,6 +275,9 @@ class TypeCheckContext
std::set<HirId> querys_in_progress;
std::set<DefId> trait_queries_in_progress;

// variance analysis
TyTy::VarianceAnalysis::CrateCtx variance_analysis_ctx;

/** Used to resolve (interned) lifetime names to their bounding scope. */
class LifetimeResolver
{
Expand Down
6 changes: 6 additions & 0 deletions gcc/rust/typecheck/rust-typecheck-context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,12 @@ TypeCheckContext::compute_inference_variables (bool error)
});
}

TyTy::VarianceAnalysis::CrateCtx &
TypeCheckContext::get_variance_analysis_ctx ()
{
return variance_analysis_ctx;
}

// TypeCheckContextItem

TypeCheckContextItem::Item::Item (HIR::Function *item) : item (item) {}
Expand Down
11 changes: 11 additions & 0 deletions gcc/rust/typecheck/rust-tyty-subst.cc
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,17 @@ SubstitutionRef::get_used_arguments () const
return used_arguments;
}

tl::optional<SubstitutionArg>
SubstitutionRef::get_arg_at (size_t i) const
{
auto param_ty = get_substs ().at (i).get_param_ty ();
SubstitutionArg arg = SubstitutionArg::error ();
get_used_arguments ().get_argument_for_symbol (param_ty, &arg);
if (arg.is_error ())
return tl::nullopt;
return arg;
}

const RegionConstraints &
SubstitutionRef::get_region_constraints () const
{
Expand Down
Loading
Loading