about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2020-05-29 09:44:41 -0700
committerEsteban Küber <esteban@kuber.com.ar>2020-05-29 10:11:59 -0700
commit0d18136b776cd7cda2c4932f447e6484377f42b2 (patch)
treed62304ae99b750b4ebcc952a1eb1b223a420b9ad
parent04243710a0077ad140b8259c21bc1aac02057a48 (diff)
downloadrust-0d18136b776cd7cda2c4932f447e6484377f42b2.tar.gz
rust-0d18136b776cd7cda2c4932f447e6484377f42b2.zip
Move common code to `WhereClause`
-rw-r--r--src/librustc_hir/hir.rs7
-rw-r--r--src/librustc_middle/ty/diagnostics.rs13
-rw-r--r--src/librustc_trait_selection/traits/error_reporting/suggestions.rs11
3 files changed, 9 insertions, 22 deletions
diff --git a/src/librustc_hir/hir.rs b/src/librustc_hir/hir.rs
index ef398ab25d3..a46871ac807 100644
--- a/src/librustc_hir/hir.rs
+++ b/src/librustc_hir/hir.rs
@@ -525,6 +525,13 @@ impl WhereClause<'_> {
     pub fn span_for_predicates_or_empty_place(&self) -> Span {
         self.span
     }
+
+    /// `Span` where further predicates would be suggested, accounting for trailing commas, like
+    ///  in `fn foo<T>(t: T) where T: Foo,` so we don't suggest two trailing commas.
+    pub fn tail_span_for_suggestion(&self) -> Span {
+        let end = self.span_for_predicates_or_empty_place().shrink_to_hi();
+        self.predicates.last().map(|p| p.span()).unwrap_or(end).shrink_to_hi().to(end)
+    }
 }
 
 /// A single predicate in a where-clause.
diff --git a/src/librustc_middle/ty/diagnostics.rs b/src/librustc_middle/ty/diagnostics.rs
index 96c36d989e9..ba3cc0a7af2 100644
--- a/src/librustc_middle/ty/diagnostics.rs
+++ b/src/librustc_middle/ty/diagnostics.rs
@@ -220,22 +220,11 @@ pub fn suggest_constraining_type_param(
             }
         }
 
-        // Account for `fn foo<T>(t: T) where T: Foo,` so we don't suggest two trailing commas.
-        let end = generics.where_clause.span_for_predicates_or_empty_place().shrink_to_hi();
-        let where_clause_span = generics
-            .where_clause
-            .predicates
-            .last()
-            .map(|p| p.span())
-            .unwrap_or(end)
-            .shrink_to_hi()
-            .to(end);
-
         match &param_spans[..] {
             &[&param_span] => suggest_restrict(param_span.shrink_to_hi()),
             _ => {
                 err.span_suggestion_verbose(
-                    where_clause_span,
+                    generics.where_clause.tail_span_for_suggestion(),
                     &msg_restrict_type_further,
                     format!(", {}: {}", param_name, constraint),
                     Applicability::MachineApplicable,
diff --git a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs
index eb281b270ff..a8d4de873f9 100644
--- a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs
+++ b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs
@@ -169,17 +169,8 @@ pub trait InferCtxtExt<'tcx> {
 }
 
 fn predicate_constraint(generics: &hir::Generics<'_>, pred: String) -> (Span, String) {
-    let end = generics.where_clause.span_for_predicates_or_empty_place().shrink_to_hi();
     (
-        // Account for `where T: Foo,` so we don't suggest two trailing commas.
-        generics
-            .where_clause
-            .predicates
-            .last()
-            .map(|p| p.span())
-            .unwrap_or(end)
-            .shrink_to_hi()
-            .to(end),
+        generics.where_clause.tail_span_for_suggestion(),
         format!(
             "{} {}",
             if !generics.where_clause.predicates.is_empty() { "," } else { " where" },