Skip to content

Commit e9ecff2

Browse files
Lower Return Position Impl Trait In Traits correctly
Instead of lowering them as opaque type, in the trait they should get lowered into a synthesized associated type. Opaques "mostly worked" here, but they break when trying to implement Return Type Notation, which is essentially a way to refer in code to this synthesized associated type. So we need to do the correct thing.
1 parent 9a281f2 commit e9ecff2

File tree

7 files changed

+824
-78
lines changed

7 files changed

+824
-78
lines changed

crates/hir-ty/src/chalk_db.rs

+397-18
Large diffs are not rendered by default.

crates/hir-ty/src/display.rs

+8-44
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
55
use std::{
66
fmt::{self, Debug},
7-
iter, mem,
7+
mem,
88
};
99

1010
use base_db::Crate;
11-
use chalk_ir::{BoundVar, Safety, TyKind, cast::Cast};
12-
use chalk_solve::rust_ir;
11+
use chalk_ir::{BoundVar, Safety, TyKind};
1312
use either::Either;
1413
use hir_def::{
1514
GenericDefId, HasModule, ImportPathConfig, ItemContainerId, LocalFieldId, Lookup, ModuleDefId,
@@ -47,6 +46,7 @@ use crate::{
4746
LifetimeData, LifetimeOutlives, MemoryMap, Mutability, OpaqueTy, ProjectionTy, ProjectionTyExt,
4847
QuantifiedWhereClause, Scalar, Substitution, TraitEnvironment, TraitRef, TraitRefExt, Ty,
4948
TyExt, WhereClause,
49+
chalk_db::inline_bound_to_generic_predicate,
5050
consteval::try_const_usize,
5151
db::{HirDatabase, InternedClosure},
5252
from_assoc_type_id, from_foreign_def_id, from_placeholder_idx,
@@ -643,7 +643,10 @@ impl HirDisplay for ProjectionTy {
643643
.iter()
644644
.map(|bound| {
645645
// We ignore `Self` anyway when formatting, so it's fine put an error type in it.
646-
inline_bound_to_generic_predicate(bound)
646+
inline_bound_to_generic_predicate(
647+
bound,
648+
TyKind::Error.intern(Interner),
649+
)
647650
})
648651
.collect::<Vec<_>>(),
649652
SizedByDefault::Sized {
@@ -656,45 +659,6 @@ impl HirDisplay for ProjectionTy {
656659
}
657660
}
658661

659-
/// Fills `Self` with an error type.
660-
pub(crate) fn inline_bound_to_generic_predicate(
661-
bound: &chalk_ir::Binders<rust_ir::InlineBound<Interner>>,
662-
) -> QuantifiedWhereClause {
663-
let (bound, binders) = bound.as_ref().into_value_and_skipped_binders();
664-
match bound {
665-
rust_ir::InlineBound::TraitBound(trait_bound) => {
666-
let trait_ref = TraitRef {
667-
trait_id: trait_bound.trait_id,
668-
substitution: Substitution::from_iter(
669-
Interner,
670-
iter::once(TyKind::Error.intern(Interner).cast(Interner))
671-
.chain(trait_bound.args_no_self.iter().cloned()),
672-
),
673-
};
674-
chalk_ir::Binders::new(binders, WhereClause::Implemented(trait_ref))
675-
}
676-
rust_ir::InlineBound::AliasEqBound(alias_eq) => {
677-
let substitution = Substitution::from_iter(
678-
Interner,
679-
alias_eq
680-
.parameters
681-
.iter()
682-
.cloned()
683-
.chain(iter::once(TyKind::Error.intern(Interner).cast(Interner)))
684-
.chain(alias_eq.trait_bound.args_no_self.iter().cloned()),
685-
);
686-
let alias = AliasEq {
687-
ty: alias_eq.value.clone(),
688-
alias: AliasTy::Projection(ProjectionTy {
689-
associated_ty_id: alias_eq.associated_ty_id,
690-
substitution,
691-
}),
692-
};
693-
chalk_ir::Binders::new(binders, WhereClause::AliasEq(alias))
694-
}
695-
}
696-
}
697-
698662
impl HirDisplay for OpaqueTy {
699663
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
700664
if f.should_truncate() {
@@ -1845,7 +1809,7 @@ pub fn write_bounds_like_dyn_trait_with_prefix(
18451809
}
18461810
}
18471811

1848-
fn write_bounds_like_dyn_trait(
1812+
pub(crate) fn write_bounds_like_dyn_trait(
18491813
f: &mut HirFormatter<'_>,
18501814
this: Either<&Ty, &Lifetime>,
18511815
predicates: &[QuantifiedWhereClause],

crates/hir-ty/src/generics.rs

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ where
5151
}
5252

5353
impl Generics {
54+
pub(crate) fn self_params(&self) -> &GenericParams {
55+
&self.params
56+
}
57+
5458
pub(crate) fn def(&self) -> GenericDefId {
5559
self.def
5660
}

0 commit comments

Comments
 (0)