Skip to content

rustc does not suggest importing the old Range type #140240

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

Closed
ShE3py opened this issue Apr 24, 2025 · 1 comment · Fixed by #140795
Closed

rustc does not suggest importing the old Range type #140240

ShE3py opened this issue Apr 24, 2025 · 1 comment · Fixed by #140795
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ShE3py
Copy link
Contributor

ShE3py commented Apr 24, 2025

Code

const _: Range = 0..1;

Current output

error[E0412]: cannot find type `Range` in this scope
 --> <anon>:1:10
  |
1 | const _: Range = 0..1;
  |          ^^^^^ not found in this scope
  |
help: consider importing one of these structs
  |
1 + use std::collections::btree_map::Range;
  |
1 + use std::collections::btree_set::Range;
  |
1 + use std::range::Range;
  |
1 + use std::range::legacy::Range;
  |

error: aborting due to 1 previous error

Desired output

error[E0412]: cannot find type `Range` in this scope
 --> <anon>:1:10
  |
1 | const _: Range = 0..1;
  |          ^^^^^ not found in this scope
  |
help: consider importing one of these structs
  |
1 + use std::ops::Range;
  |
1 + use std::collections::btree_map::Range;
  |
1 + use std::collections::btree_set::Range;
  |
1 + use std::range::Range;
  |

error: aborting due to 1 previous error

Rationale and extra context

It should suggests importing the stable ops::Range; and ideally should points that range::Range is unstable, and not suggest legacy::Range (a unstable pub reexport of a stable type) as the reexported type is available.

I.e. importing one of those types will yield an error:

error[E0658]: use of unstable library feature `new_range_api`
 --> <anon>:1:5
  |
1 | use std::range::Range;
  |     ^^^^^^^^^^^^^^^^^
  |
  = note: see issue #125687 <https://github.com/rust-lang/rust/issues/125687> for more information

error[E0658]: use of unstable library feature `new_range_api`
 --> <anon>:2:5
  |
2 | use std::range::legacy::Range;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: see issue #125687 <https://github.com/rust-lang/rust/issues/125687> for more information

Rust Version

$ rustc -Vv
rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7
@ShE3py ShE3py added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 24, 2025
@jieyouxu jieyouxu added the A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` label Apr 24, 2025
@mu001999
Copy link
Contributor

@rustbot claim

@bors bors closed this as completed in 15df333 May 11, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue May 11, 2025
Rollup merge of rust-lang#140795 - mu001999-contrib:sugg-stable-import-first, r=petrochenkov

Prefer to suggest stable candidates rather than unstable ones

Fixes rust-lang#140240

The logic is to replace unstable suggestions if we meet a new stable one, and do nothing if any other situation. In old logic, we just use the first candidate we meet as the suggestion for the same items.

E.g., `std::range::legacy::Range` vs `std::ops::Range`, `legacy` in the former is unstable, we prefer to suggest use the latter.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants