Skip to content

Commit

Permalink
Only emit one error per unsized binding, instead of one per usage
Browse files Browse the repository at this point in the history
Fix #56607.
  • Loading branch information
estebank committed Jun 30, 2023
1 parent ea465d0 commit a6579e6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
19 changes: 19 additions & 0 deletions compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,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
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/sized/unsized-binding.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn main() {
let x = *""; //~ ERROR E0277
println!("{}", x); //~ ERROR E0277
println!("{}", x); //~ ERROR E0277
println!("{}", x);
println!("{}", x);
}
28 changes: 1 addition & 27 deletions tests/ui/sized/unsized-binding.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

0 comments on commit a6579e6

Please sign in to comment.