1
Fork 0

review comments and rebase fixes

This commit is contained in:
Esteban Küber 2023-11-29 19:55:24 +00:00
parent dfe32b6a43
commit 25c75bc156
2 changed files with 66 additions and 67 deletions

View file

@ -17,7 +17,7 @@ use rustc_errors::{
ErrorGuaranteed, MultiSpan, Style, SuggestionStyle, ErrorGuaranteed, MultiSpan, Style, SuggestionStyle,
}; };
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::DefKind; use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::Visitor; use rustc_hir::intravisit::Visitor;
use rustc_hir::is_range_literal; use rustc_hir::is_range_literal;
@ -36,7 +36,7 @@ use rustc_middle::ty::{
TypeSuperFoldable, TypeVisitableExt, TypeckResults, TypeSuperFoldable, TypeVisitableExt, TypeckResults,
}; };
use rustc_span::def_id::LocalDefId; use rustc_span::def_id::LocalDefId;
use rustc_span::symbol::{sym, Ident, Symbol}; use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{BytePos, DesugaringKind, ExpnKind, MacroKind, Span, DUMMY_SP}; use rustc_span::{BytePos, DesugaringKind, ExpnKind, MacroKind, Span, DUMMY_SP};
use rustc_target::spec::abi; use rustc_target::spec::abi;
use std::borrow::Cow; use std::borrow::Cow;
@ -1043,7 +1043,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind else { let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind else {
return; return;
}; };
let hir::def::Res::Local(hir_id) = path.res else { let Res::Local(hir_id) = path.res else {
return; return;
}; };
let Some(hir::Node::Pat(pat)) = self.tcx.hir().find(hir_id) else { let Some(hir::Node::Pat(pat)) = self.tcx.hir().find(hir_id) else {
@ -1627,7 +1627,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
} }
} }
if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
&& let hir::def::Res::Local(hir_id) = path.res && let Res::Local(hir_id) = path.res
&& let Some(hir::Node::Pat(binding)) = self.tcx.hir().find(hir_id) && let Some(hir::Node::Pat(binding)) = self.tcx.hir().find(hir_id)
&& let Some(hir::Node::Local(local)) = self.tcx.hir().find_parent(binding.hir_id) && let Some(hir::Node::Local(local)) = self.tcx.hir().find_parent(binding.hir_id)
&& let None = local.ty && let None = local.ty
@ -2047,9 +2047,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
let hir::ExprKind::Path(path) = arg.kind else { let hir::ExprKind::Path(path) = arg.kind else {
return; return;
}; };
let expected_inputs = self.tcx.erase_late_bound_regions(*expected).inputs(); let expected_inputs = self.tcx.instantiate_bound_regions_with_erased(*expected).inputs();
let found_inputs = self.tcx.erase_late_bound_regions(*found).inputs(); let found_inputs = self.tcx.instantiate_bound_regions_with_erased(*found).inputs();
let both_tys = expected_inputs.iter().cloned().zip(found_inputs.iter().cloned()); let both_tys = expected_inputs.iter().copied().zip(found_inputs.iter().copied());
let arg_expr = |infcx: &InferCtxt<'tcx>, name, expected: Ty<'tcx>, found: Ty<'tcx>| { let arg_expr = |infcx: &InferCtxt<'tcx>, name, expected: Ty<'tcx>, found: Ty<'tcx>| {
let (expected_ty, expected_refs) = get_deref_type_and_refs(expected); let (expected_ty, expected_refs) = get_deref_type_and_refs(expected);
@ -2057,29 +2057,24 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
if infcx.can_eq(param_env, found_ty, expected_ty) { if infcx.can_eq(param_env, found_ty, expected_ty) {
if found_refs.len() == expected_refs.len() if found_refs.len() == expected_refs.len()
&& found_refs.iter().zip(expected_refs.iter()).all(|(e, f)| e == f) && found_refs.iter().eq(expected_refs.iter())
{ {
name name
} else if found_refs.len() > expected_refs.len() { } else if found_refs.len() > expected_refs.len() {
if found_refs[..found_refs.len() - expected_refs.len()] let refs = &found_refs[..found_refs.len() - expected_refs.len()];
.iter() if found_refs[..expected_refs.len()].iter().eq(expected_refs.iter()) {
.zip(expected_refs.iter())
.any(|(e, f)| e != f)
{
// The refs have different mutability.
format!( format!(
"{}*{name}", "{}{name}",
found_refs[..found_refs.len() - expected_refs.len()] refs.iter()
.iter()
.map(|mutbl| format!("&{}", mutbl.prefix_str())) .map(|mutbl| format!("&{}", mutbl.prefix_str()))
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(""), .join(""),
) )
} else { } else {
// The refs have different mutability.
format!( format!(
"{}{name}", "{}*{name}",
found_refs[..found_refs.len() - expected_refs.len()] refs.iter()
.iter()
.map(|mutbl| format!("&{}", mutbl.prefix_str())) .map(|mutbl| format!("&{}", mutbl.prefix_str()))
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(""), .join(""),
@ -2108,48 +2103,52 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
format!("/* {found} */") format!("/* {found} */")
} }
}; };
let (closure_names, call_names): (Vec<_>, Vec<_>) = let args_have_same_underlying_type = both_tys.clone().all(|(expected, found)| {
if both_tys.clone().all(|(expected, found)| { let (expected_ty, _) = get_deref_type_and_refs(expected);
let (expected_ty, _) = get_deref_type_and_refs(expected); let (found_ty, _) = get_deref_type_and_refs(found);
let (found_ty, _) = get_deref_type_and_refs(found); self.can_eq(param_env, found_ty, expected_ty)
self.can_eq(param_env, found_ty, expected_ty) });
}) && !expected_inputs.is_empty() let (closure_names, call_names): (Vec<_>, Vec<_>) = if args_have_same_underlying_type
&& expected_inputs.len() == found_inputs.len() && !expected_inputs.is_empty()
&& let hir::QPath::Resolved(_, path) = path && expected_inputs.len() == found_inputs.len()
&& let hir::def::Res::Def(_, fn_def_id) = path.res && let Some(typeck) = &self.typeck_results
&& let Some(node) = self.tcx.hir().get_if_local(fn_def_id) && let Res::Def(_, fn_def_id) = typeck.qpath_res(&path, *arg_hir_id)
&& let Some(body_id) = node.body_id() {
{ let closure: Vec<_> = self
let closure = self .tcx
.tcx .fn_arg_names(fn_def_id)
.hir() .iter()
.body_param_names(body_id) .enumerate()
.map(|name| format!("{name}")) .map(|(i, ident)| {
.collect(); if ident.name.is_empty() || ident.name == kw::SelfLower {
let args = self format!("arg{i}")
.tcx } else {
.hir() format!("{ident}")
.body_param_names(body_id) }
.zip(both_tys) })
.map(|(name, (expected, found))| { .collect();
arg_expr(self.infcx, format!("{name}"), expected, found) let args = closure
}) .iter()
.collect(); .zip(both_tys)
(closure, args) .map(|(name, (expected, found))| {
} else { arg_expr(self.infcx, name.to_owned(), expected, found)
let closure_args = expected_inputs })
.iter() .collect();
.enumerate() (closure, args)
.map(|(i, _)| format!("arg{i}")) } else {
.collect::<Vec<_>>(); let closure_args = expected_inputs
let call_args = both_tys .iter()
.enumerate() .enumerate()
.map(|(i, (expected, found))| { .map(|(i, _)| format!("arg{i}"))
arg_expr(self.infcx, format!("arg{i}"), expected, found) .collect::<Vec<_>>();
}) let call_args = both_tys
.collect::<Vec<_>>(); .enumerate()
(closure_args, call_args) .map(|(i, (expected, found))| {
}; arg_expr(self.infcx, format!("arg{i}"), expected, found)
})
.collect::<Vec<_>>();
(closure_args, call_args)
};
let closure_names: Vec<_> = closure_names let closure_names: Vec<_> = closure_names
.into_iter() .into_iter()
.zip(expected_inputs.iter()) .zip(expected_inputs.iter())
@ -3790,7 +3789,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
} }
} }
if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
&& let hir::Path { res: hir::def::Res::Local(hir_id), .. } = path && let hir::Path { res: Res::Local(hir_id), .. } = path
&& let Some(hir::Node::Pat(binding)) = self.tcx.hir().find(*hir_id) && let Some(hir::Node::Pat(binding)) = self.tcx.hir().find(*hir_id)
&& let parent_hir_id = self.tcx.hir().parent_id(binding.hir_id) && let parent_hir_id = self.tcx.hir().parent_id(binding.hir_id)
&& let Some(hir::Node::Local(local)) = self.tcx.hir().find(parent_hir_id) && let Some(hir::Node::Local(local)) = self.tcx.hir().find(parent_hir_id)
@ -4050,7 +4049,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
); );
if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
&& let hir::Path { res: hir::def::Res::Local(hir_id), .. } = path && let hir::Path { res: Res::Local(hir_id), .. } = path
&& let Some(hir::Node::Pat(binding)) = self.tcx.hir().find(*hir_id) && let Some(hir::Node::Pat(binding)) = self.tcx.hir().find(*hir_id)
&& let Some(parent) = self.tcx.hir().find_parent(binding.hir_id) && let Some(parent) = self.tcx.hir().find_parent(binding.hir_id)
{ {
@ -4664,7 +4663,7 @@ impl<'a, 'hir> hir::intravisit::Visitor<'hir> for ReplaceImplTraitVisitor<'a> {
fn visit_ty(&mut self, t: &'hir hir::Ty<'hir>) { fn visit_ty(&mut self, t: &'hir hir::Ty<'hir>) {
if let hir::TyKind::Path(hir::QPath::Resolved( if let hir::TyKind::Path(hir::QPath::Resolved(
None, None,
hir::Path { res: hir::def::Res::Def(_, segment_did), .. }, hir::Path { res: Res::Def(_, segment_did), .. },
)) = t.kind )) = t.kind
{ {
if self.param_did == *segment_did { if self.param_did == *segment_did {

View file

@ -87,8 +87,8 @@ note: required by a bound in `Option::<T>::and_then`
--> $SRC_DIR/core/src/option.rs:LL:COL --> $SRC_DIR/core/src/option.rs:LL:COL
help: consider wrapping the function in a closure help: consider wrapping the function in a closure
| |
LL | let _ = produces_string().and_then(|: String| generic_ref(&)); LL | let _ = produces_string().and_then(|arg0: String| generic_ref(&arg0));
| ++++++++++ +++ | ++++++++++++++ +++++++
help: consider adjusting the signature so it does not borrow its argument help: consider adjusting the signature so it does not borrow its argument
| |
LL - fn generic_ref<T>(_: &T) -> Option<()> { LL - fn generic_ref<T>(_: &T) -> Option<()> {