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

Paths to extension trait functions in diagnostics can be confusing #125315

Open
vporton opened this issue May 20, 2024 · 2 comments
Open

Paths to extension trait functions in diagnostics can be confusing #125315

vporton opened this issue May 20, 2024 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@vporton
Copy link

vporton commented May 20, 2024

The following code compiles with a warning (on take function):

use std::ops::Deref;

pub trait MutexGuard<T>: Deref<Target = T> /*+ DerefMut<Target = T>*/ {
    fn set(&mut self, value: T);
    fn take(&self) -> Option<T>;
}

impl<T> MutexGuard<T> for tokio::sync::MutexGuard<'_, T> {
    fn set(&mut self, value: T) {
        *self.deref_mut() = value;
    }

    fn take(&self) -> Option<T> {
        tokio::sync::MutexGuard::take(self)
    }
}
warning: function cannot return without recursing
  --> src/lib.rs:13:5
   |
13 |     fn take(&self) -> Option<T> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
14 |         tokio::sync::MutexGuard::take(self)
   |         ----------------------------------- recursive call site
   |
   = help: a `loop` may express intention better if this is on purpose
   = note: `#[warn(unconditional_recursion)]` on by default

I specified a "full path" (tokio::sync::MutexGuard::take) to take function and it should use that path rather than using a trait.

$ rustc --version
rustc 1.78.0 (9b00956e5 2024-04-29)
@vporton vporton added the C-bug Category: This is a bug. label May 20, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 20, 2024
@workingjubilee
Copy link
Contributor

workingjubilee commented May 20, 2024

@vporton Is there a take function on tokio::sync::MutexGuard?

Not seeing one here? https://docs.rs/tokio/latest/tokio/sync/struct.MutexGuard.html

@workingjubilee
Copy link
Contributor

workingjubilee commented May 20, 2024

To be clear, @vporton, what you said would in fact be true if there was in fact a function to call. The detail is that by specifying the path to the type, you still perform lookup on the type, which will prefer to look up an inherent impl, and then will look for a trait impl, in that order, as demonstrated by this Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a6ddeec5f1848915158f205f5b1ffff0

Is there a diagnostic you would prefer? We obviously cannot emit a diagnostic every time someone gives the path to a type and it performs lookup that reaches a trait implementation, as very often that will be intentional. I suppose we could diagnose this specific recursion case?

@saethlin saethlin added A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 31, 2024
@saethlin saethlin changed the title Rust calls a wrong function Paths to extension trait functions in diagnostics can be confusing May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants