@@ -808,32 +808,60 @@ SubstitutionRef::get_mappings_from_generic_args (
808
808
auto specified_type = const_param->get_ty ();
809
809
810
810
// validate this const generic is of the correct type
811
- auto coereced_type
812
- = Resolver::coercion_site (expr.get_mappings ().get_hirid (),
813
- TyTy::TyWithLocation (specified_type),
814
- TyTy::TyWithLocation (expr_type,
815
- expr.get_locus ()),
816
- arg.get_locus ());
817
- if (coereced_type->is <ErrorType> ())
818
- return SubstitutionArgumentMappings::error ();
819
-
820
- // const fold it
821
- auto ctx = Compile::Context::get ();
822
- tree folded
823
- = Compile::HIRCompileBase::query_compile_const_expr (ctx, coereced_type,
824
- expr);
811
+ TyTy::BaseType *coereced_type = nullptr ;
812
+ if (expr_type->is <ConstType> ())
813
+ {
814
+ TyTy::ConstType *const_expr_type
815
+ = static_cast <TyTy::ConstType *> (expr_type);
816
+ TyTy::BaseType *const_value_type = const_expr_type->get_ty ();
817
+ coereced_type
818
+ = Resolver::coercion_site (expr.get_mappings ().get_hirid (),
819
+ TyTy::TyWithLocation (specified_type),
820
+ TyTy::TyWithLocation (const_value_type,
821
+ expr.get_locus ()),
822
+ arg.get_locus ());
823
+ }
824
+ else
825
+ {
826
+ coereced_type
827
+ = Resolver::coercion_site (expr.get_mappings ().get_hirid (),
828
+ TyTy::TyWithLocation (specified_type),
829
+ TyTy::TyWithLocation (expr_type,
830
+ expr.get_locus ()),
831
+ arg.get_locus ());
832
+ }
825
833
826
- if (folded == error_mark_node )
834
+ if (coereced_type == nullptr || coereced_type-> is <ErrorType> () )
827
835
return SubstitutionArgumentMappings::error ();
828
836
829
- // create const type
830
- auto const_value
831
- = new TyTy::ConstType (TyTy::ConstType::ConstKind::Value, " " ,
832
- coereced_type, folded, {}, expr.get_locus (),
833
- expr.get_mappings ().get_hirid (),
834
- expr.get_mappings ().get_hirid (), {});
837
+ TyTy::BaseType *const_value_ty = nullptr ;
838
+ if (expr_type->is <ConstType> ())
839
+ const_value_ty = expr_type;
840
+ else
841
+ {
842
+ // const fold it if available
843
+ auto ctx = Compile::Context::get ();
844
+ tree folded
845
+ = Compile::HIRCompileBase::query_compile_const_expr (ctx,
846
+ coereced_type,
847
+ expr);
848
+
849
+ if (folded == error_mark_node)
850
+ {
851
+ rich_location r (line_table, arg.get_locus ());
852
+ r.add_range (expr.get_locus ());
853
+ rust_error_at (r, " failed to resolve const expression" );
854
+ return SubstitutionArgumentMappings::error ();
855
+ }
856
+
857
+ const_value_ty
858
+ = new TyTy::ConstType (TyTy::ConstType::ConstKind::Value, " " ,
859
+ coereced_type, folded, {}, expr.get_locus (),
860
+ expr.get_mappings ().get_hirid (),
861
+ expr.get_mappings ().get_hirid (), {});
862
+ }
835
863
836
- mappings.emplace_back (¶m_mapping, const_value );
864
+ mappings.emplace_back (¶m_mapping, const_value_ty );
837
865
offs++;
838
866
}
839
867
0 commit comments