]> git.lizzy.rs Git - rust.git/commitdiff
Address review comments
authorPaweł Romanowski <pawroman@gmail.com>
Tue, 2 Oct 2018 21:10:04 +0000 (22:10 +0100)
committerPaweł Romanowski <pawroman@gmail.com>
Tue, 2 Oct 2018 21:10:04 +0000 (22:10 +0100)
src/librustc_typeck/check/demand.rs
src/test/ui/range/issue-54505-no-literals.fixed
src/test/ui/range/issue-54505-no-literals.rs
src/test/ui/range/issue-54505-no-literals.stderr
src/test/ui/range/issue-54505-no-std.rs
src/test/ui/range/issue-54505-no-std.stderr
src/test/ui/range/issue-54505.fixed
src/test/ui/range/issue-54505.rs
src/test/ui/range/issue-54505.stderr

index e0577add61c2168f993cc425c4c2adfbdb0a4500..6eaeee5c0e28ba1c49af78a4d05e7cee6f5fa10e 100644 (file)
@@ -317,7 +317,11 @@ pub fn check_ref(&self,
                             _ 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);
@@ -379,67 +383,56 @@ pub fn check_ref(&self,
         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()``).
     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| {
-            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())
+            {
+                // "{{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| {
+            // Tell if expression span snippet doesn't look like an explicit
+            // Range struct or `new()` call.  This is to allow inferring
+            // 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;
                     }
                 }
             }
index 5be024dff5309dfa1b4abe0e79fee1315905322b..4d8f67182b9ace5b62fefefd7685d99534cc16aa 100644 (file)
@@ -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
index 57d1938aca87a04ac340a01319abeccc622c251d..dc21dcbc2db4170aabbf0104f30e50b15f133f80 100644 (file)
@@ -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
index a8be4de5f0e78f23e8b30a916b36799f1b539ba2..b8811c98d21bdd5c28a296b83ee51021f4b71ba7 100644 (file)
@@ -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 });
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -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 });
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -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 });
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -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 });
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -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 {});
    |                ^^^^^^^^^^^^^^^^^^^^^^
@@ -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 {});
    |                ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -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));
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -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));
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -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 });
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -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 });
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -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 });
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -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 });
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
index 0dadaecd7d8a3606bf9c5353876991af77fb7739..c2b17b53ba16b83c8c9596dfd5851b2a5f944539 100644 (file)
@@ -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`
 
index 035bc6406586c16c20be0647363c67835146c0dc..2810a3f076d4bae51b3ee28f70e1512123550be7 100644 (file)
@@ -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);
    |                ^^^^
@@ -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..);
    |                ^^^
@@ -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(..);
    |                ^^
@@ -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);
    |                ^^^^^
@@ -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);
    |                ^^^
@@ -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);
    |                ^^^^^
index ef8ded0f7a3407cdf42102203c9262c7d618a1d4..f8298c0b5ceffeebb7a87904cf6d04bf13f7344e 100644 (file)
@@ -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
index b81c879a37ea4ff4566233103756e83b20e176e1..03673252dd3bae32fc06c27962589716369602a2 100644 (file)
@@ -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
index 5a59594ff50082e4c78b8dd595608b44892e5817..d6e1fb0cef23860b3a65b5e46477a22ce14489ae 100644 (file)
@@ -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);
    |                ^^^^
@@ -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..);
    |                ^^^
@@ -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(..);
    |                ^^
@@ -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);
    |                ^^^^^
@@ -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);
    |                ^^^
@@ -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);
    |                ^^^^^