-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Provide hint when cast needs a dereference #37442
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,6 +102,7 @@ enum CastError { | |
/// Cast of thin to fat raw ptr (eg. `*const () as *const [u8]`) | ||
SizedUnsizedCast, | ||
IllegalCast, | ||
NeedDeref, | ||
NeedViaPtr, | ||
NeedViaThinPtr, | ||
NeedViaInt, | ||
|
@@ -138,6 +139,25 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> { | |
|
||
fn report_cast_error(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>, e: CastError) { | ||
match e { | ||
CastError::NeedDeref => { | ||
let cast_ty = fcx.ty_to_string(self.cast_ty); | ||
let mut err = fcx.type_error_struct(self.cast_span, | ||
|actual| { | ||
format!("casting `{}` as `{}` is invalid", | ||
actual, | ||
cast_ty) | ||
}, | ||
self.expr_ty); | ||
err.span_label(self.expr.span, | ||
&format!("cannot cast `{}` as `{}`", | ||
fcx.ty_to_string(self.expr_ty), | ||
cast_ty)); | ||
if let Ok(snippet) = fcx.sess().codemap().span_to_snippet(self.expr.span) { | ||
err.span_label(self.expr.span, | ||
&format!("did you mean `*{}`?", snippet)); | ||
} | ||
err.emit(); | ||
} | ||
CastError::NeedViaThinPtr | | ||
CastError::NeedViaPtr => { | ||
let mut err = fcx.type_error_struct(self.span, | ||
|
@@ -390,8 +410,28 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> { | |
(Ptr(m_e), Ptr(m_c)) => self.check_ptr_ptr_cast(fcx, m_e, m_c), // ptr-ptr-cast | ||
(Ptr(m_expr), Int(_)) => self.check_ptr_addr_cast(fcx, m_expr), // ptr-addr-cast | ||
(FnPtr, Int(_)) => Ok(CastKind::FnPtrAddrCast), | ||
(RPtr(_), Int(_)) | | ||
(RPtr(_), Float) => Err(CastError::NeedViaPtr), | ||
(RPtr(p), Int(_)) | | ||
(RPtr(p), Float) => { | ||
match p.ty.sty { | ||
ty::TypeVariants::TyInt(_) | | ||
ty::TypeVariants::TyUint(_) | | ||
ty::TypeVariants::TyFloat(_) => { | ||
Err(CastError::NeedDeref) | ||
} | ||
ty::TypeVariants::TyInfer(t) => { | ||
match t { | ||
ty::InferTy::IntVar(_) | | ||
ty::InferTy::FloatVar(_) | | ||
ty::InferTy::FreshIntTy(_) | | ||
ty::InferTy::FreshFloatTy(_) => { | ||
Err(CastError::NeedDeref) | ||
} | ||
_ => Err(CastError::NeedViaPtr), | ||
} | ||
} | ||
_ => Err(CastError::NeedViaPtr), | ||
} | ||
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. struct A;
&A as A; stays without a note/help. I’m fine with it personally, but that’s something that could use such a help message. |
||
} | ||
// * -> ptr | ||
(Int(_), Ptr(mt)) => self.check_addr_ptr_cast(fcx, mt), // addr-ptr-cast | ||
(FnPtr, Ptr(mt)) => self.check_fptr_ptr_cast(fcx, mt), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Pointing at the
self.cast_span
instead ofself.span
(i16
instead ofs as i16
in the example) because when trying to apply the secondary label on justself.expr.span
(s
in the example) the actual output looks like this:losing the fact that the secondary label's hint should only apply to the
s
only.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.
Filed #37453 for this.