@@ -63,12 +63,13 @@ use hir_ty::{
6363 all_super_traits, autoderef,
6464 consteval:: { try_const_usize, unknown_const_as_generic, ConstEvalError , ConstExt } ,
6565 diagnostics:: BodyValidationDiagnostic ,
66+ known_const_to_ast,
6667 layout:: { Layout as TyLayout , RustcEnumVariantIdx , TagEncoding } ,
6768 method_resolution:: { self , TyFingerprint } ,
6869 mir:: { self , interpret_mir} ,
6970 primitive:: UintTy ,
7071 traits:: FnTrait ,
71- AliasTy , CallableDefId , CallableSig , Canonical , CanonicalVarKinds , Cast , ClosureId ,
72+ AliasTy , CallableDefId , CallableSig , Canonical , CanonicalVarKinds , Cast , ClosureId , GenericArg ,
7273 GenericArgData , Interner , ParamKind , QuantifiedWhereClause , Scalar , Substitution ,
7374 TraitEnvironment , TraitRefExt , Ty , TyBuilder , TyDefId , TyExt , TyKind , ValueTyDefId ,
7475 WhereClause ,
@@ -3128,12 +3129,8 @@ impl TypeParam {
31283129 }
31293130
31303131 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 ( ) ) ?;
31333133 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) ;
31373134 match ty. data ( Interner ) {
31383135 GenericArgData :: Ty ( it) => {
31393136 Some ( Type :: new_with_resolver_inner ( db, & resolver, it. clone ( ) ) )
@@ -3195,6 +3192,19 @@ impl ConstParam {
31953192 pub fn ty ( self , db : & dyn HirDatabase ) -> Type {
31963193 Type :: new ( db, self . id . parent ( ) , db. const_param_ty ( self . id ) )
31973194 }
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) )
31983208}
31993209
32003210#[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
0 commit comments