From 0e995adcf690778739fe94fb94ae317d42b4e51b Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 28 Feb 2021 20:39:43 +0100 Subject: Turn Ty::Tuple variant into a tuple-variant --- crates/hir/src/code_model.rs | 4 ++-- crates/hir_ty/src/display.rs | 2 +- crates/hir_ty/src/infer/expr.rs | 8 ++++---- crates/hir_ty/src/infer/pat.rs | 2 +- crates/hir_ty/src/lib.rs | 16 +++++++--------- crates/hir_ty/src/lower.rs | 2 +- crates/hir_ty/src/method_resolution.rs | 4 ++-- crates/hir_ty/src/traits/chalk/mapping.rs | 4 ++-- 8 files changed, 20 insertions(+), 22 deletions(-) diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index cdb54eca2..1c31e29ac 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -1547,7 +1547,7 @@ impl Type { } pub fn is_unit(&self) -> bool { - matches!(self.ty.value, Ty::Tuple { cardinality: 0, .. }) + matches!(self.ty.value, Ty::Tuple(0, ..)) } pub fn is_bool(&self) -> bool { matches!(self.ty.value, Ty::Scalar(Scalar::Bool)) @@ -1741,7 +1741,7 @@ impl Type { } pub fn tuple_fields(&self, _db: &dyn HirDatabase) -> Vec { - if let Ty::Tuple { substs, .. } = &self.ty.value { + if let Ty::Tuple(_, substs) = &self.ty.value { substs.iter().map(|ty| self.derived(ty.clone())).collect() } else { Vec::new() diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index cd9dcf6c0..666bb1f9d 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -330,7 +330,7 @@ impl HirDisplay for Ty { write!(f, "{}", ty_display)?; } } - Ty::Tuple { substs, .. } => { + Ty::Tuple(_, substs) => { if substs.len() == 1 { write!(f, "(")?; substs[0].hir_fmt(f)?; diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 2369c9bef..13240f790 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -82,7 +82,7 @@ impl<'a> InferenceContext<'a> { arg_tys.push(arg); } let parameters = param_builder.build(); - let arg_ty = Ty::Tuple { cardinality: num_args as u16, substs: parameters }; + let arg_ty = Ty::Tuple(num_args, parameters); let substs = Substs::build_for_generics(&generic_params).push(ty.clone()).push(arg_ty).build(); @@ -424,7 +424,7 @@ impl<'a> InferenceContext<'a> { }, ) .find_map(|derefed_ty| match canonicalized.decanonicalize_ty(derefed_ty.value) { - Ty::Tuple { substs, .. } => { + Ty::Tuple(_, substs) => { name.as_tuple_index().and_then(|idx| substs.0.get(idx).cloned()) } Ty::Adt(AdtId::StructId(s), parameters) => { @@ -635,7 +635,7 @@ impl<'a> InferenceContext<'a> { } Expr::Tuple { exprs } => { let mut tys = match &expected.ty { - Ty::Tuple { substs, .. } => substs + Ty::Tuple(_, substs) => substs .iter() .cloned() .chain(repeat_with(|| self.table.new_type_var())) @@ -648,7 +648,7 @@ impl<'a> InferenceContext<'a> { self.infer_expr_coerce(*expr, &Expectation::has_type(ty.clone())); } - Ty::Tuple { cardinality: tys.len() as u16, substs: Substs(tys.into()) } + Ty::Tuple(tys.len(), Substs(tys.into())) } Expr::Array(array) => { let elem_ty = match &expected.ty { diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index e96e08c3c..a318e47f3 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs @@ -138,7 +138,7 @@ impl<'a> InferenceContext<'a> { inner_tys.extend(expectations_iter.by_ref().take(n_uncovered_patterns).cloned()); inner_tys.extend(post.iter().zip(expectations_iter).map(infer_pat)); - Ty::Tuple { cardinality: inner_tys.len() as u16, substs: Substs(inner_tys.into()) } + Ty::Tuple(inner_tys.len(), Substs(inner_tys.into())) } Pat::Or(ref pats) => { if let Some((first_pat, rest)) = pats.split_first() { diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 117d69f01..3d134453f 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -120,7 +120,7 @@ pub enum Ty { Scalar(Scalar), /// A tuple type. For example, `(i32, bool)`. - Tuple { cardinality: u16, substs: Substs }, + Tuple(usize, Substs), /// An array with the given length. Written as `[T; n]`. Array(Substs), @@ -582,7 +582,7 @@ impl TypeWalk for FnSig { impl Ty { pub fn unit() -> Self { - Ty::Tuple { cardinality: 0, substs: Substs::empty() } + Ty::Tuple(0, Substs::empty()) } pub fn fn_ptr(sig: FnSig) -> Self { @@ -642,7 +642,7 @@ impl Ty { pub fn as_tuple(&self) -> Option<&Substs> { match self { - Ty::Tuple { substs: parameters, .. } => Some(parameters), + Ty::Tuple(_, substs) => Some(substs), _ => None, } } @@ -684,9 +684,7 @@ impl Ty { Ty::FnPtr { num_args, is_varargs, .. }, Ty::FnPtr { num_args: num_args2, is_varargs: is_varargs2, .. }, ) => num_args == num_args2 && is_varargs == is_varargs2, - (Ty::Tuple { cardinality, .. }, Ty::Tuple { cardinality: cardinality2, .. }) => { - cardinality == cardinality2 - } + (Ty::Tuple(cardinality, _), Ty::Tuple(cardinality2, _)) => cardinality == cardinality2, (Ty::Str, Ty::Str) | (Ty::Never, Ty::Never) => true, (Ty::Scalar(scalar), Ty::Scalar(scalar2)) => scalar == scalar2, _ => false, @@ -754,7 +752,7 @@ impl Ty { | Ty::Ref(_, substs) | Ty::FnDef(_, substs) | Ty::FnPtr { substs, .. } - | Ty::Tuple { substs, .. } + | Ty::Tuple(_, substs) | Ty::OpaqueType(_, substs) | Ty::AssociatedType(_, substs) | Ty::ForeignType(_, substs) @@ -778,7 +776,7 @@ impl Ty { | Ty::Ref(_, substs) | Ty::FnDef(_, substs) | Ty::FnPtr { substs, .. } - | Ty::Tuple { substs, .. } + | Ty::Tuple(_, substs) | Ty::OpaqueType(_, substs) | Ty::AssociatedType(_, substs) | Ty::ForeignType(_, substs) @@ -796,7 +794,7 @@ impl Ty { | Ty::Ref(_, substs) | Ty::FnDef(_, substs) | Ty::FnPtr { substs, .. } - | Ty::Tuple { substs, .. } + | Ty::Tuple(_, substs) | Ty::OpaqueType(_, substs) | Ty::AssociatedType(_, substs) | Ty::ForeignType(_, substs) diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 6b919a09e..8295f4c31 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -148,7 +148,7 @@ impl Ty { TypeRef::Never => Ty::Never, TypeRef::Tuple(inner) => { let inner_tys: Arc<[Ty]> = inner.iter().map(|tr| Ty::from_hir(ctx, tr)).collect(); - Ty::Tuple { cardinality: inner_tys.len() as u16, substs: Substs(inner_tys) } + Ty::Tuple(inner_tys.len(), Substs(inner_tys)) } TypeRef::Path(path) => { let (ty, res_) = Ty::from_hir_path(ctx, path); diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index 087b67935..422e61f0a 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs @@ -33,7 +33,7 @@ pub enum TyFingerprint { Scalar(Scalar), Adt(AdtId), Dyn(TraitId), - Tuple { cardinality: u16 }, + Tuple(usize), ForeignType(TypeAliasId), FnPtr { num_args: u16, is_varargs: bool }, } @@ -50,7 +50,7 @@ impl TyFingerprint { &Ty::Array(..) => TyFingerprint::Array, &Ty::Scalar(scalar) => TyFingerprint::Scalar(scalar), &Ty::Adt(adt, _) => TyFingerprint::Adt(adt), - &Ty::Tuple { cardinality: u16, .. } => TyFingerprint::Tuple { cardinality: u16 }, + &Ty::Tuple(cardinality, _) => TyFingerprint::Tuple(cardinality), &Ty::RawPtr(mutability, ..) => TyFingerprint::RawPtr(mutability), &Ty::ForeignType(alias_id, ..) => TyFingerprint::ForeignType(alias_id), &Ty::FnPtr { num_args, is_varargs, .. } => { diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 297ddeabd..09e5a82b8 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -63,7 +63,7 @@ impl ToChalk for Ty { Ty::Scalar(scalar) => chalk_ir::TyKind::Scalar(scalar).intern(&Interner), - Ty::Tuple { cardinality, substs } => { + Ty::Tuple(cardinality, substs) => { let substitution = substs.to_chalk(db); chalk_ir::TyKind::Tuple(cardinality.into(), substitution).intern(&Interner) } @@ -199,7 +199,7 @@ impl ToChalk for Ty { chalk_ir::TyKind::Scalar(scalar) => Ty::Scalar(scalar), chalk_ir::TyKind::Tuple(cardinality, subst) => { - Ty::Tuple { cardinality: cardinality as u16, substs: from_chalk(db, subst) } + Ty::Tuple(cardinality, from_chalk(db, subst)) } chalk_ir::TyKind::Raw(mutability, ty) => { Ty::RawPtr(from_chalk(db, mutability), Substs::single(from_chalk(db, ty))) -- cgit v1.2.3 From a3fd2faba5b0736fd51c7b94cae55e0a9609cdb0 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 28 Feb 2021 20:44:09 +0100 Subject: Remove Substs from Ty::ForeignType --- crates/hir_ty/src/display.rs | 7 +------ crates/hir_ty/src/lib.rs | 5 +---- crates/hir_ty/src/lower.rs | 4 ++-- crates/hir_ty/src/method_resolution.rs | 2 +- crates/hir_ty/src/traits/chalk/mapping.rs | 9 ++++----- 5 files changed, 9 insertions(+), 18 deletions(-) diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 666bb1f9d..4a25a49e3 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -471,14 +471,9 @@ impl HirDisplay for Ty { projection_ty.hir_fmt(f)?; } } - Ty::ForeignType(type_alias, parameters) => { + Ty::ForeignType(type_alias) => { let type_alias = f.db.type_alias_data(*type_alias); write!(f, "{}", type_alias.name)?; - if parameters.len() > 0 { - write!(f, "<")?; - f.write_joined(&*parameters.0, ", ")?; - write!(f, ">")?; - } } Ty::OpaqueType(opaque_ty_id, parameters) => { match opaque_ty_id { diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 3d134453f..4c0ebcfe3 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -169,7 +169,7 @@ pub enum Ty { Closure { def: DefWithBodyId, expr: ExprId, substs: Substs }, /// Represents a foreign type declared in external blocks. - ForeignType(TypeAliasId, Substs), + ForeignType(TypeAliasId), /// A pointer to a function. Written as `fn() -> i32`. /// @@ -755,7 +755,6 @@ impl Ty { | Ty::Tuple(_, substs) | Ty::OpaqueType(_, substs) | Ty::AssociatedType(_, substs) - | Ty::ForeignType(_, substs) | Ty::Closure { substs, .. } => { assert_eq!(substs.len(), new_substs.len()); *substs = new_substs; @@ -779,7 +778,6 @@ impl Ty { | Ty::Tuple(_, substs) | Ty::OpaqueType(_, substs) | Ty::AssociatedType(_, substs) - | Ty::ForeignType(_, substs) | Ty::Closure { substs, .. } => Some(substs), _ => None, } @@ -797,7 +795,6 @@ impl Ty { | Ty::Tuple(_, substs) | Ty::OpaqueType(_, substs) | Ty::AssociatedType(_, substs) - | Ty::ForeignType(_, substs) | Ty::Closure { substs, .. } => Some(substs), _ => None, } diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 8295f4c31..84734bc0b 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -1100,10 +1100,10 @@ fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders { let resolver = t.resolver(db.upcast()); let ctx = TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); - let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); if db.type_alias_data(t).is_extern { - Binders::new(substs.len(), Ty::ForeignType(t, substs)) + Binders::new(0, Ty::ForeignType(t)) } else { + let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); let type_ref = &db.type_alias_data(t).type_ref; let inner = Ty::from_hir(&ctx, type_ref.as_ref().unwrap_or(&TypeRef::Error)); Binders::new(substs.len(), inner) diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index 422e61f0a..ff8ce5599 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs @@ -235,7 +235,7 @@ impl Ty { Ty::Adt(def_id, _) => { return mod_to_crate_ids(def_id.module(db.upcast())); } - Ty::ForeignType(type_alias_id, _) => { + Ty::ForeignType(type_alias_id) => { return mod_to_crate_ids(type_alias_id.lookup(db.upcast()).module(db.upcast())); } Ty::Scalar(Scalar::Bool) => lang_item_crate!("bool"), diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 09e5a82b8..c17c19638 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -55,7 +55,7 @@ impl ToChalk for Ty { chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner) } - Ty::ForeignType(type_alias, _) => { + Ty::ForeignType(type_alias) => { let foreign_type = TypeAliasAsForeignType(type_alias); let foreign_type_id = foreign_type.to_chalk(db); chalk_ir::TyKind::Foreign(foreign_type_id).intern(&Interner) @@ -221,10 +221,9 @@ impl ToChalk for Ty { Ty::Closure { def, expr, substs: from_chalk(db, subst) } } - chalk_ir::TyKind::Foreign(foreign_def_id) => Ty::ForeignType( - from_chalk::(db, foreign_def_id).0, - Substs::empty(), - ), + chalk_ir::TyKind::Foreign(foreign_def_id) => { + Ty::ForeignType(from_chalk::(db, foreign_def_id).0) + } chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME } -- cgit v1.2.3