-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Disallow Self in type param defaults of ADTs #64842
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bors
merged 3 commits into
rust-lang:master
from
pnkfelix:fix-issue-61631-self-in-type-param-default
Oct 4, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -468,6 +468,19 @@ impl<'a, 'tcx> Visitor<'tcx> for LateResolutionVisitor<'a, '_> { | |
} | ||
})); | ||
|
||
// rust-lang/rust#61631: The type `Self` is essentially | ||
// another type parameter. For ADTs, we consider it | ||
// well-defined only after all of the ADT type parameters have | ||
// been provided. Therefore, we do not allow use of `Self` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I disagree with the phrasing (although the conclusion is similar) - see #61631 (comment) In short, |
||
// anywhere in ADT type parameter defaults. | ||
// | ||
// (We however cannot ban `Self` for defaults on *all* generic | ||
// lists; e.g. trait generics can usefully refer to `Self`, | ||
// such as in the case of `trait Add<Rhs = Self>`.) | ||
if self.current_self_item.is_some() { // (`Some` if + only if we are in ADT's generics.) | ||
default_ban_rib.bindings.insert(Ident::with_dummy_span(kw::SelfUpper), Res::Err); | ||
} | ||
|
||
// We also ban access to type parameters for use as the types of const parameters. | ||
let mut const_ty_param_ban_rib = Rib::new(TyParamAsConstParamTy); | ||
const_ty_param_ban_rib.bindings.extend(generics.params.iter() | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/test/ui/generics/issue-61631-default-type-param-can-reference-self-in-trait.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#![crate_type="lib"] | ||
|
||
// rust-lang/rust#61631: The use of `Self` in the defaults of generic | ||
// types in a *trait* definition are allowed. | ||
// | ||
// It *must* be accepted; we have used this pattern extensively since | ||
// Rust 1.0 (see e.g. `trait Add<Rhs=Self>`). | ||
trait Tnobound<P = Self> {} | ||
|
||
impl Tnobound for () { } | ||
|
||
// This variant is accepted at the definition site; but it will be | ||
// rejected at every possible usage site (such as the one immediately | ||
// below). Maybe one day we will attempt to catch it at the definition | ||
// site, but today this is accepted due to compiler implementation | ||
// limitations. | ||
trait Tsized<P: Sized = [Self]> {} | ||
|
||
impl Tsized for () {} | ||
//~^ ERROR the size for values of type `[()]` cannot be known at compilation time [E0277] |
12 changes: 12 additions & 0 deletions
12
src/test/ui/generics/issue-61631-default-type-param-can-reference-self-in-trait.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0277]: the size for values of type `[()]` cannot be known at compilation time | ||
--> $DIR/issue-61631-default-type-param-can-reference-self-in-trait.rs:19:6 | ||
| | ||
LL | impl Tsized for () {} | ||
| ^^^^^^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `std::marker::Sized` is not implemented for `[()]` | ||
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait> | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
45 changes: 45 additions & 0 deletions
45
src/test/ui/generics/issue-61631-default-type-param-cannot-reference-self.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#![crate_type="lib"] | ||
|
||
// rust-lang/rust#61631: Uses of `Self` in the defaults of generic | ||
// types for ADT's are not allowed. We justify this because the `Self` | ||
// type could be considered the "final" type parameter, that is only | ||
// well-defined after all of the other type parameters on the ADT have | ||
// been instantiated. | ||
// | ||
// These were previously were ICE'ing at the usage point anyway (see | ||
// `demo_usages` below), so there should not be any backwards | ||
// compatibility concern. | ||
|
||
struct Snobound<'a, P = Self> { x: Option<&'a P> } | ||
//~^ ERROR type parameters cannot use `Self` in their defaults [E0735] | ||
|
||
enum Enobound<'a, P = Self> { A, B(Option<&'a P>) } | ||
//~^ ERROR type parameters cannot use `Self` in their defaults [E0735] | ||
|
||
union Unobound<'a, P = Self> { x: i32, y: Option<&'a P> } | ||
//~^ ERROR type parameters cannot use `Self` in their defaults [E0735] | ||
|
||
// Disallowing `Self` in defaults sidesteps need to check the bounds | ||
// on the defaults in cases like these. | ||
|
||
struct Ssized<'a, P: Sized = [Self]> { x: Option<&'a P> } | ||
//~^ ERROR type parameters cannot use `Self` in their defaults [E0735] | ||
|
||
enum Esized<'a, P: Sized = [Self]> { A, B(Option<&'a P>) } | ||
//~^ ERROR type parameters cannot use `Self` in their defaults [E0735] | ||
|
||
union Usized<'a, P: Sized = [Self]> { x: i32, y: Option<&'a P> } | ||
//~^ ERROR type parameters cannot use `Self` in their defaults [E0735] | ||
|
||
fn demo_usages() { | ||
// An ICE means you only get the error from the first line of the | ||
// demo; comment each out to observe the other ICEs when trying | ||
// this out on older versions of Rust. | ||
|
||
let _ice: Snobound; | ||
let _ice: Enobound; | ||
let _ice: Unobound; | ||
let _ice: Ssized; | ||
let _ice: Esized; | ||
let _ice: Usized; | ||
} |
39 changes: 39 additions & 0 deletions
39
src/test/ui/generics/issue-61631-default-type-param-cannot-reference-self.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
error[E0735]: type parameters cannot use `Self` in their defaults | ||
--> $DIR/issue-61631-default-type-param-cannot-reference-self.rs:13:25 | ||
| | ||
LL | struct Snobound<'a, P = Self> { x: Option<&'a P> } | ||
| ^^^^ `Self` in type parameter default | ||
|
||
error[E0735]: type parameters cannot use `Self` in their defaults | ||
--> $DIR/issue-61631-default-type-param-cannot-reference-self.rs:16:23 | ||
| | ||
LL | enum Enobound<'a, P = Self> { A, B(Option<&'a P>) } | ||
| ^^^^ `Self` in type parameter default | ||
|
||
error[E0735]: type parameters cannot use `Self` in their defaults | ||
--> $DIR/issue-61631-default-type-param-cannot-reference-self.rs:19:24 | ||
| | ||
LL | union Unobound<'a, P = Self> { x: i32, y: Option<&'a P> } | ||
| ^^^^ `Self` in type parameter default | ||
|
||
error[E0735]: type parameters cannot use `Self` in their defaults | ||
--> $DIR/issue-61631-default-type-param-cannot-reference-self.rs:25:31 | ||
| | ||
LL | struct Ssized<'a, P: Sized = [Self]> { x: Option<&'a P> } | ||
| ^^^^ `Self` in type parameter default | ||
|
||
error[E0735]: type parameters cannot use `Self` in their defaults | ||
--> $DIR/issue-61631-default-type-param-cannot-reference-self.rs:28:29 | ||
| | ||
LL | enum Esized<'a, P: Sized = [Self]> { A, B(Option<&'a P>) } | ||
| ^^^^ `Self` in type parameter default | ||
|
||
error[E0735]: type parameters cannot use `Self` in their defaults | ||
--> $DIR/issue-61631-default-type-param-cannot-reference-self.rs:31:30 | ||
| | ||
LL | union Usized<'a, P: Sized = [Self]> { x: i32, y: Option<&'a P> } | ||
| ^^^^ `Self` in type parameter default | ||
|
||
error: aborting due to 6 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0735`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.