]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: track generic const generics properly
authorPhilip Herron <herron.philip@googlemail.com>
Sun, 3 Aug 2025 18:30:18 +0000 (19:30 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 30 Oct 2025 19:59:02 +0000 (20:59 +0100)
gcc/rust/ChangeLog:

* typecheck/rust-tyty-subst.cc: const generic arguments dont have a value yet

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/typecheck/rust-tyty-subst.cc

index c4582f68e431385cec5969f8effa6d3660660f3d..4699b71197a4e31a9aa34dd124c2d0f3ade8c6aa 100644 (file)
@@ -808,32 +808,60 @@ SubstitutionRef::get_mappings_from_generic_args (
       auto specified_type = const_param->get_ty ();
 
       // validate this const generic is of the correct type
-      auto coereced_type
-       = Resolver::coercion_site (expr.get_mappings ().get_hirid (),
-                                  TyTy::TyWithLocation (specified_type),
-                                  TyTy::TyWithLocation (expr_type,
-                                                        expr.get_locus ()),
-                                  arg.get_locus ());
-      if (coereced_type->is<ErrorType> ())
-       return SubstitutionArgumentMappings::error ();
-
-      // const fold it
-      auto ctx = Compile::Context::get ();
-      tree folded
-       = Compile::HIRCompileBase::query_compile_const_expr (ctx, coereced_type,
-                                                            expr);
+      TyTy::BaseType *coereced_type = nullptr;
+      if (expr_type->is<ConstType> ())
+       {
+         TyTy::ConstType *const_expr_type
+           = static_cast<TyTy::ConstType *> (expr_type);
+         TyTy::BaseType *const_value_type = const_expr_type->get_ty ();
+         coereced_type
+           = Resolver::coercion_site (expr.get_mappings ().get_hirid (),
+                                      TyTy::TyWithLocation (specified_type),
+                                      TyTy::TyWithLocation (const_value_type,
+                                                            expr.get_locus ()),
+                                      arg.get_locus ());
+       }
+      else
+       {
+         coereced_type
+           = Resolver::coercion_site (expr.get_mappings ().get_hirid (),
+                                      TyTy::TyWithLocation (specified_type),
+                                      TyTy::TyWithLocation (expr_type,
+                                                            expr.get_locus ()),
+                                      arg.get_locus ());
+       }
 
-      if (folded == error_mark_node)
+      if (coereced_type == nullptr || coereced_type->is<ErrorType> ())
        return SubstitutionArgumentMappings::error ();
 
-      // create const type
-      auto const_value
-       = new TyTy::ConstType (TyTy::ConstType::ConstKind::Value, "",
-                              coereced_type, folded, {}, expr.get_locus (),
-                              expr.get_mappings ().get_hirid (),
-                              expr.get_mappings ().get_hirid (), {});
+      TyTy::BaseType *const_value_ty = nullptr;
+      if (expr_type->is<ConstType> ())
+       const_value_ty = expr_type;
+      else
+       {
+         // const fold it if available
+         auto ctx = Compile::Context::get ();
+         tree folded
+           = Compile::HIRCompileBase::query_compile_const_expr (ctx,
+                                                                coereced_type,
+                                                                expr);
+
+         if (folded == error_mark_node)
+           {
+             rich_location r (line_table, arg.get_locus ());
+             r.add_range (expr.get_locus ());
+             rust_error_at (r, "failed to resolve const expression");
+             return SubstitutionArgumentMappings::error ();
+           }
+
+         const_value_ty
+           = new TyTy::ConstType (TyTy::ConstType::ConstKind::Value, "",
+                                  coereced_type, folded, {}, expr.get_locus (),
+                                  expr.get_mappings ().get_hirid (),
+                                  expr.get_mappings ().get_hirid (), {});
+       }
 
-      mappings.emplace_back (&param_mapping, const_value);
+      mappings.emplace_back (&param_mapping, const_value_ty);
       offs++;
     }