-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Refactor + improve diagnostics for &mut T
/T
mismatch inside Option/Result
#114150
Merged
bors
merged 2 commits into
rust-lang:master
from
clubby789:improve-option-ref-suggestion
Jul 29, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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 |
---|---|---|
@@ -1,8 +1,6 @@ | ||
use super::FnCtxt; | ||
|
||
use crate::errors::{ | ||
AddReturnTypeSuggestion, ExpectedReturnTypeLabel, SuggestBoxing, SuggestConvertViaMethod, | ||
}; | ||
use crate::errors; | ||
use crate::fluent_generated as fluent; | ||
use crate::method::probe::{IsSuggestion, Mode, ProbeScope}; | ||
use rustc_ast::util::parser::{ExprPrecedence, PREC_POSTFIX}; | ||
|
@@ -434,7 +432,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
// FIXME: This could/should be extended to suggest `as_mut` and `as_deref_mut`, | ||
// but those checks need to be a bit more delicate and the benefit is diminishing. | ||
if self.can_eq(self.param_env, found_ty_inner, peeled) && error_tys_equate_as_ref { | ||
err.subdiagnostic(SuggestConvertViaMethod { | ||
err.subdiagnostic(errors::SuggestConvertViaMethod { | ||
span: expr.span.shrink_to_hi(), | ||
sugg: ".as_ref()", | ||
expected, | ||
|
@@ -447,7 +445,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
&& self.can_eq(self.param_env, deref_ty, peeled) | ||
&& error_tys_equate_as_ref | ||
{ | ||
err.subdiagnostic(SuggestConvertViaMethod { | ||
err.subdiagnostic(errors::SuggestConvertViaMethod { | ||
span: expr.span.shrink_to_hi(), | ||
sugg: ".as_deref()", | ||
expected, | ||
|
@@ -521,17 +519,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
if self.can_coerce(Ty::new_box(self.tcx, found), expected) { | ||
let suggest_boxing = match found.kind() { | ||
ty::Tuple(tuple) if tuple.is_empty() => { | ||
SuggestBoxing::Unit { start: span.shrink_to_lo(), end: span } | ||
errors::SuggestBoxing::Unit { start: span.shrink_to_lo(), end: span } | ||
} | ||
ty::Generator(def_id, ..) | ||
if matches!( | ||
self.tcx.generator_kind(def_id), | ||
Some(GeneratorKind::Async(AsyncGeneratorKind::Closure)) | ||
) => | ||
{ | ||
SuggestBoxing::AsyncBody | ||
errors::SuggestBoxing::AsyncBody | ||
} | ||
_ => SuggestBoxing::Other { start: span.shrink_to_lo(), end: span.shrink_to_hi() }, | ||
_ => errors::SuggestBoxing::Other { | ||
start: span.shrink_to_lo(), | ||
end: span.shrink_to_hi(), | ||
}, | ||
}; | ||
err.subdiagnostic(suggest_boxing); | ||
|
||
|
@@ -756,23 +757,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
match &fn_decl.output { | ||
&hir::FnRetTy::DefaultReturn(span) if expected.is_unit() && !can_suggest => { | ||
// `fn main()` must return `()`, do not suggest changing return type | ||
err.subdiagnostic(ExpectedReturnTypeLabel::Unit { span }); | ||
err.subdiagnostic(errors::ExpectedReturnTypeLabel::Unit { span }); | ||
return true; | ||
} | ||
&hir::FnRetTy::DefaultReturn(span) if expected.is_unit() => { | ||
if let Some(found) = found.make_suggestable(self.tcx, false) { | ||
err.subdiagnostic(AddReturnTypeSuggestion::Add { span, found: found.to_string() }); | ||
err.subdiagnostic(errors::AddReturnTypeSuggestion::Add { span, found: found.to_string() }); | ||
return true; | ||
} else if let ty::Closure(_, args) = found.kind() | ||
// FIXME(compiler-errors): Get better at printing binders... | ||
&& let closure = args.as_closure() | ||
&& closure.sig().is_suggestable(self.tcx, false) | ||
{ | ||
err.subdiagnostic(AddReturnTypeSuggestion::Add { span, found: closure.print_as_impl_trait().to_string() }); | ||
err.subdiagnostic(errors::AddReturnTypeSuggestion::Add { span, found: closure.print_as_impl_trait().to_string() }); | ||
return true; | ||
} else { | ||
// FIXME: if `found` could be `impl Iterator` we should suggest that. | ||
err.subdiagnostic(AddReturnTypeSuggestion::MissingHere { span }); | ||
err.subdiagnostic(errors::AddReturnTypeSuggestion::MissingHere { span }); | ||
return true | ||
} | ||
} | ||
|
@@ -794,10 +795,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
debug!(?found); | ||
if found.is_suggestable(self.tcx, false) { | ||
if term.span.is_empty() { | ||
err.subdiagnostic(AddReturnTypeSuggestion::Add { span, found: found.to_string() }); | ||
err.subdiagnostic(errors::AddReturnTypeSuggestion::Add { span, found: found.to_string() }); | ||
return true; | ||
} else { | ||
err.subdiagnostic(ExpectedReturnTypeLabel::Other { span, expected }); | ||
err.subdiagnostic(errors::ExpectedReturnTypeLabel::Other { span, expected }); | ||
} | ||
} | ||
} | ||
|
@@ -813,7 +814,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
let ty = self.normalize(span, ty); | ||
let ty = self.tcx.erase_late_bound_regions(ty); | ||
if self.can_coerce(expected, ty) { | ||
err.subdiagnostic(ExpectedReturnTypeLabel::Other { span, expected }); | ||
err.subdiagnostic(errors::ExpectedReturnTypeLabel::Other { span, expected }); | ||
self.try_suggest_return_impl_trait(err, expected, ty, fn_id); | ||
return true; | ||
} | ||
|
@@ -1103,65 +1104,46 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
return false; | ||
} | ||
|
||
let mut suggest_copied_cloned_or_as_ref = || { | ||
if Some(adt_def.did()) == self.tcx.get_diagnostic_item(sym::Result) | ||
&& self.can_eq(self.param_env, args.type_at(1), expected_args.type_at(1)) | ||
|| Some(adt_def.did()) == self.tcx.get_diagnostic_item(sym::Option) | ||
{ | ||
let expr_inner_ty = args.type_at(0); | ||
let expected_inner_ty = expected_args.type_at(0); | ||
if let &ty::Ref(_, ty, hir::Mutability::Not) = expr_inner_ty.kind() | ||
&& self.can_eq(self.param_env, ty, expected_inner_ty) | ||
{ | ||
let def_path = self.tcx.def_path_str(adt_def.did()); | ||
if self.type_is_copy_modulo_regions(self.param_env, ty) { | ||
diag.span_suggestion_verbose( | ||
expr.span.shrink_to_hi(), | ||
format!( | ||
"use `{def_path}::copied` to copy the value inside the `{def_path}`" | ||
), | ||
".copied()", | ||
Applicability::MachineApplicable, | ||
); | ||
return true; | ||
} else if let Some(expected_ty_expr) = expected_ty_expr { | ||
diag.span_suggestion_verbose( | ||
expected_ty_expr.span.shrink_to_hi(), | ||
format!( | ||
"use `{def_path}::as_ref()` to convert `{expected_ty}` to `{expr_ty}`" | ||
), | ||
".as_ref()", | ||
Applicability::MachineApplicable, | ||
); | ||
return true; | ||
} else if let Some(clone_did) = self.tcx.lang_items().clone_trait() | ||
&& rustc_trait_selection::traits::type_known_to_meet_bound_modulo_regions( | ||
self, | ||
self.param_env, | ||
ty, | ||
clone_did, | ||
) | ||
if let &ty::Ref(_, ty, mutability) = expr_inner_ty.kind() | ||
&& self.can_eq(self.param_env, ty, expected_inner_ty) | ||
{ | ||
diag.span_suggestion_verbose( | ||
expr.span.shrink_to_hi(), | ||
format!( | ||
"use `{def_path}::cloned` to clone the value inside the `{def_path}`" | ||
), | ||
".cloned()", | ||
Applicability::MachineApplicable, | ||
); | ||
let def_path = self.tcx.def_path_str(adt_def.did()); | ||
let span = expr.span.shrink_to_hi(); | ||
let subdiag = if self.type_is_copy_modulo_regions(self.param_env, ty) { | ||
errors::OptionResultRefMismatch::Copied { | ||
span, def_path | ||
} | ||
} else if let Some(expected_ty_expr) = expected_ty_expr | ||
// FIXME: suggest changes to both expressions to convert both to | ||
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. Possibly we could have a multipart suggestion to add |
||
// Option/Result<&T> | ||
&& mutability.is_not() | ||
{ | ||
errors::OptionResultRefMismatch::AsRef { | ||
span: expected_ty_expr.span.shrink_to_hi(), expected_ty, expr_ty, def_path | ||
} | ||
} else if let Some(clone_did) = self.tcx.lang_items().clone_trait() | ||
&& rustc_trait_selection::traits::type_known_to_meet_bound_modulo_regions( | ||
self, | ||
self.param_env, | ||
ty, | ||
clone_did, | ||
) | ||
{ | ||
errors::OptionResultRefMismatch::Cloned { | ||
span, def_path | ||
} | ||
} else { | ||
return false; | ||
}; | ||
diag.subdiagnostic(subdiag); | ||
return true; | ||
} | ||
} | ||
false | ||
}; | ||
|
||
if let Some(result_did) = self.tcx.get_diagnostic_item(sym::Result) | ||
&& adt_def.did() == result_did | ||
// Check that the error types are equal | ||
&& self.can_eq(self.param_env, args.type_at(1), expected_args.type_at(1)) | ||
{ | ||
return suggest_copied_cloned_or_as_ref(); | ||
} else if let Some(option_did) = self.tcx.get_diagnostic_item(sym::Option) | ||
&& adt_def.did() == option_did | ||
{ | ||
return suggest_copied_cloned_or_as_ref(); | ||
} | ||
|
||
false | ||
|
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
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
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
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.
Any particular reason to remove those imports?
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.
We should be using
errors::
in the code instead of importing them.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.
iirc the main reason being preventing merge conflicts as diagnostic imports tend to get changed a lot
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.
Yup