-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Don't require associated types with Self: Sized bounds in dyn Trait
objects
#112319
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 9 commits into
rust-lang:master
from
oli-obk:assoc_ty_sized_bound_for_object_safety2
Jul 5, 2023
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ca581f9
Don't require associated types with `Self: Sized` bounds in `dyn Trai…
oli-obk a49b736
Lint now-unnecessary associated type bounds
oli-obk 62fbbac
tidy: move a large function out of an even larger file
oli-obk ce3cff4
Add more tests
oli-obk 307b5ff
Make all generics_require_sized_self go through the query to get cach…
oli-obk 25e3785
Make `unused_associated_type_bounds`'s lint level changeable
oli-obk 243687a
Prefer `retain` over hand-rolling an inefficient version of it
oli-obk 3219993
Only use a single loop over the associated types
oli-obk f80aec7
Test that you can't circumvent the `Sized` bound check
oli-obk 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
Large diffs are not rendered by default.
Oops, something went wrong.
408 changes: 408 additions & 0 deletions
408
compiler/rustc_hir_analysis/src/astconv/object_safety.rs
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,9 +1,24 @@ | ||
//! This test checks that associated types only need to be | ||
//! mentioned in trait objects, if they don't require `Self: Sized`. | ||
|
||
// check-pass | ||
|
||
trait Foo { | ||
type Bar | ||
where | ||
Self: Sized; | ||
} | ||
|
||
fn foo(_: &dyn Foo) {} //~ ERROR: the value of the associated type `Bar` (from trait `Foo`) must be specified | ||
fn foo(_: &dyn Foo) {} | ||
|
||
trait Other: Sized {} | ||
|
||
trait Boo { | ||
type Assoc | ||
where | ||
Self: Other; | ||
} | ||
|
||
fn boo(_: &dyn Boo) {} | ||
|
||
fn main() {} |
This file was deleted.
Oops, something went wrong.
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,25 @@ | ||
//! This test checks that even if some associated types have | ||
//! `where Self: Sized` bounds, those without still need to be | ||
//! mentioned in trait objects. | ||
|
||
trait Foo { | ||
type Bar | ||
where | ||
Self: Sized; | ||
type Bop; | ||
} | ||
|
||
fn foo(_: &dyn Foo) {} | ||
//~^ ERROR the value of the associated type `Bop` (from trait `Foo`) must be specified | ||
|
||
trait Bar { | ||
type Bop; | ||
type Bar | ||
where | ||
Self: Sized; | ||
} | ||
|
||
fn bar(_: &dyn Bar) {} | ||
//~^ ERROR the value of the associated type `Bop` (from trait `Bar`) must be specified | ||
|
||
fn main() {} |
21 changes: 21 additions & 0 deletions
21
tests/ui/object-safety/assoc_type_bounds_sized_others.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,21 @@ | ||
error[E0191]: the value of the associated type `Bop` (from trait `Foo`) must be specified | ||
--> $DIR/assoc_type_bounds_sized_others.rs:12:16 | ||
| | ||
LL | type Bop; | ||
| -------- `Bop` defined here | ||
... | ||
LL | fn foo(_: &dyn Foo) {} | ||
| ^^^ help: specify the associated type: `Foo<Bop = Type>` | ||
|
||
error[E0191]: the value of the associated type `Bop` (from trait `Bar`) must be specified | ||
--> $DIR/assoc_type_bounds_sized_others.rs:22:16 | ||
| | ||
LL | type Bop; | ||
| -------- `Bop` defined here | ||
... | ||
LL | fn bar(_: &dyn Bar) {} | ||
| ^^^ help: specify the associated type: `Bar<Bop = Type>` | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0191`. |
17 changes: 17 additions & 0 deletions
17
tests/ui/object-safety/assoc_type_bounds_sized_unnecessary.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,17 @@ | ||
// check-pass | ||
|
||
trait Foo { | ||
type Bar | ||
where | ||
Self: Sized; | ||
} | ||
|
||
fn foo(_: &dyn Foo<Bar = ()>) {} | ||
//~^ WARN: unnecessary associated type bound for not object safe associated type | ||
//~| WARN: unnecessary associated type bound for not object safe associated type | ||
//~| WARN: unnecessary associated type bound for not object safe associated type | ||
Comment on lines
+10
to
+12
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. These get deduplicated for users |
||
|
||
#[allow(unused_associated_type_bounds)] | ||
fn bar(_: &dyn Foo<Bar = ()>) {} | ||
|
||
fn main() {} |
27 changes: 27 additions & 0 deletions
27
tests/ui/object-safety/assoc_type_bounds_sized_unnecessary.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,27 @@ | ||
warning: unnecessary associated type bound for not object safe associated type | ||
--> $DIR/assoc_type_bounds_sized_unnecessary.rs:9:20 | ||
| | ||
LL | fn foo(_: &dyn Foo<Bar = ()>) {} | ||
| ^^^^^^^^ help: remove this bound | ||
| | ||
= note: this associated type has a `where Self: Sized` bound. Thus, while the associated type can be specified, it cannot be used in any way, because trait objects are not `Sized`. | ||
= note: `#[warn(unused_associated_type_bounds)]` on by default | ||
|
||
warning: unnecessary associated type bound for not object safe associated type | ||
--> $DIR/assoc_type_bounds_sized_unnecessary.rs:9:20 | ||
| | ||
LL | fn foo(_: &dyn Foo<Bar = ()>) {} | ||
| ^^^^^^^^ help: remove this bound | ||
| | ||
= note: this associated type has a `where Self: Sized` bound. Thus, while the associated type can be specified, it cannot be used in any way, because trait objects are not `Sized`. | ||
|
||
warning: unnecessary associated type bound for not object safe associated type | ||
--> $DIR/assoc_type_bounds_sized_unnecessary.rs:9:20 | ||
| | ||
LL | fn foo(_: &dyn Foo<Bar = ()>) {} | ||
| ^^^^^^^^ help: remove this bound | ||
| | ||
= note: this associated type has a `where Self: Sized` bound. Thus, while the associated type can be specified, it cannot be used in any way, because trait objects are not `Sized`. | ||
|
||
warning: 3 warnings emitted | ||
|
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 @@ | ||
//! This test checks that even if some associated types have | ||
//! `where Self: Sized` bounds, those without still need to be | ||
//! mentioned in trait objects. | ||
|
||
trait Bop { | ||
type Bar: Default | ||
where | ||
Self: Sized; | ||
} | ||
|
||
fn bop<T: Bop + ?Sized>() { | ||
let _ = <T as Bop>::Bar::default(); | ||
//~^ ERROR: trait bounds were not satisfied | ||
//~| ERROR: the size for values of type `T` cannot be known at compilation time | ||
} | ||
|
||
fn main() { | ||
bop::<dyn Bop>(); | ||
//~^ ERROR: the size for values of type `dyn Bop` cannot be known at compilation time | ||
} |
53 changes: 53 additions & 0 deletions
53
tests/ui/object-safety/assoc_type_bounds_sized_used.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,53 @@ | ||
error[E0599]: the function or associated item `default` exists for associated type `<T as Bop>::Bar`, but its trait bounds were not satisfied | ||
--> $DIR/assoc_type_bounds_sized_used.rs:12:30 | ||
| | ||
LL | let _ = <T as Bop>::Bar::default(); | ||
| ^^^^^^^ function or associated item cannot be called on `<T as Bop>::Bar` due to unsatisfied trait bounds | ||
| | ||
= note: the following trait bounds were not satisfied: | ||
`T: Sized` | ||
which is required by `<T as Bop>::Bar: Default` | ||
help: consider restricting the type parameter to satisfy the trait bound | ||
| | ||
LL | fn bop<T: Bop + ?Sized>() where T: Sized { | ||
| ++++++++++++++ | ||
|
||
error[E0277]: the size for values of type `T` cannot be known at compilation time | ||
--> $DIR/assoc_type_bounds_sized_used.rs:12:13 | ||
| | ||
LL | fn bop<T: Bop + ?Sized>() { | ||
| - this type parameter needs to be `Sized` | ||
LL | let _ = <T as Bop>::Bar::default(); | ||
| ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | ||
| | ||
note: required by a bound in `Bop::Bar` | ||
--> $DIR/assoc_type_bounds_sized_used.rs:8:15 | ||
| | ||
LL | type Bar: Default | ||
| --- required by a bound in this associated type | ||
LL | where | ||
LL | Self: Sized; | ||
| ^^^^^ required by this bound in `Bop::Bar` | ||
help: consider removing the `?Sized` bound to make the type parameter `Sized` | ||
| | ||
LL - fn bop<T: Bop + ?Sized>() { | ||
LL + fn bop<T: Bop>() { | ||
| | ||
|
||
error[E0277]: the size for values of type `dyn Bop` cannot be known at compilation time | ||
--> $DIR/assoc_type_bounds_sized_used.rs:18:11 | ||
| | ||
LL | bop::<dyn Bop>(); | ||
| ^^^^^^^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `dyn Bop` | ||
note: required by a bound in `bop` | ||
--> $DIR/assoc_type_bounds_sized_used.rs:11:11 | ||
| | ||
LL | fn bop<T: Bop + ?Sized>() { | ||
| ^^^ required by this bound in `bop` | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
Some errors have detailed explanations: E0277, E0599. | ||
For more information about an error, try `rustc --explain E0277`. |
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.