Skip to content

Mismatched types suggestion has incorrect precedence: 0..10.into() instead of (0..10).into() #148344

@kpreid

Description

@kpreid

Code

use std::ops::Range;

/// Could also use std::range::Range but that’s unstable
struct Strange;
impl From<Range<usize>> for Strange {
    fn from(_: Range<usize>) -> Self {
        Self
    }
}

fn example() {
    let _: Strange = 0..10;
}

Current output

error[E0308]: mismatched types
  --> src/lib.rs:12:22
   |
12 |     let _: Strange = 0..10;
   |            -------   ^^^^^ expected `Strange`, found `Range<{integer}>`
   |            |
   |            expected due to this
   |
   = note: expected struct `Strange`
              found struct `std::ops::Range<{integer}>`
help: call `Into::into` on this expression to convert `std::ops::Range<{integer}>` into `Strange`
   |
12 |     let _: Strange = 0..10.into();
   |                           +++++++

Desired output

|
12 |     let _: Strange = (0..10).into();
   |                      +     ++++++++

Rationale and extra context

The suggestion does not respect the precedence of the .. operator.

I found this problem while trying out feature(new_range), which frequently requires converting new ranges to legacy ranges.

Rust Version

rustc 1.93.0-nightly (292be5c7c 2025-10-29)
binary: rustc
commit-hash: 292be5c7c05138d753bbd4b30db7a3f1a5c914f7
commit-date: 2025-10-29
host: aarch64-apple-darwin
release: 1.93.0-nightly
LLVM version: 21.1.3

rustc 1.91.0 (f8297e351 2025-10-28)
binary: rustc
commit-hash: f8297e351a40c1439a467bbbb6879088047f50b3
commit-date: 2025-10-28
host: aarch64-apple-darwin
release: 1.91.0
LLVM version: 21.1.2

Anything else?

@rustbot label D-invalid-suggestion

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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