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

rust-analyzer emits type error where rustc compiles fine #15984

Open
cdoepmann opened this issue Nov 28, 2023 · 4 comments
Open

rust-analyzer emits type error where rustc compiles fine #15984

cdoepmann opened this issue Nov 28, 2023 · 4 comments
Labels
A-ty type system / type inference / traits / method resolution C-bug Category: bug

Comments

@cdoepmann
Copy link

rust-analyzer version: rust-analyzer version: 0.3.1748-standalone

rustc version: rustc 1.74.0 (79e9716c9 2023-11-13)

src/main.rs:

use crossbeam_channel::bounded;

fn main() {
    let (sender, _) = bounded(2);
    let value = 0usize;

    crossbeam_channel::select! {
        send(sender, value) -> _ => { todo!(); }
    };
}

Cargo.toml:

[package]
name = "bugtest"
version = "0.1.0"
edition = "2021"

[dependencies]
crossbeam-channel = "0.5.8"

This example compiles fine with rustc/cargo, but rust-analyzer emits the following error at the macro invocation:
expected &dyn SelectHandle, found &Sender<usize>

The error vanishes, if you add an explicit type during the channel construction: let (sender, _) = bounded::<usize>(2);

Since rustc compiles this fine, rust-analyzer should also accept it.

@cdoepmann cdoepmann added the C-bug Category: bug label Nov 28, 2023
@Veykril Veykril added the A-ty type system / type inference / traits / method resolution label Nov 28, 2023
@Austaras
Copy link
Contributor

Minimal reproduction

struct Sender<T> {
    inner: [T; 0],
}

impl<T> Sender<T> {
    fn send(&self, msg: T) {}
}

trait SelectHandle {}

impl<T> SelectHandle for Sender<T> {}

impl<T: SelectHandle> SelectHandle for &T {}

fn foo() {
    let sender = Sender { inner: [] };

    let _handle: &dyn SelectHandle = &sender;

    sender.send(0_usize);
}

@Austaras
Copy link
Contributor

So the problem here is r-a created a new type var '0 in

let sender = Sender { inner: [] };

for Sender<T>, then try to check if &Sender<'0> can be coerced to &dyn SelectHandle. So chalk would try to resolve Sender<'0>: SelectHandle, but fails because it doesn't know if '0 is sized or not.

The solution maybe is to explicitly tell chalk that '0 is Sized from the Sized bound of struct Sender, or maybe chalk could somehow infer that itself. But I don't know chalk enough to decide if it's possible.

This is quite similar to #11847.

@Austaras
Copy link
Contributor

A symmetric false positive

struct Sender<T> { val: [T; 0] }

impl<T> Sender<T> {
    fn send(&self, msg: T) {}
}

trait Foo {}

fn u<T: Foo>(sender: &Sender<T>) {}

fn foo() {
    let sender = Sender { val: []};

    u(&sender);

    sender.send(0_usize);
}

@jobafr
Copy link

jobafr commented Jan 9, 2025

The error vanishes, if you add an explicit type during the channel construction: let (sender, _) = bounded::<usize>(2);

Thank you for posting the workaround! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ty type system / type inference / traits / method resolution C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

4 participants