about summary refs log tree commit diff
diff options
context:
space:
mode:
authorKevin Per <kevin.per@protonmail.com>2020-05-03 11:00:25 +0200
committerKevin Per <kevin.per@protonmail.com>2020-05-03 11:00:25 +0200
commitdfbc143e65dd4dc8499f7296ddc7889854a8cc7d (patch)
tree9e3843dce6aee313beed2aa98ca6743a7d882587
parent7184d137f65bb8d143ce8b5b664e50d33c4b5fbd (diff)
downloadrust-dfbc143e65dd4dc8499f7296ddc7889854a8cc7d.tar.gz
rust-dfbc143e65dd4dc8499f7296ddc7889854a8cc7d.zip
Adding if to prevent borrowing suggestion in structs #71136
-rw-r--r--src/librustc_trait_selection/traits/error_reporting/suggestions.rs52
1 files changed, 46 insertions, 6 deletions
diff --git a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs
index 5ec2d68ab2a..37f567401b7 100644
--- a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs
+++ b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs
@@ -562,6 +562,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
                 param_env,
                 new_trait_ref.without_const().to_predicate(),
             );
+
             if self.predicate_must_hold_modulo_regions(&new_obligation) {
                 if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
                     // We have a very specific type of error, where just borrowing this argument
@@ -569,6 +570,34 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
                     // original type obligation, not the last one that failed, which is arbitrary.
                     // Because of this, we modify the error to refer to the original obligation and
                     // return early in the caller.
+                    
+
+                    let has_colon = self
+                            .tcx
+                            .sess
+                            .source_map()
+                            .span_to_snippet(span)
+                            .map(|w| w.contains(":"))
+                            .unwrap_or(false);
+
+                    let has_double_colon = self
+                            .tcx
+                            .sess
+                            .source_map()
+                            .span_to_snippet(span)
+                            .map(|w| w.contains("::"))
+                            .unwrap_or(false);
+
+                    let has_bracket = self
+                            .tcx
+                            .sess
+                            .source_map()
+                            .span_to_snippet(span)
+                            .map(|w| w.contains("{"))
+                            .unwrap_or(false);
+
+                   
+
                     let msg = format!(
                         "the trait bound `{}: {}` is not satisfied",
                         found,
@@ -591,12 +620,23 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
                             obligation.parent_trait_ref.skip_binder().print_only_trait_path(),
                         ),
                     );
-                    err.span_suggestion(
-                        span,
-                        "consider borrowing here",
-                        format!("&{}", snippet),
-                        Applicability::MaybeIncorrect,
-                    );
+                
+                    // This if is to prevent a special edge-case
+                    if !has_colon || has_double_colon || has_bracket {
+                        // We don't want a borrowing suggestion on the fields in structs,
+                        // ```
+                        // struct Foo {
+                        //  the_foos: Vec<Foo>
+                        // }
+                        // ```
+
+                        err.span_suggestion(
+                            span,
+                            "consider borrowing here",
+                            format!("&{}", snippet),
+                            Applicability::MaybeIncorrect,
+                        );
+                    }
                     return true;
                 }
             }