rust/compiler
bors 10913c0001 Auto merge of #87835 - xFrednet:rfc-2383-expect-attribute-with-ids, r=wesleywiser
Implementation of the `expect` attribute (RFC 2383)

This is an implementation of the `expect` attribute as described in [RFC-2383](https://rust-lang.github.io/rfcs/2383-lint-reasons.html). The attribute allows the suppression of lint message by expecting them. Unfulfilled lint expectations (meaning no expected lint was caught) will emit the `unfulfilled_lint_expectations` lint at the `expect` attribute.

### Example
#### input
```rs
// required feature flag
#![feature(lint_reasons)]

#[expect(unused_mut)] // Will warn about an unfulfilled expectation
#[expect(unused_variables)] // Will be fulfilled by x
fn main() {
    let x = 0;
}
```

#### output

```txt
warning: this lint expectation is unfulfilled
  --> $DIR/trigger_lint.rs:3:1
   |
LL | #[expect(unused_mut)] // Will warn about an unfulfilled expectation
   |          ^^^^^^^^^^
   |
   = note: `#[warn(unfulfilled_lint_expectations)]` on by default
```

### Implementation

This implementation introduces `Expect` as a new lint level for diagnostics, which have been expected. All lint expectations marked via the `expect` attribute are collected in the [`LintLevelsBuilder`] and assigned an ID that is stored in the new lint level. The `LintLevelsBuilder` stores all found expectations and the data needed to emit the `unfulfilled_lint_expectations` in the [`LintLevelsMap`] which is the result of the [`lint_levels()`] query.

The [`rustc_errors::HandlerInner`] is the central error handler in rustc and handles the emission of all diagnostics. Lint message with the level `Expect` are suppressed during this emission, while the expectation ID is stored in a set which marks them as fulfilled. The last step is then so simply check if all expectations collected by the [`LintLevelsBuilder`] in the [`LintLevelsMap`] have been marked as fulfilled in the [`rustc_errors::HandlerInner`]. Otherwise, a new lint message will be emitted.

The implementation of the `LintExpectationId` required some special handling to make it stable between sessions. Lints can be emitted during [`EarlyLintPass`]es. At this stage, it's not possible to create a stable identifier. The level instead stores an unstable identifier, which is later converted to a stable `LintExpectationId`.

### Followup TO-DOs
All open TO-DOs have been marked with `FIXME` comments in the code. This is the combined list of them:

* [ ] The current implementation doesn't cover cases where the `unfulfilled_lint_expectations` lint is actually expected by another `expect` attribute.
   * This should be easily possible, but I wanted to get some feedback before putting more work into this.
   * This could also be done in a new PR to not add to much more code to this one
* [ ] Update unstable documentation to reflect this change.
* [ ] Update unstable expectation ids in [`HandlerInner::stashed_diagnostics`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.HandlerInner.html#structfield.stashed_diagnostics)

### Open questions
I also have a few open questions where I would like to get feedback on:
1. The RFC discussion included a suggestion to change the `expect` attribute to something else. (Initiated by `@Ixrec` [here](https://github.com/rust-lang/rfcs/pull/2383#issuecomment-378424091), suggestion from `@scottmcm` to use `#[should_lint(...)]` [here](https://github.com/rust-lang/rfcs/pull/2383#issuecomment-378648877)). No real conclusion was drawn on that point from my understanding. Is this still open for discussion, or was this discarded with the merge of the RFC?
2. How should the expect attribute deal with the new `force-warn` lint level?

---

This approach was inspired by a discussion with `@LeSeulArtichaut.`

RFC tracking issue: #54503

Mentoring/Implementation issue: #85549

[`LintLevelsBuilder`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/levels/struct.LintLevelsBuilder.html
[`LintLevelsMap`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/lint/struct.LintLevelMap.html
[`lint_levels()`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html#method.lint_levels
[`rustc_errors::HandlerInner`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.HandlerInner.html
[`EarlyLintPass`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/trait.EarlyLintPass.html
2022-03-03 18:59:32 +00:00
..
rustc remove unused jemallocator crate 2022-01-28 16:56:05 +01:00
rustc_apfloat Stabilize iter::zip. 2021-12-14 18:50:31 -04:00
rustc_arena Remove unused dep from rustc_arena 2022-02-02 17:37:14 +01:00
rustc_ast 2 - Make more use of let_chains 2022-02-26 13:45:43 -03:00
rustc_ast_lowering compiler: fix some typos 2022-03-01 20:02:47 +08:00
rustc_ast_passes Switch bootstrap cfgs 2022-02-25 08:00:52 -05:00
rustc_ast_pretty Pretty print ItemKind::Use in rustfmt style 2022-02-07 21:51:05 -08:00
rustc_attr Improve allowness of the unexpected_cfgs lint 2022-03-01 14:29:12 +01:00
rustc_borrowck Rollup merge of #94375 - WaffleLapkin:copy-suggestion, r=estebank 2022-03-03 01:09:11 +01:00
rustc_builtin_macros Rollup merge of #94433 - Urgau:check-cfg-allowness, r=petrochenkov 2022-03-03 01:09:12 +01:00
rustc_codegen_cranelift rename ErrorReported -> ErrorGuaranteed 2022-03-02 09:45:25 -06:00
rustc_codegen_gcc rename ErrorReported -> ErrorGuaranteed 2022-03-02 09:45:25 -06:00
rustc_codegen_llvm Rollup merge of #94529 - GuillaumeGomez:unused-doc-comments-blocks, r=estebank 2022-03-03 01:09:15 +01:00
rustc_codegen_ssa Rollup merge of #94433 - Urgau:check-cfg-allowness, r=petrochenkov 2022-03-03 01:09:12 +01:00
rustc_const_eval Auto merge of #94512 - RalfJung:sdiv-ub, r=oli-obk 2022-03-03 12:56:24 +00:00
rustc_data_structures Auto merge of #94514 - matthiaskrgr:rollup-pdzn82h, r=matthiaskrgr 2022-03-02 05:32:00 +00:00
rustc_driver rename ErrorReported -> ErrorGuaranteed 2022-03-02 09:45:25 -06:00
rustc_error_codes Remove out-of-context line at end of E0284 message 2022-03-02 10:09:02 -03:00
rustc_errors Auto merge of #87835 - xFrednet:rfc-2383-expect-attribute-with-ids, r=wesleywiser 2022-03-03 18:59:32 +00:00
rustc_expand Rollup merge of #94544 - mark-i-m:macro-comments, r=petrochenkov 2022-03-03 11:02:56 +01:00
rustc_feature Added Expect lint level and attribute (RFC-2383) 2022-03-02 17:46:05 +01:00
rustc_fs_util Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_graphviz eplace usages of vec![].into_iter with [].into_iter 2022-01-09 14:09:25 +11:00
rustc_hir compiler: fix some typos 2022-03-01 20:02:47 +08:00
rustc_hir_pretty resolve: Fix incorrect results of opt_def_kind query for some built-in macros 2022-02-24 22:54:36 +03:00
rustc_incremental rename ErrorReported -> ErrorGuaranteed 2022-03-02 09:45:25 -06:00
rustc_index Fix test 2022-02-24 16:02:07 -05:00
rustc_infer 8 - Make more use of let_chains 2022-03-02 16:02:37 -03:00
rustc_interface Rollup merge of #94433 - Urgau:check-cfg-allowness, r=petrochenkov 2022-03-03 01:09:12 +01:00
rustc_lexer Improve scan_escape. 2022-02-24 17:01:01 +11:00
rustc_lint Auto merge of #87835 - xFrednet:rfc-2383-expect-attribute-with-ids, r=wesleywiser 2022-03-03 18:59:32 +00:00
rustc_lint_defs Address review comments 2022-03-02 17:46:12 +01:00
rustc_llvm Auto merge of #94229 - erikdesjardins:rem2, r=nikic 2022-03-02 08:48:33 +00:00
rustc_log Make rustc_log doc test runnable 2022-01-03 22:31:56 -08:00
rustc_macros rename ErrorReported -> ErrorGuaranteed 2022-03-02 09:45:25 -06:00
rustc_metadata Improve allowness of the unexpected_cfgs lint 2022-03-01 14:29:12 +01:00
rustc_middle Auto merge of #87835 - xFrednet:rfc-2383-expect-attribute-with-ids, r=wesleywiser 2022-03-03 18:59:32 +00:00
rustc_mir_build rename ErrorReported -> ErrorGuaranteed 2022-03-02 09:45:25 -06:00
rustc_mir_dataflow Rollup merge of #94087 - tmiasko:rm-ignore-borrow-on-drop, r=jackh726 2022-02-26 07:52:42 +01:00
rustc_mir_transform Auto merge of #94512 - RalfJung:sdiv-ub, r=oli-obk 2022-03-03 12:56:24 +00:00
rustc_monomorphize rename ErrorReported -> ErrorGuaranteed 2022-03-02 09:45:25 -06:00
rustc_parse rename ErrorReported -> ErrorGuaranteed 2022-03-02 09:45:25 -06:00
rustc_parse_format Correctly mark the span of captured arguments in format_args!() 2022-02-16 07:34:06 +00:00
rustc_passes Rollup merge of #94448 - c410-f3r:yet-more-let-chains, r=estebank 2022-03-01 03:41:52 +01:00
rustc_plugin_impl replace dynamic library module with libloading 2021-12-06 12:03:47 -05:00
rustc_privacy Switch bootstrap cfgs 2022-02-25 08:00:52 -05:00
rustc_query_impl Auto merge of #94084 - Mark-Simulacrum:drop-sharded, r=cjgillot 2022-02-27 14:04:07 +00:00
rustc_query_system rename ErrorReported -> ErrorGuaranteed 2022-03-02 09:45:25 -06:00
rustc_resolve rename ErrorReported -> ErrorGuaranteed 2022-03-02 09:45:25 -06:00
rustc_save_analysis Switch bootstrap cfgs 2022-02-25 08:00:52 -05:00
rustc_serialize Delete Decoder::read_unit 2022-02-22 18:14:51 -05:00
rustc_session Auto merge of #87835 - xFrednet:rfc-2383-expect-attribute-with-ids, r=wesleywiser 2022-03-03 18:59:32 +00:00
rustc_span Auto merge of #94427 - cjgillot:inline-fresh-expn, r=oli-obk 2022-02-28 08:25:26 +00:00
rustc_symbol_mangling Rollup merge of #94359 - tmiasko:legacy-verbose-const, r=petrochenkov 2022-03-01 12:00:43 +01:00
rustc_target Auto merge of #94216 - psumbera:sparc64-abi-fix2, r=nagisa 2022-02-28 11:54:17 +00:00
rustc_trait_selection Auto merge of #84944 - lcnr:obligation-dedup, r=jackh726 2022-03-03 15:43:42 +00:00
rustc_traits chalk/db: use correct debrujin index when replacing opaque type. 2022-02-22 00:16:56 +01:00
rustc_ty_utils rename ErrorReported -> ErrorGuaranteed 2022-03-02 09:45:25 -06:00
rustc_type_ir Inline UnifyKey::index and UnifyKey::from_index 2022-02-15 19:07:06 +01:00
rustc_typeck Rollup merge of #93072 - m-ou-se:compatible-variants-suggestion-with-desugaring, r=estebank 2022-03-03 01:09:08 +01:00