nice_region_error: rustfmt

This commit is contained in:
Niko Matsakis 2017-12-12 08:04:29 -05:00
parent 3720242c75
commit a28ab84025
3 changed files with 98 additions and 83 deletions

View file

@ -72,22 +72,28 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
let ty_sub = or_false!(self.find_anon_type(sub, &bregion_sub));
debug!("try_report_anon_anon_conflict: found_arg1={:?} sup={:?} br1={:?}",
ty_sub,
sup,
bregion_sup);
debug!("try_report_anon_anon_conflict: found_arg2={:?} sub={:?} br2={:?}",
ty_sup,
sub,
bregion_sub);
debug!(
"try_report_anon_anon_conflict: found_arg1={:?} sup={:?} br1={:?}",
ty_sub,
sup,
bregion_sup
);
debug!(
"try_report_anon_anon_conflict: found_arg2={:?} sub={:?} br2={:?}",
ty_sup,
sub,
bregion_sub
);
let (ty_sup, ty_fndecl_sup) = ty_sup;
let (ty_sub, ty_fndecl_sub) = ty_sub;
let AnonymousArgInfo { arg: anon_arg_sup, .. } =
or_false!(self.find_arg_with_region(sup, sup));
let AnonymousArgInfo { arg: anon_arg_sub, .. } =
or_false!(self.find_arg_with_region(sub, sub));
let AnonymousArgInfo {
arg: anon_arg_sup, ..
} = or_false!(self.find_arg_with_region(sup, sup));
let AnonymousArgInfo {
arg: anon_arg_sub, ..
} = or_false!(self.find_arg_with_region(sub, sub));
let sup_is_ret_type =
self.is_return_type_anon(scope_def_id_sup, bregion_sup, ty_fndecl_sup);
@ -110,34 +116,45 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
let (span_1, span_2, main_label, span_label) = match (sup_is_ret_type, sub_is_ret_type) {
(None, None) => {
let (main_label_1, span_label_1) = if ty_sup == ty_sub {
(format!("this type is declared with multiple lifetimes..."),
format!("...but data{} flows{} here",
format!(" with one lifetime"),
format!(" into the other")))
(
format!("this type is declared with multiple lifetimes..."),
format!(
"...but data{} flows{} here",
format!(" with one lifetime"),
format!(" into the other")
),
)
} else {
(format!("these two types are declared with different lifetimes..."),
format!("...but data{} flows{} here",
span_label_var1,
span_label_var2))
(
format!("these two types are declared with different lifetimes..."),
format!(
"...but data{} flows{} here",
span_label_var1,
span_label_var2
),
)
};
(ty_sup.span, ty_sub.span, main_label_1, span_label_1)
}
(Some(ret_span), _) => {
(ty_sub.span,
ret_span,
format!("this parameter and the return type are declared \
with different lifetimes...",),
format!("...but data{} is returned here", span_label_var1))
}
(_, Some(ret_span)) => {
(ty_sup.span,
ret_span,
format!("this parameter and the return type are declared \
with different lifetimes...",),
format!("...but data{} is returned here", span_label_var1))
}
(Some(ret_span), _) => (
ty_sub.span,
ret_span,
format!(
"this parameter and the return type are declared \
with different lifetimes...",
),
format!("...but data{} is returned here", span_label_var1),
),
(_, Some(ret_span)) => (
ty_sup.span,
ret_span,
format!(
"this parameter and the return type are declared \
with different lifetimes...",
),
format!("...but data{} is returned here", span_label_var1),
),
};
@ -149,4 +166,3 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
return true;
}
}

View file

@ -13,7 +13,7 @@ use infer::InferCtxt;
use ty::{self, Region};
use hir::map as hir_map;
use middle::resolve_lifetime as rl;
use hir::intravisit::{self, Visitor, NestedVisitorMap};
use hir::intravisit::{self, NestedVisitorMap, Visitor};
impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
/// This function calls the `visit_ty` method for the parameters

View file

