Skip to content

Commit 82816cd

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 e9c25d8 commit 82816cd

File tree

8 files changed

+42
-74
lines changed

8 files changed

+42
-74
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
@@ -665,6 +665,14 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
665665
| PredicateFilter::SelfOnly
666666
| PredicateFilter::SelfTraitThatDefines(_)
667667
| PredicateFilter::SelfAndAssociatedTypeBounds => {
668+
icx.lowerer().add_sizedness_bounds(
669+
&mut bounds,
670+
self_param_ty,
671+
superbounds,
672+
None,
673+
Some(trait_def_id),
674+
item.span,
675+
);
668676
icx.lowerer().add_default_super_traits(
669677
trait_def_id,
670678
&mut bounds,

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_trait.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use rustc_errors::codes::*;
44
use rustc_errors::{
55
Applicability, Diag, EmissionGuarantee, StashKey, Suggestions, struct_span_code_err,
66
};
7-
use rustc_hir as hir;
87
use rustc_hir::def::{DefKind, Res};
98
use rustc_hir::def_id::DefId;
9+
use rustc_hir::{self as hir, LangItem};
1010
use rustc_lint_defs::builtin::{BARE_TRAIT_OBJECTS, UNUSED_ASSOCIATED_TYPE_BOUNDS};
1111
use rustc_middle::ty::elaborate::ClauseWithSupertraitSpan;
1212
use rustc_middle::ty::{
@@ -77,8 +77,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
7777
span,
7878
);
7979

80-
let (elaborated_trait_bounds, elaborated_projection_bounds) =
80+
let (mut elaborated_trait_bounds, elaborated_projection_bounds) =
8181
traits::expand_trait_aliases(tcx, user_written_bounds.iter().copied());
82+
83+
// FIXME(sized-hierarchy): https://github.com/rust-lang/rust/pull/142712#issuecomment-3013231794
84+
let meta_sized_did = tcx.require_lang_item(LangItem::MetaSized, span);
85+
elaborated_trait_bounds.retain(|(pred, _)| pred.def_id() != meta_sized_did);
86+
8287
let (regular_traits, mut auto_traits): (Vec<_>, Vec<_>) = elaborated_trait_bounds
8388
.into_iter()
8489
.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
@@ -18,22 +18,6 @@ error: relaxed bounds are not permitted in supertrait bounds
1818
LL | trait NegPointeeSized: ?PointeeSized { }
1919
| ^^^^^^^^^^^^^
2020

21-
error[E0277]: the size for values of type `T` cannot be known
22-
--> $DIR/default-supertrait.rs:52:38
23-
|
24-
LL | fn with_bare_trait<T: PointeeSized + Bare>() {
25-
| ^^^^ doesn't have a known size
26-
|
27-
note: required by a bound in `Bare`
28-
--> $DIR/default-supertrait.rs:22:1
29-
|
30-
LL | trait Bare {}
31-
| ^^^^^^^^^^^^^ required by this bound in `Bare`
32-
help: consider further restricting type parameter `T` with unstable trait `MetaSized`
33-
|
34-
LL | fn with_bare_trait<T: PointeeSized + Bare + std::marker::MetaSized>() {
35-
| ++++++++++++++++++++++++
36-
3721
error[E0277]: the size for values of type `T` cannot be known at compilation time
3822
--> $DIR/default-supertrait.rs:35:22
3923
|
@@ -79,11 +63,10 @@ LL | fn with_pointeesized_supertrait<T: PointeeSized + PointeeSized_ + std::mark
7963
| ++++++++++++++++++++++++
8064

8165
error[E0277]: the size for values of type `T` cannot be known at compilation time
82-
--> $DIR/default-supertrait.rs:54:22
66+
--> $DIR/default-supertrait.rs:52:22
8367
|
8468
LL | fn with_bare_trait<T: PointeeSized + Bare>() {
8569
| - this type parameter needs to be `Sized`
86-
LL |
8770
LL | requires_sized::<T>();
8871
| ^ doesn't have a size known at compile-time
8972
|
@@ -93,22 +76,6 @@ note: required by a bound in `requires_sized`
9376
LL | fn requires_sized<T: Sized>() {}
9477
| ^^^^^ required by this bound in `requires_sized`
9578

96-
error[E0277]: the size for values of type `T` cannot be known
97-
--> $DIR/default-supertrait.rs:56:26
98-
|
99-
LL | requires_metasized::<T>();
100-
| ^ doesn't have a known size
101-
|
102-
note: required by a bound in `requires_metasized`
103-
--> $DIR/default-supertrait.rs:25:26
104-
|
105-
LL | fn requires_metasized<T: MetaSized>() {}
106-
| ^^^^^^^^^ required by this bound in `requires_metasized`
107-
help: consider further restricting type parameter `T` with unstable trait `MetaSized`
108-
|
109-
LL | fn with_bare_trait<T: PointeeSized + Bare + std::marker::MetaSized>() {
110-
| ++++++++++++++++++++++++
111-
112-
error: aborting due to 9 previous errors
79+
error: aborting due to 7 previous errors
11380

11481
For more information about this 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`.

0 commit comments

Comments
 (0)