Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2346,6 +2346,17 @@ impl<'a, 'b, 'tcx> FnCallDiagCtxt<'a, 'b, 'tcx> {
provided_span,
format!("unexpected argument{idx}{provided_ty_name}"),
));
if self.provided_arg_tys.len() == 1
&& let Some(span) = self.maybe_suggest_expect_for_unwrap(provided_ty)
{
err.span_suggestion_verbose(
span,
"did you mean to use `expect`?",
"expect",
Applicability::MaybeIncorrect,
);
continue;
}
let mut span = provided_span;
if span.can_be_used_for_suggestions()
&& self.call_metadata.error_span.can_be_used_for_suggestions()
Expand Down Expand Up @@ -2776,6 +2787,22 @@ impl<'a, 'b, 'tcx> FnCallDiagCtxt<'a, 'b, 'tcx> {

(suggestion_span, suggestion)
}

fn maybe_suggest_expect_for_unwrap(&self, provided_ty: Ty<'tcx>) -> Option<Span> {
let tcx = self.tcx();
if let Some(call_ident) = self.call_metadata.call_ident
&& call_ident.name == sym::unwrap
&& let Some(callee_ty) = self.callee_ty
&& let ty::Adt(adt, _) = callee_ty.peel_refs().kind()
&& (tcx.is_diagnostic_item(sym::Option, adt.did())
|| tcx.is_diagnostic_item(sym::Result, adt.did()))
&& self.may_coerce(provided_ty, Ty::new_static_str(tcx))
{
Some(call_ident.span)
} else {
None
}
}
}

struct ArgMatchingCtxt<'a, 'b, 'tcx> {
Expand Down
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
#![feature(const_destruct)]
#![feature(const_eval_select)]
#![feature(const_heap)]
#![feature(const_index)]
#![feature(const_option_ops)]
#![feature(const_try)]
#![feature(copied_into_inner)]
Expand Down
12 changes: 8 additions & 4 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3747,7 +3747,8 @@ impl<T: TrivialClone, A: Allocator> ExtendFromWithinSpec for Vec<T, A> {
////////////////////////////////////////////////////////////////////////////////

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, A: Allocator> ops::Deref for Vec<T, A> {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<T, A: Allocator> const ops::Deref for Vec<T, A> {
type Target = [T];

#[inline]
Expand All @@ -3757,7 +3758,8 @@ impl<T, A: Allocator> ops::Deref for Vec<T, A> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, A: Allocator> ops::DerefMut for Vec<T, A> {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<T, A: Allocator> const ops::DerefMut for Vec<T, A> {
#[inline]
fn deref_mut(&mut self) -> &mut [T] {
self.as_mut_slice()
Expand Down Expand Up @@ -3822,7 +3824,8 @@ impl<T: Hash, A: Allocator> Hash for Vec<T, A> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, I: SliceIndex<[T]>, A: Allocator> Index<I> for Vec<T, A> {
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
impl<T, I: [const] SliceIndex<[T]>, A: Allocator> const Index<I> for Vec<T, A> {
type Output = I::Output;

#[inline]
Expand All @@ -3832,7 +3835,8 @@ impl<T, I: SliceIndex<[T]>, A: Allocator> Index<I> for Vec<T, A> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, I: SliceIndex<[T]>, A: Allocator> IndexMut<I> for Vec<T, A> {
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
impl<T, I: [const] SliceIndex<[T]>, A: Allocator> const IndexMut<I> for Vec<T, A> {
#[inline]
fn index_mut(&mut self, index: I) -> &mut Self::Output {
IndexMut::index_mut(&mut **self, index)
Expand Down
Loading
Loading