Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 75 additions & 65 deletions library/core/src/ops/range.rs

Large diffs are not rendered by default.

15 changes: 6 additions & 9 deletions src/tools/clippy/clippy_lints/src/ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ fn can_switch_ranges<'tcx>(
cx: &LateContext<'tcx>,
expr: &'tcx Expr<'_>,
original: RangeLimits,
inner_ty: Ty<'tcx>,
inner_ty_x: Ty<'tcx>,
inner_ty_y: Ty<'tcx>,
) -> bool {
let use_ctxt = expr_use_ctxt(cx, expr);
let (Node::Expr(parent_expr), false) = (use_ctxt.node, use_ctxt.is_ty_unified) else {
Expand Down Expand Up @@ -447,7 +448,7 @@ fn can_switch_ranges<'tcx>(
&& let switched_range_ty = cx
.tcx
.type_of(switched_range_def_id)
.instantiate(cx.tcx, &[inner_ty.into()])
.instantiate(cx.tcx, &[inner_ty_x.into(), inner_ty_y.into()])
// Check that the switched range type can be used for indexing the original expression
// through the `Index` or `IndexMut` trait.
&& let ty::Ref(_, outer_ty, mutability) = cx.typeck_results().expr_ty_adjusted(outer_expr).kind()
Expand Down Expand Up @@ -505,23 +506,19 @@ fn check_range_switch<'tcx>(
) {
if let Some(range) = higher::Range::hir(cx, expr)
&& let higher::Range {
start,
start: Some(start),
end: Some(end),
limits,
span,
} = range
&& span.can_be_used_for_suggestions()
&& limits == kind
&& let Some(y) = predicate(cx, end)
&& can_switch_ranges(cx, expr, kind, cx.typeck_results().expr_ty(y))
&& can_switch_ranges(cx, expr, kind, cx.typeck_results().expr_ty(start), cx.typeck_results().expr_ty(y))
{
span_lint_and_then(cx, lint, span, msg, |diag| {
let mut app = Applicability::MachineApplicable;
let start = start.map_or(String::new(), |x| {
Sugg::hir_with_applicability(cx, x, "<x>", &mut app)
.maybe_paren()
.to_string()
});
let start = Sugg::hir_with_applicability(cx, start, "<x>", &mut app).maybe_paren();
let end = Sugg::hir_with_applicability(cx, y, "<y>", &mut app).maybe_paren();
match span.with_source_text(cx, |src| src.starts_with('(') && src.ends_with(')')) {
Some(true) => {
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/feature-gates/feature-gate-new_range.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ error[E0308]: mismatched types
--> $DIR/feature-gate-new_range.rs:6:37
|
LL | let b: core::range::Range<u8> = 2..3;
| ---------------------- ^^^^ expected `Range<u8>`, found `Range<{integer}>`
| ---------------------- ^^^^ expected `Range<u8>`, found `Range<{integer}, {integer}>`
| |
| expected due to this
|
= note: expected struct `std::range::Range<u8>`
found struct `std::ops::Range<{integer}>`
help: call `Into::into` on this expression to convert `std::ops::Range<{integer}>` into `std::range::Range<u8>`
found struct `std::ops::Range<{integer}, {integer}>`
help: call `Into::into` on this expression to convert `std::ops::Range<{integer}, {integer}>` into `std::range::Range<u8>`
|
LL | let b: core::range::Range<u8> = (2..3).into();
| + ++++++++
Expand All @@ -32,13 +32,13 @@ error[E0308]: mismatched types
--> $DIR/feature-gate-new_range.rs:8:46
|
LL | let c: core::range::RangeInclusive<u8> = 4..=5;
| ------------------------------- ^^^^^ expected `RangeInclusive<u8>`, found `RangeInclusive<{integer}>`
| ------------------------------- ^^^^^ expected `RangeInclusive<u8>`, found `RangeInclusive<{integer}, {integer}>`
| |
| expected due to this
|
= note: expected struct `std::range::RangeInclusive<u8>`
found struct `std::ops::RangeInclusive<{integer}>`
help: call `Into::into` on this expression to convert `std::ops::RangeInclusive<{integer}>` into `std::range::RangeInclusive<u8>`
found struct `std::ops::RangeInclusive<{integer}, {integer}>`
help: call `Into::into` on this expression to convert `std::ops::RangeInclusive<{integer}, {integer}>` into `std::range::RangeInclusive<u8>`
|
LL | let c: core::range::RangeInclusive<u8> = (4..=5).into();
| + ++++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ error[E0308]: mismatched types
--> $DIR/exclusive_range_pattern_syntax_collision.rs:5:13
|
LL | match [5..4, 99..105, 43..44] {
| ----------------------- this expression has type `[std::ops::Range<{integer}>; 3]`
| ----------------------- this expression has type `[std::ops::Range<{integer}, {integer}>; 3]`
LL | [_, 99.., _] => {},
| ^^ expected `Range<{integer}>`, found integer
| ^^ expected `Range<{integer}, {integer}>`, found integer
|
= note: expected struct `std::ops::Range<{integer}>`
= note: expected struct `std::ops::Range<{integer}, {integer}>`
found type `{integer}`

error: aborting due to 1 previous error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ error[E0308]: mismatched types
--> $DIR/exclusive_range_pattern_syntax_collision2.rs:5:13
|
LL | match [5..4, 99..105, 43..44] {
| ----------------------- this expression has type `[std::ops::Range<{integer}>; 3]`
| ----------------------- this expression has type `[std::ops::Range<{integer}, {integer}>; 3]`
LL | [_, 99..] => {},
| ^^ expected `Range<{integer}>`, found integer
| ^^ expected `Range<{integer}, {integer}>`, found integer
|
= note: expected struct `std::ops::Range<{integer}>`
= note: expected struct `std::ops::Range<{integer}, {integer}>`
found type `{integer}`

error: aborting due to 2 previous errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@ error[E0308]: mismatched types
--> $DIR/exclusive_range_pattern_syntax_collision3.rs:3:12
|
LL | match [5..4, 99..105, 43..44] {
| ----------------------- this expression has type `[std::ops::Range<{integer}>; 3]`
| ----------------------- this expression has type `[std::ops::Range<{integer}, {integer}>; 3]`
LL | [..9, 99..100, _] => {},
| ^ expected `Range<{integer}>`, found integer
| ^ expected `Range<{integer}, {integer}>`, found integer
|
= note: expected struct `std::ops::Range<{integer}>`
= note: expected struct `std::ops::Range<{integer}, {integer}>`
found type `{integer}`

error[E0308]: mismatched types
--> $DIR/exclusive_range_pattern_syntax_collision3.rs:3:15
|
LL | match [5..4, 99..105, 43..44] {
| ----------------------- this expression has type `[std::ops::Range<{integer}>; 3]`
| ----------------------- this expression has type `[std::ops::Range<{integer}, {integer}>; 3]`
LL | [..9, 99..100, _] => {},
| ^^ --- this is of type `{integer}`
| |
| expected `Range<{integer}>`, found integer
| expected `Range<{integer}, {integer}>`, found integer
|
= note: expected struct `std::ops::Range<{integer}>`
= note: expected struct `std::ops::Range<{integer}, {integer}>`
found type `{integer}`

error[E0308]: mismatched types
--> $DIR/exclusive_range_pattern_syntax_collision3.rs:3:19
|
LL | match [5..4, 99..105, 43..44] {
| ----------------------- this expression has type `[std::ops::Range<{integer}>; 3]`
| ----------------------- this expression has type `[std::ops::Range<{integer}, {integer}>; 3]`
LL | [..9, 99..100, _] => {},
| -- ^^^ expected `Range<{integer}>`, found integer
| -- ^^^ expected `Range<{integer}, {integer}>`, found integer
| |
| this is of type `{integer}`
|
= note: expected struct `std::ops::Range<{integer}>`
= note: expected struct `std::ops::Range<{integer}, {integer}>`
found type `{integer}`

error: aborting due to 3 previous errors
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error[E0277]: expected a `FnMut(&<std::ops::RangeInclusive<{integer}> as Iterator>::Item)` closure, found `{closure@$DIR/closure-arg-type-mismatch-issue-45727.rs:6:29: 6:37}`
error[E0277]: expected a `FnMut(&<std::ops::RangeInclusive<{integer}, {integer}> as Iterator>::Item)` closure, found `{closure@$DIR/closure-arg-type-mismatch-issue-45727.rs:6:29: 6:37}`
--> $DIR/closure-arg-type-mismatch-issue-45727.rs:6:29
|
LL | let _ = (-10..=10).find(|x: i32| x.signum() == 0);
| ---- ^^^^^^^^^^^^^^^^^^^^^^^^ expected an `FnMut(&<std::ops::RangeInclusive<{integer}> as Iterator>::Item)` closure, found `{closure@$DIR/closure-arg-type-mismatch-issue-45727.rs:6:29: 6:37}`
| ---- ^^^^^^^^^^^^^^^^^^^^^^^^ expected an `FnMut(&<std::ops::RangeInclusive<{integer}, {integer}> as Iterator>::Item)` closure, found `{closure@$DIR/closure-arg-type-mismatch-issue-45727.rs:6:29: 6:37}`
| |
| required by a bound introduced by this call
|
= help: the trait `for<'a> FnMut(&'a <std::ops::RangeInclusive<{integer}> as Iterator>::Item)` is not implemented for closure `{closure@$DIR/closure-arg-type-mismatch-issue-45727.rs:6:29: 6:37}`
= note: expected a closure with signature `for<'a> fn(&'a <std::ops::RangeInclusive<{integer}> as Iterator>::Item)`
= help: the trait `for<'a> FnMut(&'a <std::ops::RangeInclusive<{integer}, {integer}> as Iterator>::Item)` is not implemented for closure `{closure@$DIR/closure-arg-type-mismatch-issue-45727.rs:6:29: 6:37}`
= note: expected a closure with signature `for<'a> fn(&'a <std::ops::RangeInclusive<{integer}, {integer}> as Iterator>::Item)`
found a closure with signature `fn(i32)`
note: required by a bound in `find`
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
Expand All @@ -18,16 +18,16 @@ error[E0271]: expected `RangeInclusive<{integer}>` to be an iterator that yields
LL | let _ = (-10..=10).find(|x: &&&i32| x.signum() == 0);
| ^^^^ expected `&&i32`, found integer

error[E0277]: expected a `FnMut(&<std::ops::RangeInclusive<{integer}> as Iterator>::Item)` closure, found `{closure@$DIR/closure-arg-type-mismatch-issue-45727.rs:9:29: 9:40}`
error[E0277]: expected a `FnMut(&<std::ops::RangeInclusive<{integer}, {integer}> as Iterator>::Item)` closure, found `{closure@$DIR/closure-arg-type-mismatch-issue-45727.rs:9:29: 9:40}`
--> $DIR/closure-arg-type-mismatch-issue-45727.rs:9:29
|
LL | let _ = (-10..=10).find(|x: &&&i32| x.signum() == 0);
| ---- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an `FnMut(&<std::ops::RangeInclusive<{integer}> as Iterator>::Item)` closure, found `{closure@$DIR/closure-arg-type-mismatch-issue-45727.rs:9:29: 9:40}`
| ---- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an `FnMut(&<std::ops::RangeInclusive<{integer}, {integer}> as Iterator>::Item)` closure, found `{closure@$DIR/closure-arg-type-mismatch-issue-45727.rs:9:29: 9:40}`
| |
| required by a bound introduced by this call
|
= help: the trait `for<'a> FnMut(&'a <std::ops::RangeInclusive<{integer}> as Iterator>::Item)` is not implemented for closure `{closure@$DIR/closure-arg-type-mismatch-issue-45727.rs:9:29: 9:40}`
= note: expected a closure with signature `for<'a> fn(&'a <std::ops::RangeInclusive<{integer}> as Iterator>::Item)`
= help: the trait `for<'a> FnMut(&'a <std::ops::RangeInclusive<{integer}, {integer}> as Iterator>::Item)` is not implemented for closure `{closure@$DIR/closure-arg-type-mismatch-issue-45727.rs:9:29: 9:40}`
= note: expected a closure with signature `for<'a> fn(&'a <std::ops::RangeInclusive<{integer}, {integer}> as Iterator>::Item)`
found a closure with signature `fn(&&&i32)`
note: required by a bound in `find`
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
Expand Down
17 changes: 1 addition & 16 deletions tests/ui/never_type/issue-96335.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,5 @@ LL - 0.....{loop{}1};
LL + 0..=..{loop{}1};
|

error[E0308]: mismatched types
--> $DIR/issue-96335.rs:2:9
|
LL | 0.....{loop{}1};
| ----^^^^^^^^^^^
| | |
| | expected integer, found `RangeTo<{integer}>`
| arguments to this function are incorrect
|
= note: expected type `{integer}`
found struct `RangeTo<{integer}>`
note: associated function defined here
--> $SRC_DIR/core/src/ops/range.rs:LL:COL

error: aborting due to 2 previous errors
error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
16 changes: 8 additions & 8 deletions tests/ui/range/issue-54505-no-literals.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:16:16
|
LL | take_range(std::ops::Range { start: 0, end: 1 });
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `Range<{integer}>`
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `Range<{integer}, {integer}>`
| |
| arguments to this function are incorrect
|
= note: expected reference `&_`
found struct `std::ops::Range<{integer}>`
found struct `std::ops::Range<{integer}, {integer}>`
note: function defined here
--> $DIR/issue-54505-no-literals.rs:12:4
|
Expand All @@ -22,12 +22,12 @@ error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:21:16
|
LL | take_range(::std::ops::Range { start: 0, end: 1 });
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `Range<{integer}>`
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `Range<{integer}, {integer}>`
| |
| arguments to this function are incorrect
|
= note: expected reference `&_`
found struct `std::ops::Range<{integer}>`
found struct `std::ops::Range<{integer}, {integer}>`
note: function defined here
--> $DIR/issue-54505-no-literals.rs:12:4
|
Expand Down Expand Up @@ -122,12 +122,12 @@ error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:46:16
|
LL | take_range(std::ops::RangeInclusive::new(0, 1));
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeInclusive<{integer}>`
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeInclusive<{integer}, {integer}>`
| |
| arguments to this function are incorrect
|
= note: expected reference `&_`
found struct `std::ops::RangeInclusive<{integer}>`
found struct `std::ops::RangeInclusive<{integer}, {integer}>`
note: function defined here
--> $DIR/issue-54505-no-literals.rs:12:4
|
Expand All @@ -142,12 +142,12 @@ error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:51:16
|
LL | take_range(::std::ops::RangeInclusive::new(0, 1));
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeInclusive<{integer}>`
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeInclusive<{integer}, {integer}>`
| |
| arguments to this function are incorrect
|
= note: expected reference `&_`
found struct `std::ops::RangeInclusive<{integer}>`
found struct `std::ops::RangeInclusive<{integer}, {integer}>`
note: function defined here
--> $DIR/issue-54505-no-literals.rs:12:4
|
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/range/issue-54505-no-std.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ error[E0308]: mismatched types
--> $DIR/issue-54505-no-std.rs:29:16
|
LL | take_range(0..1);
| ---------- ^^^^ expected `&_`, found `Range<{integer}>`
| ---------- ^^^^ expected `&_`, found `Range<{integer}, {integer}>`
| |
| arguments to this function are incorrect
|
= note: expected reference `&_`
found struct `core::ops::Range<{integer}>`
found struct `core::ops::Range<{integer}, {integer}>`
note: function defined here
--> $DIR/issue-54505-no-std.rs:25:4
|
Expand Down Expand Up @@ -67,12 +67,12 @@ error[E0308]: mismatched types
--> $DIR/issue-54505-no-std.rs:45:16
|
LL | take_range(0..=1);
| ---------- ^^^^^ expected `&_`, found `RangeInclusive<{integer}>`
| ---------- ^^^^^ expected `&_`, found `RangeInclusive<{integer}, {integer}>`
| |
| arguments to this function are incorrect
|
= note: expected reference `&_`
found struct `core::ops::RangeInclusive<{integer}>`
found struct `core::ops::RangeInclusive<{integer}, {integer}>`
note: function defined here
--> $DIR/issue-54505-no-std.rs:25:4
|
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/range/issue-54505.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ error[E0308]: mismatched types
--> $DIR/issue-54505.rs:14:16
|
LL | take_range(0..1);
| ---------- ^^^^ expected `&_`, found `Range<{integer}>`
| ---------- ^^^^ expected `&_`, found `Range<{integer}, {integer}>`
| |
| arguments to this function are incorrect
|
= note: expected reference `&_`
found struct `std::ops::Range<{integer}>`
found struct `std::ops::Range<{integer}, {integer}>`
note: function defined here
--> $DIR/issue-54505.rs:10:4
|
Expand Down Expand Up @@ -67,12 +67,12 @@ error[E0308]: mismatched types
--> $DIR/issue-54505.rs:30:16
|
LL | take_range(0..=1);
| ---------- ^^^^^ expected `&_`, found `RangeInclusive<{integer}>`
| ---------- ^^^^^ expected `&_`, found `RangeInclusive<{integer}, {integer}>`
| |
| arguments to this function are incorrect
|
= note: expected reference `&_`
found struct `std::ops::RangeInclusive<{integer}>`
found struct `std::ops::RangeInclusive<{integer}, {integer}>`
note: function defined here
--> $DIR/issue-54505.rs:10:4
|
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/range/issue-73553-misinterp-range-literal.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ error[E0308]: mismatched types
--> $DIR/issue-73553-misinterp-range-literal.rs:14:10
|
LL | demo(1..10);
| ---- ^^^^^ expected `&Range<usize>`, found `Range<{integer}>`
| ---- ^^^^^ expected `&Range<usize>`, found `Range<{integer}, {integer}>`
| |
| arguments to this function are incorrect
|
= note: expected reference `&std::ops::Range<usize>`
found struct `std::ops::Range<{integer}>`
= note: expected reference `&std::ops::Range<usize, usize>`
found struct `std::ops::Range<{integer}, {integer}>`
note: function defined here
--> $DIR/issue-73553-misinterp-range-literal.rs:3:4
|
Expand Down
11 changes: 2 additions & 9 deletions tests/ui/range/range-1.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
error[E0308]: mismatched types
--> $DIR/range-1.rs:5:19
|
LL | let _ = 0u32..10i32;
| ^^^^^ expected `u32`, found `i32`

error[E0277]: `std::ops::Range<bool>` is not an iterator
--> $DIR/range-1.rs:9:14
|
Expand All @@ -25,7 +19,6 @@ LL | let range = *arr..;
note: required by an implicit `Sized` bound in `std::ops::RangeFrom`
--> $SRC_DIR/core/src/ops/range.rs:LL:COL

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
For more information about this error, try `rustc --explain E0277`.
Loading
Loading