Skip to content

Commit f16fddc

Browse files
committed
hir_analysis: add missing sizedness bounds
Default sizedness bounds were not being added to `explicit_super_predicates_of` and `explicit_implied_predicates_of` which meant that a trait bound added to a associated type projection would be missing the implied predicate of the default sizedness supertrait of that trait. An unexpected consequence of this change was that the check for multiple principals was now finding an additional `MetaSized` principal when eagerly expanding trait aliases. Instead of special-casing trait aliases as different from traits and not adding a `MetaSized` supertrait to trait aliases, filter out `MetaSized` when lowering `dyn Trait`.
1 parent 076a0a2 commit f16fddc

File tree

11 files changed

+64
-81
lines changed

11 files changed

+64
-81
lines changed

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,14 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
660660
| PredicateFilter::SelfOnly
661661
| PredicateFilter::SelfTraitThatDefines(_)
662662
| PredicateFilter::SelfAndAssociatedTypeBounds => {
663+
icx.lowerer().add_sizedness_bounds(
664+
&mut bounds,
665+
self_param_ty,
666+
superbounds,
667+
None,
668+
Some(trait_def_id),
669+
item.span,
670+
);
663671
icx.lowerer().add_default_super_traits(
664672
trait_def_id,
665673
&mut bounds,

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
6464

6565
self.add_default_traits(&mut user_written_bounds, dummy_self, &ast_bounds, None, span);
6666

67-
let (elaborated_trait_bounds, elaborated_projection_bounds) =
67+
let (mut elaborated_trait_bounds, elaborated_projection_bounds) =
6868
traits::expand_trait_aliases(tcx, user_written_bounds.iter().copied());
69+
70+
// FIXME(sized-hierarchy): https://github.com/rust-lang/rust/pull/142712#issuecomment-3013231794
71+
let meta_sized_did = tcx.require_lang_item(LangItem::MetaSized, span);
72+
elaborated_trait_bounds.retain(|(pred, _)| pred.def_id() != meta_sized_did);
73+
6974
let (regular_traits, mut auto_traits): (Vec<_>, Vec<_>) = elaborated_trait_bounds
7075
.into_iter()
7176
.partition(|(trait_ref, _)| !tcx.trait_is_auto(trait_ref.def_id()));
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ check-pass
2+
#![crate_type = "lib"]
3+
#![feature(sized_hierarchy)]
4+
5+
trait FalseDeref {
6+
type Target: std::marker::PointeeSized;
7+
}
8+
9+
trait Bar {}
10+
11+
fn foo<T: FalseDeref>()
12+
where
13+
T::Target: Bar,
14+
{
15+
}

tests/ui/sized-hierarchy/default-supertrait.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,11 @@ fn with_pointeesized_supertrait<T: PointeeSized + PointeeSized_>() {
4747
requires_pointeesized::<T>();
4848
}
4949

50-
// `T` won't inherit the `const MetaSized` implicit supertrait of `Bare`, so there is an error on
51-
// the bound, which is expected.
50+
// `T` inherits the `const MetaSized` implicit supertrait of `Bare`.
5251
fn with_bare_trait<T: PointeeSized + Bare>() {
53-
//~^ ERROR the size for values of type `T` cannot be known
5452
requires_sized::<T>();
5553
//~^ ERROR the size for values of type `T` cannot be known
5654
requires_metasized::<T>();
57-
//~^ ERROR the size for values of type `T` cannot be known
5855
requires_pointeesized::<T>();
5956
}
6057

tests/ui/sized-hierarchy/default-supertrait.stderr

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,6 @@ LL | trait NegPointeeSized: ?PointeeSized { }
2828
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
2929
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
3030

31-
error[E0277]: the size for values of type `T` cannot be known
32-
--> $DIR/default-supertrait.rs:52:38
33-
|
34-
LL | fn with_bare_trait<T: PointeeSized + Bare>() {
35-
| ^^^^ doesn't have a known size
36-
|
37-
note: required by a bound in `Bare`
38-
--> $DIR/default-supertrait.rs:22:1
39-
|
40-
LL | trait Bare {}
41-
| ^^^^^^^^^^^^^ required by this bound in `Bare`
42-
help: consider further restricting type parameter `T` with unstable trait `MetaSized`
43-
|
44-
LL | fn with_bare_trait<T: PointeeSized + Bare + std::marker::MetaSized>() {
45-
| ++++++++++++++++++++++++
46-
4731
error[E0277]: the size for values of type `T` cannot be known at compilation time
4832
--> $DIR/default-supertrait.rs:35:22
4933
|
@@ -89,11 +73,10 @@ LL | fn with_pointeesized_supertrait<T: PointeeSized + PointeeSized_ + std::mark
8973
| ++++++++++++++++++++++++
9074

9175
error[E0277]: the size for values of type `T` cannot be known at compilation time
92-
--> $DIR/default-supertrait.rs:54:22
76+
--> $DIR/default-supertrait.rs:52:22
9377
|
9478
LL | fn with_bare_trait<T: PointeeSized + Bare>() {
9579
| - this type parameter needs to be `Sized`
96-
LL |
9780
LL | requires_sized::<T>();
9881
| ^ doesn't have a size known at compile-time
9982
|
@@ -103,23 +86,7 @@ note: required by a bound in `requires_sized`
10386
LL | fn requires_sized<T: Sized>() {}
10487
| ^^^^^ required by this bound in `requires_sized`
10588

106-
error[E0277]: the size for values of type `T` cannot be known
107-
--> $DIR/default-supertrait.rs:56:26
108-
|
109-
LL | requires_metasized::<T>();
110-
| ^ doesn't have a known size
111-
|
112-
note: required by a bound in `requires_metasized`
113-
--> $DIR/default-supertrait.rs:25:26
114-
|
115-
LL | fn requires_metasized<T: MetaSized>() {}
116-
| ^^^^^^^^^ required by this bound in `requires_metasized`
117-
help: consider further restricting type parameter `T` with unstable trait `MetaSized`
118-
|
119-
LL | fn with_bare_trait<T: PointeeSized + Bare + std::marker::MetaSized>() {
120-
| ++++++++++++++++++++++++
121-
122-
error: aborting due to 9 previous errors
89+
error: aborting due to 7 previous errors
12390

12491
Some errors have detailed explanations: E0277, E0658.
12592
For more information about an error, try `rustc --explain E0277`.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//@ check-pass
2+
//@ compile-flags: --crate-type=lib
3+
#![feature(sized_hierarchy)]
4+
5+
trait Trait {}
6+
7+
fn f<T: Trait + std::marker::PointeeSized>() {}

tests/ui/traits/next-solver/normalize/normalize-param-env-2.stderr

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,6 @@ error[E0275]: overflow evaluating the requirement `<() as A<T>>::Assoc: A<T>`
1919
LL | Self::Assoc: A<T>,
2020
| ^^^^
2121

22-
error[E0275]: overflow evaluating the requirement `<() as A<T>>::Assoc: MetaSized`
23-
--> $DIR/normalize-param-env-2.rs:24:22
24-
|
25-
LL | Self::Assoc: A<T>,
26-
| ^^^^
27-
|
28-
note: required by a bound in `A`
29-
--> $DIR/normalize-param-env-2.rs:9:1
30-
|
31-
LL | / trait A<T> {
32-
LL | | type Assoc;
33-
LL | |
34-
LL | | fn f()
35-
... |
36-
LL | | }
37-
| |_^ required by this bound in `A`
38-
3922
error[E0275]: overflow evaluating the requirement `<() as A<T>>::Assoc well-formed`
4023
--> $DIR/normalize-param-env-2.rs:24:22
4124
|
@@ -63,6 +46,6 @@ LL | where
6346
LL | Self::Assoc: A<T>,
6447
| ^^^^ required by this bound in `A::f`
6548

66-
error: aborting due to 6 previous errors
49+
error: aborting due to 5 previous errors
6750

6851
For more information about this error, try `rustc --explain E0275`.

tests/ui/traits/next-solver/normalize/normalize-param-env-4.next.stderr

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,6 @@ error[E0275]: overflow evaluating the requirement `<T as Trait>::Assoc: Trait`
44
LL | <T as Trait>::Assoc: Trait,
55
| ^^^^^
66

7-
error[E0275]: overflow evaluating the requirement `<T as Trait>::Assoc: MetaSized`
8-
--> $DIR/normalize-param-env-4.rs:19:26
9-
|
10-
LL | <T as Trait>::Assoc: Trait,
11-
| ^^^^^
12-
|
13-
note: required by a bound in `Trait`
14-
--> $DIR/normalize-param-env-4.rs:7:1
15-
|
16-
LL | / trait Trait {
17-
LL | | type Assoc;
18-
LL | | }
19-
| |_^ required by this bound in `Trait`
20-
21-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
228

239
For more information about this error, try `rustc --explain E0275`.
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: the feature `non_lifetime_binders` is incomplete and may not be safe to use and/or cause compiler crashes
2-
--> $DIR/drop-impl-pred.rs:6:12
2+
--> $DIR/drop-impl-pred.rs:5:12
33
|
44
LL | #![feature(non_lifetime_binders)]
55
| ^^^^^^^^^^^^^^^^^^^^
@@ -8,17 +8,24 @@ LL | #![feature(non_lifetime_binders)]
88
= note: `#[warn(incomplete_features)]` on by default
99

1010
error[E0367]: `Drop` impl requires `H: Foo` but the struct it is implemented for does not
11-
--> $DIR/drop-impl-pred.rs:19:15
11+
--> $DIR/drop-impl-pred.rs:20:15
1212
|
1313
LL | for<H> H: Foo,
1414
| ^^^
1515
|
1616
note: the implementor must specify the same requirement
17-
--> $DIR/drop-impl-pred.rs:12:1
17+
--> $DIR/drop-impl-pred.rs:11:1
1818
|
1919
LL | struct Bar<T>(T) where T: Foo;
2020
| ^^^^^^^^^^^^^
2121

22-
error: aborting due to 1 previous error; 1 warning emitted
22+
error[E0282]: type annotations needed
23+
--> $DIR/drop-impl-pred.rs:16:18
24+
|
25+
LL | impl<T> Drop for Bar<T>
26+
| ^^^^^^ cannot infer type for struct `Bar<T>`
27+
28+
error: aborting due to 2 previous errors; 1 warning emitted
2329

24-
For more information about this error, try `rustc --explain E0367`.
30+
Some errors have detailed explanations: E0282, E0367.
31+
For more information about an error, try `rustc --explain E0282`.

tests/ui/traits/non_lifetime_binders/drop-impl-pred.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@ revisions: no yes
2-
//@[yes] check-pass
32

43
// Issue 110557
54

@@ -15,6 +14,8 @@ struct Bar<T>(T) where T: Foo;
1514
struct Bar<T>(T) where for<H> H: Foo;
1615

1716
impl<T> Drop for Bar<T>
17+
//[yes]~^ ERROR type annotations needed
18+
//[no]~^^ ERROR type annotations needed
1819
where
1920
for<H> H: Foo,
2021
//[no]~^ ERROR `Drop` impl requires `H: Foo` but the struct it is implemented for does not

0 commit comments

Comments
 (0)