-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Object safe for dispatch #57545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Object safe for dispatch #57545
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2246,7 +2246,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { | |
} | ||
|
||
if let Some(principal) = data.principal() { | ||
principal.with_self_ty(self.tcx(), self_ty) | ||
if !self.infcx.tcx.features().object_safe_for_dispatch { | ||
principal.with_self_ty(self.tcx(), self_ty) | ||
} else if self.tcx().is_object_safe(principal.def_id()) { | ||
principal.with_self_ty(self.tcx(), self_ty) | ||
} else { | ||
return; | ||
} | ||
|
||
} else { | ||
// Only auto-trait bounds exist. | ||
return; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -209,6 +209,11 @@ impl<'a> DiagnosticBuilder<'a> { | |
found_extra: &dyn fmt::Display, | ||
) -> &mut Self); | ||
|
||
forward!(pub fn note_unsuccessfull_coercion(&mut self, | ||
|
||
expected: DiagnosticStyledString, | ||
found: DiagnosticStyledString, | ||
) -> &mut Self); | ||
|
||
forward!(pub fn note(&mut self, msg: &str) -> &mut Self); | ||
forward!(pub fn span_note<S: Into<MultiSpan>>(&mut self, | ||
sp: S, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -428,21 +428,36 @@ impl<'a, 'tcx> CastCheck<'tcx> { | |
self.report_cast_to_unsized_type(fcx); | ||
} else if self.expr_ty.references_error() || self.cast_ty.references_error() { | ||
// No sense in giving duplicate error messages | ||
} else if self.try_coercion_cast(fcx) { | ||
self.trivial_cast_lint(fcx); | ||
debug!(" -> CoercionCast"); | ||
fcx.tables.borrow_mut().set_coercion_cast(self.expr.hir_id.local_id); | ||
|
||
} else { | ||
match self.do_check(fcx) { | ||
Ok(k) => { | ||
debug!(" -> {:?}", k); | ||
match self.try_coercion_cast(fcx) { | ||
Ok(()) => { | ||
self.trivial_cast_lint(fcx); | ||
debug!(" -> CoercionCast"); | ||
fcx.tables.borrow_mut() | ||
.set_coercion_cast(self.expr.hir_id.local_id); | ||
} | ||
Err(ty::error::TypeError::ObjectUnsafeCoercion(did)) => { | ||
self.report_object_unsafe_cast(&fcx, did); | ||
|
||
} | ||
Err(_) => { | ||
match self.do_check(fcx) { | ||
Ok(k) => { | ||
debug!(" -> {:?}", k); | ||
} | ||
Err(e) => self.report_cast_error(fcx, e), | ||
}; | ||
} | ||
Err(e) => self.report_cast_error(fcx, e), | ||
}; | ||
} | ||
} | ||
|
||
fn report_object_unsafe_cast(&self, fcx: &FnCtxt<'a, 'tcx>, did: DefId) { | ||
let violations = fcx.tcx.object_safety_violations(did); | ||
let mut err = fcx.tcx.report_object_safety_error(self.cast_span, did, violations); | ||
err.note(&format!("required by cast to type '{}'", fcx.ty_to_string(self.cast_ty))); | ||
err.emit(); | ||
} | ||
|
||
/// Checks a cast, and report an error if one exists. In some cases, this | ||
/// can return Ok and create type errors in the fcx rather than returning | ||
/// directly. coercion-cast is handled in check instead of here. | ||
|
@@ -646,8 +661,14 @@ impl<'a, 'tcx> CastCheck<'tcx> { | |
} | ||
} | ||
|
||
fn try_coercion_cast(&self, fcx: &FnCtxt<'a, 'tcx>) -> bool { | ||
fcx.try_coerce(self.expr, self.expr_ty, self.cast_ty, AllowTwoPhase::No).is_ok() | ||
fn try_coercion_cast( | ||
&self, | ||
fcx: &FnCtxt<'a, 'tcx>, | ||
) -> Result<(), ty::error::TypeError<'_>> { | ||
match fcx.try_coerce(self.expr, self.expr_ty, self.cast_ty, AllowTwoPhase::No) { | ||
Ok(_) => Ok(()), | ||
Err(err) => Err(err), | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else if
?