Skip to content

Commit 3f1bce5

Browse files
authored
Rollup merge of #148567 - chenyukang:yukang-fix-148344-incorrect-precedence, r=hkBst,Kivooeo
Fix incorrect precedence caused by range expression Fixes #148344 The testcase `tests/ui/feature-gates/feature-gate-new_range` is also fixed.
2 parents 3186f21 + ff5440e commit 3f1bce5

File tree

5 files changed

+62
-6
lines changed

5 files changed

+62
-6
lines changed

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7777
}
7878
false
7979
};
80+
81+
// Special case: range expressions are desugared to struct literals in HIR,
82+
// so they would normally return `Unambiguous` precedence in expr.precedence.
83+
// we should return `Range` precedence for correct parenthesization in suggestions.
84+
if is_range_literal(expr) {
85+
return ExprPrecedence::Range;
86+
}
87+
8088
expr.precedence(&has_attr)
8189
}
8290

tests/ui/feature-gates/feature-gate-new_range.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ LL | let a: core::range::RangeFrom<u8> = 1..;
1010
found struct `std::ops::RangeFrom<{integer}>`
1111
help: call `Into::into` on this expression to convert `std::ops::RangeFrom<{integer}>` into `std::range::RangeFrom<u8>`
1212
|
13-
LL | let a: core::range::RangeFrom<u8> = 1...into();
14-
| +++++++
13+
LL | let a: core::range::RangeFrom<u8> = (1..).into();
14+
| + ++++++++
1515

1616
error[E0308]: mismatched types
1717
--> $DIR/feature-gate-new_range.rs:6:37
@@ -25,8 +25,8 @@ LL | let b: core::range::Range<u8> = 2..3;
2525
found struct `std::ops::Range<{integer}>`
2626
help: call `Into::into` on this expression to convert `std::ops::Range<{integer}>` into `std::range::Range<u8>`
2727
|
28-
LL | let b: core::range::Range<u8> = 2..3.into();
29-
| +++++++
28+
LL | let b: core::range::Range<u8> = (2..3).into();
29+
| + ++++++++
3030

3131
error[E0308]: mismatched types
3232
--> $DIR/feature-gate-new_range.rs:8:46
@@ -40,8 +40,8 @@ LL | let c: core::range::RangeInclusive<u8> = 4..=5;
4040
found struct `std::ops::RangeInclusive<{integer}>`
4141
help: call `Into::into` on this expression to convert `std::ops::RangeInclusive<{integer}>` into `std::range::RangeInclusive<u8>`
4242
|
43-
LL | let c: core::range::RangeInclusive<u8> = 4..=5.into();
44-
| +++++++
43+
LL | let c: core::range::RangeInclusive<u8> = (4..=5).into();
44+
| + ++++++++
4545

4646
error: aborting due to 3 previous errors
4747

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ run-rustfix
2+
use std::ops::Range;
3+
4+
struct Strange;
5+
impl From<Range<usize>> for Strange {
6+
fn from(_: Range<usize>) -> Self {
7+
Self
8+
}
9+
}
10+
11+
fn main() {
12+
let _: Strange = (0..10).into();
13+
//~^ ERROR mismatched types
14+
//~| HELP call `Into::into` on this expression
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ run-rustfix
2+
use std::ops::Range;
3+
4+
struct Strange;
5+
impl From<Range<usize>> for Strange {
6+
fn from(_: Range<usize>) -> Self {
7+
Self
8+
}
9+
}
10+
11+
fn main() {
12+
let _: Strange = 0..10;
13+
//~^ ERROR mismatched types
14+
//~| HELP call `Into::into` on this expression
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/into-convert-range-issue-148344.rs:12:22
3+
|
4+
LL | let _: Strange = 0..10;
5+
| ------- ^^^^^ expected `Strange`, found `Range<{integer}>`
6+
| |
7+
| expected due to this
8+
|
9+
= note: expected struct `Strange`
10+
found struct `std::ops::Range<{integer}>`
11+
help: call `Into::into` on this expression to convert `std::ops::Range<{integer}>` into `Strange`
12+
|
13+
LL | let _: Strange = (0..10).into();
14+
| + ++++++++
15+
16+
error: aborting due to 1 previous error
17+
18+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)