-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Suggest -> impl Trait
and -> Box<dyn Trait>
on fn that doesn't return
#70998
Merged
Conversation
This file contains 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
r? @varkor (rust_highfive has picked a reviewer for you, use r? to override) |
rust-highfive
added
the
S-waiting-on-review
Status: Awaiting review from the assignee but also interested parties.
label
Apr 10, 2020
This comment has been minimized.
This comment has been minimized.
Centril
reviewed
Apr 11, 2020
src/librustc_trait_selection/traits/error_reporting/suggestions.rs
Outdated
Show resolved
Hide resolved
src/librustc_trait_selection/traits/error_reporting/suggestions.rs
Outdated
Show resolved
Hide resolved
src/librustc_trait_selection/traits/error_reporting/suggestions.rs
Outdated
Show resolved
Hide resolved
src/librustc_trait_selection/traits/error_reporting/suggestions.rs
Outdated
Show resolved
Hide resolved
src/librustc_trait_selection/traits/error_reporting/suggestions.rs
Outdated
Show resolved
Hide resolved
src/librustc_trait_selection/traits/error_reporting/suggestions.rs
Outdated
Show resolved
Hide resolved
src/librustc_trait_selection/traits/error_reporting/suggestions.rs
Outdated
Show resolved
Hide resolved
src/librustc_trait_selection/traits/error_reporting/suggestions.rs
Outdated
Show resolved
Hide resolved
estebank
commented
Apr 12, 2020
Centril
reviewed
Apr 13, 2020
src/librustc_trait_selection/traits/error_reporting/suggestions.rs
Outdated
Show resolved
Hide resolved
estebank
force-pushed
the
suggest-impl-trait-empty-fn
branch
from
April 14, 2020 03:15
e53a129
to
ea1cdc7
Compare
This comment has been minimized.
This comment has been minimized.
estebank
force-pushed
the
suggest-impl-trait-empty-fn
branch
from
April 14, 2020 04:37
ea1cdc7
to
adce610
Compare
varkor
reviewed
Apr 19, 2020
src/librustc_trait_selection/traits/error_reporting/suggestions.rs
Outdated
Show resolved
Hide resolved
varkor
reviewed
Apr 19, 2020
src/librustc_trait_selection/traits/error_reporting/suggestions.rs
Outdated
Show resolved
Hide resolved
src/librustc_trait_selection/traits/error_reporting/suggestions.rs
Outdated
Show resolved
Hide resolved
src/librustc_trait_selection/traits/error_reporting/suggestions.rs
Outdated
Show resolved
Hide resolved
…turn During development, a function could have a return type set that is a bare trait object by accident. We already suggest using either a boxed trait object or `impl Trait` if the return paths will allow it. We now do so too when there are *no* return paths or they all resolve to `!`. We still don't handle cases where the trait object is *not* the entirety of the return type gracefully.
estebank
force-pushed
the
suggest-impl-trait-empty-fn
branch
from
April 20, 2020 16:46
adce610
to
19fb509
Compare
This comment has been minimized.
This comment has been minimized.
@estebank: could you squash the review comment and rustfmt commits? |
When the return type is `!Sized` we look for all the returned expressions in the body to fetch their types and provide a reasonable suggestion. The tail expression of the body is normally evaluated after checking whether the return type is `Sized`. Changing the order of the evaluation produces undesirable knock down effects, so we detect the specific case that newcomers are likely to encounter ,returning a single bare trait object, and only in that case we evaluate the tail expression's type so that the suggestion will be accurate.
estebank
force-pushed
the
suggest-impl-trait-empty-fn
branch
from
April 20, 2020 18:17
19fb509
to
e536257
Compare
@varkor done. |
r=me when CI passes. |
@bors r=varkor |
1 similar comment
@bors r=varkor |
📌 Commit e536257 has been approved by |
bors
added
S-waiting-on-bors
Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
and removed
S-waiting-on-review
Status: Awaiting review from the assignee but also interested parties.
labels
Apr 21, 2020
📌 Commit e536257 has been approved by |
bors
added a commit
to rust-lang-ci/rust
that referenced
this pull request
Apr 22, 2020
Rollup of 7 pull requests Successful merges: - rust-lang#70998 (Suggest `-> impl Trait` and `-> Box<dyn Trait>` on fn that doesn't return) - rust-lang#71236 (Remove unused rustc_serialize::hex module) - rust-lang#71366 (Use assoc int consts3) - rust-lang#71372 (Fix #! (shebang) stripping account space issue) - rust-lang#71384 (Fix stage0.txt version number comment) - rust-lang#71390 (Fix incorrect description of E0690) - rust-lang#71399 (Clean up E0554 explanation) Failed merges: r? @ghost
estebank
added a commit
to estebank/rust
that referenced
this pull request
Feb 3, 2021
The following code is supposed to compile ```rust use std::ops::BitOr; pub trait IntWrapper { type InternalStorage; } impl<T> BitOr for dyn IntWrapper<InternalStorage = T> where Self: Sized, T: BitOr + BitOr<Output = T>, { type Output = Self; fn bitor(self, _other: Self) -> Self { todo!() } } ``` Before this change it would ICE. In rust-lang#70998 the removed logic was added to provide better suggestions, and the `delay_span_bug` guard was added to protect against a potential logic error when returning traits. As it happens, there are cases, like the one above, where traits can indeed be returned, so valid code was being rejected. Fix rust-lang#80207.
m-ou-se
added a commit
to m-ou-se/rust
that referenced
this pull request
Feb 3, 2021
Remove incorrect `delay_span_bug` The following code is supposed to compile ```rust use std::ops::BitOr; pub trait IntWrapper { type InternalStorage; } impl<T> BitOr for dyn IntWrapper<InternalStorage = T> where Self: Sized, T: BitOr + BitOr<Output = T>, { type Output = Self; fn bitor(self, _other: Self) -> Self { todo!() } } ``` Before this change it would ICE. In rust-lang#70998 the removed logic was added to provide better suggestions, and the `delay_span_bug` guard was added to protect against a potential logic error when returning traits. As it happens, there are cases, like the one above, where traits can indeed be returned, so valid code was being rejected. Fix (but not close) rust-lang#80207.
ehuss
pushed a commit
to ehuss/rust
that referenced
this pull request
Feb 5, 2021
Remove incorrect `delay_span_bug` The following code is supposed to compile ```rust use std::ops::BitOr; pub trait IntWrapper { type InternalStorage; } impl<T> BitOr for dyn IntWrapper<InternalStorage = T> where Self: Sized, T: BitOr + BitOr<Output = T>, { type Output = Self; fn bitor(self, _other: Self) -> Self { todo!() } } ``` Before this change it would ICE. In rust-lang#70998 the removed logic was added to provide better suggestions, and the `delay_span_bug` guard was added to protect against a potential logic error when returning traits. As it happens, there are cases, like the one above, where traits can indeed be returned, so valid code was being rejected. Fix (but not close) rust-lang#80207.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
S-waiting-on-bors
Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
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.
During development, a function could have a return type set that is a
bare trait object by accident. We already suggest using either a boxed
trait object or
impl Trait
if the return paths will allow it. We nowdo so too when there are no return paths or they all resolve to
!
.We still don't handle cases where the trait object is not the entirety
of the return type gracefully.
Closes #38376.