Skip to content
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

Fix suggestions when returning a bare trait from an async fn. #131882

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

hirschenberger
Copy link
Contributor

Don't assume we always return a trait object and suggest the dyn keyword. Suggest adding impl instead.

fixes #131661

@rustbot
Copy link
Collaborator

rustbot commented Oct 18, 2024

r? @davidtwco

rustbot has assigned @davidtwco.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 18, 2024
@rustbot
Copy link
Collaborator

rustbot commented Oct 18, 2024

HIR ty lowering was modified

cc @fmease

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

Don't assume we always return a trait object and suggest the dyn keyword.
Suggest adding impl instead.
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-18 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
------
 > importing cache manifest from ghcr.io/rust-lang/rust-ci-cache:c32c805632780b5c1de330e3f44561b336c2efe163bc0990acb392390157a8e1d9f855d75914a239aa40c49d77f4a837247d05d2f8d46f554b98e1f46712a3e3:
------
##[endgroup]
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-18]
debug: `DISABLE_CI_RUSTC_IF_INCOMPATIBLE` configured.
---
sccache: Starting the server...
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-18', '--enable-llvm-link-shared', '--set', 'rust.randomize-layout=true', '--set', 'rust.thin-lto-import-instr-limit=10', '--set', 'change-id=99999999', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--set', 'rust.lld=false', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--enable-new-symbol-mangling']
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-18/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.randomize-layout := True
configure: rust.thin-lto-import-instr-limit := 10
---
+   --> $DIR/suggest-impl-on-bare-trait-return.rs:4:19
3    |
- LL | fn fun() -> Trait {
-    |             ^^^^^
+ LL | async fn fun() -> Trait {
6    |
7 help: use `impl Trait` to return an opaque type, as long as you return a single underlying type
8    |

---
11 help: alternatively, you can return an owned trait object
12    |
- LL | fn fun() -> Box<dyn Trait> {
-    |             +++++++      +
+ LL | Box<dyn async fn fun() -> Trait> {
+    | +++++++                        +
+ help: you might have meant to write a bound here
+    |
+ LL | : Trait {
+    | ~
16 error: aborting due to 1 previous error
17 



The actual stderr differed from the expected stderr.
Actual stderr saved to /checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/traits/suggest-impl-on-bare-trait-return/suggest-impl-on-bare-trait-return.stderr
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args traits/suggest-impl-on-bare-trait-return.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/traits/suggest-impl-on-bare-trait-return.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/traits/suggest-impl-on-bare-trait-return" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/traits/suggest-impl-on-bare-trait-return/auxiliary" "--edition=2021"
--- stderr -------------------------------
error[E0782]: expected a type, found a trait
##[error]  --> /checkout/tests/ui/traits/suggest-impl-on-bare-trait-return.rs:4:19
   |
   |
LL | async fn fun() -> Trait {  //~ ERROR expected a type, found a trait
   |
help: use `impl Trait` to return an opaque type, as long as you return a single underlying type
   |
   |
LL | async fn fun() -> impl Trait {  //~ ERROR expected a type, found a trait
help: alternatively, you can return an owned trait object
   |
   |
LL | Box<dyn async fn fun() -> Trait> {  //~ ERROR expected a type, found a trait
   | +++++++                        +
help: you might have meant to write a bound here
   |
LL | : Trait {  //~ ERROR expected a type, found a trait
   | ~
error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0782`.
------------------------------------------

@hirschenberger hirschenberger marked this pull request as draft October 18, 2024 10:02
/// returns the `i32`.
///
/// [`OpaqueDef`]: hir::TyKind::OpaqueDef
pub fn get_future_inner_return_ty<'a>(hir_ty: &'a hir::Ty<'a>) -> Option<&'a hir::Ty<'a>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just make this an inherent method please. There's no use of &self. Also rename the 'a to 'tcx, as it is more consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function/method was moved from Mir to Hir. I thought compiler stages should only have access to previous stages, not following ones, right? I hope this is ok.

I saw that in the definition of get_future_inner_return_ty in Mir, self was not used so I made it a non-inherent (static?) method to provide access to the one place in Mir where it is used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, typo, I mean make it a free function. There's no reason to put this into an impl :)

@@ -223,10 +223,17 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
tcx.parent_hir_node(self_ty.hir_id),
hir::Node::Ty(hir::Ty { kind: hir::TyKind::Ref(..), .. })
);
let is_non_trait_object = |ty: &'tcx hir::Ty<'_>| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels unfortunately somewhat ad-hoc, since any place we want to inspect the hir::TyKind, like for any diagnostics, we'll need to call the get_future_inner_return_ty.

@hirschenberger
Copy link
Contributor Author

@compiler-errors @fmease

I'm working on this, as some cases are not correct yet. So feel free to delay reviews, although I appreciate your feedback.

@bors
Copy link
Contributor

bors commented Oct 31, 2024

☔ The latest upstream changes (presumably #132371) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Returning bare trait from an async function gives subpar suggestion
7 participants