Skip to content

Type inference breaks from a seemingly unrelated change #100882

Closed as not planned
Closed as not planned
@kamulos

Description

@kamulos

I was just making changes in one of the crates in my workspace, when the code broke in another crate of the workspace. Rust is no longer able to infer the type automatically, with an explicit annotation it works again.

Feel free to close this bug report if this is to be expected, but to me this seems very unusual for Rust.

This is the code:

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct S {}

fn main() {
    // let s: S = serde_json::from_str("").unwrap();
}

fn check_len(len: &str) {
    let vec = vec![1, 2, 3];

    if vec.len() != len.parse().unwrap() {
        panic!();
    }
}

Uncomment the serde_json line to trigger the bug. After uncommenting Rust needs an explicit type annotation as usize for len.parse().

Meta

rustc --version --verbose:

rustc 1.65.0-nightly (c0941dfb5 2022-08-21)
binary: rustc
commit-hash: c0941dfb5a7d07ef2d70cc54d319669d9d6f6c01
commit-date: 2022-08-21
host: x86_64-unknown-linux-gnu
release: 1.65.0-nightly
LLVM version: 15.0.0

This occurs also with the stable and beta compiler

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions