1
Fork 0

Change find_anon_type method to function

This commit is contained in:
0yoyoyo 2021-02-21 18:29:14 +09:00
parent 8599bff5a3
commit fece59b56c
4 changed files with 62 additions and 64 deletions

View file

@ -1,6 +1,7 @@
//! Error Reporting for Anonymous Region Lifetime Errors //! Error Reporting for Anonymous Region Lifetime Errors
//! where both the regions are anonymous. //! where both the regions are anonymous.
use crate::infer::error_reporting::nice_region_error::find_anon_type::find_anon_type;
use crate::infer::error_reporting::nice_region_error::util::AnonymousParamInfo; use crate::infer::error_reporting::nice_region_error::util::AnonymousParamInfo;
use crate::infer::error_reporting::nice_region_error::NiceRegionError; use crate::infer::error_reporting::nice_region_error::NiceRegionError;
use crate::infer::lexical_region_resolve::RegionResolutionError; use crate::infer::lexical_region_resolve::RegionResolutionError;
@ -66,9 +67,9 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
let scope_def_id_sub = anon_reg_sub.def_id; let scope_def_id_sub = anon_reg_sub.def_id;
let bregion_sub = anon_reg_sub.boundregion; let bregion_sub = anon_reg_sub.boundregion;
let ty_sup = self.find_anon_type(sup, &bregion_sup)?; let ty_sup = find_anon_type(self.tcx(), sup, &bregion_sup)?;
let ty_sub = self.find_anon_type(sub, &bregion_sub)?; let ty_sub = find_anon_type(self.tcx(), sub, &bregion_sub)?;
debug!( debug!(
"try_report_anon_anon_conflict: found_param1={:?} sup={:?} br1={:?}", "try_report_anon_anon_conflict: found_param1={:?} sup={:?} br1={:?}",

View file

@ -1,4 +1,3 @@
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor}; use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::Node; use rustc_hir::Node;
@ -6,67 +5,64 @@ use rustc_middle::hir::map::Map;
use rustc_middle::middle::resolve_lifetime as rl; use rustc_middle::middle::resolve_lifetime as rl;
use rustc_middle::ty::{self, Region, TyCtxt}; use rustc_middle::ty::{self, Region, TyCtxt};
impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { /// This function calls the `visit_ty` method for the parameters
/// This function calls the `visit_ty` method for the parameters /// corresponding to the anonymous regions. The `nested_visitor.found_type`
/// corresponding to the anonymous regions. The `nested_visitor.found_type` /// contains the anonymous type.
/// contains the anonymous type. ///
/// /// # Arguments
/// # Arguments /// region - the anonymous region corresponding to the anon_anon conflict
/// region - the anonymous region corresponding to the anon_anon conflict /// br - the bound region corresponding to the above region which is of type `BrAnon(_)`
/// br - the bound region corresponding to the above region which is of type `BrAnon(_)` ///
/// /// # Example
/// # Example /// ```
/// ``` /// fn foo(x: &mut Vec<&u8>, y: &u8)
/// fn foo(x: &mut Vec<&u8>, y: &u8) /// { x.push(y); }
/// { x.push(y); } /// ```
/// ``` /// The function returns the nested type corresponding to the anonymous region
/// The function returns the nested type corresponding to the anonymous region /// for e.g., `&u8` and Vec<`&u8`.
/// for e.g., `&u8` and Vec<`&u8`. pub(crate) fn find_anon_type(
pub(super) fn find_anon_type( tcx: TyCtxt<'tcx>,
&self,
region: Region<'tcx>, region: Region<'tcx>,
br: &ty::BoundRegionKind, br: &ty::BoundRegionKind,
) -> Option<(&hir::Ty<'tcx>, &hir::FnDecl<'tcx>)> { ) -> Option<(&'tcx hir::Ty<'tcx>, &'tcx hir::FnDecl<'tcx>)> {
if let Some(anon_reg) = self.tcx().is_suitable_region(region) { if let Some(anon_reg) = tcx.is_suitable_region(region) {
let hir_id = self.tcx().hir().local_def_id_to_hir_id(anon_reg.def_id); let hir_id = tcx.hir().local_def_id_to_hir_id(anon_reg.def_id);
let fndecl = match self.tcx().hir().get(hir_id) { let fndecl = match tcx.hir().get(hir_id) {
Node::Item(&hir::Item { kind: hir::ItemKind::Fn(ref m, ..), .. }) Node::Item(&hir::Item { kind: hir::ItemKind::Fn(ref m, ..), .. })
| Node::TraitItem(&hir::TraitItem { | Node::TraitItem(&hir::TraitItem {
kind: hir::TraitItemKind::Fn(ref m, ..), kind: hir::TraitItemKind::Fn(ref m, ..), ..
..
}) })
| Node::ImplItem(&hir::ImplItem { | Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(ref m, ..), .. }) => {
kind: hir::ImplItemKind::Fn(ref m, ..), .. &m.decl
}) => &m.decl, }
_ => return None, _ => return None,
}; };
fndecl fndecl
.inputs .inputs
.iter() .iter()
.find_map(|arg| self.find_component_for_bound_region(arg, br)) .find_map(|arg| find_component_for_bound_region(tcx, arg, br))
.map(|ty| (ty, &**fndecl)) .map(|ty| (ty, &**fndecl))
} else { } else {
None None
} }
} }
// This method creates a FindNestedTypeVisitor which returns the type corresponding // This method creates a FindNestedTypeVisitor which returns the type corresponding
// to the anonymous region. // to the anonymous region.
fn find_component_for_bound_region( fn find_component_for_bound_region(
&self, tcx: TyCtxt<'tcx>,
arg: &'tcx hir::Ty<'tcx>, arg: &'tcx hir::Ty<'tcx>,
br: &ty::BoundRegionKind, br: &ty::BoundRegionKind,
) -> Option<&'tcx hir::Ty<'tcx>> { ) -> Option<&'tcx hir::Ty<'tcx>> {
let mut nested_visitor = FindNestedTypeVisitor { let mut nested_visitor = FindNestedTypeVisitor {
tcx: self.tcx(), tcx,
bound_region: *br, bound_region: *br,
found_type: None, found_type: None,
current_index: ty::INNERMOST, current_index: ty::INNERMOST,
}; };
nested_visitor.visit_ty(arg); nested_visitor.visit_ty(arg);
nested_visitor.found_type nested_visitor.found_type
}
} }
// The FindNestedTypeVisitor captures the corresponding `hir::Ty` of the // The FindNestedTypeVisitor captures the corresponding `hir::Ty` of the