@ -68,17 +68,17 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
// i32, which is the type of y but with the anonymous region replaced
// with 'a, the corresponding bound region and is_first which is true if
// the hir::Arg is the first argument in the function declaration.
pub(super) fn find_arg_with_region(&self,
anon_region: Region<'tcx>,
replace_region: Region<'tcx>)
-> Option<AnonymousArgInfo> {
pub(super) fn find_arg_with_region(
&self,
anon_region: Region<'tcx>,
replace_region: Region<'tcx>,
) -> Option<AnonymousArgInfo> {
let (id, bound_region) = match *anon_region {
ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
ty::ReEarlyBound(ref ebr) => {
(self.tcx.parent_def_id(ebr.def_id).unwrap(),
ty::BoundRegion::BrNamed(ebr.def_id, ebr.name))
}
ty::ReEarlyBound(ref ebr) => (
self.tcx.parent_def_id(ebr.def_id).unwrap(),
ty::BoundRegion::BrNamed(ebr.def_id, ebr.name),
),
_ => return None, // not a free region
};
@ -94,21 +94,22 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
// May return None; sometimes the tables are not yet populated.
let ty = tables.borrow().node_id_to_type_opt(arg.hir_id)?;
let mut found_anon_region = false;
let new_arg_ty = self.tcx
.fold_regions(&ty, &mut false, |r, _| if *r == *anon_region {
let new_arg_ty = self.tcx.fold_regions(&ty, &mut false, |r, _| {
if *r == *anon_region {
found_anon_region = true;
replace_region
} else {
r
});
}
});
if found_anon_region {
let is_first = index == 0;
Some(AnonymousArgInfo {
arg: arg,
arg_ty: new_arg_ty,
bound_region: bound_region,
is_first: is_first,
})
arg: arg,
arg_ty: new_arg_ty,
bound_region: bound_region,
is_first: is_first,
})
} else {
None
}
@ -127,13 +128,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
// This method returns the DefId and the BoundRegion corresponding to the given region.
pub(super) fn is_suitable_region(&self, region: Region<'tcx>) -> Option<FreeRegionInfo> {
let (suitable_region_binding_scope, bound_region) = match *region {
ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
ty::ReEarlyBound(ref ebr) => {
(self.tcx.parent_def_id(ebr.def_id).unwrap(),
ty::BoundRegion::BrNamed(ebr.def_id, ebr.name))
}
ty::ReEarlyBound(ref ebr) => (
self.tcx.parent_def_id(ebr.def_id).unwrap(),
ty::BoundRegion::BrNamed(ebr.def_id, ebr.name),
),
_ => return None, // not a free region
};
@ -142,9 +142,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
.as_local_node_id(suitable_region_binding_scope)
.unwrap();
let is_impl_item = match self.tcx.hir.find(node_id) {
Some(hir_map::NodeItem(..)) |
Some(hir_map::NodeTraitItem(..)) => false,
Some(hir_map::NodeItem(..)) | Some(hir_map::NodeTraitItem(..)) => false,
Some(hir_map::NodeImplItem(..)) => {
self.is_bound_region_in_impl_item(suitable_region_binding_scope)
}
@ -152,21 +150,21 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
};
return Some(FreeRegionInfo {
def_id: suitable_region_binding_scope,
boundregion: bound_region,
is_impl_item: is_impl_item,
});
def_id: suitable_region_binding_scope,
boundregion: bound_region,
is_impl_item: is_impl_item,
});
}
// Here, we check for the case where the anonymous region
// is in the return type.
// FIXME(#42703) - Need to handle certain cases here.
pub(super) fn is_return_type_anon(&self,
scope_def_id: DefId,
br: ty::BoundRegion,
decl: &hir::FnDecl)
-> Option<Span> {
pub(super) fn is_return_type_anon(
&self,
scope_def_id: DefId,
br: ty::BoundRegion,
decl: &hir::FnDecl,
) -> Option<Span> {
let ret_ty = self.tcx.type_of(scope_def_id);
match ret_ty.sty {
ty::TyFnDef(_, _) => {
@ -186,14 +184,17 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
// FIXME(#42700) - Need to format self properly to
// enable E0621 for it.
pub(super) fn is_self_anon(&self, is_first: bool, scope_def_id: DefId) -> bool {
is_first &&
self.tcx
.opt_associated_item(scope_def_id)
.map(|i| i.method_has_self_argument) == Some(true)
is_first
&& self.tcx
.opt_associated_item(scope_def_id)
.map(|i| i.method_has_self_argument) == Some(true)
}
// Here we check if the bound region is in Impl Item.
pub(super) fn is_bound_region_in_impl_item(&self, suitable_region_binding_scope: DefId) -> bool {
pub(super) fn is_bound_region_in_impl_item(
&self,
suitable_region_binding_scope: DefId,
) -> bool {
let container_id = self.tcx
.associated_item(suitable_region_binding_scope)
.container
@ -213,12 +214,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
// This method returns whether the given Region is Named
pub(super) fn is_named_region(&self, region: Region<'tcx>) -> bool {
match *region {
ty::ReFree(ref free_region) => {
match free_region.bound_region {
ty::BrNamed(..) => true,
_ => false,
}
}
ty::ReFree(ref free_region) => match free_region.bound_region {
ty::BrNamed(..) => true,
_ => false,
},
ty::ReEarlyBound(_) => true,
_ => false,
}