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 la_arena::{Arena, Idx};
use mir::{MirEvalError, VTableMap}; use mir::{MirEvalError, VTableMap};
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use syntax::AstNode; use syntax::ast::{make, ConstArg};
use traits::FnTrait; use traits::FnTrait;
use triomphe::Arc; use triomphe::Arc;
use utils::Generics; use utils::Generics;
@ -722,15 +722,15 @@ where
collector.placeholders.into_iter().collect() 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 { if let ConstValue::Concrete(c) = &konst.interned().value {
match c.interned { match c.interned {
ConstScalar::UnevaluatedConst(GeneralConstId::InTypeConstId(cid), _) => { ConstScalar::UnevaluatedConst(GeneralConstId::InTypeConstId(cid), _) => {
return Some(cid.source(db.upcast()).syntax().to_string()); return Some(cid.source(db.upcast()));
} }
ConstScalar::Unknown => return None, 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 // Type variable default referring to parameter coming
// after it is forbidden (FIXME: report diagnostic) // after it is forbidden (FIXME: report diagnostic)
ty = fallback_bound_vars(ty, idx, parent_start_idx); 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) => { TypeOrConstParamData::ConstParamData(p) => {
let unknown = unknown_const_as_generic( let mut val = p.default.as_ref().map_or_else(
db.const_param_ty(ConstParamId::from_unchecked(id)), || {
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. // Each default can only refer to previous parameters, see above.
val = fallback_bound_vars(val, idx, parent_start_idx); 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 // FIXME: use `Arc::from_iter` when it becomes available
.collect::<Vec<_>>(), .collect::<Vec<_>>(),

View file

@ -63,7 +63,7 @@ use hir_ty::{
all_super_traits, autoderef, all_super_traits, autoderef,
consteval::{try_const_usize, unknown_const_as_generic, ConstEvalError, ConstExt}, consteval::{try_const_usize, unknown_const_as_generic, ConstEvalError, ConstExt},
diagnostics::BodyValidationDiagnostic, diagnostics::BodyValidationDiagnostic,
known_const_to_string, known_const_to_ast,
layout::{Layout as TyLayout, RustcEnumVariantIdx, TagEncoding}, layout::{Layout as TyLayout, RustcEnumVariantIdx, TagEncoding},
method_resolution::{self, TyFingerprint}, method_resolution::{self, TyFingerprint},
mir::{self, interpret_mir}, mir::{self, interpret_mir},
@ -3207,9 +3207,9 @@ impl ConstParam {
Type::new(db, self.id.parent(), db.const_param_ty(self.id)) 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())?; 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 itertools::Itertools;
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use syntax::{ use syntax::{
ast::{self, HasName}, ast::{self, make, HasName},
utils::path_to_string_stripping_turbo_fish, utils::path_to_string_stripping_turbo_fish,
AstNode, SyntaxNode, AstNode, SyntaxNode,
}; };
@ -607,7 +607,7 @@ impl ImportCandidate {
fn for_name(sema: &Semantics<'_, RootDatabase>, name: &ast::Name) -> Option<Self> { fn for_name(sema: &Semantics<'_, RootDatabase>, name: &ast::Name) -> Option<Self> {
if sema if sema
.scope(name.syntax())? .scope(name.syntax())?
.speculative_resolve(&ast::make::ext::ident_path(&name.text())) .speculative_resolve(&make::ext::ident_path(&name.text()))
.is_some() .is_some()
{ {
return None; return None;

View file

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

View file

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