about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2023-06-30 03:14:44 +0000
committerEsteban Küber <esteban@kuber.com.ar>2023-10-09 23:00:15 +0000
commit2f79681fb9346ba511ad42499d12faee15e6c63a (patch)
tree8988dcb2de19888d79ad14659eaac52d4fcf3073
parent3d86b8acdab25399fc921ab6b522943a02bac410 (diff)
downloadrust-2f79681fb9346ba511ad42499d12faee15e6c63a.tar.gz
rust-2f79681fb9346ba511ad42499d12faee15e6c63a.zip
Only emit one error per unsized binding, instead of one per usage
Fix #56607.
-rw-r--r--compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs19
-rw-r--r--tests/ui/sized/unsized-binding.rs4
-rw-r--r--tests/ui/sized/unsized-binding.stderr28
3 files changed, 22 insertions, 29 deletions
diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs
index 8adfb27a3f4..18e62fd6319 100644
--- a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs
+++ b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs
@@ -428,6 +428,25 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
                         {
                             return;
                         }
+                        if let ObligationCauseCode::FunctionArgumentObligation {
+                            arg_hir_id,
+                            ..
+                        } = obligation.cause.code()
+                            && let Some(Node::Expr(arg)) = self.tcx.hir().find(*arg_hir_id)
+                            && let arg = arg.peel_borrows()
+                            && let hir::ExprKind::Path(hir::QPath::Resolved(
+                                None,
+                                hir::Path { res: hir::def::Res::Local(hir_id), .. },
+                            )) = arg.kind
+                            && let Some(Node::Pat(pat)) = self.tcx.hir().find(*hir_id)
+                            && let Some(preds) = self.reported_trait_errors.borrow().get(&pat.span)
+                            && preds.contains(&obligation.predicate)
+                            && self.tcx.sess.has_errors().is_some()
+                        {
+                            // Silence redundant errors on binding acccess that are already
+                            // reported on the binding definition (#56607).
+                            return;
+                        }
                         let trait_ref = trait_predicate.to_poly_trait_ref();
 
                         let (post_message, pre_message, type_def) = self
diff --git a/tests/ui/sized/unsized-binding.rs b/tests/ui/sized/unsized-binding.rs
index 7d410718612..3b99b0f6e96 100644
--- a/tests/ui/sized/unsized-binding.rs
+++ b/tests/ui/sized/unsized-binding.rs
@@ -1,5 +1,5 @@
 fn main() {
     let x = *""; //~ ERROR E0277
-    println!("{}", x); //~ ERROR E0277
-    println!("{}", x); //~ ERROR E0277
+    println!("{}", x);
+    println!("{}", x);
 }
diff --git a/tests/ui/sized/unsized-binding.stderr b/tests/ui/sized/unsized-binding.stderr
index d508d84930a..af306685021 100644
--- a/tests/ui/sized/unsized-binding.stderr
+++ b/tests/ui/sized/unsized-binding.stderr
@@ -8,32 +8,6 @@ LL |     let x = *"";
    = note: all local variables must have a statically known size
    = help: unsized locals are gated as an unstable feature
 
-error[E0277]: the size for values of type `str` cannot be known at compilation time
-  --> $DIR/unsized-binding.rs:3:20
-   |
-LL |     println!("{}", x);
-   |               --   ^ doesn't have a size known at compile-time
-   |               |
-   |               required by a bound introduced by this call
-   |
-   = help: the trait `Sized` is not implemented for `str`
-note: required by a bound in `core::fmt::rt::Argument::<'a>::new_display`
-  --> $SRC_DIR/core/src/fmt/rt.rs:LL:COL
-   = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
-
-error[E0277]: the size for values of type `str` cannot be known at compilation time
-  --> $DIR/unsized-binding.rs:4:20
-   |
-LL |     println!("{}", x);
-   |               --   ^ doesn't have a size known at compile-time
-   |               |
-   |               required by a bound introduced by this call
-   |
-   = help: the trait `Sized` is not implemented for `str`
-note: required by a bound in `core::fmt::rt::Argument::<'a>::new_display`
-  --> $SRC_DIR/core/src/fmt/rt.rs:LL:COL
-   = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
-
-error: aborting due to 3 previous errors
+error: aborting due to previous error
 
 For more information about this error, try `rustc --explain E0277`.