Skip to content

Commit 5e96d74

Browse files
author
Alexander Regueiro
committed
Added diagnostic to suggest enabling trait_upcasting feature.
1 parent 654f14e commit 5e96d74

File tree

1 file changed

+22
-0
lines changed
  • src/librustc_infer/infer/error_reporting

1 file changed

+22
-0
lines changed

src/librustc_infer/infer/error_reporting/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,6 +1586,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15861586
}
15871587
if let Some(exp_found) = exp_found {
15881588
self.suggest_as_ref_where_appropriate(span, &exp_found, diag);
1589+
1590+
if let &TypeError::Traits(ref exp_found_traits) = terr {
1591+
self.note_enable_trait_upcasting_where_appropriate(
1592+
&exp_found_traits, diag
1593+
);
1594+
}
15891595
}
15901596

15911597
// In some (most?) cases cause.body_id points to actual body, but in some cases
@@ -1666,6 +1672,22 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
16661672
}
16671673
}
16681674

1675+
/// When encountering a coercion to a supertrait object is attempted,
1676+
/// note that this is permitted with the "trait upcasting" feature enabled.
1677+
fn note_enable_trait_upcasting_where_appropriate(
1678+
&self,
1679+
exp_found: &ty::error::ExpectedFound<DefId>,
1680+
diag: &mut DiagnosticBuilder<'tcx>,
1681+
) {
1682+
use rustc::ty::{Binder, TraitRef};
1683+
1684+
let trait_ref = Binder::bind(TraitRef::identity(self.tcx, exp_found.found));
1685+
let supertraits = crate::traits::supertraits(self.tcx, trait_ref);
1686+
if supertraits.into_iter().any(|trait_ref| trait_ref.def_id() == exp_found.expected) {
1687+
diag.note("enable the `trait_upcasting` feature to permit upcasting of trait objects");
1688+
}
1689+
}
1690+
16691691
pub fn report_and_explain_type_error(
16701692
&self,
16711693
trace: TypeTrace<'tcx>,

0 commit comments

Comments
 (0)