Skip to content
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

More suggestion for converting Option<&Vec<T>> to Option<&[T]> #127596

Merged
merged 3 commits into from
Jul 12, 2024
Merged
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
52 changes: 37 additions & 15 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,21 +466,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
borrow_removal_span,
});
return true;
} else if let Some((deref_ty, _)) =
self.autoderef(expr.span, found_ty_inner).silence_errors().nth(1)
&& self.can_eq(self.param_env, deref_ty, peeled)
&& error_tys_equate_as_ref
{
let sugg = prefix_wrap(".as_deref()");
err.subdiagnostic(errors::SuggestConvertViaMethod {
span: expr.span.shrink_to_hi(),
sugg,
expected,
found,
borrow_removal_span,
});
return true;
} else if let ty::Adt(adt, _) = found_ty_inner.peel_refs().kind()
} else if let ty::Ref(_, peeled_found_ty, _) = found_ty_inner.kind()
Copy link
Contributor Author

@tesuji tesuji Jul 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is necessary. Because suggesting Option<String>.map(|v| v.as_str()) violates ownership rules.

&& let ty::Adt(adt, _) = peeled_found_ty.peel_refs().kind()
&& self.tcx.is_lang_item(adt.did(), LangItem::String)
&& peeled.is_str()
// `Result::map`, conversely, does not take ref of the error type.
Expand All @@ -496,12 +483,47 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Applicability::MachineApplicable,
);
return true;
} else {
if !error_tys_equate_as_ref {
return false;
}
let mut steps = self.autoderef(expr.span, found_ty_inner).silence_errors();
if let Some((deref_ty, _)) = steps.nth(1)
&& self.can_eq(self.param_env, deref_ty, peeled)
{
let sugg = prefix_wrap(".as_deref()");
err.subdiagnostic(errors::SuggestConvertViaMethod {
span: expr.span.shrink_to_hi(),
sugg,
expected,
found,
borrow_removal_span,
});
return true;
}
for (deref_ty, n_step) in steps {
if self.can_eq(self.param_env, deref_ty, peeled) {
let explicit_deref = "*".repeat(n_step);
let sugg = prefix_wrap(&format!(".map(|v| &{explicit_deref}v)"));
err.subdiagnostic(errors::SuggestConvertViaMethod {
span: expr.span.shrink_to_hi(),
sugg,
expected,
found,
borrow_removal_span,
});
return true;
}
}
}
}

false
}

/// If `ty` is `Option<T>`, returns `T, T, None`.
/// If `ty` is `Result<T, E>`, returns `T, T, Some(E, E)`.
/// Otherwise, returns `None`.
fn deconstruct_option_or_result(
&self,
found_ty: Ty<'tcx>,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Regression test for <https://github.com/rust-lang/rust/issues/127545>.
#![crate_type = "lib"]

pub fn foo(arg: Option<&Vec<i32>>) -> Option<&[i32]> {
arg //~ ERROR 5:5: 5:8: mismatched types [E0308]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error[E0308]: mismatched types
--> $DIR/transforming-option-ref-issue-127545.rs:5:5
|
LL | pub fn foo(arg: Option<&Vec<i32>>) -> Option<&[i32]> {
| -------------- expected `Option<&[i32]>` because of return type
LL | arg
| ^^^ expected `Option<&[i32]>`, found `Option<&Vec<i32>>`
|
= note: expected enum `Option<&[i32]>`
found enum `Option<&Vec<i32>>`
help: try using `.map(|v| &**v)` to convert `Option<&Vec<i32>>` to `Option<&[i32]>`
|
LL | arg.map(|v| &**v)
| ++++++++++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
Loading