refactoring

This commit is contained in:
ponyii 2023-08-08 21:51:59 +04:00
parent 61cabe029f
commit e4c45427dc
6 changed files with 35 additions and 34 deletions

View File

@ -52,7 +52,7 @@ use hir_expand::name;
use la_arena::{Arena, Idx};
use mir::{MirEvalError, VTableMap};
use rustc_hash::FxHashSet;
use syntax::AstNode;
use syntax::ast::{make, ConstArg};
use traits::FnTrait;
use triomphe::Arc;
use utils::Generics;
@ -722,15 +722,15 @@ where
collector.placeholders.into_iter().collect()
}
pub fn known_const_to_string(konst: &Const, db: &dyn HirDatabase) -> Option<String> {
pub fn known_const_to_ast(konst: &Const, db: &dyn HirDatabase) -> Option<ConstArg> {
if let ConstValue::Concrete(c) = &konst.interned().value {
match c.interned {
ConstScalar::UnevaluatedConst(GeneralConstId::InTypeConstId(cid), _) => {
return Some(cid.source(db.upcast()).syntax().to_string());
return Some(cid.source(db.upcast()));
}
ConstScalar::Unknown => return None,
_ => (),
}
}
Some(konst.display(db).to_string())
Some(make::expr_const_value(konst.display(db).to_string().as_str()))
}

View File

@ -1606,21 +1606,25 @@ pub(crate) fn generic_defaults_query(
// Type variable default referring to parameter coming
// after it is forbidden (FIXME: report diagnostic)
ty = fallback_bound_vars(ty, idx, parent_start_idx);
return crate::make_binders(db, &generic_params, ty.cast(Interner));
crate::make_binders(db, &generic_params, ty.cast(Interner))
}
TypeOrConstParamData::ConstParamData(p) => {
let unknown = unknown_const_as_generic(
db.const_param_ty(ConstParamId::from_unchecked(id)),
let mut val = p.default.as_ref().map_or_else(
|| {
unknown_const_as_generic(
db.const_param_ty(ConstParamId::from_unchecked(id)),
)
},
|c| {
let c = ctx.lower_const(c, ctx.lower_ty(&p.ty));
c.cast(Interner)
},
);
let mut val = p.default.as_ref().map_or(unknown, |c| {
let c = ctx.lower_const(c, ctx.lower_ty(&p.ty));
chalk_ir::GenericArg::new(Interner, GenericArgData::Const(c))
});
// Each default can only refer to previous parameters, see above.
val = fallback_bound_vars(val, idx, parent_start_idx);
return make_binders(db, &generic_params, val);
make_binders(db, &generic_params, val)
}
};
}
})
// FIXME: use `Arc::from_iter` when it becomes available
.collect::<Vec<_>>(),

View File

@ -63,7 +63,7 @@ use hir_ty::{
all_super_traits, autoderef,
consteval::{try_const_usize, unknown_const_as_generic, ConstEvalError, ConstExt},
diagnostics::BodyValidationDiagnostic,
known_const_to_string,
known_const_to_ast,
layout::{Layout as TyLayout, RustcEnumVariantIdx, TagEncoding},
method_resolution::{self, TyFingerprint},
mir::{self, interpret_mir},
@ -3207,9 +3207,9 @@ impl ConstParam {
Type::new(db, self.id.parent(), db.const_param_ty(self.id))
}
pub fn default(self, db: &dyn HirDatabase) -> Option<String> {
pub fn default(self, db: &dyn HirDatabase) -> Option<ast::ConstArg> {
let arg = generic_arg_from_param(db, self.id.into())?;
known_const_to_string(arg.constant(Interner)?, db)
known_const_to_ast(arg.constant(Interner)?, db)
}
}

View File

@ -6,7 +6,7 @@ use hir::{
use itertools::Itertools;
use rustc_hash::FxHashSet;
use syntax::{
ast::{self, HasName},
ast::{self, make, HasName},
utils::path_to_string_stripping_turbo_fish,
AstNode, SyntaxNode,
};
@ -607,7 +607,7 @@ impl ImportCandidate {
fn for_name(sema: &Semantics<'_, RootDatabase>, name: &ast::Name) -> Option<Self> {
if sema
.scope(name.syntax())?
.speculative_resolve(&ast::make::ext::ident_path(&name.text()))
.speculative_resolve(&make::ext::ident_path(&name.text()))
.is_some()
{
return None;

View File

@ -5,7 +5,7 @@ use either::Either;
use hir::{AsAssocItem, HirDisplay, SemanticsScope};
use rustc_hash::FxHashMap;
use syntax::{
ast::{self, AstNode},
ast::{self, make, AstNode},
ted, SyntaxNode,
};
@ -139,7 +139,7 @@ impl<'a> PathTransform<'a> {
if let Some(default) =
&default.display_source_code(db, source_module.into(), false).ok()
{
type_substs.insert(k, ast::make::ty(default).clone_for_update());
type_substs.insert(k, make::ty(default).clone_for_update());
defaulted_params.push(Either::Left(k));
}
}
@ -162,7 +162,7 @@ impl<'a> PathTransform<'a> {
}
(Either::Left(k), None) => {
if let Some(default) = k.default(db) {
if let Some(default) = ast::make::expr_const_value(&default).expr() {
if let Some(default) = default.expr() {
const_substs.insert(k, default.syntax().clone_for_update());
defaulted_params.push(Either::Right(k));
}
@ -278,15 +278,14 @@ impl Ctx<'_> {
hir::ModuleDef::Trait(trait_ref),
false,
)?;
match ast::make::ty_path(mod_path_to_ast(&found_path)) {
match make::ty_path(mod_path_to_ast(&found_path)) {
ast::Type::PathType(path_ty) => Some(path_ty),
_ => None,
}
});
let segment = ast::make::path_segment_ty(subst.clone(), trait_ref);
let qualified =
ast::make::path_from_segments(std::iter::once(segment), false);
let segment = make::path_segment_ty(subst.clone(), trait_ref);
let qualified = make::path_from_segments(std::iter::once(segment), false);
ted::replace(path.syntax(), qualified.clone_for_update().syntax());
} else if let Some(path_ty) = ast::PathType::cast(parent) {
ted::replace(

View File

@ -1,31 +1,29 @@
//! Functionality for generating trivial constructors
use hir::StructKind;
use syntax::ast;
use syntax::ast::{make, Expr, Path};
/// given a type return the trivial constructor (if one exists)
pub fn use_trivial_constructor(
db: &crate::RootDatabase,
path: ast::Path,
path: Path,
ty: &hir::Type,
) -> Option<ast::Expr> {
) -> Option<Expr> {
match ty.as_adt() {
Some(hir::Adt::Enum(x)) => {
if let &[variant] = &*x.variants(db) {
if variant.kind(db) == hir::StructKind::Unit {
let path = ast::make::path_qualified(
let path = make::path_qualified(
path,
syntax::ast::make::path_segment(ast::make::name_ref(
&variant.name(db).to_smol_str(),
)),
make::path_segment(make::name_ref(&variant.name(db).to_smol_str())),
);
return Some(syntax::ast::make::expr_path(path));
return Some(make::expr_path(path));
}
}
}
Some(hir::Adt::Struct(x)) if x.kind(db) == StructKind::Unit => {
return Some(syntax::ast::make::expr_path(path));
return Some(make::expr_path(path));
}
_ => {}
}