Skip to content

Type inference error when importing unrelated function. #85565

Closed
@neuring

Description

@neuring

I tried this code:

use serde_json::to_string;

pub fn some_function() {
    assert_eq!(3_u32, (0..3).sum());
}

Playground

I expected this to compile, but a type inference error was emitted.

error[E0283]: type annotations needed
 --> src/lib.rs:4:30
  |
4 |     assert_eq!(3_u32, (0..3).sum());
  |                              ^^^ cannot infer type for type parameter `S` declared on the associated function `sum`
  |
  = note: cannot satisfy `_: Sum<i32>`
help: consider specifying the type argument in the method call
  |
4 |     assert_eq!(3_u32, (0..3).sum::<S>());
  |                                 ^^^^^

If the import of serde_json::to_string is removed the code compiles just fine.

Meta

It fails on all stable, beta and nightly.

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