Skip to content

Commit ddbbd6a

Browse files
committed
Auto merge of rust-lang#15417 - lowr:patch/purge-generic-arg-data-in-expr, r=HKalbasi
internal: use `Cast::cast()` instead of explicit interning I firmly believe that we should generally use `cast()` instead of interning `GenericArgData` to construct `GenericArg` because it's less verbose and more readable.
2 parents e13fac3 + 6aa03c5 commit ddbbd6a

File tree

7 files changed

+45
-73
lines changed

7 files changed

+45
-73
lines changed

crates/hir-ty/src/builder.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use smallvec::SmallVec;
1717
use crate::{
1818
consteval::unknown_const_as_generic, db::HirDatabase, infer::unify::InferenceTable, primitive,
1919
to_assoc_type_id, to_chalk_trait_id, utils::generics, Binders, BoundVar, CallableSig,
20-
GenericArg, Interner, ProjectionTy, Substitution, TraitRef, Ty, TyDefId, TyExt, TyKind,
20+
GenericArg, GenericArgData, Interner, ProjectionTy, Substitution, TraitRef, Ty, TyDefId, TyExt,
21+
TyKind,
2122
};
2223

2324
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -79,9 +80,9 @@ impl<D> TyBuilder<D> {
7980
let expected_kind = &self.param_kinds[self.vec.len()];
8081

8182
let arg_kind = match arg.data(Interner) {
82-
chalk_ir::GenericArgData::Ty(_) => ParamKind::Type,
83-
chalk_ir::GenericArgData::Lifetime(_) => panic!("Got lifetime in TyBuilder::push"),
84-
chalk_ir::GenericArgData::Const(c) => {
83+
GenericArgData::Ty(_) => ParamKind::Type,
84+
GenericArgData::Lifetime(_) => panic!("Got lifetime in TyBuilder::push"),
85+
GenericArgData::Const(c) => {
8586
let c = c.data(Interner);
8687
ParamKind::Const(c.ty.clone())
8788
}
@@ -139,8 +140,8 @@ impl<D> TyBuilder<D> {
139140

140141
fn assert_match_kind(&self, a: &chalk_ir::GenericArg<Interner>, e: &ParamKind) {
141142
match (a.data(Interner), e) {
142-
(chalk_ir::GenericArgData::Ty(_), ParamKind::Type)
143-
| (chalk_ir::GenericArgData::Const(_), ParamKind::Const(_)) => (),
143+
(GenericArgData::Ty(_), ParamKind::Type)
144+
| (GenericArgData::Const(_), ParamKind::Const(_)) => (),
144145
_ => panic!("Mismatched kinds: {a:?}, {:?}, {:?}", self.vec, self.param_kinds),
145146
}
146147
}

crates/hir-ty/src/consteval.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Constant evaluation details
22
33
use base_db::CrateId;
4-
use chalk_ir::{BoundVar, DebruijnIndex, GenericArgData};
4+
use chalk_ir::{cast::Cast, BoundVar, DebruijnIndex};
55
use hir_def::{
66
hir::Expr,
77
path::Path,
@@ -120,7 +120,7 @@ pub fn unknown_const(ty: Ty) -> Const {
120120
}
121121

122122
pub fn unknown_const_as_generic(ty: Ty) -> GenericArg {
123-
GenericArgData::Const(unknown_const(ty)).intern(Interner)
123+
unknown_const(ty).cast(Interner)
124124
}
125125

126126
/// Interns a constant scalar with the given type

crates/hir-ty/src/infer/expr.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ use std::{
55
mem,
66
};
77

8-
use chalk_ir::{
9-
cast::Cast, fold::Shift, DebruijnIndex, GenericArgData, Mutability, TyVariableKind,
10-
};
8+
use chalk_ir::{cast::Cast, fold::Shift, DebruijnIndex, Mutability, TyVariableKind};
119
use hir_def::{
1210
generics::TypeOrConstParamData,
1311
hir::{
@@ -750,7 +748,7 @@ impl InferenceContext<'_> {
750748
self.resolve_associated_type_with_params(
751749
self_ty,
752750
self.resolve_ops_index_output(),
753-
&[GenericArgData::Ty(index_ty).intern(Interner)],
751+
&[index_ty.cast(Interner)],
754752
)
755753
} else {
756754
self.err_ty()
@@ -1721,16 +1719,13 @@ impl InferenceContext<'_> {
17211719
for (id, data) in def_generics.iter().skip(substs.len()) {
17221720
match data {
17231721
TypeOrConstParamData::TypeParamData(_) => {
1724-
substs.push(GenericArgData::Ty(self.table.new_type_var()).intern(Interner))
1725-
}
1726-
TypeOrConstParamData::ConstParamData(_) => {
1727-
substs.push(
1728-
GenericArgData::Const(self.table.new_const_var(
1729-
self.db.const_param_ty(ConstParamId::from_unchecked(id)),
1730-
))
1731-
.intern(Interner),
1732-
)
1722+
substs.push(self.table.new_type_var().cast(Interner))
17331723
}
1724+
TypeOrConstParamData::ConstParamData(_) => substs.push(
1725+
self.table
1726+
.new_const_var(self.db.const_param_ty(ConstParamId::from_unchecked(id)))
1727+
.cast(Interner),
1728+
),
17341729
}
17351730
}
17361731
assert_eq!(substs.len(), total_len);

crates/hir-ty/src/infer/unify.rs

+13-23
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use chalk_solve::infer::ParameterEnaVariableExt;
1010
use either::Either;
1111
use ena::unify::UnifyKey;
1212
use hir_expand::name;
13-
use stdx::never;
1413
use triomphe::Arc;
1514

1615
use super::{InferOk, InferResult, InferenceContext, TypeError};
@@ -92,15 +91,10 @@ pub(crate) fn unify(
9291
let vars = Substitution::from_iter(
9392
Interner,
9493
tys.binders.iter(Interner).map(|it| match &it.kind {
95-
chalk_ir::VariableKind::Ty(_) => {
96-
GenericArgData::Ty(table.new_type_var()).intern(Interner)
97-
}
98-
chalk_ir::VariableKind::Lifetime => {
99-
GenericArgData::Ty(table.new_type_var()).intern(Interner)
100-
} // FIXME: maybe wrong?
101-
chalk_ir::VariableKind::Const(ty) => {
102-
GenericArgData::Const(table.new_const_var(ty.clone())).intern(Interner)
103-
}
94+
chalk_ir::VariableKind::Ty(_) => table.new_type_var().cast(Interner),
95+
// FIXME: maybe wrong?
96+
chalk_ir::VariableKind::Lifetime => table.new_type_var().cast(Interner),
97+
chalk_ir::VariableKind::Const(ty) => table.new_const_var(ty.clone()).cast(Interner),
10498
}),
10599
);
106100
let ty1_with_vars = vars.apply(tys.value.0.clone(), Interner);
@@ -111,10 +105,10 @@ pub(crate) fn unify(
111105
// default any type vars that weren't unified back to their original bound vars
112106
// (kind of hacky)
113107
let find_var = |iv| {
114-
vars.iter(Interner).position(|v| match v.interned() {
115-
chalk_ir::GenericArgData::Ty(ty) => ty.inference_var(Interner),
116-
chalk_ir::GenericArgData::Lifetime(lt) => lt.inference_var(Interner),
117-
chalk_ir::GenericArgData::Const(c) => c.inference_var(Interner),
108+
vars.iter(Interner).position(|v| match v.data(Interner) {
109+
GenericArgData::Ty(ty) => ty.inference_var(Interner),
110+
GenericArgData::Lifetime(lt) => lt.inference_var(Interner),
111+
GenericArgData::Const(c) => c.inference_var(Interner),
118112
} == Some(iv))
119113
};
120114
let fallback = |iv, kind, default, binder| match kind {
@@ -611,9 +605,9 @@ impl<'a> InferenceTable<'a> {
611605
fn check_changed(&mut self, canonicalized: &Canonicalized<InEnvironment<Goal>>) -> bool {
612606
canonicalized.free_vars.iter().any(|var| {
613607
let iv = match var.data(Interner) {
614-
chalk_ir::GenericArgData::Ty(ty) => ty.inference_var(Interner),
615-
chalk_ir::GenericArgData::Lifetime(lt) => lt.inference_var(Interner),
616-
chalk_ir::GenericArgData::Const(c) => c.inference_var(Interner),
608+
GenericArgData::Ty(ty) => ty.inference_var(Interner),
609+
GenericArgData::Lifetime(lt) => lt.inference_var(Interner),
610+
GenericArgData::Const(c) => c.inference_var(Interner),
617611
}
618612
.expect("free var is not inference var");
619613
if self.var_unification_table.probe_var(iv).is_some() {
@@ -690,14 +684,10 @@ impl<'a> InferenceTable<'a> {
690684
.fill(|it| {
691685
let arg = match it {
692686
ParamKind::Type => self.new_type_var(),
693-
ParamKind::Const(ty) => {
694-
never!("Tuple with const parameter");
695-
return GenericArgData::Const(self.new_const_var(ty.clone()))
696-
.intern(Interner);
697-
}
687+
ParamKind::Const(_) => unreachable!("Tuple with const parameter"),
698688
};
699689
arg_tys.push(arg.clone());
700-
GenericArgData::Ty(arg).intern(Interner)
690+
arg.cast(Interner)
701691
})
702692
.build();
703693

crates/hir-ty/src/lower.rs

+8-20
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,9 @@ use crate::{
5858
InTypeConstIdMetadata,
5959
},
6060
AliasEq, AliasTy, Binders, BoundVar, CallableSig, Const, ConstScalar, DebruijnIndex, DynTy,
61-
FnPointer, FnSig, FnSubst, GenericArgData, ImplTraitId, Interner, ParamKind, PolyFnSig,
62-
ProjectionTy, QuantifiedWhereClause, QuantifiedWhereClauses, ReturnTypeImplTrait,
63-
ReturnTypeImplTraits, Substitution, TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder,
64-
TyKind, WhereClause,
61+
FnPointer, FnSig, FnSubst, ImplTraitId, Interner, ParamKind, PolyFnSig, ProjectionTy,
62+
QuantifiedWhereClause, QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits,
63+
Substitution, TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyKind, WhereClause,
6564
};
6665

6766
#[derive(Debug)]
@@ -1643,9 +1642,7 @@ pub(crate) fn generic_defaults_recover(
16431642
.iter_id()
16441643
.map(|id| {
16451644
let val = match id {
1646-
Either::Left(_) => {
1647-
GenericArgData::Ty(TyKind::Error.intern(Interner)).intern(Interner)
1648-
}
1645+
Either::Left(_) => TyKind::Error.intern(Interner).cast(Interner),
16491646
Either::Right(id) => unknown_const_as_generic(db.const_param_ty(id)),
16501647
};
16511648
crate::make_binders(db, &generic_params, val)
@@ -1991,16 +1988,9 @@ pub(crate) fn generic_arg_to_chalk<'a, T>(
19911988
}
19921989
};
19931990
Some(match (arg, kind) {
1994-
(GenericArg::Type(type_ref), ParamKind::Type) => {
1995-
let ty = for_type(this, type_ref);
1996-
GenericArgData::Ty(ty).intern(Interner)
1997-
}
1998-
(GenericArg::Const(c), ParamKind::Const(c_ty)) => {
1999-
GenericArgData::Const(for_const(this, c, c_ty)).intern(Interner)
2000-
}
2001-
(GenericArg::Const(_), ParamKind::Type) => {
2002-
GenericArgData::Ty(TyKind::Error.intern(Interner)).intern(Interner)
2003-
}
1991+
(GenericArg::Type(type_ref), ParamKind::Type) => for_type(this, type_ref).cast(Interner),
1992+
(GenericArg::Const(c), ParamKind::Const(c_ty)) => for_const(this, c, c_ty).cast(Interner),
1993+
(GenericArg::Const(_), ParamKind::Type) => TyKind::Error.intern(Interner).cast(Interner),
20041994
(GenericArg::Type(t), ParamKind::Const(c_ty)) => {
20051995
// We want to recover simple idents, which parser detects them
20061996
// as types. Maybe here is not the best place to do it, but
@@ -2010,9 +2000,7 @@ pub(crate) fn generic_arg_to_chalk<'a, T>(
20102000
if p.kind == PathKind::Plain {
20112001
if let [n] = p.segments() {
20122002
let c = ConstRef::Path(n.clone());
2013-
return Some(
2014-
GenericArgData::Const(for_const(this, &c, c_ty)).intern(Interner),
2015-
);
2003+
return Some(for_const(this, &c, c_ty).cast(Interner));
20162004
}
20172005
}
20182006
}

crates/hir-ty/src/mir/eval.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{
1010
};
1111

1212
use base_db::{CrateId, FileId};
13-
use chalk_ir::Mutability;
13+
use chalk_ir::{cast::Cast, Mutability};
1414
use either::Either;
1515
use hir_def::{
1616
builtin_type::BuiltinType,
@@ -40,8 +40,8 @@ use crate::{
4040
name, static_lifetime,
4141
traits::FnTrait,
4242
utils::{detect_variant_from_bytes, ClosureSubst},
43-
CallableDefId, ClosureId, Const, ConstScalar, FnDefId, GenericArgData, Interner, MemoryMap,
44-
Substitution, TraitEnvironment, Ty, TyBuilder, TyExt, TyKind,
43+
CallableDefId, ClosureId, Const, ConstScalar, FnDefId, Interner, MemoryMap, Substitution,
44+
TraitEnvironment, Ty, TyBuilder, TyExt, TyKind,
4545
};
4646

4747
use super::{
@@ -2248,7 +2248,7 @@ impl Evaluator<'_> {
22482248
interval: args_for_target[0].interval.slice(0..self.ptr_size()),
22492249
ty: ty.clone(),
22502250
};
2251-
let ty = GenericArgData::Ty(ty.clone()).intern(Interner);
2251+
let ty = ty.clone().cast(Interner);
22522252
let generics_for_target = Substitution::from_iter(
22532253
Interner,
22542254
generic_args.iter(Interner).enumerate().map(|(i, it)| {

crates/hir/src/lib.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,7 @@ impl Adt {
12731273
.fill(|x| {
12741274
let r = it.next().unwrap_or_else(|| TyKind::Error.intern(Interner));
12751275
match x {
1276-
ParamKind::Type => GenericArgData::Ty(r).intern(Interner),
1276+
ParamKind::Type => r.cast(Interner),
12771277
ParamKind::Const(ty) => unknown_const_as_generic(ty.clone()),
12781278
}
12791279
})
@@ -3716,7 +3716,7 @@ impl Type {
37163716
.fill(|x| {
37173717
let r = it.next().unwrap();
37183718
match x {
3719-
ParamKind::Type => GenericArgData::Ty(r).intern(Interner),
3719+
ParamKind::Type => r.cast(Interner),
37203720
ParamKind::Const(ty) => {
37213721
// FIXME: this code is not covered in tests.
37223722
unknown_const_as_generic(ty.clone())
@@ -3749,9 +3749,7 @@ impl Type {
37493749
.fill(|it| {
37503750
// FIXME: this code is not covered in tests.
37513751
match it {
3752-
ParamKind::Type => {
3753-
GenericArgData::Ty(args.next().unwrap().ty.clone()).intern(Interner)
3754-
}
3752+
ParamKind::Type => args.next().unwrap().ty.clone().cast(Interner),
37553753
ParamKind::Const(ty) => unknown_const_as_generic(ty.clone()),
37563754
}
37573755
})

0 commit comments

Comments
 (0)