View file

@ -5,8 +5,8 @@ use rustc_errors::{DiagnosticBuilder, ErrorReported};
use rustc_middle::ty::{self, TyCtxt}; use rustc_middle::ty::{self, TyCtxt};
use rustc_span::source_map::Span; use rustc_span::source_map::Span;
pub mod find_anon_type;
mod different_lifetimes; mod different_lifetimes;
mod find_anon_type;
mod named_anon_conflict; mod named_anon_conflict;
mod placeholder_error; mod placeholder_error;
mod static_impl_trait; mod static_impl_trait;

View file

@ -1,5 +1,6 @@
//! Error Reporting for Anonymous Region Lifetime Errors //! Error Reporting for Anonymous Region Lifetime Errors
//! where one region is named and the other is anonymous. //! where one region is named and the other is anonymous.
use crate::infer::error_reporting::nice_region_error::find_anon_type::find_anon_type;
use crate::infer::error_reporting::nice_region_error::NiceRegionError; use crate::infer::error_reporting::nice_region_error::NiceRegionError;
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder}; use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
use rustc_hir::intravisit::Visitor; use rustc_hir::intravisit::Visitor;
@ -74,7 +75,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
return None; return None;
} }
if let Some((_, fndecl)) = self.find_anon_type(anon, &br) { if let Some((_, fndecl)) = find_anon_type(self.tcx(), anon, &br) {
if self.is_self_anon(is_first, scope_def_id) { if self.is_self_anon(is_first, scope_def_id) {
return None; return None;
} }