aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust
diff options
context:
space:
mode:
authorPhilip Herron <herron.philip@googlemail.com>2025-07-25 16:56:17 +0100
committerArthur Cohen <arthur.cohen@embecosm.com>2025-08-05 16:37:00 +0200
commit4dc145a41507f7ee7be87a1f825b67a65437abe4 (patch)
tree38342eda9cbf0ff58090898fe94111c9bdbbfe32 /gcc/rust
parent948e4a63455c090afd491c7b669896d47ae08559 (diff)
downloadgcc-4dc145a41507f7ee7be87a1f825b67a65437abe4.zip
gcc-4dc145a41507f7ee7be87a1f825b67a65437abe4.tar.gz
gcc-4dc145a41507f7ee7be87a1f825b67a65437abe4.tar.bz2
gccrs: Refactor substitution param mapping to be more abstract
This is an initial patch required to refactor our generics code to be simpler and more abstract so we return the HIR::GenericParam in ParamMappings instead of assuming its a TypeParam so we can slowly introduce ConstGenericParam into this same flow. gcc/rust/ChangeLog: * typecheck/rust-hir-type-check-base.cc: check for type param * typecheck/rust-tyty-subst.cc (SubstitutionParamMapping::SubstitutionParamMapping): return HIR::GenericParam base class (SubstitutionParamMapping::get_generic_param): likewise (SubstitutionParamMapping::get_type_representation): new helper (SubstitutionParamMapping::param_has_default_ty): check for param type (SubstitutionParamMapping::get_default_ty): likewise * typecheck/rust-tyty-subst.h: get the locus from the subst HIR::GenericParam now * typecheck/rust-tyty-variance-analysis.cc (GenericTyPerCrateCtx::debug_print_solutions): likwise (GenericTyVisitorCtx::process_type): likewise Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Diffstat (limited to 'gcc/rust')
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-base.cc8
-rw-r--r--gcc/rust/typecheck/rust-tyty-subst.cc59
-rw-r--r--gcc/rust/typecheck/rust-tyty-subst.h12
-rw-r--r--gcc/rust/typecheck/rust-tyty-variance-analysis.cc7
4 files changed, 59 insertions, 27 deletions
diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc b/gcc/rust/typecheck/rust-hir-type-check-base.cc
index c1f15af..acc3316 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-base.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-base.cc
@@ -512,9 +512,13 @@ TypeCheckBase::resolve_generic_params (
// now walk them to setup any specified type param bounds
for (auto &subst : substitutions)
{
+ auto &generic = subst.get_generic_param ();
+ if (generic.get_kind () != HIR::GenericParam::GenericKind::TYPE)
+ continue;
+
+ auto &type_param = static_cast<HIR::TypeParam &> (generic);
auto pty = subst.get_param_ty ();
- TypeResolveGenericParam::ApplyAnyTraitBounds (subst.get_generic_param (),
- pty);
+ TypeResolveGenericParam::ApplyAnyTraitBounds (type_param, pty);
}
}
diff --git a/gcc/rust/typecheck/rust-tyty-subst.cc b/gcc/rust/typecheck/rust-tyty-subst.cc
index 2d5e87e..13aca42 100644
--- a/gcc/rust/typecheck/rust-tyty-subst.cc
+++ b/gcc/rust/typecheck/rust-tyty-subst.cc
@@ -28,7 +28,7 @@
namespace Rust {
namespace TyTy {
-SubstitutionParamMapping::SubstitutionParamMapping (HIR::TypeParam &generic,
+SubstitutionParamMapping::SubstitutionParamMapping (HIR::GenericParam &generic,
ParamType *param)
: generic (generic), param (param)
{}
@@ -66,13 +66,13 @@ SubstitutionParamMapping::get_param_ty () const
return param;
}
-HIR::TypeParam &
+HIR::GenericParam &
SubstitutionParamMapping::get_generic_param ()
{
return generic;
}
-const HIR::TypeParam &
+const HIR::GenericParam &
SubstitutionParamMapping::get_generic_param () const
{
return generic;
@@ -84,6 +84,14 @@ SubstitutionParamMapping::needs_substitution () const
return !(get_param_ty ()->is_concrete ());
}
+Identifier
+SubstitutionParamMapping::get_type_representation () const
+{
+ rust_assert (generic.get_kind () == HIR::GenericParam::GenericKind::TYPE);
+ const auto &type_param = static_cast<const HIR::TypeParam &> (generic);
+ return type_param.get_type_representation ();
+}
+
location_t
SubstitutionParamMapping::get_param_locus () const
{
@@ -93,13 +101,20 @@ SubstitutionParamMapping::get_param_locus () const
bool
SubstitutionParamMapping::param_has_default_ty () const
{
- return generic.has_type ();
+ if (generic.get_kind () != HIR::GenericParam::GenericKind::TYPE)
+ return false;
+
+ const auto &type_param = static_cast<const HIR::TypeParam &> (generic);
+ return type_param.has_type ();
}
BaseType *
SubstitutionParamMapping::get_default_ty () const
{
- TyVar var (generic.get_type_mappings ().get_hirid ());
+ rust_assert (generic.get_kind () == HIR::GenericParam::GenericKind::TYPE);
+
+ const auto &type_param = static_cast<const HIR::TypeParam &> (generic);
+ TyVar var (type_param.get_type_mappings ().get_hirid ());
return var.get_tyty ();
}
@@ -675,7 +690,11 @@ SubstitutionRef::get_mappings_from_generic_args (
{
rich_location r (line_table, args.get_locus ());
if (!substitutions.empty ())
- r.add_range (substitutions.front ().get_param_locus ());
+ {
+ const auto &subst = substitutions.front ();
+ const auto &generic = subst.get_generic_param ();
+ r.add_range (generic.get_locus ());
+ }
rust_error_at (
r,
@@ -688,7 +707,12 @@ SubstitutionRef::get_mappings_from_generic_args (
if (args.get_type_args ().size () + offs < min_required_substitutions ())
{
rich_location r (line_table, args.get_locus ());
- r.add_range (substitutions.front ().get_param_locus ());
+ if (!substitutions.empty ())
+ {
+ const auto &subst = substitutions.front ();
+ const auto &generic = subst.get_generic_param ();
+ r.add_range (generic.get_locus ());
+ }
rust_error_at (
r, ErrorCode::E0107,
@@ -708,15 +732,20 @@ SubstitutionRef::get_mappings_from_generic_args (
}
const auto &param_mapping = substitutions.at (offs);
- const auto &type_param = param_mapping.get_generic_param ();
- if (type_param.from_impl_trait ())
+ const auto &generic = param_mapping.get_generic_param ();
+ if (generic.get_kind () == HIR::GenericParam::GenericKind::TYPE)
{
- rich_location r (line_table, arg->get_locus ());
- r.add_fixit_remove (arg->get_locus ());
- rust_error_at (r, ErrorCode::E0632,
- "cannot provide explicit generic arguments when "
- "%<impl Trait%> is used in argument position");
- return SubstitutionArgumentMappings::error ();
+ const auto &type_param
+ = static_cast<const HIR::TypeParam &> (generic);
+ if (type_param.from_impl_trait ())
+ {
+ rich_location r (line_table, arg->get_locus ());
+ r.add_fixit_remove (arg->get_locus ());
+ rust_error_at (r, ErrorCode::E0632,
+ "cannot provide explicit generic arguments when "
+ "%<impl Trait%> is used in argument position");
+ return SubstitutionArgumentMappings::error ();
+ }
}
SubstitutionArg subst_arg (&param_mapping, resolved);
diff --git a/gcc/rust/typecheck/rust-tyty-subst.h b/gcc/rust/typecheck/rust-tyty-subst.h
index 141db3d..f782424 100644
--- a/gcc/rust/typecheck/rust-tyty-subst.h
+++ b/gcc/rust/typecheck/rust-tyty-subst.h
@@ -24,6 +24,7 @@
#include "rust-hir-full-decls.h"
#include "rust-tyty-bounds.h"
#include "rust-tyty-region.h"
+#include "rust-ast.h"
#include "optional.h"
namespace Rust {
@@ -44,7 +45,7 @@ class SubstitutionArgumentMappings;
class SubstitutionParamMapping
{
public:
- SubstitutionParamMapping (HIR::TypeParam &generic, ParamType *param);
+ SubstitutionParamMapping (HIR::GenericParam &generic, ParamType *param);
SubstitutionParamMapping (const SubstitutionParamMapping &other);
@@ -56,11 +57,12 @@ public:
SubstitutionParamMapping clone () const;
ParamType *get_param_ty ();
-
const ParamType *get_param_ty () const;
- HIR::TypeParam &get_generic_param ();
- const HIR::TypeParam &get_generic_param () const;
+ HIR::GenericParam &get_generic_param ();
+ const HIR::GenericParam &get_generic_param () const;
+
+ Identifier get_type_representation () const;
// this is used for the backend to override the HirId ref of the param to
// what the concrete type is for the rest of the context
@@ -77,7 +79,7 @@ public:
bool need_substitution () const;
private:
- HIR::TypeParam &generic;
+ HIR::GenericParam &generic;
ParamType *param;
};
diff --git a/gcc/rust/typecheck/rust-tyty-variance-analysis.cc b/gcc/rust/typecheck/rust-tyty-variance-analysis.cc
index 38f9d52..7971ccf 100644
--- a/gcc/rust/typecheck/rust-tyty-variance-analysis.cc
+++ b/gcc/rust/typecheck/rust-tyty-variance-analysis.cc
@@ -199,9 +199,7 @@ GenericTyPerCrateCtx::debug_print_solutions ()
{
if (i > solution_index)
result += ", ";
- result += param.get_generic_param ()
- .get_type_representation ()
- .as_string ();
+ result += param.get_type_representation ().as_string ();
result += "=";
result += solutions[i].as_string ();
i++;
@@ -239,8 +237,7 @@ GenericTyVisitorCtx::process_type (ADTType &ty)
first_type = first_lifetime + ty.get_used_arguments ().get_regions ().size ();
for (auto &param : ty.get_substs ())
- param_names.push_back (
- param.get_generic_param ().get_type_representation ().as_string ());
+ param_names.push_back (param.get_type_representation ().as_string ());
for (const auto &variant : ty.get_variants ())
{