You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rollup merge of rust-lang#137289 - compiler-errors:coerce-unsized-errors, r=oli-obk
Consolidate and improve error messaging for `CoerceUnsized` and `DispatchFromDyn`
Firstly, this PR consolidates and reworks the error diagnostics for `CoercePointee` and `DispatchFromDyn`. There was a ton of duplication for no reason -- this reworks both the errors and also the error codes, since they can be shared between both traits since they report the same thing.
Secondly, when encountering a struct with multiple fields that must be coerced, point out the field spans, rather than mentioning the fields by name. This makes the error message clearer, but also means that we don't mention the `__S` dummy parameter for `derive(CoercePointee)`.
Thirdly, emit a custom error message when we encounter a trait error that comes from the recursive field `CoerceUnsized`/`DispatchFromDyn` trait check. **Note:** This is the only one I'm not too satisfied with -- I think it could use some more refinement, but ideally it explains that the field must be an unsize-able pointer... Feedback welcome.
Finally, don't emit `DispatchFromDyn` validity errors if we detect `CoerceUnsized` validity errors from an impl of the same ADT.
This is best reviewed per commit.
r? `@oli-obk` perhaps?
cc `@dingxiangfei2009` -- sorry for making my own attempt at this PR, but I wanted to see if I could implement a fix for rust-lang#136796 in a less complicated way, since communicating over github review comments can be a bit slow. I'll leave comments inline to explain my thinking about the diagnostics changes.
.note1 = functions with the `{$abi}` ABI must pass their result via the available return registers
86
86
.note2 = the result must either be a (transparently wrapped) i64, u64 or f64, or be at most 4 bytes in size
87
87
88
+
hir_analysis_coerce_multi = implementing `{$trait_name}` does not allow multiple fields to be coerced
89
+
.note = the trait `{$trait_name}` may only be implemented when a single field is being coerced
90
+
.label = these fields must be coerced for `{$trait_name}` to be valid
91
+
88
92
hir_analysis_coerce_pointee_no_field = `CoercePointee` can only be derived on `struct`s with at least one field
89
93
90
94
hir_analysis_coerce_pointee_no_user_validity_assertion = asserting applicability of `derive(CoercePointee)` on a target data is forbidden
@@ -95,12 +99,12 @@ hir_analysis_coerce_pointee_not_struct = `derive(CoercePointee)` is only applica
95
99
96
100
hir_analysis_coerce_pointee_not_transparent = `derive(CoercePointee)` is only applicable to `struct` with `repr(transparent)` layout
97
101
102
+
hir_analysis_coerce_unsized_field_validity = for `{$ty}` to have a valid implementation of `{$trait_name}`, it must be possible to coerce the field of type `{$field_ty}`
103
+
.label = `{$field_ty}` must be a pointer, reference, or smart pointer that is allowed to be unsized
104
+
98
105
hir_analysis_coerce_unsized_may = the trait `{$trait_name}` may only be implemented for a coercion between structures
99
106
100
-
hir_analysis_coerce_unsized_multi = implementing the trait `CoerceUnsized` requires multiple coercions
101
-
.note = `CoerceUnsized` may only be implemented for a coercion between structures with one field being coerced
102
-
.coercions_note = currently, {$number} fields need coercions: {$coercions}
103
-
.label = requires multiple coercions
107
+
hir_analysis_coerce_zero = implementing `{$trait_name}` requires a field to be coerced
104
108
105
109
hir_analysis_coercion_between_struct_same_note = expected coercion between the same definition; expected `{$source_path}`, found `{$target_path}`
106
110
@@ -139,10 +143,6 @@ hir_analysis_cross_crate_traits = cross-crate traits with a default impl, like `
139
143
hir_analysis_cross_crate_traits_defined = cross-crate traits with a default impl, like `{$traits}`, can only be implemented for a struct/enum type defined in the current crate
140
144
.label = can't implement cross-crate trait for type in another crate
141
145
142
-
hir_analysis_dispatch_from_dyn_multi = implementing the `DispatchFromDyn` trait requires multiple coercions
143
-
.note = the trait `DispatchFromDyn` may only be implemented for a coercion between structures with a single field being coerced
144
-
.coercions_note = currently, {$number} fields need coercions: {$coercions}
145
-
146
146
hir_analysis_dispatch_from_dyn_repr = structs implementing `DispatchFromDyn` may not have `#[repr(packed)]` or `#[repr(C)]`
147
147
148
148
hir_analysis_dispatch_from_dyn_zst = the trait `DispatchFromDyn` may only be implemented for structs containing the field being coerced, ZST fields with 1 byte alignment that don't mention type/const generics, and nothing else
0 commit comments