Skip to content

Commit a6579e6

Browse files
committed
Only emit one error per unsized binding, instead of one per usage
Fix #56607.
1 parent ea465d0 commit a6579e6

File tree

3 files changed

+22
-29
lines changed

3 files changed

+22
-29
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+19
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,25 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
691691
{
692692
return;
693693
}
694+
if let ObligationCauseCode::FunctionArgumentObligation {
695+
arg_hir_id,
696+
..
697+
} = obligation.cause.code()
698+
&& let Some(Node::Expr(arg)) = self.tcx.hir().find(*arg_hir_id)
699+
&& let arg = arg.peel_borrows()
700+
&& let hir::ExprKind::Path(hir::QPath::Resolved(
701+
None,
702+
hir::Path { res: hir::def::Res::Local(hir_id), .. },
703+
)) = arg.kind
704+
&& let Some(Node::Pat(pat)) = self.tcx.hir().find(*hir_id)
705+
&& let Some(preds) = self.reported_trait_errors.borrow().get(&pat.span)
706+
&& preds.contains(&obligation.predicate)
707+
&& self.tcx.sess.has_errors().is_some()
708+
{
709+
// Silence redundant errors on binding acccess that are already
710+
// reported on the binding definition (#56607).
711+
return;
712+
}
694713
let trait_ref = trait_predicate.to_poly_trait_ref();
695714

696715
let (post_message, pre_message, type_def) = self

tests/ui/sized/unsized-binding.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn main() {
22
let x = *""; //~ ERROR E0277
3-
println!("{}", x); //~ ERROR E0277
4-
println!("{}", x); //~ ERROR E0277
3+
println!("{}", x);
4+
println!("{}", x);
55
}

tests/ui/sized/unsized-binding.stderr

+1-27
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,6 @@ LL | let x = *"";
88
= note: all local variables must have a statically known size
99
= help: unsized locals are gated as an unstable feature
1010

11-
error[E0277]: the size for values of type `str` cannot be known at compilation time
12-
--> $DIR/unsized-binding.rs:3:20
13-
|
14-
LL | println!("{}", x);
15-
| -- ^ doesn't have a size known at compile-time
16-
| |
17-
| required by a bound introduced by this call
18-
|
19-
= help: the trait `Sized` is not implemented for `str`
20-
note: required by a bound in `core::fmt::rt::Argument::<'a>::new_display`
21-
--> $SRC_DIR/core/src/fmt/rt.rs:LL:COL
22-
= 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)
23-
24-
error[E0277]: the size for values of type `str` cannot be known at compilation time
25-
--> $DIR/unsized-binding.rs:4:20
26-
|
27-
LL | println!("{}", x);
28-
| -- ^ doesn't have a size known at compile-time
29-
| |
30-
| required by a bound introduced by this call
31-
|
32-
= help: the trait `Sized` is not implemented for `str`
33-
note: required by a bound in `core::fmt::rt::Argument::<'a>::new_display`
34-
--> $SRC_DIR/core/src/fmt/rt.rs:LL:COL
35-
= 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)
36-
37-
error: aborting due to 3 previous errors
11+
error: aborting due to previous error
3812

3913
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)