mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
the "add missing members" assists: implemented substitution of default values of const params
This commit is contained in:
parent
6918eb6ebd
commit
52b4392724
8 changed files with 127 additions and 52 deletions
|
@ -62,13 +62,13 @@ use hir_expand::{name::name, MacroCallKind};
|
|||
use hir_ty::{
|
||||
all_super_traits, autoderef,
|
||||
consteval::{try_const_usize, unknown_const_as_generic, ConstEvalError, ConstExt},
|
||||
diagnostics::BodyValidationDiagnostic,
|
||||
diagnostics::BodyValidationDiagnostic, known_const_to_string,
|
||||
layout::{Layout as TyLayout, RustcEnumVariantIdx, TagEncoding},
|
||||
method_resolution::{self, TyFingerprint},
|
||||
mir::{self, interpret_mir},
|
||||
primitive::UintTy,
|
||||
traits::FnTrait,
|
||||
AliasTy, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast, ClosureId,
|
||||
AliasTy, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast, ClosureId, GenericArg,
|
||||
GenericArgData, Interner, ParamKind, QuantifiedWhereClause, Scalar, Substitution,
|
||||
TraitEnvironment, TraitRefExt, Ty, TyBuilder, TyDefId, TyExt, TyKind, ValueTyDefId,
|
||||
WhereClause,
|
||||
|
@ -3142,12 +3142,8 @@ impl TypeParam {
|
|||
}
|
||||
|
||||
pub fn default(self, db: &dyn HirDatabase) -> Option<Type> {
|
||||
let params = db.generic_defaults(self.id.parent());
|
||||
let local_idx = hir_ty::param_idx(db, self.id.into())?;
|
||||
let ty = generic_arg_from_param(db, self.id.into())?;
|
||||
let resolver = self.id.parent().resolver(db.upcast());
|
||||
let ty = params.get(local_idx)?.clone();
|
||||
let subst = TyBuilder::placeholder_subst(db, self.id.parent());
|
||||
let ty = ty.substitute(Interner, &subst);
|
||||
match ty.data(Interner) {
|
||||
GenericArgData::Ty(it) => {
|
||||
Some(Type::new_with_resolver_inner(db, &resolver, it.clone()))
|
||||
|
@ -3209,6 +3205,19 @@ impl ConstParam {
|
|||
pub fn ty(self, db: &dyn HirDatabase) -> Type {
|
||||
Type::new(db, self.id.parent(), db.const_param_ty(self.id))
|
||||
}
|
||||
|
||||
pub fn default(self, db: &dyn HirDatabase) -> Option<String> {
|
||||
let arg = generic_arg_from_param(db, self.id.into())?;
|
||||
known_const_to_string(arg.constant(Interner)?, db)
|
||||
}
|
||||
}
|
||||
|
||||
fn generic_arg_from_param(db: &dyn HirDatabase, id: TypeOrConstParamId) -> Option<GenericArg> {
|
||||
let params = db.generic_defaults(id.parent);
|
||||
let local_idx = hir_ty::param_idx(db, id)?;
|
||||
let ty = params.get(local_idx)?.clone();
|
||||
let subst = TyBuilder::placeholder_subst(db, id.parent);
|
||||
Some(ty.substitute(Interner, &subst))
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue