Skip to content

Commit 49048ea

Browse files
authored
Rollup merge of #96378 - compiler-errors:trait-upcast-error, r=nagisa
Mention traits and types involved in unstable trait upcasting Fixes #95972 by printing the traits being upcasted and the types being coerced that cause that upcasting... --- the poor span mentioned in the original issue has nothing to do with trait upcasting diagnostic here... > The original example I had that made me run into this issue had an even longer expression there (multiple chained iterator methods) which just got all highlighted as one big block saying "somewhere here trait coercion is used and it's not allowed". I don't think I can solve that issue in general without fixing the ObligationCauseCode and span that gets passed into Coerce.
2 parents 04f9038 + 0de7568 commit 49048ea

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
@@ -616,7 +616,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
616616
)];
617617

618618
let mut has_unsized_tuple_coercion = false;
619-
let mut has_trait_upcasting_coercion = false;
619+
let mut has_trait_upcasting_coercion = None;
620620

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

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

720725
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)