Skip to content

Commit 7af2dd9

Browse files
authored
Merge pull request #20446 from lcnr/kinda-unhelpful-3
next-solver fun time
2 parents ac9d0f8 + 164d42c commit 7af2dd9

File tree

4 files changed

+88
-81
lines changed

4 files changed

+88
-81
lines changed

crates/hir-ty/src/layout.rs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ pub fn layout_of_ty_ns_query<'db>(
182182
};
183183
let dl = &*target;
184184
let cx = LayoutCx::new(dl);
185-
let infer_ctxt = interner.infer_ctxt().build(TypingMode::non_body_analysis());
185+
let infer_ctxt = interner.infer_ctxt().build(TypingMode::PostAnalysis);
186186
let cause = ObligationCause::dummy();
187187
let ty = deeply_normalize(infer_ctxt.at(&cause, ParamEnv::empty()), ty).unwrap_or(ty);
188188
let result = match ty.kind() {
@@ -335,27 +335,6 @@ pub fn layout_of_ty_ns_query<'db>(
335335
ptr.valid_range_mut().start = 1;
336336
Layout::scalar(dl, ptr)
337337
}
338-
TyKind::Alias(_, ty) => match ty.def_id {
339-
SolverDefId::TypeAliasId(_) => {
340-
return Err(LayoutError::HasPlaceholder);
341-
}
342-
SolverDefId::InternedOpaqueTyId(opaque) => {
343-
let impl_trait_id = db.lookup_intern_impl_trait_id(opaque);
344-
match impl_trait_id {
345-
crate::ImplTraitId::ReturnTypeImplTrait(func, idx) => {
346-
let infer = db.infer(func.into());
347-
return db.layout_of_ty(infer.type_of_rpit[idx].clone(), trait_env);
348-
}
349-
crate::ImplTraitId::TypeAliasImplTrait(..) => {
350-
return Err(LayoutError::NotImplemented);
351-
}
352-
crate::ImplTraitId::AsyncBlockTypeImplTrait(_, _) => {
353-
return Err(LayoutError::NotImplemented);
354-
}
355-
}
356-
}
357-
_ => unreachable!(),
358-
},
359338
TyKind::Closure(c, args) => {
360339
let id = match c {
361340
SolverDefId::InternedClosureId(id) => id,
@@ -389,7 +368,11 @@ pub fn layout_of_ty_ns_query<'db>(
389368
}
390369

391370
TyKind::Error(_) => return Err(LayoutError::HasErrorType),
392-
TyKind::Placeholder(_) | TyKind::Bound(..) | TyKind::Infer(..) | TyKind::Param(..) => {
371+
TyKind::Placeholder(_)
372+
| TyKind::Bound(..)
373+
| TyKind::Infer(..)
374+
| TyKind::Param(..)
375+
| TyKind::Alias(..) => {
393376
return Err(LayoutError::HasPlaceholder);
394377
}
395378
};

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

Lines changed: 65 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use rustc_type_ir::inherent::{
2222
use rustc_type_ir::lang_items::TraitSolverLangItem;
2323
use rustc_type_ir::solve::SizedTraitKind;
2424
use rustc_type_ir::{
25-
AliasTerm, AliasTermKind, AliasTy, EarlyBinder, FlagComputation, Flags, ImplPolarity, InferTy,
26-
ProjectionPredicate, TraitPredicate, TraitRef, Upcast,
25+
AliasTerm, AliasTermKind, AliasTy, AliasTyKind, EarlyBinder, FlagComputation, Flags,
26+
ImplPolarity, InferTy, ProjectionPredicate, TraitPredicate, TraitRef, Upcast,
2727
};
2828
use salsa::plumbing::AsId;
2929
use smallvec::{SmallVec, smallvec};
@@ -626,12 +626,7 @@ impl<'db> inherent::AdtDef<DbInterner<'db>> for AdtDef {
626626
fn struct_tail_ty(
627627
self,
628628
interner: DbInterner<'db>,
629-
) -> Option<
630-
rustc_type_ir::EarlyBinder<
631-
DbInterner<'db>,
632-
<DbInterner<'db> as rustc_type_ir::Interner>::Ty,
633-
>,
634-
> {
629+
) -> Option<EarlyBinder<DbInterner<'db>, Ty<'db>>> {
635630
let db = interner.db();
636631
let hir_def::AdtId::StructId(struct_id) = self.inner().id else {
637632
return None;
@@ -645,10 +640,7 @@ impl<'db> inherent::AdtDef<DbInterner<'db>> for AdtDef {
645640
fn all_field_tys(
646641
self,
647642
interner: DbInterner<'db>,
648-
) -> rustc_type_ir::EarlyBinder<
649-
DbInterner<'db>,
650-
impl IntoIterator<Item = <DbInterner<'db> as rustc_type_ir::Interner>::Ty>,
651-
> {
643+
) -> EarlyBinder<DbInterner<'db>, impl IntoIterator<Item = Ty<'db>>> {
652644
let db = interner.db();
653645
// FIXME: this is disabled just to match the behavior with chalk right now
654646
let field_tys = |id: VariantId| {
@@ -679,19 +671,14 @@ impl<'db> inherent::AdtDef<DbInterner<'db>> for AdtDef {
679671
.collect(),
680672
};
681673

682-
rustc_type_ir::EarlyBinder::bind(tys)
674+
EarlyBinder::bind(tys)
683675
}
684676

685677
fn sizedness_constraint(
686678
self,
687679
interner: DbInterner<'db>,
688680
sizedness: SizedTraitKind,
689-
) -> Option<
690-
rustc_type_ir::EarlyBinder<
691-
DbInterner<'db>,
692-
<DbInterner<'db> as rustc_type_ir::Interner>::Ty,
693-
>,
694-
> {
681+
) -> Option<EarlyBinder<DbInterner<'db>, Ty<'db>>> {
695682
if self.is_struct() {
696683
let tail_ty = self.all_field_tys(interner).skip_binder().into_iter().last()?;
697684

@@ -1024,8 +1011,8 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
10241011
false
10251012
}
10261013

1027-
fn expand_abstract_consts<T: rustc_type_ir::TypeFoldable<Self>>(self, t: T) -> T {
1028-
t
1014+
fn expand_abstract_consts<T: rustc_type_ir::TypeFoldable<Self>>(self, _: T) -> T {
1015+
unreachable!("only used by the old trait solver in rustc");
10291016
}
10301017

10311018
fn generics_of(self, def_id: Self::DefId) -> Self::GenericsOf {
@@ -1054,6 +1041,9 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
10541041
),
10551042
SolverDefId::InternedOpaqueTyId(_def_id) => {
10561043
// FIXME(next-solver): track variances
1044+
//
1045+
// We compute them based on the only `Ty` level info in rustc,
1046+
// move `variances_of_opaque` into `rustc_next_trait_solver` for reuse.
10571047
VariancesOf::new_from_iter(
10581048
self,
10591049
(0..self.generics_of(def_id).count()).map(|_| Variance::Invariant),
@@ -1063,7 +1053,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
10631053
}
10641054
}
10651055

1066-
fn type_of(self, def_id: Self::DefId) -> rustc_type_ir::EarlyBinder<Self, Self::Ty> {
1056+
fn type_of(self, def_id: Self::DefId) -> EarlyBinder<Self, Self::Ty> {
10671057
let def_id = match def_id {
10681058
SolverDefId::TypeAliasId(id) => {
10691059
use hir_def::Lookup;
@@ -1074,6 +1064,13 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
10741064
crate::TyDefId::TypeAliasId(id)
10751065
}
10761066
SolverDefId::AdtId(id) => crate::TyDefId::AdtId(id),
1067+
// FIXME(next-solver): This uses the types of `query mir_borrowck` in rustc.
1068+
//
1069+
// We currently always use the type from HIR typeck which ignores regions. This
1070+
// should be fine.
1071+
SolverDefId::InternedOpaqueTyId(_) => {
1072+
return self.type_of_opaque_hir_typeck(def_id);
1073+
}
10771074
_ => panic!("Unexpected def_id `{def_id:?}` provided for `type_of`"),
10781075
};
10791076
self.db().ty_ns(def_id)
@@ -1087,9 +1084,12 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
10871084
AdtDef::new(def_id, self)
10881085
}
10891086

1090-
fn alias_ty_kind(self, alias: rustc_type_ir::AliasTy<Self>) -> rustc_type_ir::AliasTyKind {
1091-
// FIXME: not currently creating any others
1092-
rustc_type_ir::AliasTyKind::Projection
1087+
fn alias_ty_kind(self, alias: rustc_type_ir::AliasTy<Self>) -> AliasTyKind {
1088+
match alias.def_id {
1089+
SolverDefId::InternedOpaqueTyId(_) => AliasTyKind::Opaque,
1090+
SolverDefId::TypeAliasId(_) => AliasTyKind::Projection,
1091+
_ => unimplemented!("Unexpected alias: {:?}", alias.def_id),
1092+
}
10931093
}
10941094

10951095
fn alias_term_kind(
@@ -1100,7 +1100,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
11001100
SolverDefId::InternedOpaqueTyId(_) => AliasTermKind::OpaqueTy,
11011101
SolverDefId::TypeAliasId(_) => AliasTermKind::ProjectionTy,
11021102
SolverDefId::ConstId(_) => AliasTermKind::UnevaluatedConst,
1103-
_ => unreachable!("Unexpected alias: {:?}", alias.def_id),
1103+
_ => unimplemented!("Unexpected alias: {:?}", alias.def_id),
11041104
}
11051105
}
11061106

@@ -1194,8 +1194,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
11941194
fn fn_sig(
11951195
self,
11961196
def_id: Self::DefId,
1197-
) -> rustc_type_ir::EarlyBinder<Self, rustc_type_ir::Binder<Self, rustc_type_ir::FnSig<Self>>>
1198-
{
1197+
) -> EarlyBinder<Self, rustc_type_ir::Binder<Self, rustc_type_ir::FnSig<Self>>> {
11991198
let id = match def_id {
12001199
SolverDefId::FunctionId(id) => CallableDefId::FunctionId(id),
12011200
SolverDefId::Ctor(ctor) => match ctor {
@@ -1248,7 +1247,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
12481247
fn item_bounds(
12491248
self,
12501249
def_id: Self::DefId,
1251-
) -> rustc_type_ir::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
1250+
) -> EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
12521251
explicit_item_bounds(self, def_id).map_bound(|bounds| {
12531252
Clauses::new_from_iter(self, elaborate(self, bounds).collect::<Vec<_>>())
12541253
})
@@ -1258,7 +1257,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
12581257
fn item_self_bounds(
12591258
self,
12601259
def_id: Self::DefId,
1261-
) -> rustc_type_ir::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
1260+
) -> EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
12621261
explicit_item_bounds(self, def_id).map_bound(|bounds| {
12631262
Clauses::new_from_iter(
12641263
self,
@@ -1270,7 +1269,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
12701269
fn item_non_self_bounds(
12711270
self,
12721271
def_id: Self::DefId,
1273-
) -> rustc_type_ir::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
1272+
) -> EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
12741273
let all_bounds: FxHashSet<_> = self.item_bounds(def_id).skip_binder().into_iter().collect();
12751274
let own_bounds: FxHashSet<_> =
12761275
self.item_self_bounds(def_id).skip_binder().into_iter().collect();
@@ -1288,7 +1287,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
12881287
fn predicates_of(
12891288
self,
12901289
def_id: Self::DefId,
1291-
) -> rustc_type_ir::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
1290+
) -> EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
12921291
let predicates = self.db().generic_predicates_ns(def_id.try_into().unwrap());
12931292
let predicates: Vec<_> = predicates.iter().cloned().collect();
12941293
EarlyBinder::bind(predicates.into_iter())
@@ -1298,7 +1297,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
12981297
fn own_predicates_of(
12991298
self,
13001299
def_id: Self::DefId,
1301-
) -> rustc_type_ir::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
1300+
) -> EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
13021301
let predicates = self.db().generic_predicates_without_parent_ns(def_id.try_into().unwrap());
13031302
let predicates: Vec<_> = predicates.iter().cloned().collect();
13041303
EarlyBinder::bind(predicates.into_iter())
@@ -1308,38 +1307,36 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
13081307
fn explicit_super_predicates_of(
13091308
self,
13101309
def_id: Self::DefId,
1311-
) -> rustc_type_ir::EarlyBinder<Self, impl IntoIterator<Item = (Self::Clause, Self::Span)>>
1312-
{
1310+
) -> EarlyBinder<Self, impl IntoIterator<Item = (Self::Clause, Self::Span)>> {
13131311
let predicates: Vec<(Clause<'db>, Span)> = self
13141312
.db()
13151313
.generic_predicates_ns(def_id.try_into().unwrap())
13161314
.iter()
13171315
.cloned()
13181316
.map(|p| (p, Span::dummy()))
13191317
.collect();
1320-
rustc_type_ir::EarlyBinder::bind(predicates)
1318+
EarlyBinder::bind(predicates)
13211319
}
13221320

13231321
#[tracing::instrument(skip(self), ret)]
13241322
fn explicit_implied_predicates_of(
13251323
self,
13261324
def_id: Self::DefId,
1327-
) -> rustc_type_ir::EarlyBinder<Self, impl IntoIterator<Item = (Self::Clause, Self::Span)>>
1328-
{
1325+
) -> EarlyBinder<Self, impl IntoIterator<Item = (Self::Clause, Self::Span)>> {
13291326
let predicates: Vec<(Clause<'db>, Span)> = self
13301327
.db()
13311328
.generic_predicates_ns(def_id.try_into().unwrap())
13321329
.iter()
13331330
.cloned()
13341331
.map(|p| (p, Span::dummy()))
13351332
.collect();
1336-
rustc_type_ir::EarlyBinder::bind(predicates)
1333+
EarlyBinder::bind(predicates)
13371334
}
13381335

13391336
fn impl_super_outlives(
13401337
self,
13411338
impl_def_id: Self::DefId,
1342-
) -> rustc_type_ir::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
1339+
) -> EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
13431340
let impl_id = match impl_def_id {
13441341
SolverDefId::ImplId(id) => id,
13451342
_ => unreachable!(),
@@ -1362,11 +1359,11 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
13621359
fn const_conditions(
13631360
self,
13641361
def_id: Self::DefId,
1365-
) -> rustc_type_ir::EarlyBinder<
1362+
) -> EarlyBinder<
13661363
Self,
13671364
impl IntoIterator<Item = rustc_type_ir::Binder<Self, rustc_type_ir::TraitRef<Self>>>,
13681365
> {
1369-
rustc_type_ir::EarlyBinder::bind([unimplemented!()])
1366+
EarlyBinder::bind([unimplemented!()])
13701367
}
13711368

13721369
fn has_target_features(self, def_id: Self::DefId) -> bool {
@@ -1740,7 +1737,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
17401737
}
17411738

17421739
fn has_item_definition(self, def_id: Self::DefId) -> bool {
1743-
// FIXME: should check if has value
1740+
// FIXME(next-solver): should check if the associated item has a value.
17441741
true
17451742
}
17461743

@@ -1753,7 +1750,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
17531750
fn impl_trait_ref(
17541751
self,
17551752
impl_def_id: Self::DefId,
1756-
) -> rustc_type_ir::EarlyBinder<Self, rustc_type_ir::TraitRef<Self>> {
1753+
) -> EarlyBinder<Self, rustc_type_ir::TraitRef<Self>> {
17571754
let impl_id = match impl_def_id {
17581755
SolverDefId::ImplId(id) => id,
17591756
_ => panic!("Unexpected SolverDefId in impl_trait_ref"),
@@ -1815,7 +1812,8 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
18151812
}
18161813

18171814
fn trait_may_be_implemented_via_object(self, trait_def_id: Self::DefId) -> bool {
1818-
// FIXME(next-solver)
1815+
// FIXME(next-solver): should check the `TraitFlags` for
1816+
// the `#[rustc_do_not_implement_via_object]` flag
18191817
true
18201818
}
18211819

@@ -1954,12 +1952,12 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
19541952
fn explicit_implied_const_bounds(
19551953
self,
19561954
def_id: Self::DefId,
1957-
) -> rustc_type_ir::EarlyBinder<
1955+
) -> EarlyBinder<
19581956
Self,
19591957
impl IntoIterator<Item = rustc_type_ir::Binder<Self, rustc_type_ir::TraitRef<Self>>>,
19601958
> {
19611959
// FIXME(next-solver)
1962-
rustc_type_ir::EarlyBinder::bind([])
1960+
EarlyBinder::bind([])
19631961
}
19641962

19651963
fn fn_is_const(self, def_id: Self::DefId) -> bool {
@@ -1982,21 +1980,31 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
19821980
None
19831981
}
19841982

1985-
fn type_of_opaque_hir_typeck(
1986-
self,
1987-
def_id: Self::LocalDefId,
1988-
) -> rustc_type_ir::EarlyBinder<Self, Self::Ty> {
1989-
// FIXME(next-solver)
1990-
unimplemented!()
1983+
fn type_of_opaque_hir_typeck(self, def_id: Self::LocalDefId) -> EarlyBinder<Self, Self::Ty> {
1984+
match def_id {
1985+
SolverDefId::InternedOpaqueTyId(opaque) => {
1986+
let impl_trait_id = self.db().lookup_intern_impl_trait_id(opaque);
1987+
match impl_trait_id {
1988+
crate::ImplTraitId::ReturnTypeImplTrait(func, idx) => {
1989+
let infer = self.db().infer(func.into());
1990+
EarlyBinder::bind(infer.type_of_rpit[idx].to_nextsolver(self))
1991+
}
1992+
crate::ImplTraitId::TypeAliasImplTrait(..)
1993+
| crate::ImplTraitId::AsyncBlockTypeImplTrait(_, _) => {
1994+
// FIXME(next-solver)
1995+
EarlyBinder::bind(Ty::new_error(self, ErrorGuaranteed))
1996+
}
1997+
}
1998+
}
1999+
_ => panic!("Unexpected SolverDefId in type_of_opaque_hir_typeck"),
2000+
}
19912001
}
19922002

19932003
fn coroutine_hidden_types(
19942004
self,
19952005
def_id: Self::DefId,
1996-
) -> rustc_type_ir::EarlyBinder<
1997-
Self,
1998-
rustc_type_ir::Binder<Self, rustc_type_ir::CoroutineWitnessTypes<Self>>,
1999-
> {
2006+
) -> EarlyBinder<Self, rustc_type_ir::Binder<Self, rustc_type_ir::CoroutineWitnessTypes<Self>>>
2007+
{
20002008
// FIXME(next-solver)
20012009
unimplemented!()
20022010
}

crates/hir-ty/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ mod incremental;
66
mod macros;
77
mod method_resolution;
88
mod never_type;
9+
mod opaque_types;
910
mod patterns;
1011
mod regression;
1112
mod simple;
1213
mod trait_aliases;
1314
mod traits;
14-
mod type_alias_impl_traits;
1515

1616
use base_db::{Crate, SourceDatabase};
1717
use expect_test::Expect;

0 commit comments

Comments
 (0)