Skip to content

Commit cb468ae

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 53a4455 commit cb468ae

File tree

10 files changed

+795
-74
lines changed

10 files changed

+795
-74
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ tracing-subscriber = { version = "0.3.19", default-features = false, features =
160160
triomphe = { version = "0.1.14", default-features = false, features = ["std"] }
161161
url = "2.5.4"
162162
xshell = "0.2.7"
163+
thin-vec = "0.2.14"
163164

164165
# We need to freeze the version of the crate, as the raw-api feature is considered unstable
165166
dashmap = { version = "=6.1.0", features = ["raw-api", "inline"] }

crates/hir-def/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ mbe.workspace = true
4444
cfg.workspace = true
4545
tt.workspace = true
4646
span.workspace = true
47-
thin-vec = "0.2.14"
47+
thin-vec.workspace = true
4848

4949
[dev-dependencies]
5050
expect-test.workspace = true

crates/hir-ty/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ rustc_apfloat = "0.2.2"
3535
query-group.workspace = true
3636
salsa.workspace = true
3737
salsa-macros.workspace = true
38+
thin-vec.workspace = true
3839

3940
ra-ap-rustc_abi.workspace = true
4041
ra-ap-rustc_index.workspace = true

crates/hir-ty/src/chalk_db.rs

Lines changed: 401 additions & 18 deletions
Large diffs are not rendered by default.

crates/hir-ty/src/display.rs

Lines changed: 8 additions & 41 deletions
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,
@@ -49,6 +48,7 @@ use crate::{
4948
LifetimeData, LifetimeOutlives, MemoryMap, Mutability, OpaqueTy, ProjectionTy, ProjectionTyExt,
5049
QuantifiedWhereClause, Scalar, Substitution, TraitEnvironment, TraitRef, TraitRefExt, Ty,
5150
TyExt, WhereClause,
51+
chalk_db::inline_bound_to_generic_predicate,
5252
consteval::try_const_usize,
5353
db::{HirDatabase, InternedClosure},
5454
from_assoc_type_id, from_foreign_def_id, from_placeholder_idx,
@@ -688,7 +688,10 @@ impl HirDisplay for ProjectionTy {
688688
.iter()
689689
.map(|bound| {
690690
// We ignore `Self` anyway when formatting, so it's fine put an error type in it.
691-
inline_bound_to_generic_predicate(bound)
691+
inline_bound_to_generic_predicate(
692+
bound,
693+
TyKind::Error.intern(Interner),
694+
)
692695
})
693696
.collect::<Vec<_>>(),
694697
SizedByDefault::Sized {
@@ -701,42 +704,6 @@ impl HirDisplay for ProjectionTy {
701704
}
702705
}
703706

704-
/// Fills `Self` with an error type.
705-
pub(crate) fn inline_bound_to_generic_predicate(
706-
bound: &chalk_ir::Binders<rust_ir::InlineBound<Interner>>,
707-
) -> QuantifiedWhereClause {
708-
let (bound, binders) = bound.as_ref().into_value_and_skipped_binders();
709-
match bound {
710-
rust_ir::InlineBound::TraitBound(trait_bound) => {
711-
let trait_ref = TraitRef {
712-
trait_id: trait_bound.trait_id,
713-
substitution: Substitution::from_iter(
714-
Interner,
715-
iter::once(TyKind::Error.intern(Interner).cast(Interner))
716-
.chain(trait_bound.args_no_self.iter().cloned()),
717-
),
718-
};
719-
chalk_ir::Binders::new(binders, WhereClause::Implemented(trait_ref))
720-
}
721-
rust_ir::InlineBound::AliasEqBound(alias_eq) => {
722-
let substitution = Substitution::from_iter(
723-
Interner,
724-
iter::once(TyKind::Error.intern(Interner).cast(Interner))
725-
.chain(alias_eq.trait_bound.args_no_self.iter().cloned())
726-
.chain(alias_eq.parameters.iter().cloned()),
727-
);
728-
let alias = AliasEq {
729-
ty: alias_eq.value.clone(),
730-
alias: AliasTy::Projection(ProjectionTy {
731-
associated_ty_id: alias_eq.associated_ty_id,
732-
substitution,
733-
}),
734-
};
735-
chalk_ir::Binders::new(binders, WhereClause::AliasEq(alias))
736-
}
737-
}
738-
}
739-
740707
impl HirDisplay for OpaqueTy {
741708
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
742709
if f.should_truncate() {
@@ -1892,7 +1859,7 @@ pub fn write_bounds_like_dyn_trait_with_prefix(
18921859
}
18931860
}
18941861

1895-
fn write_bounds_like_dyn_trait(
1862+
pub(crate) fn write_bounds_like_dyn_trait(
18961863
f: &mut HirFormatter<'_>,
18971864
this: Either<&Ty, &Lifetime>,
18981865
predicates: &[QuantifiedWhereClause],

crates/hir-ty/src/generics.rs

Lines changed: 4 additions & 0 deletions
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)