Skip to content

Commit 62bc753

Browse files
bors[bot]Veykril
andauthored
Merge #7732
7732: Don't lower TypeBound::Lifetime as GenericPredicate::Error r=flodiebold a=Veykril Basically we just discard the typebound for now instead when lowering to `GenericPredicate`. I think this shouldn't have any other side effects? Fixes #7683(hopefully for real this time) I also played around with introducing `GenericPredicate::LifetimeOutlives` and `GenericPredicate::TypeOutlives`(see Veykril@b9d6904) but that won't fix this issue(at least not for now) due to lifetime predicate mismatches when resolving methods so I figure this is a good way to fix it for now. Co-authored-by: Lukas Wirth <[email protected]>
2 parents d0a7871 + 0799288 commit 62bc753

File tree

6 files changed

+45
-37
lines changed

6 files changed

+45
-37
lines changed

crates/hir/src/code_model.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use hir_expand::{
2828
};
2929
use hir_ty::{
3030
autoderef,
31-
display::{write_bounds_like_dyn_trait, HirDisplayError, HirFormatter},
31+
display::{write_bounds_like_dyn_trait_with_prefix, HirDisplayError, HirFormatter},
3232
method_resolution,
3333
traits::{FnTrait, Solution, SolutionVariables},
3434
ApplicationTy, BoundVar, CallableDefId, Canonical, DebruijnIndex, FnSig, GenericPredicate,
@@ -1379,8 +1379,7 @@ impl HirDisplay for TypeParam {
13791379
let substs = Substs::type_params(f.db, self.id.parent);
13801380
let predicates = bounds.iter().cloned().map(|b| b.subst(&substs)).collect::<Vec<_>>();
13811381
if !(predicates.is_empty() || f.omit_verbose_types()) {
1382-
write!(f, ": ")?;
1383-
write_bounds_like_dyn_trait(&predicates, f)?;
1382+
write_bounds_like_dyn_trait_with_prefix(":", &predicates, f)?;
13841383
}
13851384
Ok(())
13861385
}

crates/hir_ty/src/display.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,7 @@ impl HirDisplay for ApplicationTy {
467467
.as_ref()
468468
.map(|rpit| rpit.impl_traits[idx as usize].bounds.clone());
469469
let bounds = data.subst(&self.parameters);
470-
write!(f, "impl ")?;
471-
write_bounds_like_dyn_trait(&bounds.value, f)?;
470+
write_bounds_like_dyn_trait_with_prefix("impl", &bounds.value, f)?;
472471
// FIXME: it would maybe be good to distinguish this from the alias type (when debug printing), and to show the substitution
473472
}
474473
OpaqueTyId::AsyncBlockTypeImplTrait(..) => {
@@ -548,10 +547,10 @@ impl HirDisplay for Ty {
548547
write!(f, "{}", param_data.name.clone().unwrap_or_else(Name::missing))?
549548
}
550549
TypeParamProvenance::ArgumentImplTrait => {
551-
write!(f, "impl ")?;
552550
let bounds = f.db.generic_predicates_for_param(*id);
553551
let substs = Substs::type_params_for_generics(&generics);
554-
write_bounds_like_dyn_trait(
552+
write_bounds_like_dyn_trait_with_prefix(
553+
"impl",
555554
&bounds.iter().map(|b| b.clone().subst(&substs)).collect::<Vec<_>>(),
556555
f,
557556
)?;
@@ -560,8 +559,7 @@ impl HirDisplay for Ty {
560559
}
561560
Ty::Bound(idx) => write!(f, "?{}.{}", idx.debruijn.depth(), idx.index)?,
562561
Ty::Dyn(predicates) => {
563-
write!(f, "dyn ")?;
564-
write_bounds_like_dyn_trait(predicates, f)?;
562+
write_bounds_like_dyn_trait_with_prefix("dyn", predicates, f)?;
565563
}
566564
Ty::Opaque(opaque_ty) => {
567565
match opaque_ty.opaque_ty_id {
@@ -572,8 +570,7 @@ impl HirDisplay for Ty {
572570
.as_ref()
573571
.map(|rpit| rpit.impl_traits[idx as usize].bounds.clone());
574572
let bounds = data.subst(&opaque_ty.parameters);
575-
write!(f, "impl ")?;
576-
write_bounds_like_dyn_trait(&bounds.value, f)?;
573+
write_bounds_like_dyn_trait_with_prefix("impl", &bounds.value, f)?;
577574
}
578575
OpaqueTyId::AsyncBlockTypeImplTrait(..) => {
579576
write!(f, "{{async block}}")?;
@@ -627,7 +624,21 @@ fn fn_traits(db: &dyn DefDatabase, trait_: TraitId) -> impl Iterator<Item = Trai
627624
ArrayVec::from(fn_traits).into_iter().flatten().flat_map(|it| it.as_trait())
628625
}
629626

630-
pub fn write_bounds_like_dyn_trait(
627+
pub fn write_bounds_like_dyn_trait_with_prefix(
628+
prefix: &str,
629+
predicates: &[GenericPredicate],
630+
f: &mut HirFormatter,
631+
) -> Result<(), HirDisplayError> {
632+
write!(f, "{}", prefix)?;
633+
if !predicates.is_empty() {
634+
write!(f, " ")?;
635+
write_bounds_like_dyn_trait(predicates, f)
636+
} else {
637+
Ok(())
638+
}
639+
}
640+
641+
fn write_bounds_like_dyn_trait(
631642
predicates: &[GenericPredicate],
632643
f: &mut HirFormatter,
633644
) -> Result<(), HirDisplayError> {

crates/hir_ty/src/lower.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -655,17 +655,6 @@ impl TraitRef {
655655
) -> Substs {
656656
substs_from_path_segment(ctx, segment, Some(resolved.into()), false)
657657
}
658-
659-
pub(crate) fn from_type_bound(
660-
ctx: &TyLoweringContext<'_>,
661-
bound: &TypeBound,
662-
self_ty: Ty,
663-
) -> Option<TraitRef> {
664-
match bound {
665-
TypeBound::Path(path) => TraitRef::from_path(ctx, path, Some(self_ty)),
666-
TypeBound::Lifetime(_) | TypeBound::Error => None,
667-
}
668-
}
669658
}
670659

671660
impl GenericPredicate {
@@ -705,13 +694,22 @@ impl GenericPredicate {
705694
bound: &'a TypeBound,
706695
self_ty: Ty,
707696
) -> impl Iterator<Item = GenericPredicate> + 'a {
708-
let trait_ref = TraitRef::from_type_bound(ctx, bound, self_ty);
709-
iter::once(trait_ref.clone().map_or(GenericPredicate::Error, GenericPredicate::Implemented))
710-
.chain(
711-
trait_ref
712-
.into_iter()
713-
.flat_map(move |tr| assoc_type_bindings_from_type_bound(ctx, bound, tr)),
714-
)
697+
let mut bindings = None;
698+
let trait_ref = match bound {
699+
TypeBound::Path(path) => {
700+
bindings = TraitRef::from_path(ctx, path, Some(self_ty));
701+
Some(
702+
bindings.clone().map_or(GenericPredicate::Error, GenericPredicate::Implemented),
703+
)
704+
}
705+
TypeBound::Lifetime(_) => None,
706+
TypeBound::Error => Some(GenericPredicate::Error),
707+
};
708+
trait_ref.into_iter().chain(
709+
bindings
710+
.into_iter()
711+
.flat_map(move |tr| assoc_type_bindings_from_type_bound(ctx, bound, tr)),
712+
)
715713
}
716714
}
717715

crates/hir_ty/src/tests/method_resolution.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,14 +1114,14 @@ fn method_on_dyn_impl() {
11141114
trait Foo {}
11151115
11161116
impl Foo for u32 {}
1117-
impl dyn Foo {
1117+
impl dyn Foo + '_ {
11181118
pub fn dyn_foo(&self) -> u32 {
11191119
0
11201120
}
11211121
}
11221122
11231123
fn main() {
1124-
let f = &42u32 as &dyn Foo<u32>;
1124+
let f = &42u32 as &dyn Foo;
11251125
f.dyn_foo();
11261126
// ^u32
11271127
}

crates/hir_ty/src/tests/traits.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,10 +1409,10 @@ fn weird_bounds() {
14091409
fn test(a: impl Trait + 'lifetime, b: impl 'lifetime, c: impl (Trait), d: impl ('lifetime), e: impl ?Sized, f: impl Trait + ?Sized) {}
14101410
"#,
14111411
expect![[r#"
1412-
23..24 'a': impl Trait + {error}
1413-
50..51 'b': impl {error}
1412+
23..24 'a': impl Trait
1413+
50..51 'b': impl
14141414
69..70 'c': impl Trait
1415-
86..87 'd': impl {error}
1415+
86..87 'd': impl
14161416
107..108 'e': impl {error}
14171417
123..124 'f': impl Trait + {error}
14181418
147..149 '{}': ()

crates/ide/src/hover.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3417,7 +3417,7 @@ impl<T> Foo<T$0> {}
34173417
```
34183418
"#]],
34193419
);
3420-
// lifetimes aren't being substituted yet
3420+
// lifetimes bounds arent being tracked yet
34213421
check(
34223422
r#"
34233423
struct Foo<T>(T);
@@ -3427,7 +3427,7 @@ impl<T: 'static> Foo<T$0> {}
34273427
*T*
34283428
34293429
```rust
3430-
T: {error}
3430+
T
34313431
```
34323432
"#]],
34333433
);

0 commit comments

Comments
 (0)