Skip to content

Fix range literals borrowing suggestions #54734

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

Merged
merged 6 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
63 changes: 28 additions & 35 deletions src/librustc_typeck/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
_ if self.is_range_literal(expr) => true,
_ => false,
};
let sugg_expr = if needs_parens { format!("({})", src) } else { src };
let sugg_expr = if needs_parens {
format!("({})", src)
} else {
src
};

if let Some(sugg) = self.can_use_as_ref(expr) {
return Some(sugg);
Expand Down Expand Up @@ -379,67 +383,56 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
None
}

// This function checks if the specified expression is a built-in range literal
// (See: librustc/hir/lowering.rs::LoweringContext::lower_expr() )
/// This function checks if the specified expression is a built-in range literal.
/// (See: ``librustc::hir::lowering::LoweringContext::lower_expr()``).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant single rather than double backticks; I should have clarified. On second thoughts, though, it might be more convenient to include the path:

/// (See `LoweringContext::lower_expr()` in `src/librustc/hir/lowering.rs`.)

as then someone can jump straight to the path. (Like in the following.)
https://github.com/rust-lang/rust/blob/f89622a7548f69c76e5c9857e34be568ba322d02/src/librustc_privacy/lib.rs#L1388

fn is_range_literal(&self, expr: &hir::Expr) -> bool {
use hir::{Path, QPath, ExprKind, TyKind};

// we support `::std::ops::Range` and `::std::core::Range` prefixes
// (via split on "|")
let ops_path = ["{{root}}", "std|core", "ops"];

// We support `::std::ops::Range` and `::core::ops::Range` prefixes
let is_range_path = |path: &Path| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be alone in this (cc @estebank), but I think for a short path like this, a more explicit approach (like below) is more readable. (I specifically don't like splitting on "|" because it feels a little too ad hoc.)

let is_range_path = |path: &Path| {
    let segs = path.segments.iter().map(|seg| seg.ident.as_str());
	if let (Some("{{root}}"), std_core, "ops", range, None) = (segs.next(), segs.next(), segs.next(), segs.next(), segs.next()) {
		(std_core == "std" || std_core == "ops") && range.starts_with("Range")
	} else {
		false
	}
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a great suggestion! I was looking for something concise like this syntax-wise, it didn't occur to me that I could use .next() and rely on it returning None when there's no more elements.

let ident_names: Vec<_> = path.segments
.iter()
.map(|seg| seg.ident.as_str())
.collect();

if let Some((last, preceding)) = ident_names.split_last() {
last.starts_with("Range") &&
preceding.len() == 3 &&
preceding.iter()
.zip(ops_path.iter())
.all(|(seg, match_seg)| {
match_seg.split("|")
.into_iter()
.any(|ref spl_seg| seg == spl_seg)
})
let mut segs = path.segments.iter()
.map(|seg| seg.ident.as_str());

if let (Some(root), Some(std_core), Some(ops), Some(range), None) =
(segs.next(), segs.next(), segs.next(), segs.next(), segs.next())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ident.as_str() returns a LocalInternedString which can be compared with &str, but I found it too verbose to use in Some().

{
// "{{root}}" is the equivalent of `::` prefix in Path
root == "{{root}}" && (std_core == "std" || std_core == "core")
&& ops == "ops" && range.starts_with("Range")
} else {
false
}
};

let is_range_struct_snippet = |span: &Span| {
// Tell if expression span snippet looks like an explicit
// Range struct or new() call. This is to allow rejecting
// Ranges constructed with non-literals.
let is_range_literal = |span: &Span| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you pointed out, it would be clearer to give this a different name than the function it's in. Maybe span_is_range_literal or something like that?

// Tell if expression span snippet doesn't look like an explicit
// Range struct or `new()` call. This is to allow inferring
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Inferring" has a very specific meaning, so I'd tend to avoid it in favour of something like:

// Check whether a span corresponding to a range is a range literal,
// rather than an explicit struct or `new()` call.

// that this is a range literal.
let source_map = self.tcx.sess.source_map();
let end_point = source_map.end_point(*span);

if let Ok(end_string) = source_map.span_to_snippet(end_point) {
end_string.ends_with("}") || end_string.ends_with(")")
!(end_string.ends_with("}") || end_string.ends_with(")"))
} else {
false
}

};

match expr.node {
// all built-in range literals but `..=` and `..`
// desugar to Structs, `..` desugars to its struct path
// All built-in range literals but `..=` and `..` desugar to Structs
ExprKind::Struct(QPath::Resolved(None, ref path), _, _) |
// `..` desugars to its struct path
ExprKind::Path(QPath::Resolved(None, ref path)) => {
return is_range_path(&path) && !is_range_struct_snippet(&expr.span);
return is_range_path(&path) && is_range_literal(&expr.span);
}

// `..=` desugars into RangeInclusive::new(...)
// `..=` desugars into `::std::ops::RangeInclusive::new(...)`
ExprKind::Call(ref func, _) => {
if let ExprKind::Path(QPath::TypeRelative(ref ty, ref segment)) = func.node {
if let TyKind::Path(QPath::Resolved(None, ref path)) = ty.node {
let calls_new = segment.ident.as_str() == "new";
let call_to_new = segment.ident.as_str() == "new";

return is_range_path(&path) && calls_new &&
!is_range_struct_snippet(&expr.span);
return is_range_path(&path) && is_range_literal(&expr.span) && call_to_new;
}
}
}
Expand Down
10 changes: 0 additions & 10 deletions src/test/ui/range/issue-54505-no-literals.fixed
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// run-rustfix

// Regression test for changes introduced while fixing #54505
Expand Down
10 changes: 0 additions & 10 deletions src/test/ui/range/issue-54505-no-literals.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// run-rustfix

// Regression test for changes introduced while fixing #54505
Expand Down
24 changes: 12 additions & 12 deletions src/test/ui/range/issue-54505-no-literals.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:26:16
--> $DIR/issue-54505-no-literals.rs:16:16
|
LL | take_range(std::ops::Range { start: 0, end: 1 });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -11,7 +11,7 @@ LL | take_range(std::ops::Range { start: 0, end: 1 });
found type `std::ops::Range<{integer}>`

error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:31:16
--> $DIR/issue-54505-no-literals.rs:21:16
|
LL | take_range(::std::ops::Range { start: 0, end: 1 });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -23,7 +23,7 @@ LL | take_range(::std::ops::Range { start: 0, end: 1 });
found type `std::ops::Range<{integer}>`

error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:36:16
--> $DIR/issue-54505-no-literals.rs:26:16
|
LL | take_range(std::ops::RangeFrom { start: 1 });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -35,7 +35,7 @@ LL | take_range(std::ops::RangeFrom { start: 1 });
found type `std::ops::RangeFrom<{integer}>`

error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:41:16
--> $DIR/issue-54505-no-literals.rs:31:16
|
LL | take_range(::std::ops::RangeFrom { start: 1 });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -47,7 +47,7 @@ LL | take_range(::std::ops::RangeFrom { start: 1 });
found type `std::ops::RangeFrom<{integer}>`

error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:46:16
--> $DIR/issue-54505-no-literals.rs:36:16
|
LL | take_range(std::ops::RangeFull {});
| ^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -59,7 +59,7 @@ LL | take_range(std::ops::RangeFull {});
found type `std::ops::RangeFull`

error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:51:16
--> $DIR/issue-54505-no-literals.rs:41:16
|
LL | take_range(::std::ops::RangeFull {});
| ^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -71,7 +71,7 @@ LL | take_range(::std::ops::RangeFull {});
found type `std::ops::RangeFull`

error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:56:16
--> $DIR/issue-54505-no-literals.rs:46:16
|
LL | take_range(std::ops::RangeInclusive::new(0, 1));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -83,7 +83,7 @@ LL | take_range(std::ops::RangeInclusive::new(0, 1));
found type `std::ops::RangeInclusive<{integer}>`

error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:61:16
--> $DIR/issue-54505-no-literals.rs:51:16
|
LL | take_range(::std::ops::RangeInclusive::new(0, 1));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -95,7 +95,7 @@ LL | take_range(::std::ops::RangeInclusive::new(0, 1));
found type `std::ops::RangeInclusive<{integer}>`

error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:66:16
--> $DIR/issue-54505-no-literals.rs:56:16
|
LL | take_range(std::ops::RangeTo { end: 5 });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -107,7 +107,7 @@ LL | take_range(std::ops::RangeTo { end: 5 });
found type `std::ops::RangeTo<{integer}>`

error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:71:16
--> $DIR/issue-54505-no-literals.rs:61:16
|
LL | take_range(::std::ops::RangeTo { end: 5 });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -119,7 +119,7 @@ LL | take_range(::std::ops::RangeTo { end: 5 });
found type `std::ops::RangeTo<{integer}>`

error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:76:16
--> $DIR/issue-54505-no-literals.rs:66:16
|
LL | take_range(std::ops::RangeToInclusive { end: 5 });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -131,7 +131,7 @@ LL | take_range(std::ops::RangeToInclusive { end: 5 });
found type `std::ops::RangeToInclusive<{integer}>`

error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:81:16
--> $DIR/issue-54505-no-literals.rs:71:16
|
LL | take_range(::std::ops::RangeToInclusive { end: 5 });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
10 changes: 0 additions & 10 deletions src/test/ui/range/issue-54505-no-std.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// error-pattern: `#[panic_handler]` function required, but not found
// error-pattern: language item required, but not found: `eh_personality`

Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/range/issue-54505-no-std.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ error: `#[panic_handler]` function required, but not found
error: language item required, but not found: `eh_personality`

error[E0308]: mismatched types
--> $DIR/issue-54505-no-std.rs:31:16
--> $DIR/issue-54505-no-std.rs:21:16
|
LL | take_range(0..1);
| ^^^^
Expand All @@ -15,7 +15,7 @@ LL | take_range(0..1);
found type `core::ops::Range<{integer}>`

error[E0308]: mismatched types
--> $DIR/issue-54505-no-std.rs:36:16
--> $DIR/issue-54505-no-std.rs:26:16
|
LL | take_range(1..);
| ^^^
Expand All @@ -27,7 +27,7 @@ LL | take_range(1..);
found type `core::ops::RangeFrom<{integer}>`

error[E0308]: mismatched types
--> $DIR/issue-54505-no-std.rs:41:16
--> $DIR/issue-54505-no-std.rs:31:16
|
LL | take_range(..);
| ^^
Expand All @@ -39,7 +39,7 @@ LL | take_range(..);
found type `core::ops::RangeFull`

error[E0308]: mismatched types
--> $DIR/issue-54505-no-std.rs:46:16
--> $DIR/issue-54505-no-std.rs:36:16
|
LL | take_range(0..=1);
| ^^^^^
Expand All @@ -51,7 +51,7 @@ LL | take_range(0..=1);
found type `core::ops::RangeInclusive<{integer}>`

error[E0308]: mismatched types
--> $DIR/issue-54505-no-std.rs:51:16
--> $DIR/issue-54505-no-std.rs:41:16
|
LL | take_range(..5);
| ^^^
Expand All @@ -63,7 +63,7 @@ LL | take_range(..5);
found type `core::ops::RangeTo<{integer}>`

error[E0308]: mismatched types
--> $DIR/issue-54505-no-std.rs:56:16
--> $DIR/issue-54505-no-std.rs:46:16
|
LL | take_range(..=42);
| ^^^^^
Expand Down
10 changes: 0 additions & 10 deletions src/test/ui/range/issue-54505.fixed
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// run-rustfix

// Regression test for #54505 - range borrowing suggestion had
Expand Down
10 changes: 0 additions & 10 deletions src/test/ui/range/issue-54505.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// run-rustfix

// Regression test for #54505 - range borrowing suggestion had
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/range/issue-54505.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0308]: mismatched types
--> $DIR/issue-54505.rs:24:16
--> $DIR/issue-54505.rs:14:16
|
LL | take_range(0..1);
| ^^^^
Expand All @@ -11,7 +11,7 @@ LL | take_range(0..1);
found type `std::ops::Range<{integer}>`

error[E0308]: mismatched types
--> $DIR/issue-54505.rs:29:16
--> $DIR/issue-54505.rs:19:16
|
LL | take_range(1..);
| ^^^
Expand All @@ -23,7 +23,7 @@ LL | take_range(1..);
found type `std::ops::RangeFrom<{integer}>`

error[E0308]: mismatched types
--> $DIR/issue-54505.rs:34:16
--> $DIR/issue-54505.rs:24:16
|
LL | take_range(..);
| ^^
Expand All @@ -35,7 +35,7 @@ LL | take_range(..);
found type `std::ops::RangeFull`

error[E0308]: mismatched types
--> $DIR/issue-54505.rs:39:16
--> $DIR/issue-54505.rs:29:16
|
LL | take_range(0..=1);
| ^^^^^
Expand All @@ -47,7 +47,7 @@ LL | take_range(0..=1);
found type `std::ops::RangeInclusive<{integer}>`

error[E0308]: mismatched types
--> $DIR/issue-54505.rs:44:16
--> $DIR/issue-54505.rs:34:16
|
LL | take_range(..5);
| ^^^
Expand All @@ -59,7 +59,7 @@ LL | take_range(..5);
found type `std::ops::RangeTo<{integer}>`

error[E0308]: mismatched types
--> $DIR/issue-54505.rs:49:16
--> $DIR/issue-54505.rs:39:16
|
LL | take_range(..=42);
| ^^^^^
Expand Down