Skip to content

Commit e443e1b

Browse files
committed
Regression tests.
Update: incorporate review feedback.
1 parent a18d424 commit e443e1b

4 files changed

+116
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![crate_type="lib"]
2+
3+
// rust-lang/rust#61631: The use of `Self` in the defaults of generic
4+
// types in a *trait* definition are allowed.
5+
//
6+
// It *must* be accepted; we have used this pattern extensively since
7+
// Rust 1.0 (see e.g. `trait Add<Rhs=Self>`).
8+
trait Tnobound<P = Self> {}
9+
10+
impl Tnobound for () { }
11+
12+
// This variant is accepted at the definition site; but it will be
13+
// rejected at every possible usage site (such as the one immediately
14+
// below). Maybe one day we will attempt to catch it at the definition
15+
// site, but today this is accepted due to compiler implementation
16+
// limitations.
17+
trait Tsized<P: Sized = [Self]> {}
18+
19+
impl Tsized for () {}
20+
//~^ ERROR the size for values of type `[()]` cannot be known at compilation time [E0277]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0277]: the size for values of type `[()]` cannot be known at compilation time
2+
--> $DIR/issue-61631-default-type-param-can-reference-self-in-trait.rs:19:6
3+
|
4+
LL | impl Tsized for () {}
5+
| ^^^^^^ doesn't have a size known at compile-time
6+
|
7+
= help: the trait `std::marker::Sized` is not implemented for `[()]`
8+
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#![crate_type="lib"]
2+
3+
// rust-lang/rust#61631: Uses of `Self` in the defaults of generic
4+
// types for ADT's are not allowed. We justify this because the `Self`
5+
// type could be considered the "final" type parameter, that is only
6+
// well-defined after all of the other type parameters on the ADT have
7+
// been instantiated.
8+
//
9+
// These were previously were ICE'ing at the usage point anyway (see
10+
// `demo_usages` below), so there should not be any backwards
11+
// compatibility concern.
12+
13+
struct Snobound<'a, P = Self> { x: Option<&'a P> }
14+
//~^ ERROR type parameters cannot use `Self` in their defaults [E0735]
15+
16+
enum Enobound<'a, P = Self> { A, B(Option<&'a P>) }
17+
//~^ ERROR type parameters cannot use `Self` in their defaults [E0735]
18+
19+
union Unobound<'a, P = Self> { x: i32, y: Option<&'a P> }
20+
//~^ ERROR type parameters cannot use `Self` in their defaults [E0735]
21+
22+
// Disallowing `Self` in defaults sidesteps need to check the bounds
23+
// on the defaults in cases like these.
24+
25+
struct Ssized<'a, P: Sized = [Self]> { x: Option<&'a P> }
26+
//~^ ERROR type parameters cannot use `Self` in their defaults [E0735]
27+
28+
enum Esized<'a, P: Sized = [Self]> { A, B(Option<&'a P>) }
29+
//~^ ERROR type parameters cannot use `Self` in their defaults [E0735]
30+
31+
union Usized<'a, P: Sized = [Self]> { x: i32, y: Option<&'a P> }
32+
//~^ ERROR type parameters cannot use `Self` in their defaults [E0735]
33+
34+
fn demo_usages() {
35+
// An ICE means you only get the error from the first line of the
36+
// demo; comment each out to observe the other ICEs when trying
37+
// this out on older versions of Rust.
38+
39+
let _ice: Snobound;
40+
let _ice: Enobound;
41+
let _ice: Unobound;
42+
let _ice: Ssized;
43+
let _ice: Esized;
44+
let _ice: Usized;
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error[E0735]: type parameters cannot use `Self` in their defaults
2+
--> $DIR/issue-61631-default-type-param-cannot-reference-self.rs:13:25
3+
|
4+
LL | struct Snobound<'a, P = Self> { x: Option<&'a P> }
5+
| ^^^^ `Self` in type parameter default
6+
7+
error[E0735]: type parameters cannot use `Self` in their defaults
8+
--> $DIR/issue-61631-default-type-param-cannot-reference-self.rs:16:23
9+
|
10+
LL | enum Enobound<'a, P = Self> { A, B(Option<&'a P>) }
11+
| ^^^^ `Self` in type parameter default
12+
13+
error[E0735]: type parameters cannot use `Self` in their defaults
14+
--> $DIR/issue-61631-default-type-param-cannot-reference-self.rs:19:24
15+
|
16+
LL | union Unobound<'a, P = Self> { x: i32, y: Option<&'a P> }
17+
| ^^^^ `Self` in type parameter default
18+
19+
error[E0735]: type parameters cannot use `Self` in their defaults
20+
--> $DIR/issue-61631-default-type-param-cannot-reference-self.rs:25:31
21+
|
22+
LL | struct Ssized<'a, P: Sized = [Self]> { x: Option<&'a P> }
23+
| ^^^^ `Self` in type parameter default
24+
25+
error[E0735]: type parameters cannot use `Self` in their defaults
26+
--> $DIR/issue-61631-default-type-param-cannot-reference-self.rs:28:29
27+
|
28+
LL | enum Esized<'a, P: Sized = [Self]> { A, B(Option<&'a P>) }
29+
| ^^^^ `Self` in type parameter default
30+
31+
error[E0735]: type parameters cannot use `Self` in their defaults
32+
--> $DIR/issue-61631-default-type-param-cannot-reference-self.rs:31:30
33+
|
34+
LL | union Usized<'a, P: Sized = [Self]> { x: i32, y: Option<&'a P> }
35+
| ^^^^ `Self` in type parameter default
36+
37+
error: aborting due to 6 previous errors
38+
39+
For more information about this error, try `rustc --explain E0735`.

0 commit comments

Comments
 (0)