Skip to content

Commit f7d35cc

Browse files
committed
Fix range borrowing suggestions logic
1 parent 0626afb commit f7d35cc

File tree

5 files changed

+351
-13
lines changed

5 files changed

+351
-13
lines changed

src/librustc_typeck/check/demand.rs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
309309
};
310310
if self.can_coerce(ref_ty, expected) {
311311
if let Ok(src) = cm.span_to_snippet(sp) {
312-
let sugg_expr = match expr.node { // parenthesize if needed (Issue #46756)
313-
hir::ExprKind::Cast(_, _) |
314-
hir::ExprKind::Binary(_, _, _) |
315-
_ if self.is_range_literal(expr) => format!("({})", src),
316-
_ => src,
312+
let needs_parens = match expr.node {
313+
// parenthesize if needed (Issue #46756)
314+
hir::ExprKind::Cast(_, _) |
315+
hir::ExprKind::Binary(_, _, _) => true,
316+
// parenthesize borrows of range literals (Issue #54505)
317+
_ if self.is_range_literal(expr) => true,
318+
_ => false,
317319
};
320+
let sugg_expr = if needs_parens { format!("({})", src) } else { src };
321+
318322
if let Some(sugg) = self.can_use_as_ref(expr) {
319323
return Some(sugg);
320324
}
@@ -380,8 +384,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
380384
fn is_range_literal(&self, expr: &hir::Expr) -> bool {
381385
use hir::{Path, QPath, ExprKind, TyKind};
382386

383-
// TODO how to work out std vs core here?
384-
let ops_path = ["{{root}}", "std", "ops"];
387+
// we support `::std::ops::Range` and `::std::core::Range` prefixes
388+
// (via split on "|")
389+
let ops_path = ["{{root}}", "std|core", "ops"];
385390

386391
let is_range_path = |path: &Path| {
387392
let ident_names: Vec<_> = path.segments
@@ -394,24 +399,47 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
394399
preceding.len() == 3 &&
395400
preceding.iter()
396401
.zip(ops_path.iter())
397-
.all(|(a, b)| a == b)
402+
.all(|(seg, match_seg)| {
403+
match_seg.split("|")
404+
.into_iter()
405+
.any(|ref spl_seg| seg == spl_seg)
406+
})
407+
} else {
408+
false
409+
}
410+
};
411+
412+
let is_range_struct_snippet = |span: &Span| {
413+
// Tell if expression span snippet looks like an explicit
414+
// Range struct or new() call. This is to allow rejecting
415+
// Ranges constructed with non-literals.
416+
let source_map = self.tcx.sess.source_map();
417+
let end_point = source_map.end_point(*span);
418+
419+
if let Ok(end_string) = source_map.span_to_snippet(end_point) {
420+
end_string.ends_with("}") || end_string.ends_with(")")
398421
} else {
399422
false
400423
}
424+
401425
};
402426

403427
match expr.node {
428+
// all built-in range literals but `..=` and `..`
429+
// desugar to Structs, `..` desugars to its struct path
404430
ExprKind::Struct(QPath::Resolved(None, ref path), _, _) |
405431
ExprKind::Path(QPath::Resolved(None, ref path)) => {
406-
return is_range_path(&path);
432+
return is_range_path(&path) && !is_range_struct_snippet(&expr.span);
407433
}
408434

435+
// `..=` desugars into RangeInclusive::new(...)
409436
ExprKind::Call(ref func, _) => {
410437
if let ExprKind::Path(QPath::TypeRelative(ref ty, ref segment)) = func.node {
411438
if let TyKind::Path(QPath::Resolved(None, ref path)) = ty.node {
412439
let calls_new = segment.ident.as_str() == "new";
413440

414-
return is_range_path(&path) && calls_new;
441+
return is_range_path(&path) && calls_new &&
442+
!is_range_struct_snippet(&expr.span);
415443
}
416444
}
417445
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// run-rustfix
12+
13+
// Regression test for changes introduced while fixing #54505
14+
15+
// This test uses non-literals for Ranges
16+
// (expecting no parens with borrow suggestion)
17+
18+
use std::ops::RangeBounds;
19+
20+
21+
// take a reference to any built-in range
22+
fn take_range(_r: &impl RangeBounds<i8>) {}
23+
24+
25+
fn main() {
26+
take_range(&std::ops::Range { start: 0, end: 1 });
27+
//~^ ERROR mismatched types [E0308]
28+
//~| HELP consider borrowing here
29+
//~| SUGGESTION &std::ops::Range { start: 0, end: 1 }
30+
31+
take_range(&::std::ops::Range { start: 0, end: 1 });
32+
//~^ ERROR mismatched types [E0308]
33+
//~| HELP consider borrowing here
34+
//~| SUGGESTION &::std::ops::Range { start: 0, end: 1 }
35+
36+
take_range(&std::ops::RangeFrom { start: 1 });
37+
//~^ ERROR mismatched types [E0308]
38+
//~| HELP consider borrowing here
39+
//~| SUGGESTION &std::ops::RangeFrom { start: 1 }
40+
41+
take_range(&::std::ops::RangeFrom { start: 1 });
42+
//~^ ERROR mismatched types [E0308]
43+
//~| HELP consider borrowing here
44+
//~| SUGGESTION &::std::ops::RangeFrom { start: 1 }
45+
46+
take_range(&std::ops::RangeFull {});
47+
//~^ ERROR mismatched types [E0308]
48+
//~| HELP consider borrowing here
49+
//~| SUGGESTION &std::ops::RangeFull {}
50+
51+
take_range(&::std::ops::RangeFull {});
52+
//~^ ERROR mismatched types [E0308]
53+
//~| HELP consider borrowing here
54+
//~| SUGGESTION &::std::ops::RangeFull {}
55+
56+
take_range(&std::ops::RangeInclusive::new(0, 1));
57+
//~^ ERROR mismatched types [E0308]
58+
//~| HELP consider borrowing here
59+
//~| SUGGESTION &std::ops::RangeInclusive::new(0, 1)
60+
61+
take_range(&::std::ops::RangeInclusive::new(0, 1));
62+
//~^ ERROR mismatched types [E0308]
63+
//~| HELP consider borrowing here
64+
//~| SUGGESTION &::std::ops::RangeInclusive::new(0, 1)
65+
66+
take_range(&std::ops::RangeTo { end: 5 });
67+
//~^ ERROR mismatched types [E0308]
68+
//~| HELP consider borrowing here
69+
//~| SUGGESTION &std::ops::RangeTo { end: 5 }
70+
71+
take_range(&::std::ops::RangeTo { end: 5 });
72+
//~^ ERROR mismatched types [E0308]
73+
//~| HELP consider borrowing here
74+
//~| SUGGESTION &::std::ops::RangeTo { end: 5 }
75+
76+
take_range(&std::ops::RangeToInclusive { end: 5 });
77+
//~^ ERROR mismatched types [E0308]
78+
//~| HELP consider borrowing here
79+
//~| SUGGESTION &std::ops::RangeToInclusive { end: 5 }
80+
81+
take_range(&::std::ops::RangeToInclusive { end: 5 });
82+
//~^ ERROR mismatched types [E0308]
83+
//~| HELP consider borrowing here
84+
//~| SUGGESTION &::std::ops::RangeToInclusive { end: 5 }
85+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-54505-no-literals.rs:26:16
3+
|
4+
LL | take_range(std::ops::Range { start: 0, end: 1 });
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| expected reference, found struct `std::ops::Range`
8+
| help: consider borrowing here: `&std::ops::Range { start: 0, end: 1 }`
9+
|
10+
= note: expected type `&_`
11+
found type `std::ops::Range<{integer}>`
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/issue-54505-no-literals.rs:31:16
15+
|
16+
LL | take_range(::std::ops::Range { start: 0, end: 1 });
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
| |
19+
| expected reference, found struct `std::ops::Range`
20+
| help: consider borrowing here: `&::std::ops::Range { start: 0, end: 1 }`
21+
|
22+
= note: expected type `&_`
23+
found type `std::ops::Range<{integer}>`
24+
25+
error[E0308]: mismatched types
26+
--> $DIR/issue-54505-no-literals.rs:36:16
27+
|
28+
LL | take_range(std::ops::RangeFrom { start: 1 });
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
| |
31+
| expected reference, found struct `std::ops::RangeFrom`
32+
| help: consider borrowing here: `&std::ops::RangeFrom { start: 1 }`
33+
|
34+
= note: expected type `&_`
35+
found type `std::ops::RangeFrom<{integer}>`
36+
37+
error[E0308]: mismatched types
38+
--> $DIR/issue-54505-no-literals.rs:41:16
39+
|
40+
LL | take_range(::std::ops::RangeFrom { start: 1 });
41+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
42+
| |
43+
| expected reference, found struct `std::ops::RangeFrom`
44+
| help: consider borrowing here: `&::std::ops::RangeFrom { start: 1 }`
45+
|
46+
= note: expected type `&_`
47+
found type `std::ops::RangeFrom<{integer}>`
48+
49+
error[E0308]: mismatched types
50+
--> $DIR/issue-54505-no-literals.rs:46:16
51+
|
52+
LL | take_range(std::ops::RangeFull {});
53+
| ^^^^^^^^^^^^^^^^^^^^^^
54+
| |
55+
| expected reference, found struct `std::ops::RangeFull`
56+
| help: consider borrowing here: `&std::ops::RangeFull {}`
57+
|
58+
= note: expected type `&_`
59+
found type `std::ops::RangeFull`
60+
61+
error[E0308]: mismatched types
62+
--> $DIR/issue-54505-no-literals.rs:51:16
63+
|
64+
LL | take_range(::std::ops::RangeFull {});
65+
| ^^^^^^^^^^^^^^^^^^^^^^^^
66+
| |
67+
| expected reference, found struct `std::ops::RangeFull`
68+
| help: consider borrowing here: `&::std::ops::RangeFull {}`
69+
|
70+
= note: expected type `&_`
71+
found type `std::ops::RangeFull`
72+
73+
error[E0308]: mismatched types
74+
--> $DIR/issue-54505-no-literals.rs:56:16
75+
|
76+
LL | take_range(std::ops::RangeInclusive::new(0, 1));
77+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
78+
| |
79+
| expected reference, found struct `std::ops::RangeInclusive`
80+
| help: consider borrowing here: `&std::ops::RangeInclusive::new(0, 1)`
81+
|
82+
= note: expected type `&_`
83+
found type `std::ops::RangeInclusive<{integer}>`
84+
85+
error[E0308]: mismatched types
86+
--> $DIR/issue-54505-no-literals.rs:61:16
87+
|
88+
LL | take_range(::std::ops::RangeInclusive::new(0, 1));
89+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
90+
| |
91+
| expected reference, found struct `std::ops::RangeInclusive`
92+
| help: consider borrowing here: `&::std::ops::RangeInclusive::new(0, 1)`
93+
|
94+
= note: expected type `&_`
95+
found type `std::ops::RangeInclusive<{integer}>`
96+
97+
error[E0308]: mismatched types
98+
--> $DIR/issue-54505-no-literals.rs:66:16
99+
|
100+
LL | take_range(std::ops::RangeTo { end: 5 });
101+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
102+
| |
103+
| expected reference, found struct `std::ops::RangeTo`
104+
| help: consider borrowing here: `&std::ops::RangeTo { end: 5 }`
105+
|
106+
= note: expected type `&_`
107+
found type `std::ops::RangeTo<{integer}>`
108+
109+
error[E0308]: mismatched types
110+
--> $DIR/issue-54505-no-literals.rs:71:16
111+
|
112+
LL | take_range(::std::ops::RangeTo { end: 5 });
113+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
114+
| |
115+
| expected reference, found struct `std::ops::RangeTo`
116+
| help: consider borrowing here: `&::std::ops::RangeTo { end: 5 }`
117+
|
118+
= note: expected type `&_`
119+
found type `std::ops::RangeTo<{integer}>`
120+
121+
error[E0308]: mismatched types
122+
--> $DIR/issue-54505-no-literals.rs:76:16
123+
|
124+
LL | take_range(std::ops::RangeToInclusive { end: 5 });
125+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
126+
| |
127+
| expected reference, found struct `std::ops::RangeToInclusive`
128+
| help: consider borrowing here: `&std::ops::RangeToInclusive { end: 5 }`
129+
|
130+
= note: expected type `&_`
131+
found type `std::ops::RangeToInclusive<{integer}>`
132+
133+
error[E0308]: mismatched types
134+
--> $DIR/issue-54505-no-literals.rs:81:16
135+
|
136+
LL | take_range(::std::ops::RangeToInclusive { end: 5 });
137+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
138+
| |
139+
| expected reference, found struct `std::ops::RangeToInclusive`
140+
| help: consider borrowing here: `&::std::ops::RangeToInclusive { end: 5 }`
141+
|
142+
= note: expected type `&_`
143+
found type `std::ops::RangeToInclusive<{integer}>`
144+
145+
error: aborting due to 12 previous errors
146+
147+
For more information about this error, try `rustc --explain E0308`.

src/test/ui/range/issue-54505-no-std.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// run-rustfix
12-
// error-pattern: error: `#[panic_handler]` function required, but not found
13-
// error-pattern: language item required, but not found: `panic_info`
11+
// error-pattern: `#[panic_handler]` function required, but not found
12+
// error-pattern: language item required, but not found: `eh_personality`
1413

1514

1615
// Regression test for #54505 - range borrowing suggestion had

0 commit comments

Comments
 (0)