Skip to content

Commit 8929e40

Browse files
committed
refactoring
1 parent 5ce26e3 commit 8929e40

File tree

6 files changed

+35
-34
lines changed

6 files changed

+35
-34
lines changed

crates/hir-ty/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use hir_expand::name;
5252
use la_arena::{Arena, Idx};
5353
use mir::{MirEvalError, VTableMap};
5454
use rustc_hash::FxHashSet;
55-
use syntax::AstNode;
55+
use syntax::ast::{make, ConstArg};
5656
use traits::FnTrait;
5757
use triomphe::Arc;
5858
use utils::Generics;
@@ -722,15 +722,15 @@ where
722722
collector.placeholders.into_iter().collect()
723723
}
724724

725-
pub fn known_const_to_string(konst: &Const, db: &dyn HirDatabase) -> Option<String> {
725+
pub fn known_const_to_ast(konst: &Const, db: &dyn HirDatabase) -> Option<ConstArg> {
726726
if let ConstValue::Concrete(c) = &konst.interned().value {
727727
match c.interned {
728728
ConstScalar::UnevaluatedConst(GeneralConstId::InTypeConstId(cid), _) => {
729-
return Some(cid.source(db.upcast()).syntax().to_string());
729+
return Some(cid.source(db.upcast()));
730730
}
731731
ConstScalar::Unknown => return None,
732732
_ => (),
733733
}
734734
}
735-
Some(konst.display(db).to_string())
735+
Some(make::expr_const_value(konst.display(db).to_string().as_str()))
736736
}

crates/hir-ty/src/lower.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,21 +1606,25 @@ pub(crate) fn generic_defaults_query(
16061606
// Type variable default referring to parameter coming
16071607
// after it is forbidden (FIXME: report diagnostic)
16081608
ty = fallback_bound_vars(ty, idx, parent_start_idx);
1609-
return crate::make_binders(db, &generic_params, ty.cast(Interner));
1609+
crate::make_binders(db, &generic_params, ty.cast(Interner))
16101610
}
16111611
TypeOrConstParamData::ConstParamData(p) => {
1612-
let unknown = unknown_const_as_generic(
1613-
db.const_param_ty(ConstParamId::from_unchecked(id)),
1612+
let mut val = p.default.as_ref().map_or_else(
1613+
|| {
1614+
unknown_const_as_generic(
1615+
db.const_param_ty(ConstParamId::from_unchecked(id)),
1616+
)
1617+
},
1618+
|c| {
1619+
let c = ctx.lower_const(c, ctx.lower_ty(&p.ty));
1620+
c.cast(Interner)
1621+
},
16141622
);
1615-
let mut val = p.default.as_ref().map_or(unknown, |c| {
1616-
let c = ctx.lower_const(c, ctx.lower_ty(&p.ty));
1617-
chalk_ir::GenericArg::new(Interner, GenericArgData::Const(c))
1618-
});
16191623
// Each default can only refer to previous parameters, see above.
16201624
val = fallback_bound_vars(val, idx, parent_start_idx);
1621-
return make_binders(db, &generic_params, val);
1625+
make_binders(db, &generic_params, val)
16221626
}
1623-
};
1627+
}
16241628
})
16251629
// FIXME: use `Arc::from_iter` when it becomes available
16261630
.collect::<Vec<_>>(),

crates/hir/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use hir_ty::{
6262
all_super_traits, autoderef,
6363
consteval::{try_const_usize, unknown_const_as_generic, ConstEvalError, ConstExt},
6464
diagnostics::BodyValidationDiagnostic,
65-
known_const_to_string,
65+
known_const_to_ast,
6666
layout::{Layout as TyLayout, RustcEnumVariantIdx, TagEncoding},
6767
method_resolution::{self, TyFingerprint},
6868
mir::{self, interpret_mir},
@@ -3160,9 +3160,9 @@ impl ConstParam {
31603160
Type::new(db, self.id.parent(), db.const_param_ty(self.id))
31613161
}
31623162

3163-
pub fn default(self, db: &dyn HirDatabase) -> Option<String> {
3163+
pub fn default(self, db: &dyn HirDatabase) -> Option<ast::ConstArg> {
31643164
let arg = generic_arg_from_param(db, self.id.into())?;
3165-
known_const_to_string(arg.constant(Interner)?, db)
3165+
known_const_to_ast(arg.constant(Interner)?, db)
31663166
}
31673167
}
31683168

crates/ide-db/src/imports/import_assets.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use hir::{
66
use itertools::Itertools;
77
use rustc_hash::FxHashSet;
88
use syntax::{
9-
ast::{self, HasName},
9+
ast::{self, make, HasName},
1010
utils::path_to_string_stripping_turbo_fish,
1111
AstNode, SyntaxNode,
1212
};
@@ -607,7 +607,7 @@ impl ImportCandidate {
607607
fn for_name(sema: &Semantics<'_, RootDatabase>, name: &ast::Name) -> Option<Self> {
608608
if sema
609609
.scope(name.syntax())?
610-
.speculative_resolve(&ast::make::ext::ident_path(&name.text()))
610+
.speculative_resolve(&make::ext::ident_path(&name.text()))
611611
.is_some()
612612
{
613613
return None;

crates/ide-db/src/path_transform.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use either::Either;
55
use hir::{AsAssocItem, HirDisplay, SemanticsScope};
66
use rustc_hash::FxHashMap;
77
use syntax::{
8-
ast::{self, AstNode},
8+
ast::{self, make, AstNode},
99
ted, SyntaxNode,
1010
};
1111

@@ -139,7 +139,7 @@ impl<'a> PathTransform<'a> {
139139
if let Some(default) =
140140
&default.display_source_code(db, source_module.into(), false).ok()
141141
{
142-
type_substs.insert(k, ast::make::ty(default).clone_for_update());
142+
type_substs.insert(k, make::ty(default).clone_for_update());
143143
defaulted_params.push(Either::Left(k));
144144
}
145145
}
@@ -162,7 +162,7 @@ impl<'a> PathTransform<'a> {
162162
}
163163
(Either::Left(k), None) => {
164164
if let Some(default) = k.default(db) {
165-
if let Some(default) = ast::make::expr_const_value(&default).expr() {
165+
if let Some(default) = default.expr() {
166166
const_substs.insert(k, default.syntax().clone_for_update());
167167
defaulted_params.push(Either::Right(k));
168168
}
@@ -278,15 +278,14 @@ impl Ctx<'_> {
278278
hir::ModuleDef::Trait(trait_ref),
279279
false,
280280
)?;
281-
match ast::make::ty_path(mod_path_to_ast(&found_path)) {
281+
match make::ty_path(mod_path_to_ast(&found_path)) {
282282
ast::Type::PathType(path_ty) => Some(path_ty),
283283
_ => None,
284284
}
285285
});
286286

287-
let segment = ast::make::path_segment_ty(subst.clone(), trait_ref);
288-
let qualified =
289-
ast::make::path_from_segments(std::iter::once(segment), false);
287+
let segment = make::path_segment_ty(subst.clone(), trait_ref);
288+
let qualified = make::path_from_segments(std::iter::once(segment), false);
290289
ted::replace(path.syntax(), qualified.clone_for_update().syntax());
291290
} else if let Some(path_ty) = ast::PathType::cast(parent) {
292291
ted::replace(

crates/ide-db/src/use_trivial_constructor.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
//! Functionality for generating trivial constructors
22
33
use hir::StructKind;
4-
use syntax::ast;
4+
use syntax::ast::{make, Expr, Path};
55

66
/// given a type return the trivial constructor (if one exists)
77
pub fn use_trivial_constructor(
88
db: &crate::RootDatabase,
9-
path: ast::Path,
9+
path: Path,
1010
ty: &hir::Type,
11-
) -> Option<ast::Expr> {
11+
) -> Option<Expr> {
1212
match ty.as_adt() {
1313
Some(hir::Adt::Enum(x)) => {
1414
if let &[variant] = &*x.variants(db) {
1515
if variant.kind(db) == hir::StructKind::Unit {
16-
let path = ast::make::path_qualified(
16+
let path = make::path_qualified(
1717
path,
18-
syntax::ast::make::path_segment(ast::make::name_ref(
19-
&variant.name(db).to_smol_str(),
20-
)),
18+
make::path_segment(make::name_ref(&variant.name(db).to_smol_str())),
2119
);
2220

23-
return Some(syntax::ast::make::expr_path(path));
21+
return Some(make::expr_path(path));
2422
}
2523
}
2624
}
2725
Some(hir::Adt::Struct(x)) if x.kind(db) == StructKind::Unit => {
28-
return Some(syntax::ast::make::expr_path(path));
26+
return Some(make::expr_path(path));
2927
}
3028
_ => {}
3129
}

0 commit comments

Comments
 (0)