Skip to content

Commit 7b9ad94

Browse files
committed
Make Field::ty return TypeNs
1 parent 839645b commit 7b9ad94

File tree

11 files changed

+51
-31
lines changed

11 files changed

+51
-31
lines changed

crates/hir-ty/src/next_solver/mapping.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ pub fn convert_args_for_result<'db>(
12661266
Substitution::from_iter(Interner, substs)
12671267
}
12681268

1269-
pub(crate) fn convert_ty_for_result<'db>(interner: DbInterner<'db>, ty: Ty<'db>) -> crate::Ty {
1269+
pub fn convert_ty_for_result<'db>(interner: DbInterner<'db>, ty: Ty<'db>) -> crate::Ty {
12701270
use crate::{Scalar, TyKind};
12711271
use chalk_ir::{FloatTy, IntTy, UintTy};
12721272
match ty.kind() {

crates/hir/src/display.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::{
2424
Adt, AsAssocItem, AssocItem, AssocItemContainer, Const, ConstParam, Crate, Enum,
2525
ExternCrateDecl, Field, Function, GenericParam, HasCrate, HasVisibility, Impl, LifetimeParam,
2626
Macro, Module, SelfParam, Static, Struct, StructKind, Trait, TraitRef, TupleField, TyBuilder,
27-
Type, TypeAlias, TypeOrConstParam, TypeParam, Union, Variant,
27+
Type, TypeAlias, TypeNs, TypeOrConstParam, TypeParam, Union, Variant,
2828
};
2929

3030
impl HirDisplay for Function {
@@ -437,6 +437,12 @@ impl HirDisplay for Type<'_> {
437437
}
438438
}
439439

440+
impl HirDisplay for TypeNs<'_> {
441+
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
442+
self.ty.hir_fmt(f)
443+
}
444+
}
445+
440446
impl HirDisplay for ExternCrateDecl {
441447
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
442448
write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;

crates/hir/src/lib.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ use hir_ty::{
8686
method_resolution,
8787
mir::{MutBorrowKind, interpret_mir},
8888
next_solver::{
89-
ClauseKind, DbInterner, GenericArgs, infer::InferCtxt, mapping::ChalkToNextSolver,
89+
ClauseKind, DbInterner, GenericArgs,
90+
infer::InferCtxt,
91+
mapping::{ChalkToNextSolver, convert_ty_for_result},
9092
},
9193
primitive::UintTy,
9294
traits::FnTrait,
@@ -1346,19 +1348,12 @@ impl Field {
13461348
u32::from(self.id.into_raw()) as usize
13471349
}
13481350

1349-
/// Returns the type as in the signature of the struct (i.e., with
1350-
/// placeholder types for type parameters). Only use this in the context of
1351-
/// the field definition.
1352-
pub fn ty<'db>(&self, db: &'db dyn HirDatabase) -> Type<'db> {
1351+
/// Returns the type as in the signature of the struct. Only use this in the
1352+
/// context of the field definition.
1353+
pub fn ty<'db>(&self, db: &'db dyn HirDatabase) -> TypeNs<'db> {
13531354
let var_id = self.parent.into();
1354-
let generic_def_id: GenericDefId = match self.parent {
1355-
VariantDef::Struct(it) => it.id.into(),
1356-
VariantDef::Union(it) => it.id.into(),
1357-
VariantDef::Variant(it) => it.id.lookup(db).parent.into(),
1358-
};
1359-
let substs = TyBuilder::placeholder_subst(db, generic_def_id);
1360-
let ty = db.field_types(var_id)[self.id].clone().substitute(Interner, &substs);
1361-
Type::new(db, var_id, ty)
1355+
let ty = db.field_types_ns(var_id)[self.id].skip_binder();
1356+
TypeNs::new(db, var_id, ty)
13621357
}
13631358

13641359
// FIXME: Find better API to also handle const generics
@@ -1388,9 +1383,8 @@ impl Field {
13881383
}
13891384

13901385
pub fn layout(&self, db: &dyn HirDatabase) -> Result<Layout, LayoutError> {
1391-
let interner = DbInterner::new_with(db, None, None);
13921386
db.layout_of_ty(
1393-
self.ty(db).ty.to_nextsolver(interner),
1387+
self.ty(db).ty,
13941388
db.trait_environment(match hir_def::VariantId::from(self.parent) {
13951389
hir_def::VariantId::EnumVariantId(id) => {
13961390
GenericDefId::AdtId(id.lookup(db).parent.into())
@@ -5969,6 +5963,11 @@ impl<'db> TypeNs<'db> {
59695963
TypeNs { env: environment, ty, _pd: PhantomCovariantLifetime::new() }
59705964
}
59715965

5966+
pub fn to_type(&self, db: &'db dyn HirDatabase) -> Type<'db> {
5967+
let interner = DbInterner::new_with(db, Some(self.env.krate), self.env.block);
5968+
Type { env: self.env.clone(), ty: convert_ty_for_result(interner, self.ty), _pd: self._pd }
5969+
}
5970+
59725971
// FIXME: Find better API that also handles const generics
59735972
pub fn impls_trait(&self, infcx: InferCtxt<'db>, trait_: Trait, args: &[TypeNs<'db>]) -> bool {
59745973
let args = GenericArgs::new_from_iter(
@@ -5992,6 +5991,10 @@ impl<'db> TypeNs<'db> {
59925991
let res = hir_ty::traits::next_trait_solve_in_ctxt(&infcx, goal);
59935992
res.map_or(false, |res| matches!(res.1, rustc_type_ir::solve::Certainty::Yes))
59945993
}
5994+
5995+
pub fn is_bool(&self) -> bool {
5996+
matches!(self.ty.kind(), rustc_type_ir::TyKind::Bool)
5997+
}
59955998
}
59965999

59976000
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]

crates/ide-assists/src/handlers/add_missing_match_arms.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ fn build_pat(
521521
hir::StructKind::Tuple => {
522522
let mut name_generator = suggest_name::NameGenerator::default();
523523
let pats = fields.into_iter().map(|f| {
524-
let name = name_generator.for_type(&f.ty(db), db, edition);
524+
let name = name_generator.for_type(&f.ty(db).to_type(db), db, edition);
525525
match name {
526526
Some(name) => make::ext::simple_ident_pat(make.name(&name)).into(),
527527
None => make.wildcard_pat().into(),

crates/ide-assists/src/handlers/expand_rest_pattern.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,11 @@ fn expand_tuple_struct_rest_pattern(
145145
make.ident_pat(
146146
false,
147147
false,
148-
match name_gen.for_type(&f.ty(ctx.sema.db), ctx.sema.db, ctx.edition())
149-
{
148+
match name_gen.for_type(
149+
&f.ty(ctx.sema.db).to_type(ctx.sema.db),
150+
ctx.sema.db,
151+
ctx.edition(),
152+
) {
150153
Some(name) => make.name(&name),
151154
None => make.name(&format!("_{}", f.index())),
152155
},

crates/ide-completion/src/completions/record.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ pub(crate) fn complete_record_pattern_fields(
2828
record_pat.record_pat_field_list().and_then(|fl| fl.fields().next()).is_some();
2929

3030
match were_fields_specified {
31-
false => un.fields(ctx.db).into_iter().map(|f| (f, f.ty(ctx.db))).collect(),
31+
false => un
32+
.fields(ctx.db)
33+
.into_iter()
34+
.map(|f| (f, f.ty(ctx.db).to_type(ctx.db)))
35+
.collect(),
3236
true => return,
3337
}
3438
}
@@ -56,7 +60,11 @@ pub(crate) fn complete_record_expr_fields(
5660
record_expr.record_expr_field_list().and_then(|fl| fl.fields().next()).is_some();
5761

5862
match were_fields_specified {
59-
false => un.fields(ctx.db).into_iter().map(|f| (f, f.ty(ctx.db))).collect(),
63+
false => un
64+
.fields(ctx.db)
65+
.into_iter()
66+
.map(|f| (f, f.ty(ctx.db).to_type(ctx.db)))
67+
.collect(),
6068
true => return,
6169
}
6270
}

crates/ide/src/goto_type_definition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub(crate) fn goto_type_definition(
8888
ast::Pat(it) => sema.type_of_pat(&it)?.original,
8989
ast::SelfParam(it) => sema.type_of_self(&it)?,
9090
ast::Type(it) => sema.resolve_type(&it)?,
91-
ast::RecordField(it) => sema.to_def(&it)?.ty(db),
91+
ast::RecordField(it) => sema.to_def(&it)?.ty(db).to_type(db),
9292
// can't match on RecordExprField directly as `ast::Expr` will match an iteration too early otherwise
9393
ast::NameRef(it) => {
9494
if let Some(record_field) = ast::RecordExprField::for_name_ref(&it) {

crates/ide/src/hover.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ pub(crate) fn hover_for_definition(
440440
Definition::Local(it) => Some(it.ty(db)),
441441
Definition::GenericParam(hir::GenericParam::ConstParam(it)) => Some(it.ty(db)),
442442
Definition::GenericParam(hir::GenericParam::TypeParam(it)) => Some(it.ty(db)),
443-
Definition::Field(field) => Some(field.ty(db)),
443+
Definition::Field(field) => Some(field.ty(db).to_type(db)),
444444
Definition::TupleField(it) => Some(it.ty(db)),
445445
Definition::Function(it) => Some(it.ty(db)),
446446
Definition::Adt(it) => Some(it.ty(db)),
@@ -602,7 +602,7 @@ fn goto_type_action_for_def(
602602

603603
let ty = match def {
604604
Definition::Local(it) => Some(it.ty(db)),
605-
Definition::Field(field) => Some(field.ty(db)),
605+
Definition::Field(field) => Some(field.ty(db).to_type(db)),
606606
Definition::TupleField(field) => Some(field.ty(db)),
607607
Definition::Const(it) => Some(it.ty(db)),
608608
Definition::Static(it) => Some(it.ty(db)),

crates/ide/src/hover/render.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -692,14 +692,14 @@ pub(super) fn definition(
692692
}
693693
let drop_info = match def {
694694
Definition::Field(field) => {
695-
DropInfo { drop_glue: field.ty(db).drop_glue(db), has_dtor: None }
695+
DropInfo { drop_glue: field.ty(db).to_type(db).drop_glue(db), has_dtor: None }
696696
}
697697
Definition::Adt(Adt::Struct(strukt)) => {
698698
let struct_drop_glue = strukt.ty_placeholders(db).drop_glue(db);
699699
let mut fields_drop_glue = strukt
700700
.fields(db)
701701
.iter()
702-
.map(|field| field.ty(db).drop_glue(db))
702+
.map(|field| field.ty(db).to_type(db).drop_glue(db))
703703
.max()
704704
.unwrap_or(DropGlue::None);
705705
let has_dtor = match (fields_drop_glue, struct_drop_glue) {
@@ -727,7 +727,7 @@ pub(super) fn definition(
727727
variant
728728
.fields(db)
729729
.iter()
730-
.map(|field| field.ty(db).drop_glue(db))
730+
.map(|field| field.ty(db).to_type(db).drop_glue(db))
731731
.max()
732732
.unwrap_or(DropGlue::None)
733733
})
@@ -742,7 +742,7 @@ pub(super) fn definition(
742742
let fields_drop_glue = variant
743743
.fields(db)
744744
.iter()
745-
.map(|field| field.ty(db).drop_glue(db))
745+
.map(|field| field.ty(db).to_type(db).drop_glue(db))
746746
.max()
747747
.unwrap_or(DropGlue::None);
748748
DropInfo { drop_glue: fields_drop_glue, has_dtor: None }

crates/ide/src/signature_help.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ fn signature_help_for_tuple_struct_pat(
526526
pat.syntax(),
527527
token,
528528
pat.fields(),
529-
fields.into_iter().map(|it| it.ty(db)),
529+
fields.into_iter().map(|it| it.ty(db).to_type(db)),
530530
display_target,
531531
))
532532
}

0 commit comments

Comments
 (0)