Skip to content

Commit 14695c1

Browse files
Simplify trait error message for CoercePointee validation
1 parent f24f274 commit 14695c1

File tree

6 files changed

+191
-40
lines changed

6 files changed

+191
-40
lines changed

Diff for: compiler/rustc_hir_analysis/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ hir_analysis_coerce_pointee_not_struct = `derive(CoercePointee)` is only applica
9999
100100
hir_analysis_coerce_pointee_not_transparent = `derive(CoercePointee)` is only applicable to `struct` with `repr(transparent)` layout
101101
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+
102105
hir_analysis_coerce_unsized_may = the trait `{$trait_name}` may only be implemented for a coercion between structures
103106
104107
hir_analysis_coerce_zero = implementing `{$trait_name}` requires a field to be coerced

Diff for: compiler/rustc_hir_analysis/src/coherence/builtin.rs

+61-37
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_middle::ty::print::PrintTraitRefExt as _;
1717
use rustc_middle::ty::{
1818
self, Ty, TyCtxt, TypeVisitableExt, TypingMode, suggest_constraining_type_params,
1919
};
20-
use rustc_span::{DUMMY_SP, Span};
20+
use rustc_span::{DUMMY_SP, Span, sym};
2121
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
2222
use rustc_trait_selection::traits::misc::{
2323
ConstParamTyImplementationError, CopyImplementationError, InfringingFieldsReason,
@@ -199,13 +199,21 @@ fn visit_implementation_of_coerce_unsized(checker: &Checker<'_>) -> Result<(), E
199199
tcx.at(span).ensure_ok().coerce_unsized_info(impl_did)
200200
}
201201

202+
fn is_from_coerce_pointee_derive(tcx: TyCtxt<'_>, span: Span) -> bool {
203+
span.ctxt()
204+
.outer_expn_data()
205+
.macro_def_id
206+
.is_some_and(|def_id| tcx.is_diagnostic_item(sym::CoercePointee, def_id))
207+
}
208+
202209
fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<(), ErrorGuaranteed> {
203210
let tcx = checker.tcx;
204211
let impl_did = checker.impl_def_id;
205212
let trait_ref = checker.impl_header.trait_ref.instantiate_identity();
206213
debug!("visit_implementation_of_dispatch_from_dyn: impl_did={:?}", impl_did);
207214

208215
let span = tcx.def_span(impl_did);
216+
let trait_name = "DispatchFromDyn";
209217

210218
let dispatch_from_dyn_trait = tcx.require_lang_item(LangItem::DispatchFromDyn, Some(span));
211219

@@ -245,7 +253,7 @@ fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<()
245253
let target_path = tcx.def_path_str(def_b.did());
246254
return Err(tcx.dcx().emit_err(errors::CoerceSameStruct {
247255
span,
248-
trait_name: "DispatchFromDyn",
256+
trait_name,
249257
note: true,
250258
source_path,
251259
target_path,
@@ -311,40 +319,46 @@ fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<()
311319
if coerced_fields.is_empty() {
312320
return Err(tcx.dcx().emit_err(errors::CoerceNoField {
313321
span,
314-
trait_name: "DispatchFromDyn",
322+
trait_name,
315323
note: true,
316324
}));
317-
} else if coerced_fields.len() > 1 {
318-
return Err(tcx.dcx().emit_err(errors::CoerceMulti {
319-
span,
320-
trait_name: "DispatchFromDyn",
321-
number: coerced_fields.len(),
322-
fields: coerced_fields.iter().map(|(_, _, _, s)| *s).collect::<Vec<_>>().into(),
323-
}));
324-
} else {
325+
} else if let &[(_, ty_a, ty_b, field_span)] = &coerced_fields[..] {
325326
let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
326-
for (_, ty_a, ty_b, _) in coerced_fields {
327-
ocx.register_obligation(Obligation::new(
328-
tcx,
329-
cause.clone(),
330-
param_env,
331-
ty::TraitRef::new(tcx, dispatch_from_dyn_trait, [ty_a, ty_b]),
332-
));
333-
}
327+
ocx.register_obligation(Obligation::new(
328+
tcx,
329+
cause.clone(),
330+
param_env,
331+
ty::TraitRef::new(tcx, dispatch_from_dyn_trait, [ty_a, ty_b]),
332+
));
334333
let errors = ocx.select_all_or_error();
335334
if !errors.is_empty() {
336-
return Err(infcx.err_ctxt().report_fulfillment_errors(errors));
335+
if is_from_coerce_pointee_derive(tcx, span) {
336+
return Err(tcx.dcx().emit_err(errors::CoerceFieldValidity {
337+
span,
338+
trait_name,
339+
ty: trait_ref.self_ty(),
340+
field_span,
341+
field_ty: ty_a,
342+
}));
343+
} else {
344+
return Err(infcx.err_ctxt().report_fulfillment_errors(errors));
345+
}
337346
}
338347

339348
// Finally, resolve all regions.
340349
ocx.resolve_regions_and_report_errors(impl_did, param_env, [])?;
341-
}
342350

343-
Ok(())
351+
Ok(())
352+
} else {
353+
return Err(tcx.dcx().emit_err(errors::CoerceMulti {
354+
span,
355+
trait_name,
356+
number: coerced_fields.len(),
357+
fields: coerced_fields.iter().map(|(_, _, _, s)| *s).collect::<Vec<_>>().into(),
358+
}));
359+
}
344360
}
345-
_ => Err(tcx
346-
.dcx()
347-
.emit_err(errors::CoerceUnsizedNonStruct { span, trait_name: "DispatchFromDyn" })),
361+
_ => Err(tcx.dcx().emit_err(errors::CoerceUnsizedNonStruct { span, trait_name })),
348362
}
349363
}
350364

@@ -354,13 +368,14 @@ pub(crate) fn coerce_unsized_info<'tcx>(
354368
) -> Result<CoerceUnsizedInfo, ErrorGuaranteed> {
355369
debug!("compute_coerce_unsized_info(impl_did={:?})", impl_did);
356370
let span = tcx.def_span(impl_did);
371+
let trait_name = "CoerceUnsized";
357372

358373
let coerce_unsized_trait = tcx.require_lang_item(LangItem::CoerceUnsized, Some(span));
359-
360374
let unsize_trait = tcx.require_lang_item(LangItem::Unsize, Some(span));
361375

362376
let source = tcx.type_of(impl_did).instantiate_identity();
363377
let trait_ref = tcx.impl_trait_ref(impl_did).unwrap().instantiate_identity();
378+
364379
assert_eq!(trait_ref.def_id, coerce_unsized_trait);
365380
let target = trait_ref.args.type_at(1);
366381
debug!("visit_implementation_of_coerce_unsized: {:?} -> {:?} (bound)", source, target);
@@ -387,9 +402,9 @@ pub(crate) fn coerce_unsized_info<'tcx>(
387402
)
388403
.emit();
389404
}
390-
(mt_a.ty, mt_b.ty, unsize_trait, None)
405+
(mt_a.ty, mt_b.ty, unsize_trait, None, span)
391406
};
392-
let (source, target, trait_def_id, kind) = match (source.kind(), target.kind()) {
407+
let (source, target, trait_def_id, kind, field_span) = match (source.kind(), target.kind()) {
393408
(&ty::Ref(r_a, ty_a, mutbl_a), &ty::Ref(r_b, ty_b, mutbl_b)) => {
394409
infcx.sub_regions(infer::RelateObjectBound(span), r_b, r_a);
395410
let mt_a = ty::TypeAndMut { ty: ty_a, mutbl: mutbl_a };
@@ -412,7 +427,7 @@ pub(crate) fn coerce_unsized_info<'tcx>(
412427
let target_path = tcx.def_path_str(def_b.did());
413428
return Err(tcx.dcx().emit_err(errors::CoerceSameStruct {
414429
span,
415-
trait_name: "CoerceUnsized",
430+
trait_name,
416431
note: true,
417432
source_path,
418433
target_path,
@@ -499,7 +514,7 @@ pub(crate) fn coerce_unsized_info<'tcx>(
499514
if diff_fields.is_empty() {
500515
return Err(tcx.dcx().emit_err(errors::CoerceNoField {
501516
span,
502-
trait_name: "CoerceUnsized",
517+
trait_name,
503518
note: true,
504519
}));
505520
} else if diff_fields.len() > 1 {
@@ -512,21 +527,19 @@ pub(crate) fn coerce_unsized_info<'tcx>(
512527

513528
return Err(tcx.dcx().emit_err(errors::CoerceMulti {
514529
span,
515-
trait_name: "CoerceUnsized",
530+
trait_name,
516531
number: diff_fields.len(),
517532
fields: diff_fields.iter().map(|(_, _, _, s)| *s).collect::<Vec<_>>().into(),
518533
}));
519534
}
520535

521-
let (i, a, b, _) = diff_fields[0];
536+
let (i, a, b, field_span) = diff_fields[0];
522537
let kind = ty::adjustment::CustomCoerceUnsized::Struct(i);
523-
(a, b, coerce_unsized_trait, Some(kind))
538+
(a, b, coerce_unsized_trait, Some(kind), field_span)
524539
}
525540

526541
_ => {
527-
return Err(tcx
528-
.dcx()
529-
.emit_err(errors::CoerceUnsizedNonStruct { span, trait_name: "CoerceUnsized" }));
542+
return Err(tcx.dcx().emit_err(errors::CoerceUnsizedNonStruct { span, trait_name }));
530543
}
531544
};
532545

@@ -541,8 +554,19 @@ pub(crate) fn coerce_unsized_info<'tcx>(
541554
);
542555
ocx.register_obligation(obligation);
543556
let errors = ocx.select_all_or_error();
557+
544558
if !errors.is_empty() {
545-
return Err(infcx.err_ctxt().report_fulfillment_errors(errors));
559+
if is_from_coerce_pointee_derive(tcx, span) {
560+
return Err(tcx.dcx().emit_err(errors::CoerceFieldValidity {
561+
span,
562+
trait_name,
563+
ty: trait_ref.self_ty(),
564+
field_span,
565+
field_ty: source,
566+
}));
567+
} else {
568+
return Err(infcx.err_ctxt().report_fulfillment_errors(errors));
569+
}
546570
}
547571

548572
// Finally, resolve all regions.

Diff for: compiler/rustc_hir_analysis/src/errors.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,18 @@ pub(crate) struct CoerceSameStruct {
13211321
pub target_path: String,
13221322
}
13231323

1324+
#[derive(Diagnostic)]
1325+
#[diag(hir_analysis_coerce_unsized_field_validity)]
1326+
pub(crate) struct CoerceFieldValidity<'tcx> {
1327+
#[primary_span]
1328+
pub span: Span,
1329+
pub ty: Ty<'tcx>,
1330+
pub trait_name: &'static str,
1331+
#[label]
1332+
pub field_span: Span,
1333+
pub field_ty: Ty<'tcx>,
1334+
}
1335+
13241336
#[derive(Diagnostic)]
13251337
#[diag(hir_analysis_trait_cannot_impl_for_ty, code = E0204)]
13261338
pub(crate) struct TraitCannotImplForTy {

Diff for: library/core/src/marker.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,7 @@ pub trait FnPtr: Copy + Clone {
13021302
/// ```
13031303
#[rustc_builtin_macro(CoercePointee, attributes(pointee))]
13041304
#[allow_internal_unstable(dispatch_from_dyn, coerce_unsized, unsize, coerce_pointee_validated)]
1305+
#[cfg_attr(not(test), rustc_diagnostic_item = "CoercePointee")]
13051306
#[unstable(feature = "derive_coerce_pointee", issue = "123430")]
13061307
pub macro CoercePointee($item:item) {
13071308
/* compiler built-in */

Diff for: tests/ui/deriving/deriving-coerce-pointee-neg.rs

+26
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,30 @@ struct TryToWipeRepr<'a, #[pointee] T: ?Sized> {
142142
ptr: &'a T,
143143
}
144144

145+
#[repr(transparent)]
146+
#[derive(CoercePointee)]
147+
//~^ ERROR for `RcWithId<T>` to have a valid implementation of `CoerceUnsized`, it must be possible to coerce the field of type `Rc<(i32, Box<T>)>`
148+
//~| ERROR for `RcWithId<T>` to have a valid implementation of `DispatchFromDyn`, it must be possible to coerce the field of type `Rc<(i32, Box<T>)>`
149+
struct RcWithId<T: ?Sized> {
150+
inner: std::rc::Rc<(i32, Box<T>)>,
151+
}
152+
153+
#[repr(transparent)]
154+
#[derive(CoercePointee)]
155+
//~^ ERROR implementing `CoerceUnsized` does not allow multiple fields to be coerced
156+
//~| ERROR implementing `DispatchFromDyn` does not allow multiple fields to be coerced
157+
struct MoreThanOneField<T: ?Sized> {
158+
//~^ ERROR transparent struct needs at most one field with non-trivial size or alignment, but has 2
159+
inner1: Box<T>,
160+
inner2: Box<T>,
161+
}
162+
163+
struct NotCoercePointeeData<T: ?Sized>(T);
164+
165+
#[repr(transparent)]
166+
#[derive(CoercePointee)]
167+
//~^ ERROR for `UsingNonCoercePointeeData<T>` to have a valid implementation of `CoerceUnsized`, it must be possible to coerce the field of type `NotCoercePointeeData<T>`
168+
//~| ERROR for `UsingNonCoercePointeeData<T>` to have a valid implementation of `DispatchFromDyn`, it must be possible to coerce the field of type `NotCoercePointeeData<T>`
169+
struct UsingNonCoercePointeeData<T: ?Sized>(NotCoercePointeeData<T>);
170+
145171
fn main() {}

Diff for: tests/ui/deriving/deriving-coerce-pointee-neg.stderr

+88-3
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,92 @@ error[E0802]: `derive(CoercePointee)` is only applicable to `struct` with `repr(
118118
LL | struct TryToWipeRepr<'a, #[pointee] T: ?Sized> {
119119
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
120120

121-
error: aborting due to 17 previous errors
121+
error: for `RcWithId<T>` to have a valid implementation of `DispatchFromDyn`, it must be possible to coerce the field of type `Rc<(i32, Box<T>)>`
122+
--> $DIR/deriving-coerce-pointee-neg.rs:146:10
123+
|
124+
LL | #[derive(CoercePointee)]
125+
| ^^^^^^^^^^^^^
126+
...
127+
LL | inner: std::rc::Rc<(i32, Box<T>)>,
128+
| --------------------------------- `Rc<(i32, Box<T>)>` must be a pointer, reference, or smart pointer that is allowed to be unsized
129+
|
130+
= note: this error originates in the derive macro `CoercePointee` (in Nightly builds, run with -Z macro-backtrace for more info)
131+
132+
error[E0375]: implementing `DispatchFromDyn` does not allow multiple fields to be coerced
133+
--> $DIR/deriving-coerce-pointee-neg.rs:154:10
134+
|
135+
LL | #[derive(CoercePointee)]
136+
| ^^^^^^^^^^^^^
137+
|
138+
note: the trait `DispatchFromDyn` may only be implemented when a single field is being coerced
139+
--> $DIR/deriving-coerce-pointee-neg.rs:159:5
140+
|
141+
LL | inner1: Box<T>,
142+
| ^^^^^^^^^^^^^^
143+
LL | inner2: Box<T>,
144+
| ^^^^^^^^^^^^^^
145+
= note: this error originates in the derive macro `CoercePointee` (in Nightly builds, run with -Z macro-backtrace for more info)
146+
147+
error: for `UsingNonCoercePointeeData<T>` to have a valid implementation of `DispatchFromDyn`, it must be possible to coerce the field of type `NotCoercePointeeData<T>`
148+
--> $DIR/deriving-coerce-pointee-neg.rs:166:10
149+
|
150+
LL | #[derive(CoercePointee)]
151+
| ^^^^^^^^^^^^^
152+
...
153+
LL | struct UsingNonCoercePointeeData<T: ?Sized>(NotCoercePointeeData<T>);
154+
| ----------------------- `NotCoercePointeeData<T>` must be a pointer, reference, or smart pointer that is allowed to be unsized
155+
|
156+
= note: this error originates in the derive macro `CoercePointee` (in Nightly builds, run with -Z macro-backtrace for more info)
157+
158+
error: for `RcWithId<T>` to have a valid implementation of `CoerceUnsized`, it must be possible to coerce the field of type `Rc<(i32, Box<T>)>`
159+
--> $DIR/deriving-coerce-pointee-neg.rs:146:10
160+
|
161+
LL | #[derive(CoercePointee)]
162+
| ^^^^^^^^^^^^^
163+
...
164+
LL | inner: std::rc::Rc<(i32, Box<T>)>,
165+
| --------------------------------- `Rc<(i32, Box<T>)>` must be a pointer, reference, or smart pointer that is allowed to be unsized
166+
|
167+
= note: this error originates in the derive macro `CoercePointee` (in Nightly builds, run with -Z macro-backtrace for more info)
168+
169+
error[E0375]: implementing `CoerceUnsized` does not allow multiple fields to be coerced
170+
--> $DIR/deriving-coerce-pointee-neg.rs:154:10
171+
|
172+
LL | #[derive(CoercePointee)]
173+
| ^^^^^^^^^^^^^
174+
|
175+
note: the trait `CoerceUnsized` may only be implemented when a single field is being coerced
176+
--> $DIR/deriving-coerce-pointee-neg.rs:159:5
177+
|
178+
LL | inner1: Box<T>,
179+
| ^^^^^^^^^^^^^^
180+
LL | inner2: Box<T>,
181+
| ^^^^^^^^^^^^^^
182+
= note: this error originates in the derive macro `CoercePointee` (in Nightly builds, run with -Z macro-backtrace for more info)
183+
184+
error: for `UsingNonCoercePointeeData<T>` to have a valid implementation of `CoerceUnsized`, it must be possible to coerce the field of type `NotCoercePointeeData<T>`
185+
--> $DIR/deriving-coerce-pointee-neg.rs:166:10
186+
|
187+
LL | #[derive(CoercePointee)]
188+
| ^^^^^^^^^^^^^
189+
...
190+
LL | struct UsingNonCoercePointeeData<T: ?Sized>(NotCoercePointeeData<T>);
191+
| ----------------------- `NotCoercePointeeData<T>` must be a pointer, reference, or smart pointer that is allowed to be unsized
192+
|
193+
= note: this error originates in the derive macro `CoercePointee` (in Nightly builds, run with -Z macro-backtrace for more info)
194+
195+
error[E0690]: transparent struct needs at most one field with non-trivial size or alignment, but has 2
196+
--> $DIR/deriving-coerce-pointee-neg.rs:157:1
197+
|
198+
LL | struct MoreThanOneField<T: ?Sized> {
199+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ needs at most one field with non-trivial size or alignment, but has 2
200+
LL |
201+
LL | inner1: Box<T>,
202+
| -------------- this field has non-zero size or requires alignment
203+
LL | inner2: Box<T>,
204+
| -------------- this field has non-zero size or requires alignment
205+
206+
error: aborting due to 24 previous errors
122207

123-
Some errors have detailed explanations: E0392, E0802.
124-
For more information about an error, try `rustc --explain E0392`.
208+
Some errors have detailed explanations: E0375, E0392, E0690, E0802.
209+
For more information about an error, try `rustc --explain E0375`.

0 commit comments

Comments
 (0)