Skip to content

Unrelated compiler errors affects inferring a type #95648

Closed
@RaviKappiyoor

Description

@RaviKappiyoor

When I run this code (via cargo run):

use rust_decimal::Decimal;

fn main() {
  let test = Decimal::from(42);
  println!("My number is {}", test);
}

I get the output I expected (i.e., a single line of output that says "My number is 42").

However, if I run this code (via the same cargo run with no other changes):

use rust_decimal::Decimal;

fn main() {
  foo(3);
  let test = Decimal::from(42);
  println!("My number is {}", test);
}

I get two compiler errors. The first error indicates that foo is not in scope - this makes sense, I have not defined foo anywhere. However, the second error indicates (provided below) does not make sense:

error[E0283]: type annotations needed
 --> src/main.rs:5:16
  |
5 |     let test = Decimal::from(42);
  |                ^^^^^^^^^^^^^ cannot infer type for type `{integer}`
  |
  = note: multiple `impl`s satisfying `Decimal: From<{integer}>` found in the `rust_decimal` crate:
          - impl From<i128> for Decimal;
          - impl From<i16> for Decimal;
          - impl From<i32> for Decimal;
          - impl From<i64> for Decimal;
          and 8 more

It appears that, when there are no other compiler issues, the compiler can infer a type for 42 just fine. However, as soon as any other compiler issue appears, it loses the ability to infer type. That doesn't make sense to me - the ability to infer type should be unrelated to any other compiler errors present.

Meta

rustc --version --verbose:

rustc 1.58.1 (db9d1b20b 2022-01-20)
binary: rustc
commit-hash: db9d1b20bba1968c1ec1fc49616d4742c1725b4b
commit-date: 2022-01-20
host: x86_64-unknown-linux-gnu
release: 1.58.1
LLVM version: 13.0.0

Metadata

Metadata

Labels

C-bugCategory: This is a bug.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions