rust/compiler
bors d4de1f230c Auto merge of #93368 - eddyb:diagbld-guarantee, r=estebank
rustc_errors: let `DiagnosticBuilder::emit` return a "guarantee of emission".

That is, `DiagnosticBuilder` is now generic over the return type of `.emit()`, so we'll now have:
* `DiagnosticBuilder<ErrorReported>` for error (incl. fatal/bug) diagnostics
  * can only be created via a `const L: Level`-generic constructor, that limits allowed variants via a `where` clause, so not even `rustc_errors` can accidentally bypass this limitation
  * asserts `diagnostic.is_error()` on emission, just in case the construction restriction was bypassed (e.g. by replacing the whole `Diagnostic` inside `DiagnosticBuilder`)
  * `.emit()` returns `ErrorReported`, as a "proof" token that `.emit()` was called
    (though note that this isn't a real guarantee until after completing the work on
     #69426)
* `DiagnosticBuilder<()>` for everything else (warnings, notes, etc.)
  * can also be obtained from other `DiagnosticBuilder`s by calling `.forget_guarantee()`

This PR is a companion to other ongoing work, namely:
* #69426
  and it's ongoing implementation:
  #93222
  the API changes in this PR are needed to get statically-checked "only errors produce `ErrorReported` from `.emit()`", but doesn't itself provide any really strong guarantees without those other `ErrorReported` changes
* #93244
  would make the choices of API changes (esp. naming) in this PR fit better overall

In order to be able to let `.emit()` return anything trustable, several changes had to be made:
* `Diagnostic`'s `level` field is now private to `rustc_errors`, to disallow arbitrary "downgrade"s from "some kind of error" to "warning" (or anything else that doesn't cause compilation to fail)
  * it's still possible to replace the whole `Diagnostic` inside the `DiagnosticBuilder`, sadly, that's harder to fix, but it's unlikely enough that we can paper over it with asserts on `.emit()`
* `.cancel()` now consumes `DiagnosticBuilder`, preventing `.emit()` calls on a cancelled diagnostic
  * it's also now done internally, through `DiagnosticBuilder`-private state, instead of having a `Level::Cancelled` variant that can be read (or worse, written) by the user
  * this removes a hazard of calling `.cancel()` on an error then continuing to attach details to it, and even expect to be able to `.emit()` it
  * warnings were switched to *only* `can_emit_warnings` on emission (instead of pre-cancelling early)
  * `struct_dummy` was removed (as it relied on a pre-`Cancelled` `Diagnostic`)
* since `.emit()` doesn't consume the `DiagnosticBuilder` <sub>(I tried and gave up, it's much more work than this PR)</sub>,
  we have to make `.emit()` idempotent wrt the guarantees it returns
  * thankfully, `err.emit(); err.emit();` can return `ErrorReported` both times, as the second `.emit()` call has no side-effects *only* because the first one did do the appropriate emission
