rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
93.58k stars 12.05k forks source link

[E0277] possibly false positive, error in either cae #124763

Closed bravequickcleverfibreyarn closed 1 week ago

bravequickcleverfibreyarn commented 1 week ago

Code

struct Test {
    field: String,
}

impl std::ops::Deref for Test {
    type Target = str;
    fn deref(&self) -> &Self::Target {
        &self.field
    }
}

fn main() {
    let test = Test {
        field: String::new(),
    };

    let test_ref = &test;

    let d1 = *test_ref;
    let d = *test;
}

Current output

error[E0277]: the size for values of type `str` cannot be known at compilation time
  --> src/main.rs:20:9
   |
20 |     let d = *test;
   |         ^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `str`
   = note: all local variables must have a statically known size
   = help: unsized locals are gated as an unstable feature

For more information about this error, try `rustc --explain E0277`.
error: could not compile `e0277` (bin "e0277") due to previous error

Desired output

No response

Rationale and extra context

FMPOV, analyzer/compiler tries to fit dereference (*) on existing Deref implementation. During this it happily "upgrades" deref signature from consuming ref and returning ref onto move semantics. Thus it reports no Sized implementation for str while declared is &str.

Other cases

No response

Rust Version

rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: x86_64-unknown-linux-gnu
release: 1.73.0
LLVM version: 17.0.2

Anything else?

No response

bravequickcleverfibreyarn commented 1 week ago

It is caused by automatic reference. So, * is called on &str eventually.