Skip to content

Commit 0de7568

Browse files
Mention traits being upcasted, types being coerced
1 parent 4c5f6e6 commit 0de7568

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

compiler/rustc_typeck/src/check/coercion.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
615615
)];
616616

617617
let mut has_unsized_tuple_coercion = false;
618-
let mut has_trait_upcasting_coercion = false;
618+
let mut has_trait_upcasting_coercion = None;
619619

620620
// Keep resolving `CoerceUnsized` and `Unsize` predicates to avoid
621621
// emitting a coercion in cases like `Foo<$1>` -> `Foo<$2>`, where
@@ -635,7 +635,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
635635
&& data_a.principal_def_id() != data_b.principal_def_id()
636636
{
637637
debug!("coerce_unsized: found trait upcasting coercion");
638-
has_trait_upcasting_coercion = true;
638+
has_trait_upcasting_coercion = Some((self_ty, unsize_ty));
639639
}
640640
if let ty::Tuple(..) = unsize_ty.kind() {
641641
debug!("coerce_unsized: found unsized tuple coercion");
@@ -706,14 +706,19 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
706706
.emit();
707707
}
708708

709-
if has_trait_upcasting_coercion && !self.tcx().features().trait_upcasting {
710-
feature_err(
709+
if let Some((sub, sup)) = has_trait_upcasting_coercion
710+
&& !self.tcx().features().trait_upcasting
711+
{
712+
// Renders better when we erase regions, since they're not really the point here.
713+
let (sub, sup) = self.tcx.erase_regions((sub, sup));
714+
let mut err = feature_err(
711715
&self.tcx.sess.parse_sess,
712716
sym::trait_upcasting,
713717
self.cause.span,
714-
"trait upcasting coercion is experimental",
715-
)
716-
.emit();
718+
&format!("cannot cast `{sub}` to `{sup}`, trait upcasting coercion is experimental"),
719+
);
720+
err.note(&format!("required when coercing `{source}` into `{target}`"));
721+
err.emit();
717722
}
718723

719724
Ok(coercion)

src/test/ui/feature-gates/feature-gate-trait_upcasting.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
error[E0658]: trait upcasting coercion is experimental
1+
error[E0658]: cannot cast `dyn Bar` to `dyn Foo`, trait upcasting coercion is experimental
22
--> $DIR/feature-gate-trait_upcasting.rs:11:25
33
|
44
LL | let foo: &dyn Foo = bar;
55
| ^^^
66
|
77
= note: see issue #65991 <https://github.com/rust-lang/rust/issues/65991> for more information
88
= help: add `#![feature(trait_upcasting)]` to the crate attributes to enable
9+
= note: required when coercing `&dyn Bar` into `&dyn Foo`
910

1011
error: aborting due to previous error
1112

src/test/ui/issues/issue-11515.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
error[E0658]: trait upcasting coercion is experimental
1+
error[E0658]: cannot cast `dyn Fn()` to `dyn FnMut()`, trait upcasting coercion is experimental
22
--> $DIR/issue-11515.rs:9:33
33
|
44
LL | let test = box Test { func: closure };
55
| ^^^^^^^
66
|
77
= note: see issue #65991 <https://github.com/rust-lang/rust/issues/65991> for more information
88
= help: add `#![feature(trait_upcasting)]` to the crate attributes to enable
9+
= note: required when coercing `Box<(dyn Fn() + 'static)>` into `Box<(dyn FnMut() + 'static)>`
910

1011
error: aborting due to previous error
1112

0 commit comments

Comments
 (0)