Skip to content

Commit 9c494da

Browse files
authored
Merge pull request #20867 from ChayimFriedman2/to-ns-almost-final
Migrate variance to the next solver and remove lint allows from its stuff
2 parents c7e7eb9 + 67295e8 commit 9c494da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+754
-1691
lines changed

crates/hir-ty/src/builder.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! `TyBuilder`, a helper for building instances of `Ty` and related types.
22
33
use chalk_ir::{
4-
DebruijnIndex, Scalar,
4+
DebruijnIndex,
55
cast::{Cast, Caster},
66
};
7-
use hir_def::{GenericDefId, GenericParamId, TraitId, builtin_type::BuiltinType};
7+
use hir_def::{GenericDefId, GenericParamId, TraitId};
88
use smallvec::SmallVec;
99

1010
use crate::{
@@ -18,7 +18,7 @@ use crate::{
1818
DbInterner, EarlyBinder,
1919
mapping::{ChalkToNextSolver, NextSolverToChalk},
2020
},
21-
primitive, to_chalk_trait_id,
21+
to_chalk_trait_id,
2222
};
2323

2424
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -137,23 +137,6 @@ impl TyBuilder<()> {
137137
TyKind::Scalar(chalk_ir::Scalar::Uint(chalk_ir::UintTy::Usize)).intern(Interner)
138138
}
139139

