Skip to content

Commit 70ec05e

Browse files
committed
implement type_of_opaque
1 parent f228c58 commit 70ec05e

File tree

4 files changed

+69
-72
lines changed

4 files changed

+69
-72
lines changed

crates/hir-ty/src/layout.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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,7 @@ 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(_) | TyKind::Bound(..) | TyKind::Infer(..) | TyKind::Param(..) | TyKind::Alias(..) => {
393372
return Err(LayoutError::HasPlaceholder);
394373
}
395374
};

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

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -626,12 +626,8 @@ 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>, <DbInterner<'db> as rustc_type_ir::Interner>::Ty>>
630+
{
635631
let db = interner.db();
636632
let hir_def::AdtId::StructId(struct_id) = self.inner().id else {
637633
return None;
@@ -645,7 +641,7 @@ impl<'db> inherent::AdtDef<DbInterner<'db>> for AdtDef {
645641
fn all_field_tys(
646642
self,
647643
interner: DbInterner<'db>,
648-
) -> rustc_type_ir::EarlyBinder<
644+
) -> EarlyBinder<
649645
DbInterner<'db>,
650646
impl IntoIterator<Item = <DbInterner<'db> as rustc_type_ir::Interner>::Ty>,
651647
> {
@@ -679,19 +675,15 @@ impl<'db> inherent::AdtDef<DbInterner<'db>> for AdtDef {
679675
.collect(),
680676
};
681677

682-
rustc_type_ir::EarlyBinder::bind(tys)
678+
EarlyBinder::bind(tys)
683679
}
684680

685681
fn sizedness_constraint(
686682
self,
687683
interner: DbInterner<'db>,
688684
sizedness: SizedTraitKind,
689-
) -> Option<
690-
rustc_type_ir::EarlyBinder<
691-
DbInterner<'db>,
692-
<DbInterner<'db> as rustc_type_ir::Interner>::Ty,
693-
>,
694-
> {
685+
) -> Option<EarlyBinder<DbInterner<'db>, <DbInterner<'db> as rustc_type_ir::Interner>::Ty>>
686+
{
695687
if self.is_struct() {
696688
let tail_ty = self.all_field_tys(interner).skip_binder().into_iter().last()?;
697689

@@ -1066,7 +1058,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
10661058
}
10671059
}
10681060

1069-
fn type_of(self, def_id: Self::DefId) -> rustc_type_ir::EarlyBinder<Self, Self::Ty> {
1061+
fn type_of(self, def_id: Self::DefId) -> EarlyBinder<Self, Self::Ty> {
10701062
let def_id = match def_id {
10711063
SolverDefId::TypeAliasId(id) => {
10721064
use hir_def::Lookup;
@@ -1077,9 +1069,13 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
10771069
crate::TyDefId::TypeAliasId(id)
10781070
}
10791071
SolverDefId::AdtId(id) => crate::TyDefId::AdtId(id),
1080-
// FIXME(next-solver): need to support opaque types. This uses the types of
1081-
// `query mir_borrowck` in rustc. If we're ignoring regions, we could simply
1082-
// use the type inferred by general type inference here.
1072+
// FIXME(next-solver): This uses the types of `query mir_borrowck` in rustc.
1073+
//
1074+
// We currently always use the type from HIR typeck which ignores regions. This
1075+
// should be fine.
1076+
SolverDefId::InternedOpaqueTyId(_) => {
1077+
return self.type_of_opaque_hir_typeck(def_id);
1078+
}
10831079
_ => panic!("Unexpected def_id `{def_id:?}` provided for `type_of`"),
10841080
};
10851081
self.db().ty_ns(def_id)
@@ -1109,7 +1105,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
11091105
SolverDefId::InternedOpaqueTyId(_) => AliasTermKind::OpaqueTy,
11101106
SolverDefId::TypeAliasId(_) => AliasTermKind::ProjectionTy,
11111107
SolverDefId::ConstId(_) => AliasTermKind::UnevaluatedConst,
1112-
_ => todo!("Unexpected alias: {:?}", alias.def_id),
1108+
_ => unimplemented!("Unexpected alias: {:?}", alias.def_id),
11131109
}
11141110
}
11151111

@@ -1204,8 +1200,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
12041200
fn fn_sig(
12051201
self,
12061202
def_id: Self::DefId,
1207-
) -> rustc_type_ir::EarlyBinder<Self, rustc_type_ir::Binder<Self, rustc_type_ir::FnSig<Self>>>
1208-
{
1203+
) -> EarlyBinder<Self, rustc_type_ir::Binder<Self, rustc_type_ir::FnSig<Self>>> {
12091204
let id = match def_id {
12101205
SolverDefId::FunctionId(id) => CallableDefId::FunctionId(id),
12111206
SolverDefId::Ctor(ctor) => match ctor {
@@ -1258,7 +1253,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
12581253
fn item_bounds(
12591254
self,
12601255
def_id: Self::DefId,
1261-
) -> rustc_type_ir::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
1256+
) -> EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
12621257
explicit_item_bounds(self, def_id).map_bound(|bounds| {
12631258
Clauses::new_from_iter(self, elaborate(self, bounds).collect::<Vec<_>>())
12641259
})
@@ -1268,7 +1263,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
12681263
fn item_self_bounds(
12691264
self,
12701265
def_id: Self::DefId,
1271-
) -> rustc_type_ir::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
1266+
) -> EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
12721267
explicit_item_bounds(self, def_id).map_bound(|bounds| {
12731268
Clauses::new_from_iter(
12741269
self,
@@ -1280,7 +1275,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
12801275
fn item_non_self_bounds(
12811276
self,
12821277
def_id: Self::DefId,
1283-
) -> rustc_type_ir::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
1278+
) -> EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
12841279
let all_bounds: FxHashSet<_> = self.item_bounds(def_id).skip_binder().into_iter().collect();
12851280
let own_bounds: FxHashSet<_> =
12861281
self.item_self_bounds(def_id).skip_binder().into_iter().collect();
@@ -1298,7 +1293,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
12981293
fn predicates_of(
12991294
self,
13001295
def_id: Self::DefId,
1301-
) -> rustc_type_ir::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
1296+
) -> EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
13021297
let predicates = self.db().generic_predicates_ns(def_id.try_into().unwrap());
13031298
let predicates: Vec<_> = predicates.iter().cloned().collect();
13041299
EarlyBinder::bind(predicates.into_iter())
@@ -1308,7 +1303,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
13081303
fn own_predicates_of(
13091304
self,
13101305
def_id: Self::DefId,
1311-
) -> rustc_type_ir::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
1306+
) -> EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
13121307
let predicates = self.db().generic_predicates_without_parent_ns(def_id.try_into().unwrap());
13131308
let predicates: Vec<_> = predicates.iter().cloned().collect();
13141309
EarlyBinder::bind(predicates.into_iter())
@@ -1318,38 +1313,36 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
13181313
fn explicit_super_predicates_of(
13191314
self,
13201315
def_id: Self::DefId,
1321-
) -> rustc_type_ir::EarlyBinder<Self, impl IntoIterator<Item = (Self::Clause, Self::Span)>>
1322-
{
1316+
) -> EarlyBinder<Self, impl IntoIterator<Item = (Self::Clause, Self::Span)>> {
13231317
let predicates: Vec<(Clause<'db>, Span)> = self
13241318
.db()
13251319
.generic_predicates_ns(def_id.try_into().unwrap())
13261320
.iter()
13271321
.cloned()
13281322
.map(|p| (p, Span::dummy()))
13291323
.collect();
1330-
rustc_type_ir::EarlyBinder::bind(predicates)
1324+
EarlyBinder::bind(predicates)
13311325
}
13321326

13331327
#[tracing::instrument(skip(self), ret)]
13341328
fn explicit_implied_predicates_of(
13351329
self,
13361330
def_id: Self::DefId,
1337-
) -> rustc_type_ir::EarlyBinder<Self, impl IntoIterator<Item = (Self::Clause, Self::Span)>>
1338-
{
1331+
) -> EarlyBinder<Self, impl IntoIterator<Item = (Self::Clause, Self::Span)>> {
13391332
let predicates: Vec<(Clause<'db>, Span)> = self
13401333
.db()
13411334
.generic_predicates_ns(def_id.try_into().unwrap())
13421335
.iter()
13431336
.cloned()
13441337
.map(|p| (p, Span::dummy()))
13451338
.collect();
1346-
rustc_type_ir::EarlyBinder::bind(predicates)
1339+
EarlyBinder::bind(predicates)
13471340
}
13481341

13491342
fn impl_super_outlives(
13501343
self,
13511344
impl_def_id: Self::DefId,
1352-
) -> rustc_type_ir::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
1345+
) -> EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>> {
13531346
let impl_id = match impl_def_id {
13541347
SolverDefId::ImplId(id) => id,
13551348
_ => unreachable!(),
@@ -1372,11 +1365,11 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
13721365
fn const_conditions(
13731366
self,
13741367
def_id: Self::DefId,
1375-
) -> rustc_type_ir::EarlyBinder<
1368+
) -> EarlyBinder<
13761369
Self,
13771370
impl IntoIterator<Item = rustc_type_ir::Binder<Self, rustc_type_ir::TraitRef<Self>>>,
13781371
> {
1379-
rustc_type_ir::EarlyBinder::bind([unimplemented!()])
1372+
EarlyBinder::bind([unimplemented!()])
13801373
}
13811374

13821375
fn has_target_features(self, def_id: Self::DefId) -> bool {
@@ -1763,7 +1756,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
17631756
fn impl_trait_ref(
17641757
self,
17651758
impl_def_id: Self::DefId,
1766-
) -> rustc_type_ir::EarlyBinder<Self, rustc_type_ir::TraitRef<Self>> {
1759+
) -> EarlyBinder<Self, rustc_type_ir::TraitRef<Self>> {
17671760
let impl_id = match impl_def_id {
17681761
SolverDefId::ImplId(id) => id,
17691762
_ => panic!("Unexpected SolverDefId in impl_trait_ref"),
@@ -1960,12 +1953,12 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
19601953
fn explicit_implied_const_bounds(
19611954
self,
19621955
def_id: Self::DefId,
1963-
) -> rustc_type_ir::EarlyBinder<
1956+
) -> EarlyBinder<
19641957
Self,
19651958
impl IntoIterator<Item = rustc_type_ir::Binder<Self, rustc_type_ir::TraitRef<Self>>>,
19661959
> {
19671960
// FIXME(next-solver)
1968-
rustc_type_ir::EarlyBinder::bind([])
1961+
EarlyBinder::bind([])
19691962
}
19701963

19711964
fn fn_is_const(self, def_id: Self::DefId) -> bool {
@@ -1988,22 +1981,31 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
19881981
None
19891982
}
19901983

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

20002004
fn coroutine_hidden_types(
20012005
self,
20022006
def_id: Self::DefId,
2003-
) -> rustc_type_ir::EarlyBinder<
2004-
Self,
2005-
rustc_type_ir::Binder<Self, rustc_type_ir::CoroutineWitnessTypes<Self>>,
2006-
> {
2007+
) -> EarlyBinder<Self, rustc_type_ir::Binder<Self, rustc_type_ir::CoroutineWitnessTypes<Self>>>
2008+
{
20072009
// FIXME(next-solver)
20082010
unimplemented!()
20092011
}

crates/hir-ty/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ 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 traits;
13-
mod type_alias_impl_traits;
1414

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

crates/hir-ty/src/tests/type_alias_impl_traits.rs renamed to crates/hir-ty/src/tests/opaque_types.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,19 @@ static ALIAS: i32 = {
159159
"#]],
160160
)
161161
}
162+
163+
#[test]
164+
fn leak_auto_traits() {
165+
check_no_mismatches(
166+
r#"
167+
//- minicore: send
168+
fn foo() -> impl Sized {}
169+
170+
fn is_send<T: Send>(_: T) {}
171+
172+
fn main() {
173+
is_send(foo());
174+
}
175+
"#,
176+
);
177+
}

0 commit comments

Comments
 (0)