-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the check for the feature flag necessary here? It seems like you could check if the trait is object safe anyway, even if the feature is not enabled. Or would that result in duplicate errors? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this is the code which adds the automatic 'impl Trait for dyn Trait' predicate to trait objects. Maybe the branches should be clarified and a comment added. Like: // If we should add the automatic 'impl Trait for dyn Trait' or not
let object_impl_trait = !self.infcx.tcx.features().object_safe_for_dispatch ||
self.tcx().is_object_safe(principal.def_id());
if (object_impl_trait) {
principal.with_self_ty(self.tcx(), self_ty)
} else {
return;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At minimum, a comment would be good here -- I think I vaguely prefer the But a comment like this would be great: // Prior to RFC 2027, |
||
} 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I think it is spelled |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think these changes would fall out from the approach I highlighted, and then changing the trait error handling code. |
||
} | ||
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
?