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

Unexpected type inference around unsized coercion #104859

Open
Aegrithas opened this issue Nov 25, 2022 · 1 comment
Open

Unexpected type inference around unsized coercion #104859

Aegrithas opened this issue Nov 25, 2022 · 1 comment
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions A-trait-system Area: Trait system C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@Aegrithas
Copy link

I tried this code:

use std::any::Any;
use std::rc::Rc;

fn main() {
    let x = Rc::new(42i32);
    f(Rc::clone(&x));
}
 
fn f(_: Rc<dyn Any>) {}

I expected to see this happen:
Line 6 to infer

f(Rc::<i32>::clone(&x));

and compile successfully.

Instead, this happened:

error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
 --> src/main.rs:6:17
  |
6 |     f(Rc::clone(&x));
  |       --------- ^^ expected trait object `dyn Any`, found `i32`
  |       |
  |       arguments to this function are incorrect
  |
  = note: expected reference `&Rc<dyn Any>`
             found reference `&Rc<i32>`
note: associated function defined here

Changing line 6 to

f(Rc::<i32>::clone(&x));

works as expected, leading me to believe that the current behavior is inferring

f(Rc::<dyn Any>::clone(&x));

(which gives the same error).

Also of note is that moving x rather that borrowing works as expected, e.g.

fn id<T: ?Sized>(x: Rc<T>) -> Rc<T> {
  x
}
f(id(x));

Presumably, the difference is that

f(id::<dyn Any>(x));

works fine by coercing x before the call to id, whereas &Rc<i32> can't be coerced to &Rc<dyn Any>, so the coercion must happen after the call to Rc::clone, which doesn't play well with type inference.

Meta

rustc --version --verbose:

rustc 1.65.0 (897e37553 2022-11-02)
binary: rustc
commit-hash: 897e37553bba8b42751c67658967889d11ecd120
commit-date: 2022-11-02
host: x86_64-pc-windows-msvc
release: 1.65.0
LLVM version: 15.0.0

@Aegrithas Aegrithas added the C-bug Category: This is a bug. label Nov 25, 2022
@QuineDot
Copy link

QuineDot commented Feb 8, 2023

Dupe of #58716 and known for quite awhile. I don't know if there's a dedicated open issue or if it's officially a won't-enhance.

@fmease fmease added A-trait-system Area: Trait system A-coercions Area: implicit and explicit `expr as Type` coercions T-lang Relevant to the language team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue. and removed needs-triage-legacy T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions A-trait-system Area: Trait system C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants