@@ -63,12 +63,13 @@ use hir_ty::{
63
63
all_super_traits, autoderef,
64
64
consteval:: { try_const_usize, unknown_const_as_generic, ConstEvalError , ConstExt } ,
65
65
diagnostics:: BodyValidationDiagnostic ,
66
+ known_const_to_ast,
66
67
layout:: { Layout as TyLayout , RustcEnumVariantIdx , TagEncoding } ,
67
68
method_resolution:: { self , TyFingerprint } ,
68
69
mir:: { self , interpret_mir} ,
69
70
primitive:: UintTy ,
70
71
traits:: FnTrait ,
71
- AliasTy , CallableDefId , CallableSig , Canonical , CanonicalVarKinds , Cast , ClosureId ,
72
+ AliasTy , CallableDefId , CallableSig , Canonical , CanonicalVarKinds , Cast , ClosureId , GenericArg ,
72
73
GenericArgData , Interner , ParamKind , QuantifiedWhereClause , Scalar , Substitution ,
73
74
TraitEnvironment , TraitRefExt , Ty , TyBuilder , TyDefId , TyExt , TyKind , ValueTyDefId ,
74
75
WhereClause ,
@@ -3128,12 +3129,8 @@ impl TypeParam {
3128
3129
}
3129
3130
3130
3131
pub fn default ( self , db : & dyn HirDatabase ) -> Option < Type > {
3131
- let params = db. generic_defaults ( self . id . parent ( ) ) ;
3132
- let local_idx = hir_ty:: param_idx ( db, self . id . into ( ) ) ?;
3132
+ let ty = generic_arg_from_param ( db, self . id . into ( ) ) ?;
3133
3133
let resolver = self . id . parent ( ) . resolver ( db. upcast ( ) ) ;
3134
- let ty = params. get ( local_idx) ?. clone ( ) ;
3135
- let subst = TyBuilder :: placeholder_subst ( db, self . id . parent ( ) ) ;
3136
- let ty = ty. substitute ( Interner , & subst) ;
3137
3134
match ty. data ( Interner ) {
3138
3135
GenericArgData :: Ty ( it) => {
3139
3136
Some ( Type :: new_with_resolver_inner ( db, & resolver, it. clone ( ) ) )
@@ -3195,6 +3192,19 @@ impl ConstParam {
3195
3192
pub fn ty ( self , db : & dyn HirDatabase ) -> Type {
3196
3193
Type :: new ( db, self . id . parent ( ) , db. const_param_ty ( self . id ) )
3197
3194
}
3195
+
3196
+ pub fn default ( self , db : & dyn HirDatabase ) -> Option < ast:: ConstArg > {
3197
+ let arg = generic_arg_from_param ( db, self . id . into ( ) ) ?;
3198
+ known_const_to_ast ( arg. constant ( Interner ) ?, db)
3199
+ }
3200
+ }
3201
+
3202
+ fn generic_arg_from_param ( db : & dyn HirDatabase , id : TypeOrConstParamId ) -> Option < GenericArg > {
3203
+ let params = db. generic_defaults ( id. parent ) ;
3204
+ let local_idx = hir_ty:: param_idx ( db, id) ?;
3205
+ let ty = params. get ( local_idx) ?. clone ( ) ;
3206
+ let subst = TyBuilder :: placeholder_subst ( db, id. parent ) ;
3207
+ Some ( ty. substitute ( Interner , & subst) )
3198
3208
}
3199
3209
3200
3210
#[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
0 commit comments