140-
pub(crate) fn builtin(builtin: BuiltinType) -> Ty {
141-
match builtin {
142-
BuiltinType::Char => TyKind::Scalar(Scalar::Char).intern(Interner),
143-
BuiltinType::Bool => TyKind::Scalar(Scalar::Bool).intern(Interner),
144-
BuiltinType::Str => TyKind::Str.intern(Interner),
145-
BuiltinType::Int(t) => {
146-
TyKind::Scalar(Scalar::Int(primitive::int_ty_from_builtin(t))).intern(Interner)
147-
}
148-
BuiltinType::Uint(t) => {
149-
TyKind::Scalar(Scalar::Uint(primitive::uint_ty_from_builtin(t))).intern(Interner)
150-
}
151-
BuiltinType::Float(t) => {
152-
TyKind::Scalar(Scalar::Float(primitive::float_ty_from_builtin(t))).intern(Interner)
153-
}
154-
}
155-
}
156-
157140
pub(crate) fn unknown_subst(
158141
db: &dyn HirDatabase,
159142
def: impl Into<GenericDefId>,

crates/hir-ty/src/chalk_db.rs

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,9 @@
11
//! The implementation of `RustIrDatabase` for Chalk, which provides information
22
//! about the code that Chalk needs.
3-
use hir_def::{CallableDefId, GenericDefId};
43
5-
use crate::{Interner, db::HirDatabase, mapping::from_chalk};
4+
use crate::Interner;
65

76
pub(crate) type AssocTypeId = chalk_ir::AssocTypeId<Interner>;
87
pub(crate) type TraitId = chalk_ir::TraitId<Interner>;
98
pub(crate) type AdtId = chalk_ir::AdtId<Interner>;
109
pub(crate) type ImplId = chalk_ir::ImplId<Interner>;
11-
pub(crate) type Variances = chalk_ir::Variances<Interner>;
12-
13-
impl chalk_ir::UnificationDatabase<Interner> for &dyn HirDatabase {
14-
fn fn_def_variance(
15-
&self,
16-
fn_def_id: chalk_ir::FnDefId<Interner>,
17-
) -> chalk_ir::Variances<Interner> {
18-
HirDatabase::fn_def_variance(*self, from_chalk(*self, fn_def_id))
19-
}
20-
21-
fn adt_variance(&self, adt_id: chalk_ir::AdtId<Interner>) -> chalk_ir::Variances<Interner> {
22-
HirDatabase::adt_variance(*self, adt_id.0)
23-
}
24-
}
25-
26-
pub(crate) fn fn_def_variance_query(
27-
db: &dyn HirDatabase,
28-
callable_def: CallableDefId,
29-
) -> Variances {
30-
Variances::from_iter(
31-
Interner,
32-
db.variances_of(GenericDefId::from_callable(db, callable_def))
33-
.as_deref()
34-
.unwrap_or_default()
35-
.iter()
36-
.map(|v| match v {
37-
crate::variance::Variance::Covariant => chalk_ir::Variance::Covariant,
38-
crate::variance::Variance::Invariant => chalk_ir::Variance::Invariant,
39-
crate::variance::Variance::Contravariant => chalk_ir::Variance::Contravariant,
40-
crate::variance::Variance::Bivariant => chalk_ir::Variance::Invariant,
41-
}),
42-
)
43-
}
44-
45-
pub(crate) fn adt_variance_query(db: &dyn HirDatabase, adt_id: hir_def::AdtId) -> Variances {
46-
Variances::from_iter(
47-
Interner,
48-
db.variances_of(adt_id.into()).as_deref().unwrap_or_default().iter().map(|v| match v {
49-
crate::variance::Variance::Covariant => chalk_ir::Variance::Covariant,
50-
crate::variance::Variance::Invariant => chalk_ir::Variance::Invariant,
51-
crate::variance::Variance::Contravariant => chalk_ir::Variance::Contravariant,
52-
crate::variance::Variance::Bivariant => chalk_ir::Variance::Invariant,
53-
}),
54-
)
55-
}

crates/hir-ty/src/chalk_ext.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
use hir_def::{ItemContainerId, Lookup, TraitId};
44

55
use crate::{
6-
Binders, DynTy, Interner, ProjectionTy, Substitution, TraitRef, Ty, db::HirDatabase,
7-
from_assoc_type_id, from_chalk_trait_id, generics::generics, to_chalk_trait_id,
6+
Interner, ProjectionTy, Substitution, TraitRef, Ty, db::HirDatabase, from_assoc_type_id,
7+
from_chalk_trait_id, generics::generics, to_chalk_trait_id,
88
};
99

1010
pub(crate) trait ProjectionTyExt {
@@ -35,23 +35,6 @@ impl ProjectionTyExt for ProjectionTy {
3535
}
3636
}
3737

38-
pub(crate) trait DynTyExt {
39-
fn principal(&self) -> Option<Binders<Binders<&TraitRef>>>;
40-
}
41-
42-
impl DynTyExt for DynTy {
43-
fn principal(&self) -> Option<Binders<Binders<&TraitRef>>> {
44-
self.bounds.as_ref().filter_map(|bounds| {
45-
bounds.interned().first().and_then(|b| {
46-
b.as_ref().filter_map(|b| match b {
47-
crate::WhereClause::Implemented(trait_ref) => Some(trait_ref),
48-
_ => None,
49-
})
50-
})
51-
})
52-
}
53-
}
54-
5538
pub(crate) trait TraitRefExt {
5639
fn hir_trait_id(&self) -> TraitId;
5740
}

crates/hir-ty/src/consteval.rs

Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,74 +6,28 @@ mod tests;
66
use base_db::Crate;
77
use hir_def::{
88
EnumVariantId, GeneralConstId, HasModule, StaticId,
9-
expr_store::{Body, HygieneId, path::Path},
9+
expr_store::Body,
1010
hir::{Expr, ExprId},
11-
resolver::{Resolver, ValueNs},
1211
type_ref::LiteralConstRef,
1312
};
1413
use hir_expand::Lookup;
15-
use rustc_type_ir::{UnevaluatedConst, inherent::IntoKind};
16-
use stdx::never;
14+
use rustc_type_ir::inherent::IntoKind;
1715
use triomphe::Arc;
1816

1917
use crate::{
20-
MemoryMap, TraitEnvironment,
18+
LifetimeElisionKind, MemoryMap, TraitEnvironment, TyLoweringContext,
2119
db::HirDatabase,
2220
display::DisplayTarget,
23-
generics::Generics,
2421
infer::InferenceContext,
2522
mir::{MirEvalError, MirLowerError},
2623
next_solver::{
2724
Const, ConstBytes, ConstKind, DbInterner, ErrorGuaranteed, GenericArg, GenericArgs,
28-
ParamConst, SolverDefId, Ty, ValueConst,
25+
SolverDefId, Ty, ValueConst,
2926
},
3027
};
3128

3229
use super::mir::{interpret_mir, lower_to_mir, pad16};
3330

34-
pub(crate) fn path_to_const<'a, 'g>(
35-
db: &'a dyn HirDatabase,
36-
resolver: &Resolver<'a>,
37-
path: &Path,
38-
args: impl FnOnce() -> &'g Generics,
39-
_expected_ty: Ty<'a>,
40-
) -> Option<Const<'a>> {
41-
let interner = DbInterner::new_with(db, Some(resolver.krate()), None);
42-
match resolver.resolve_path_in_value_ns_fully(db, path, HygieneId::ROOT) {
43-
Some(ValueNs::GenericParam(p)) => {
44-
let args = args();
45-
match args
46-
.type_or_const_param(p.into())
47-
.and_then(|(idx, p)| p.const_param().map(|p| (idx, p.clone())))
48-
{
49-
Some((idx, _param)) => {
50-
Some(Const::new_param(interner, ParamConst { index: idx as u32, id: p }))
51-
}
52-
None => {
53-
never!(
54-
"Generic list doesn't contain this param: {:?}, {:?}, {:?}",
55-
args,
56-
path,
57-
p
58-
);
59-
None
60-
}
61-
}
62-
}
63-
Some(ValueNs::ConstId(c)) => {
64-
let args = GenericArgs::new_from_iter(interner, []);
65-
Some(Const::new(
66-
interner,
67-
rustc_type_ir::ConstKind::Unevaluated(UnevaluatedConst::new(
68-
SolverDefId::ConstId(c),
69-
args,
70-
)),
71-
))
72-
}
73-
_ => None,
74-
}
75-
}
76-
7731
pub fn unknown_const<'db>(_ty: Ty<'db>) -> Const<'db> {
7832
Const::new(DbInterner::conjure(), rustc_type_ir::ConstKind::Error(ErrorGuaranteed))
7933
}
@@ -279,8 +233,14 @@ pub(crate) fn eval_to_const<'db>(expr: ExprId, ctx: &mut InferenceContext<'_, 'd
279233
return unknown_const(infer[expr]);
280234
}
281235
if let Expr::Path(p) = &ctx.body[expr] {
282-
let resolver = &ctx.resolver;
283-
if let Some(c) = path_to_const(ctx.db, resolver, p, || ctx.generics(), infer[expr]) {
236+
let mut ctx = TyLoweringContext::new(
237+
ctx.db,
238+
&ctx.resolver,
239+
ctx.body,
240+
ctx.generic_def,
241+
LifetimeElisionKind::Infer,
242+
);
243+
if let Some(c) = ctx.path_to_const(p) {
284244
return c;
285245
}
286246
}

crates/hir-ty/src/db.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use triomphe::Arc;
1717

1818
use crate::{
1919
Binders, ImplTraitId, ImplTraits, InferenceResult, TraitEnvironment, Ty, TyDefId, ValueTyDefId,
20-
chalk_db,
2120
consteval::ConstEvalError,
2221
dyn_compatibility::DynCompatibilityViolation,
2322
layout::{Layout, LayoutError},
@@ -308,19 +307,13 @@ pub trait HirDatabase: DefDatabase + std::fmt::Debug {
308307
#[salsa::interned]
309308
fn intern_coroutine(&self, id: InternedCoroutine) -> InternedCoroutineId;
310309

311-
#[salsa::invoke(chalk_db::fn_def_variance_query)]
312-
fn fn_def_variance(&self, fn_def_id: CallableDefId) -> chalk_db::Variances;
313-
314-
#[salsa::invoke(chalk_db::adt_variance_query)]
315-
fn adt_variance(&self, adt_id: AdtId) -> chalk_db::Variances;
316-
317310
#[salsa::invoke(crate::variance::variances_of)]
318311
#[salsa::cycle(
319312
// cycle_fn = crate::variance::variances_of_cycle_fn,
320313
// cycle_initial = crate::variance::variances_of_cycle_initial,
321314
cycle_result = crate::variance::variances_of_cycle_initial,
322315
)]
323-
fn variances_of(&self, def: GenericDefId) -> Option<Arc<[crate::variance::Variance]>>;
316+
fn variances_of(&self, def: GenericDefId) -> crate::next_solver::VariancesOf<'_>;
324317

325318
// next trait solver
326319

crates/hir-ty/src/display.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,9 +1062,9 @@ impl<'db> HirDisplay<'db> for Ty<'db> {
10621062
TyKind::Str => write!(f, "str")?,
10631063
TyKind::Bool => write!(f, "bool")?,
10641064
TyKind::Char => write!(f, "char")?,
1065-
TyKind::Float(t) => write!(f, "{}", primitive::float_ty_to_string_ns(t))?,
1066-
TyKind::Int(t) => write!(f, "{}", primitive::int_ty_to_string_ns(t))?,
1067-
TyKind::Uint(t) => write!(f, "{}", primitive::uint_ty_to_string_ns(t))?,
1065+
TyKind::Float(t) => write!(f, "{}", primitive::float_ty_to_string(t))?,
1066+
TyKind::Int(t) => write!(f, "{}", primitive::int_ty_to_string(t))?,
1067+
TyKind::Uint(t) => write!(f, "{}", primitive::uint_ty_to_string(t))?,
10681068
TyKind::Slice(t) => {
10691069
write!(f, "[")?;
10701070
t.hir_fmt(f)?;

crates/hir-ty/src/dyn_compatibility.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use hir_def::{
77
TypeAliasId, TypeOrConstParamId, TypeParamId, hir::generics::LocalTypeOrConstParamId,
88
lang_item::LangItem, signatures::TraitFlags,
99
};
10-
use intern::Symbol;
1110
use rustc_hash::FxHashSet;
1211
use rustc_type_ir::{
1312
AliasTyKind, ClauseKind, PredicatePolarity, TypeSuperVisitable as _, TypeVisitable as _,
@@ -441,8 +440,7 @@ fn receiver_is_dispatchable<'db>(
441440

442441
// Type `U`
443442
// FIXME: That seems problematic to fake a generic param like that?
444-
let unsized_self_ty =
445-
crate::next_solver::Ty::new_param(interner, self_param_id, u32::MAX, Symbol::empty());
443+
let unsized_self_ty = crate::next_solver::Ty::new_param(interner, self_param_id, u32::MAX);
446444
// `Receiver[Self => U]`
447445
let unsized_receiver_ty = receiver_for_self_ty(interner, func, receiver_ty, unsized_self_ty);
448446

@@ -454,8 +452,8 @@ fn receiver_is_dispatchable<'db>(
454452
TraitRef::new(interner, unsize_did.into(), [self_param_ty, unsized_self_ty]);
455453

456454
// U: Trait<Arg1, ..., ArgN>
457-
let args = GenericArgs::for_item(interner, trait_.into(), |name, index, kind, _| {
458-
if index == 0 { unsized_self_ty.into() } else { mk_param(interner, index, name, kind) }
455+
let args = GenericArgs::for_item(interner, trait_.into(), |index, kind, _| {
456+
if index == 0 { unsized_self_ty.into() } else { mk_param(interner, index, kind) }
459457
});
460458
let trait_predicate = TraitRef::new_from_args(interner, trait_.into(), args);
461459

@@ -494,8 +492,8 @@ fn receiver_for_self_ty<'db>(
494492
let args = crate::next_solver::GenericArgs::for_item(
495493
interner,
496494
SolverDefId::FunctionId(func),
497-
|name, index, kind, _| {
498-
if index == 0 { self_ty.into() } else { mk_param(interner, index, name, kind) }
495+
|index, kind, _| {
496+
if index == 0 { self_ty.into() } else { mk_param(interner, index, kind) }
499497
},
500498
);
501499

crates/hir-ty/src/generics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ impl Generics {
258258
}
259259

260260
/// Returns a Substitution that replaces each parameter by itself (i.e. `Ty::Param`).
261-
pub fn placeholder_subst(&self, db: &dyn HirDatabase) -> Substitution {
261+
pub(crate) fn placeholder_subst(&self, db: &dyn HirDatabase) -> Substitution {
262262
Substitution::from_iter(
263263
Interner,
264264
self.iter_id().enumerate().map(|(index, id)| match id {

crates/hir-ty/src/infer.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ use triomphe::Arc;
5757
use crate::{
5858
ImplTraitId, IncorrectGenericsLenKind, PathLoweringDiagnostic, TargetFeatures,
5959
db::{HirDatabase, InternedClosureId, InternedOpaqueTyId},
60-
generics::Generics,
6160
infer::{
6261
coerce::{CoerceMany, DynamicCoerceMany},
6362
diagnostics::{Diagnostics, InferenceTyLoweringContext as TyLoweringContext},
@@ -72,10 +71,7 @@ use crate::{
7271
Tys,
7372
abi::Safety,
7473
fold::fold_tys,
75-
infer::{
76-
DefineOpaqueTypes,
77-
traits::{Obligation, ObligationCause},
78-
},
74+
infer::traits::{Obligation, ObligationCause},
7975
mapping::ChalkToNextSolver,
8076
},
8177
traits::FnTrait,
@@ -763,8 +759,7 @@ pub(crate) struct InferenceContext<'body, 'db> {
763759
/// and resolve the path via its methods. This will ensure proper error reporting.
764760
pub(crate) resolver: Resolver<'db>,
765761
target_features: OnceCell<(TargetFeatures, TargetFeatureIsSafeInTarget)>,
766-
generic_def: GenericDefId,
767-
generics: OnceCell<Generics>,
762+
pub(crate) generic_def: GenericDefId,
768763
table: unify::InferenceTable<'db>,
769764
/// The traits in scope, disregarding block modules. This is used for caching purposes.
770765
traits_in_scope: FxHashSet<TraitId>,
@@ -873,7 +868,6 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
873868
return_ty: types.error, // set in collect_* calls
874869
types,
875870
target_features: OnceCell::new(),
876-
generics: OnceCell::new(),
877871
table,
878872
tuple_field_accesses_rev: Default::default(),
879873
resume_yield_tys: None,
@@ -902,10 +896,6 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
902896
}
903897
}
904898

905-
pub(crate) fn generics(&self) -> &Generics {
906-
self.generics.get_or_init(|| crate::generics::generics(self.db, self.generic_def))
907-
}
908-
909899
#[inline]
910900
fn krate(&self) -> Crate {
911901
self.resolver.krate()
@@ -1133,7 +1123,7 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
11331123
GenericArgs::for_item_with_defaults(
11341124
self.interner(),
11351125
va_list.into(),
1136-
|_, _, id, _| self.table.next_var_for_param(id),
1126+
|_, id, _| self.table.next_var_for_param(id),
11371127
),
11381128
),
11391129
None => self.err_ty(),
@@ -1676,7 +1666,7 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
16761666
.table
16771667
.infer_ctxt
16781668
.at(&ObligationCause::new(), self.table.trait_env.env)
1679-
.eq(DefineOpaqueTypes::Yes, expected, actual)
1669+
.eq(expected, actual)
16801670
.map(|infer_ok| self.table.register_infer_ok(infer_ok));
16811671
if let Err(_err) = result {
16821672
// FIXME: Emit diagnostic.

0 commit comments

Comments
 (0)