Skip to content

Account for type param in '_ constraint for returned trait suggestion #73497

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
estebank opened this issue Jun 19, 2020 · 1 comment · Fixed by #116244
Closed

Account for type param in '_ constraint for returned trait suggestion #73497

estebank opened this issue Jun 19, 2020 · 1 comment · Fixed by #116244
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

Given

use std::any::Any;
fn foo<T: Any>(value: &T) -> Box<dyn Any> {
Box::new(value) as Box<dyn Any>
//~^ ERROR cannot infer an appropriate lifetime
}

we currently output

error[E0759]: cannot infer an appropriate lifetime
--> $DIR/issue-16922.rs:4:14
|
LL | fn foo<T: Any>(value: &T) -> Box<dyn Any> {
| -- this data with an anonymous lifetime `'_`...
LL | Box::new(value) as Box<dyn Any>
| ^^^^^ ...is captured here, requiring it to live as long as `'static`
|
help: to declare that the trait object captures data from argument `value`, you can add an explicit `'_` lifetime bound
|
LL | fn foo<T: Any>(value: &T) -> Box<dyn Any + '_> {
| ^^^^

The suggestion is incomplete and will not result in compilable code. Because the lifetime is coming from an argument of type T, which is an unconstrained type parameter, T itself will not have the needed lifetime and causes the 'static obligation that we're trying to reverse by suggesting '_. The appropriate changes to the code would be either fn foo<'a, T: Any + 'a>(value: &'a T) -> Box<dyn Any + 'a> or fn foo(value: &dyn Any) -> Box<dyn Any + '_>, depending on whether T appears in any other argument or not.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Jun 19, 2020
@estebank
Copy link
Contributor Author

The suggested code works since 1.64.

estebank added a commit to estebank/rust that referenced this issue Sep 28, 2023
estebank added a commit to estebank/rust that referenced this issue Oct 11, 2023
estebank added a commit to estebank/rust that referenced this issue Oct 24, 2023
estebank added a commit to estebank/rust that referenced this issue Oct 24, 2023
estebank added a commit to estebank/rust that referenced this issue Nov 7, 2023
estebank added a commit to estebank/rust that referenced this issue Nov 7, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 14, 2023
Apply structured suggestion that allows test to work since 1.64

Close rust-lang#73497.
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 15, 2023
Rollup merge of rust-lang#116244 - estebank:issue-73497, r=b-naber

Apply structured suggestion that allows test to work since 1.64

Close rust-lang#73497.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant