rust/compiler/rustc_hir_analysis/src
Jana Dönszelmann 63c5a84b74
Rollup merge of #142724 - xizheyin:avoid_overwrite_args, r=oli-obk
Add runtime check to avoid overwrite arg in `Diag`

## Origin PR description
At first, I set up a `debug_assert` check for the arg method to make sure that `args` in `Diag` aren't easily overwritten, and I added the `remove_arg()` method, so that if you do need to overwrite an arg, then you can explicitly call `remove_arg()` to remove it first, then call `arg()` to overwrite it.

For the code before the rust-lang/rust#142015 change, it won't compile because it will report an error
```
arg `instance`already exists.
```

This PR also modifies all diagnostics that fail the check to pass the check. There are two cases of check failure:

1. ~~Between *the parent diagnostic and the subdiagnostic*, or *between the subdiagnostics* have the same field between them. In this case, I renamed the conflicting fields.~~
2. ~~For subdiagnostics stored in `Vec`, the rendering may iteratively write the same arg over and over again. In this case, I changed the auto-generation with `derive(SubDiagnostic)` to manually implementing `SubDiagnostic` and manually rendered it with `eagerly_translate()`, similar to https://github.com/rust-lang/rust/issues/142031#issuecomment-2984812090, and after rendering it I manually deleted useless arg with the newly added `remove_arg` method.~~

## Final Decision

After trying and discussing, we made a final decision.

For `#[derive(Subdiagnostic)]`, This PR made two changes:

1. After the subdiagnostic is rendered, remove all args of this subdiagnostic, which allows for usage like `Vec<Subdiag>`.
2. Store `diag.args` before setting arguments, so that you can restore the contents of the main diagnostic after deleting the arguments after subdiagnostic is rendered, to avoid deleting the main diagnostic's arg when they have the same name args.
2025-06-25 22:14:55 +02:00
..
check Rewrite #[track_caller] 2025-06-24 23:00:31 +02:00
coherence Replace some Option<Span> with Span and use DUMMY_SP instead of None 2025-06-05 14:14:59 +00:00
collect Use gen blocks in the compiler instead of from_coroutine 2025-06-20 18:57:52 +00:00
errors Don't compute name of associated item if it's an RPITIT 2025-04-20 16:08:39 +00:00
hir_ty_lowering Rollup merge of #142724 - xizheyin:avoid_overwrite_args, r=oli-obk 2025-06-25 22:14:55 +02:00
impl_wf_check convert some GenericArg to Term 2025-04-26 02:05:31 +00:00
outlives Fix some var names 2025-05-27 11:14:47 +00:00
variance Fix some var names 2025-05-27 11:14:47 +00:00
autoderef.rs FIXME(-Znext-solver) triage 2025-06-03 14:23:56 +02:00
check_unused.rs Move methods from Map to TyCtxt, part 4. 2025-03-12 08:55:37 +11:00
collect.rs Port #[rustc_skip_during_method_dispatch] to the new attribute system 2025-06-23 22:48:20 +03:00
constrained_generic_params.rs Remove weak alias terminology 2025-04-24 11:59:20 +01:00
delegation.rs Use more slicing and slice interning over iterable interning 2025-06-13 01:16:01 +02:00
errors.rs Add runtime check to avoid overwrite arg easily in diag and store and restore snapshot when set subdiag arg 2025-06-25 21:07:16 +08:00
hir_wf_check.rs Merge unboxed trait object error suggestion into regular dyn incompat error 2025-06-13 13:54:06 +00:00
impl_wf_check.rs Move name field from AssocItem to AssocKind variants. 2025-04-15 08:07:15 +10:00
lib.rs Use gen blocks in the compiler instead of from_coroutine 2025-06-20 18:57:52 +00:00