rust/compiler
Matthias Krüger b269213b35
Rollup merge of #91387 - graydon:E0038-clarification, r=wesleywiser
Clarify and tidy up explanation of E0038

I ran into E0038 (specifically the `Self:Sized` constraint on object-safety) the other day and it seemed to me that the explanations I found floating around the internet were a bit .. wrong. Like they didn't make sense. And then I went and checked the official explanation here and it didn't make sense either.

As far as I can tell (reading through the history of the RFCs), two totally different aspects of object-safety have got tangled up in much of the writing on the subject:
  - Object-safety related to "not even theoretically possible" issues. This includes things like "methods that take or return Self by value", which obviously will never work for an unsized type in a world with fixed-size stack frames (and it'd be an opaque type anyways, which, ugh). This sort of thing was originally decided method-by-method, with non-object-safe methods stripped from objects; but in [RFC 0255](https://rust-lang.github.io/rfcs/0255-object-safety.html) this sort of per-impossible-method reasoning was made into a per-trait safety property (with the escape hatch left in where users could mark methods `where Self:Sized` to have them stripped before the trait's object safety is considered).
  - Object-safety related to "totally possible but ergonomically a little awkward" issues. Specifically in a trait with `Trait:Sized`, there's no a priori reason why this constraint makes the trait impossible to make into an object -- imagine it had nothing but harmless `&self`-taking methods. No problem! Who cares if the Trait requires its implementing types to be sized? As far as I can tell reading the history here, in both RFC 0255 and then later in [RFC 0546](https://rust-lang.github.io/rfcs/0546-Self-not-sized-by-default.html) it seems that the motivation for making `Trait:Sized` be non-object-safe has _nothing to do_ with the impossibility of making objects out of such types, and everything to do with enabling "[a trait object SomeTrait to implement the trait SomeTrait](https://rust-lang.github.io/rfcs/0546-Self-not-sized-by-default.html#motivation)". That is, since `dyn Trait` is unsized, if `Trait:Sized` then you can never have the automatic (and reasonable) ergonomic implicit `impl Trait for dyn Trait`. And the authors of that RFC really wanted that automatic implicit implementation of `Trait` for `dyn Trait`. So they just defined `Trait:Sized` as non-object safe -- no `dyn Trait` can ever exist that the compiler can't synthesize such an impl for. Well enough!

However, I noticed in my reading-and-reconstruction that lots of documentation on the internet, including forum and Q&A site answers and (most worrying) the compiler explanation all kinda grasp at something like the first ("not theoretically possible") explanation, and fail to mention the second ("just an ergonomic constraint") explanation. So I figured I'd clean up the docs to clarify, maybe confuse the next person less (unless of course I'm misreading the history here and misunderstanding motives -- please let me know if so!)

While here I also did some cleanups:

  - Rewrote the preamble, trying to help the user get a little better oriented (I found the existing preamble a bit scattered).
  - Modernized notation (using `dyn Trait`)
  - Changed the section headings to all be written with the same logical sense: to all be written as "conditions that violate object safety" rather than a mix of that and the negated form "conditions that must not happen in order to ensure object safety".

