Skip to content

Commit bfe3670

Browse files
committed
make Sized coinductive
1 parent 3aedcf0 commit bfe3670

File tree

7 files changed

+56
-17
lines changed

7 files changed

+56
-17
lines changed

compiler/rustc_trait_selection/src/traits/select/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
843843

844844
fn coinductive_predicate(&self, predicate: ty::Predicate<'tcx>) -> bool {
845845
let result = match predicate.kind().skip_binder() {
846-
ty::PredicateKind::Trait(ref data, _) => self.tcx().trait_is_auto(data.def_id()),
846+
ty::PredicateKind::Trait(ref data, _) => {
847+
self.tcx().trait_is_auto(data.def_id())
848+
|| self.tcx().lang_items().sized_trait() == Some(data.def_id())
849+
}
847850
_ => false,
848851
};
849852
debug!(?predicate, ?result, "coinductive_predicate");

src/test/ui/generic-associated-types/projection-bound-cycle-generic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ impl<T> Foo for Number<T> {
2424
// ```
2525
// which it is :)
2626
type Item where [T]: Sized = [T];
27+
//~^ ERROR overflow evaluating the requirement `<Number<T> as Foo>::Item == _
2728
}
2829

2930
struct OnlySized<T> where T: Sized { f: T }
@@ -43,7 +44,6 @@ impl<T> Bar for T where T: Foo {
4344
// can use the bound on `Foo::Item` for this, but that requires
4445
// `wf(<T as Foo>::Item)`, which is an invalid cycle.
4546
type Assoc = OnlySized<<T as Foo>::Item>;
46-
//~^ ERROR overflow evaluating the requirement `<T as Foo>::Item: Sized`
4747
}
4848

4949
fn foo<T: Print>() {

src/test/ui/generic-associated-types/projection-bound-cycle-generic.stderr

+4-7
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@ LL | #![feature(generic_associated_types)]
77
= note: `#[warn(incomplete_features)]` on by default
88
= note: see issue #44265 <https://github.com/rust-lang/rust/issues/44265> for more information
99

10-
error[E0275]: overflow evaluating the requirement `<T as Foo>::Item: Sized`
11-
--> $DIR/projection-bound-cycle-generic.rs:45:5
10+
error[E0275]: overflow evaluating the requirement `<Number<T> as Foo>::Item == _`
11+
--> $DIR/projection-bound-cycle-generic.rs:26:5
1212
|
13-
LL | struct OnlySized<T> where T: Sized { f: T }
14-
| - required by this bound in `OnlySized`
15-
...
16-
LL | type Assoc = OnlySized<<T as Foo>::Item>;
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
LL | type Item where [T]: Sized = [T];
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1815

1916
error: aborting due to previous error; 1 warning emitted
2017

src/test/ui/generic-associated-types/projection-bound-cycle.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ impl Foo for Number {
2626
// ```
2727
// which it is :)
2828
type Item where str: Sized = str;
29+
//~^ ERROR overflow evaluating the requirement `<Number as Foo>::Item == _
2930
}
3031

3132
struct OnlySized<T> where T: Sized { f: T }
@@ -45,7 +46,6 @@ impl<T> Bar for T where T: Foo {
4546
// can use the bound on `Foo::Item` for this, but that requires
4647
// `wf(<T as Foo>::Item)`, which is an invalid cycle.
4748
type Assoc = OnlySized<<T as Foo>::Item>;
48-
//~^ ERROR overflow evaluating the requirement `<T as Foo>::Item: Sized`
4949
}
5050

5151
fn foo<T: Print>() {

src/test/ui/generic-associated-types/projection-bound-cycle.stderr

+4-7
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@ LL | #![feature(generic_associated_types)]
77
= note: `#[warn(incomplete_features)]` on by default
88
= note: see issue #44265 <https://github.com/rust-lang/rust/issues/44265> for more information
99

10-
error[E0275]: overflow evaluating the requirement `<T as Foo>::Item: Sized`
11-
--> $DIR/projection-bound-cycle.rs:47:5
10+
error[E0275]: overflow evaluating the requirement `<Number as Foo>::Item == _`
11+
--> $DIR/projection-bound-cycle.rs:28:5
1212
|
13-
LL | struct OnlySized<T> where T: Sized { f: T }
14-
| - required by this bound in `OnlySized`
15-
...
16-
LL | type Assoc = OnlySized<<T as Foo>::Item>;
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
LL | type Item where str: Sized = str;
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1815

1916
error: aborting due to previous error; 1 warning emitted
2017

src/test/ui/sized/coinductive-1.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// check-pass
2+
struct Node<C: Trait<Self>>(C::Assoc);
3+
4+
trait Trait<T> {
5+
type Assoc;
6+
}
7+
8+
impl<T> Trait<T> for Vec<()> {
9+
type Assoc = Vec<T>;
10+
}
11+
12+
fn main() {
13+
let _ = Node::<Vec<()>>(Vec::new());
14+
}

src/test/ui/sized/coinductive-2.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// run-pass
2+
struct Node<C: CollectionFactory<Self>> {
3+
_children: C::Collection,
4+
}
5+
6+
trait CollectionFactory<T> {
7+
type Collection;
8+
}
9+
10+
impl<T> CollectionFactory<T> for Vec<()> {
11+
type Collection = Vec<T>;
12+
}
13+
14+
trait Collection<T>: Sized {
15+
fn push(&mut self, v: T);
16+
}
17+
18+
impl<T> Collection<T> for Vec<T> {
19+
fn push(&mut self, v: T) {
20+
self.push(v)
21+
}
22+
}
23+
24+
fn main() {
25+
let _ = Node::<Vec<()>> {
26+
_children: Vec::new(),
27+
};
28+
}

0 commit comments

Comments
 (0)