* `&mut Diagnostic` is now used in a lot of function signatures, which used to take `&mut DiagnosticBuilder` (in the interest of not having to make those functions generic)
  * the APIs were already mostly identical, allowing for low-effort porting to this new setup
  * only some of the suggestion methods needed some rework, to have the extra `DiagnosticBuilder` functionality on the `Diagnostic` methods themselves (that change is also present in #93259)
  * `.emit()`/`.cancel()` aren't available, but IMO calling them from an "error decorator/annotator" function isn't a good practice, and can lead to strange behavior (from the caller's perspective)
  * `.downgrade_to_delayed_bug()` was added, letting you convert any `.is_error()` diagnostic into a `delay_span_bug` one (which works because in both cases the guarantees available are the same)

This PR should ideally be reviewed commit-by-commit, since there is a lot of fallout in each.

r? `@estebank` cc `@Manishearth` `@nikomatsakis` `@mark-i-m`
2022-02-25 00:46:04 +00:00
..
rustc remove unused jemallocator crate 2022-01-28 16:56:05 +01:00
rustc_apfloat
rustc_arena Remove unused dep from rustc_arena 2022-02-02 17:37:14 +01:00
rustc_ast Rollup merge of #94316 - nnethercote:improve-string-literal-unescaping, r=petrochenkov 2022-02-24 21:42:18 +01:00
rustc_ast_lowering resolve: Fix incorrect results of opt_def_kind query for some built-in macros 2022-02-24 22:54:36 +03:00
rustc_ast_passes Rollup merge of #94146 - est31:let_else, r=cjgillot 2022-02-20 00:37:34 +01:00
rustc_ast_pretty Pretty print ItemKind::Use in rustfmt style 2022-02-07 21:51:05 -08:00
rustc_attr Rollup merge of #94175 - Urgau:check-cfg-improvements, r=petrochenkov 2022-02-24 21:42:13 +01:00
rustc_borrowck Auto merge of #93368 - eddyb:diagbld-guarantee, r=estebank 2022-02-25 00:46:04 +00:00
rustc_builtin_macros rustc_errors: let DiagnosticBuilder::emit return a "guarantee of emission". 2022-02-23 06:38:52 +00:00
rustc_codegen_cranelift Normalize main return type during mono item collection & codegen 2022-02-23 22:33:50 +01:00
rustc_codegen_gcc Auto merge of #94123 - bjorn3:cg_ssa_singleton_builder, r=tmiasko 2022-02-24 12:28:19 +00:00
rustc_codegen_llvm Rollup merge of #94242 - compiler-errors:fat-uninhabitable-pointer, r=michaelwoerister 2022-02-24 21:42:15 +01:00
rustc_codegen_ssa Auto merge of #93368 - eddyb:diagbld-guarantee, r=estebank 2022-02-25 00:46:04 +00:00
rustc_const_eval Auto merge of #93368 - eddyb:diagbld-guarantee, r=estebank 2022-02-25 00:46:04 +00:00
rustc_data_structures Rollup merge of #94288 - Mark-Simulacrum:ser-opt, r=nnethercote 2022-02-24 07:48:09 +01:00
rustc_driver Rollup merge of #94146 - est31:let_else, r=cjgillot 2022-02-20 00:37:34 +01:00
rustc_error_codes Revert "Auto merge of #91403 - cjgillot:inherit-async, r=oli-obk" 2022-02-17 16:00:04 +00:00
rustc_errors Auto merge of #93368 - eddyb:diagbld-guarantee, r=estebank 2022-02-25 00:46:04 +00:00
rustc_expand rustc_errors: let DiagnosticBuilder::emit return a "guarantee of emission". 2022-02-23 06:38:52 +00:00
rustc_feature fix names in feature(...) suggestion 2022-02-21 01:17:37 -08:00
rustc_fs_util
rustc_graphviz
rustc_hir resolve: Fix incorrect results of opt_def_kind query for some built-in macros 2022-02-24 22:54:36 +03: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 Adopt let else in more places 2022-02-19 17:27:43 +01:00
rustc_index Introduce ChunkedBitSet and use it for some dataflow analyses. 2022-02-23 10:18:49 +11:00
rustc_infer Auto merge of #93368 - eddyb:diagbld-guarantee, r=estebank 2022-02-25 00:46:04 +00:00
rustc_interface Auto merge of #93368 - eddyb:diagbld-guarantee, r=estebank 2022-02-25 00:46:04 +00:00
rustc_lexer Improve scan_escape. 2022-02-24 17:01:01 +11:00
rustc_lint Auto merge of #93368 - eddyb:diagbld-guarantee, r=estebank 2022-02-25 00:46:04 +00:00
rustc_lint_defs Improve diagnostic of the unexpected_cfgs lint 2022-02-22 23:17:13 +01:00
rustc_llvm Rollup merge of #91675 - ivanloz:memtagsan, r=nagisa 2022-02-18 23:23:03 +01:00
rustc_log
rustc_macros rustc_errors: let DiagnosticBuilder::emit return a "guarantee of emission". 2022-02-23 06:38:52 +00:00
rustc_metadata Auto merge of #93368 - eddyb:diagbld-guarantee, r=estebank 2022-02-25 00:46:04 +00:00
rustc_middle Auto merge of #93368 - eddyb:diagbld-guarantee, r=estebank 2022-02-25 00:46:04 +00:00
rustc_mir_build rustc_errors: let DiagnosticBuilder::emit return a "guarantee of emission". 2022-02-23 06:38:52 +00:00
rustc_mir_dataflow Auto merge of #93984 - nnethercote:ChunkedBitSet, r=Mark-Simulacrum 2022-02-23 01:26:07 +00:00
rustc_mir_transform Auto merge of #93368 - eddyb:diagbld-guarantee, r=estebank 2022-02-25 00:46:04 +00:00
rustc_monomorphize Normalize main return type during mono item collection & codegen 2022-02-23 22:33:50 +01:00
rustc_parse rustc_errors: let DiagnosticBuilder::emit return a "guarantee of emission". 2022-02-23 06:38:52 +00:00
rustc_parse_format Correctly mark the span of captured arguments in format_args!() 2022-02-16 07:34:06 +00:00
rustc_passes Auto merge of #93368 - eddyb:diagbld-guarantee, r=estebank 2022-02-25 00:46:04 +00:00
rustc_plugin_impl
rustc_privacy resolve: Fix incorrect results of opt_def_kind query for some built-in macros 2022-02-24 22:54:36 +03:00
rustc_query_impl Auto merge of #94066 - Mark-Simulacrum:factor-out-simple-def-kind, r=davidtwco 2022-02-21 03:36:55 +00:00
rustc_query_system rustc_errors: let DiagnosticBuilder::emit return a "guarantee of emission". 2022-02-23 06:38:52 +00:00
rustc_resolve Auto merge of #93368 - eddyb:diagbld-guarantee, r=estebank 2022-02-25 00:46:04 +00:00
rustc_save_analysis resolve: Fix incorrect results of opt_def_kind query for some built-in macros 2022-02-24 22:54:36 +03:00
rustc_serialize Delete Decoder::read_unit 2022-02-22 18:14:51 -05:00
rustc_session Auto merge of #93368 - eddyb:diagbld-guarantee, r=estebank 2022-02-25 00:46:04 +00:00
rustc_span Rollup merge of #94169 - Amanieu:asm_stuff, r=nagisa 2022-02-22 12:16:28 +01:00
rustc_symbol_mangling Rename region_should_not_be_omitted to should_print_region 2022-02-23 08:58:36 +01:00
rustc_target riscv32imc_esp_espidf: set max_atomic_width to 64 2022-02-23 13:11:26 +00:00
rustc_trait_selection Auto merge of #93368 - eddyb:diagbld-guarantee, r=estebank 2022-02-25 00:46:04 +00:00
rustc_traits use List<Ty<'tcx>> for tuples 2022-02-21 07:09:11 +01:00
rustc_ty_utils use List<Ty<'tcx>> for tuples 2022-02-21 07:09:11 +01:00
rustc_type_ir Inline UnifyKey::index and UnifyKey::from_index 2022-02-15 19:07:06 +01:00
rustc_typeck Auto merge of #93368 - eddyb:diagbld-guarantee, r=estebank 2022-02-25 00:46:04 +00:00