I think there's a fair bit more to clean up in this doc -- the later sections get a bit rambly and I suspect there should be a completely separated-out section covering the `where Self:Sized` escape hatch for instructing the compiler to "do the old thing" and strip methods off traits when turning them into objects (it's a bit buried as a digression in the individual sub-error sections). But I did what I had time for now.
2021-12-02 22:16:12 +01:00
..
rustc Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_apfloat Revert "Auto merge of #89709 - clemenswasser:apply_clippy_suggestions_2, r=petrochenkov" 2021-10-15 11:28:23 +02:00
rustc_arena Add some comments. 2021-11-19 07:52:59 +11:00
rustc_ast expand: Turn ast::Crate into a first class expansion target 2021-11-28 15:48:55 +08:00
rustc_ast_lowering Merge Implicit and ImplicitMissing. 2021-11-30 22:56:47 +01:00
rustc_ast_passes Rollup merge of #91208 - estebank:eq-constraint, r=cjgillot 2021-11-27 11:46:44 +01:00
rustc_ast_pretty expand: Turn ast::Crate into a first class expansion target 2021-11-28 15:48:55 +08:00
rustc_attr re-format with new rustfmt 2021-11-30 13:08:41 -05:00
rustc_borrowck Rollup merge of #91321 - matthewjasper:constaint-placeholders, r=jackh726 2021-12-02 22:16:09 +01:00
rustc_builtin_macros Rollup merge of #91313 - petrochenkov:cratexp, r=Aaron1011 2021-12-01 20:57:43 +01:00
rustc_codegen_cranelift fix sparc64 ABI for aggregates with floating point members 2021-12-01 10:03:45 +01:00
rustc_codegen_gcc Auto merge of #91003 - psumbera:sparc64-abi, r=nagisa 2021-12-02 02:59:44 +00:00
rustc_codegen_llvm Rollup merge of #91394 - Mark-Simulacrum:bump-stage0, r=pietroalbini 2021-12-02 15:52:03 +01:00
rustc_codegen_ssa Rollup merge of #91207 - richkadel:rk-bump-coverage-version, r=tmandry 2021-12-01 10:50:20 +01:00
rustc_const_eval Auto merge of #91354 - fee1-dead:const_env, r=spastorino 2021-12-02 11:48:58 +00:00
rustc_data_structures Use intrinsic pointer methods 2021-11-27 16:59:18 +00:00
rustc_driver add rustc option for using LLVM stack smash protection 2021-11-22 20:06:22 +01:00
rustc_error_codes Clarify and tidy up explanation of E0038 2021-11-30 09:25:17 -08:00
rustc_errors Rollup merge of #91394 - Mark-Simulacrum:bump-stage0, r=pietroalbini 2021-12-02 15:52:03 +01:00
rustc_expand Rollup merge of #91394 - Mark-Simulacrum:bump-stage0, r=pietroalbini 2021-12-02 15:52:03 +01:00
rustc_feature Rollup merge of #90420 - GuillaumeGomez:rustdoc-internals-feature, r=camelid 2021-11-24 22:56:37 +01:00
rustc_fs_util Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_graphviz Revert "Auto merge of #89709 - clemenswasser:apply_clippy_suggestions_2, r=petrochenkov" 2021-10-15 11:28:23 +02:00
rustc_hir Auto merge of #91354 - fee1-dead:const_env, r=spastorino 2021-12-02 11:48:58 +00:00
rustc_hir_pretty Auto merge of #89124 - cjgillot:owner-info, r=michaelwoerister 2021-10-18 19:53:05 +00:00
rustc_incremental Add -Zassert-incr-state to assert state of incremental cache 2021-11-12 13:41:46 -06:00
rustc_index Auto merge of #90491 - Mark-Simulacrum:push-pred-faster, r=matthewjasper 2021-11-24 15:51:46 +00:00
rustc_infer Auto merge of #91354 - fee1-dead:const_env, r=spastorino 2021-12-02 11:48:58 +00:00
rustc_interface expand: Turn ast::Crate into a first class expansion target 2021-11-28 15:48:55 +08:00
rustc_lexer udpate comment to be more accurate 2021-11-23 20:37:23 +00:00
rustc_lint Rollup merge of #91394 - Mark-Simulacrum:bump-stage0, r=pietroalbini 2021-12-02 15:52:03 +01:00
rustc_lint_defs Properly register text_direction_codepoint_in_comment lint. 2021-11-05 20:12:40 +01:00
rustc_llvm Rollup merge of #91207 - richkadel:rk-bump-coverage-version, r=tmandry 2021-12-01 10:50:20 +01:00
rustc_macros Make TypeFoldable implementors short-circuit on error 2021-11-26 07:17:59 +00:00
rustc_metadata Improve suggestion for extern crate self error message 2021-12-01 21:59:54 +00:00
rustc_middle Auto merge of #91455 - matthiaskrgr:rollup-gix2hy6, r=matthiaskrgr 2021-12-02 14:53:20 +00:00
rustc_mir_build Fix stack overflow in usefulness.rs 2021-11-23 23:07:11 +01:00
rustc_mir_dataflow Auto merge of #90788 - ecstatic-morse:issue-90752, r=wesleywiser 2021-11-23 17:44:33 +00:00
rustc_mir_transform Rollup merge of #91294 - cjgillot:process-elem, r=jackh726 2021-11-30 23:43:31 +01:00
rustc_monomorphize Rollup merge of #90701 - michaelwoerister:more-artifact-sizes, r=davidtwco 2021-11-09 19:00:45 +01:00
rustc_parse Rollup merge of #91313 - petrochenkov:cratexp, r=Aaron1011 2021-12-01 20:57:43 +01:00
rustc_parse_format Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_passes Apply cfg-bootstrap switch 2021-11-30 10:51:42 -05:00
rustc_plugin_impl Move rustc_middle::middle::cstore to rustc_session. 2021-10-03 16:08:51 +02:00
rustc_privacy Take a LocalDefId in expect_*item. 2021-11-28 21:09:45 +01:00
rustc_query_impl Revert "Add rustc lint, warning when iterating over hashmaps" 2021-10-28 11:01:42 -04:00
rustc_query_system Manually outline error on incremental_verify_ich 2021-11-22 21:32:20 -05:00
rustc_resolve Rollup merge of #91394 - Mark-Simulacrum:bump-stage0, r=pietroalbini 2021-12-02 15:52:03 +01:00
rustc_save_analysis Give inline const separate DefKind 2021-11-07 03:59:06 +00:00
rustc_serialize Avoid generating empty closures for fieldless enums 2021-11-22 21:22:35 -05:00
rustc_session Accumulate all values of -C remark option 2021-11-29 09:12:01 +01:00
rustc_span Add parent crate assert to register_expn_id 2021-11-28 16:35:54 -06:00
rustc_symbol_mangling Revert "Add rustc lint, warning when iterating over hashmaps" 2021-10-28 11:01:42 -04:00
rustc_target Auto merge of #91003 - psumbera:sparc64-abi, r=nagisa 2021-12-02 02:59:44 +00:00
rustc_trait_selection Rollup merge of #91329 - Aaron1011:modulo-regions-test, r=jackh726 2021-12-02 22:16:10 +01:00
rustc_traits implement version of normalize_erasing_regions that doesn't assume value is normalizable 2021-12-01 12:12:38 +01:00
rustc_ty_utils Auto merge of #91354 - fee1-dead:const_env, r=spastorino 2021-12-02 11:48:58 +00:00
rustc_type_ir Add two inline annotations for hot functions 2021-10-03 12:43:43 -04:00
rustc_typeck Rollup merge of #91364 - FabianWolff:issue-91210-ptr-field, r=oli-obk 2021-12-02 22:16:11 +01:00