From: Philip Herron Date: Sun, 3 Aug 2025 18:30:18 +0000 (+0100) Subject: gccrs: track generic const generics properly X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5579e4484a17f200bdaddf9f0529807159f498cc;p=thirdparty%2Fgcc.git gccrs: track generic const generics properly gcc/rust/ChangeLog: * typecheck/rust-tyty-subst.cc: const generic arguments dont have a value yet Signed-off-by: Philip Herron --- diff --git a/gcc/rust/typecheck/rust-tyty-subst.cc b/gcc/rust/typecheck/rust-tyty-subst.cc index c4582f68e43..4699b71197a 100644 --- a/gcc/rust/typecheck/rust-tyty-subst.cc +++ b/gcc/rust/typecheck/rust-tyty-subst.cc @@ -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 ()) - 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 ()) + { + TyTy::ConstType *const_expr_type + = static_cast (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 ()) 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 ()) + 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 (¶m_mapping, const_value); + mappings.emplace_back (¶m_mapping, const_value_ty); offs++; }