Skip to content

Commit 78b2164

Browse files
committed
Moved helper functions from NiceRegionError to TyCtxt.
1 parent 994cdd9 commit 78b2164

File tree

7 files changed

+88
-90
lines changed

7 files changed

+88
-90
lines changed

src/librustc/infer/error_reporting/nice_region_error/different_lifetimes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
5656
let (span, sub, sup) = self.get_regions();
5757

5858
// Determine whether the sub and sup consist of both anonymous (elided) regions.
59-
let anon_reg_sup = self.is_suitable_region(sup)?;
59+
let anon_reg_sup = self.tcx.is_suitable_region(sup)?;
6060

61-
let anon_reg_sub = self.is_suitable_region(sub)?;
61+
let anon_reg_sub = self.tcx.is_suitable_region(sub)?;
6262
let scope_def_id_sup = anon_reg_sup.def_id;
6363
let bregion_sup = anon_reg_sup.boundregion;
6464
let scope_def_id_sub = anon_reg_sub.def_id;

src/librustc/infer/error_reporting/nice_region_error/find_anon_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
3636
region: Region<'tcx>,
3737
br: &ty::BoundRegion,
3838
) -> Option<(&hir::Ty, &hir::FnDecl)> {
39-
if let Some(anon_reg) = self.is_suitable_region(region) {
39+
if let Some(anon_reg) = self.tcx.is_suitable_region(region) {
4040
let def_id = anon_reg.def_id;
4141
if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
4242
let fndecl = match self.tcx.hir.get(node_id) {

src/librustc/infer/error_reporting/nice_region_error/named_anon_conflict.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,23 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
3333
// version new_ty of its type where the anonymous region is replaced
3434
// with the named one.//scope_def_id
3535
let (named, anon, anon_arg_info, region_info) = if self.is_named_region(sub)
36-
&& self.is_suitable_region(sup).is_some()
36+
&& self.tcx.is_suitable_region(sup).is_some()
3737
&& self.find_arg_with_region(sup, sub).is_some()
3838
{
3939
(
4040
sub,
4141
sup,
4242
self.find_arg_with_region(sup, sub).unwrap(),
43-
self.is_suitable_region(sup).unwrap(),
43+
self.tcx.is_suitable_region(sup).unwrap(),
4444
)
45-
} else if self.is_named_region(sup) && self.is_suitable_region(sub).is_some()
45+
} else if self.is_named_region(sup) && self.tcx.is_suitable_region(sub).is_some()
4646
&& self.find_arg_with_region(sub, sup).is_some()
4747
{
4848
(
4949
sup,
5050
sub,
5151
self.find_arg_with_region(sub, sup).unwrap(),
52-
self.is_suitable_region(sub).unwrap(),
52+
self.tcx.is_suitable_region(sub).unwrap(),
5353
)
5454
} else {
5555
return None; // inapplicable

src/librustc/infer/error_reporting/nice_region_error/static_impl_trait.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
2727
sup_origin,
2828
sup_r,
2929
) => {
30-
let anon_reg_sup = self.is_suitable_region(sup_r)?;
30+
let anon_reg_sup = self.tcx.is_suitable_region(sup_r)?;
3131
if sub_r == &RegionKind::ReStatic &&
32-
self.is_return_type_impl_trait(anon_reg_sup.def_id)
32+
self.tcx.is_return_type_impl_trait(anon_reg_sup.def_id)
3333
{
3434
let sp = var_origin.span();
3535
let return_sp = sub_origin.span();

src/librustc/infer/error_reporting/nice_region_error/util.rs

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use hir;
1515
use infer::error_reporting::nice_region_error::NiceRegionError;
1616
use ty::{self, Region, Ty};
1717
use hir::def_id::DefId;
18-
use hir::Node;
1918
use syntax_pos::Span;
2019

2120
// The struct contains the information about the anonymous region
@@ -35,18 +34,6 @@ pub(super) struct AnonymousArgInfo<'tcx> {
3534
pub is_first: bool,
3635
}
3736

38-
// This struct contains information regarding the
39-
// Refree((FreeRegion) corresponding to lifetime conflict
40-
#[derive(Debug)]
41-
pub(super) struct FreeRegionInfo {
42-
// def id corresponding to FreeRegion
43-
pub def_id: DefId,
44-
// the bound region corresponding to FreeRegion
45-
pub boundregion: ty::BoundRegion,
46-
// checks if bound region is in Impl Item
47-
pub is_impl_item: bool,
48-
}
49-
5037
impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
5138
// This method walks the Type of the function body arguments using
5239
// `fold_regions()` function and returns the
@@ -122,36 +109,6 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
122109
}
123110
}
124111

125-
// This method returns the DefId and the BoundRegion corresponding to the given region.
126-
pub(super) fn is_suitable_region(&self, region: Region<'tcx>) -> Option<FreeRegionInfo> {
127-
let (suitable_region_binding_scope, bound_region) = match *region {
128-
ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
129-
ty::ReEarlyBound(ref ebr) => (
130-
self.tcx.parent_def_id(ebr.def_id).unwrap(),
131-
ty::BoundRegion::BrNamed(ebr.def_id, ebr.name),
132-
),
133-
_ => return None, // not a free region
134-
};
135-
136-
let node_id = self.tcx
137-
.hir
138-
.as_local_node_id(suitable_region_binding_scope)
139-
.unwrap();
140-
let is_impl_item = match self.tcx.hir.find(node_id) {
141-
Some(Node::Item(..)) | Some(Node::TraitItem(..)) => false,
142-
Some(Node::ImplItem(..)) => {
143-
self.is_bound_region_in_impl_item(suitable_region_binding_scope)
144-
}
145-
_ => return None,
146-
};
147-
148-
return Some(FreeRegionInfo {
149-
def_id: suitable_region_binding_scope,
150-
boundregion: bound_region,
151-
is_impl_item: is_impl_item,
152-
});
153-
}
154-
155112
// Here, we check for the case where the anonymous region
156113
// is in the return type.
157114
// FIXME(#42703) - Need to handle certain cases here.
@@ -176,22 +133,6 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
176133
None
177134
}
178135

179-
pub(super) fn is_return_type_impl_trait(
180-
&self,
181-
scope_def_id: DefId,
182-
) -> bool {
183-
let ret_ty = self.tcx.type_of(scope_def_id);
184-
match ret_ty.sty {
185-
ty::FnDef(_, _) => {
186-
let sig = ret_ty.fn_sig(self.tcx);
187-
let output = self.tcx.erase_late_bound_regions(&sig.output());
188-
return output.is_impl_trait();
189-
}
190-
_ => {}
191-
}
192-
false
193-
}
194-
195136
// Here we check for the case where anonymous region
196137
// corresponds to self and if yes, we display E0312.
197138
// FIXME(#42700) - Need to format self properly to
@@ -203,24 +144,4 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
203144
.map(|i| i.method_has_self_argument) == Some(true)
204145
}
205146

206-
// Here we check if the bound region is in Impl Item.
207-
pub(super) fn is_bound_region_in_impl_item(
208-
&self,
209-
suitable_region_binding_scope: DefId,
210-
) -> bool {
211-
let container_id = self.tcx
212-
.associated_item(suitable_region_binding_scope)
213-
.container
214-
.id();
215-
if self.tcx.impl_trait_ref(container_id).is_some() {
216-
// For now, we do not try to target impls of traits. This is
217-
// because this message is going to suggest that the user
218-
// change the fn signature, but they may not be free to do so,
219-
// since the signature must match the trait.
220-
//
221-
// FIXME(#42706) -- in some cases, we could do better here.
222-
return true;
223-
}
224-
false
225-
}
226147
}

src/librustc/ty/context.rs

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use session::Session;
1717
use session::config::{BorrowckMode, OutputFilenames};
1818
use session::config::CrateType;
1919
use middle;
20-
use hir::{TraitCandidate, HirId, ItemLocalId};
20+
use hir::{TraitCandidate, HirId, ItemLocalId, Node};
2121
use hir::def::{Def, Export};
2222
use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
2323
use hir::map as hir_map;
@@ -870,6 +870,18 @@ impl<'tcx> CommonTypes<'tcx> {
870870
}
871871
}
872872

873+
// This struct contains information regarding the `ReFree(FreeRegion)` corresponding to a lifetime
874+
// conflict.
875+
#[derive(Debug)]
876+
pub struct FreeRegionInfo {
877+
// def id corresponding to FreeRegion
878+
pub def_id: DefId,
879+
// the bound region corresponding to FreeRegion
880+
pub boundregion: ty::BoundRegion,
881+
// checks if bound region is in Impl Item
882+
pub is_impl_item: bool,
883+
}
884+
873885
/// The central data structure of the compiler. It stores references
874886
/// to the various **arenas** and also houses the results of the
875887
/// various **compiler queries** that have been performed. See the
@@ -1545,6 +1557,71 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
15451557
}
15461558
})
15471559
}
1560+
1561+
// This method returns the DefId and the BoundRegion corresponding to the given region.
1562+
pub fn is_suitable_region(&self, region: Region<'tcx>) -> Option<FreeRegionInfo> {
1563+
let (suitable_region_binding_scope, bound_region) = match *region {
1564+
ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
1565+
ty::ReEarlyBound(ref ebr) => (
1566+
self.parent_def_id(ebr.def_id).unwrap(),
1567+
ty::BoundRegion::BrNamed(ebr.def_id, ebr.name),
1568+
),
1569+
_ => return None, // not a free region
1570+
};
1571+
1572+
let node_id = self.hir
1573+
.as_local_node_id(suitable_region_binding_scope)
1574+
.unwrap();
1575+
let is_impl_item = match self.hir.find(node_id) {
1576+
Some(Node::Item(..)) | Some(Node::TraitItem(..)) => false,
1577+
Some(Node::ImplItem(..)) => {
1578+
self.is_bound_region_in_impl_item(suitable_region_binding_scope)
1579+
}
1580+
_ => return None,
1581+
};
1582+
1583+
return Some(FreeRegionInfo {
1584+
def_id: suitable_region_binding_scope,
1585+
boundregion: bound_region,
1586+
is_impl_item: is_impl_item,
1587+
});
1588+
}
1589+
1590+
pub fn is_return_type_impl_trait(
1591+
&self,
1592+
scope_def_id: DefId,
1593+
) -> bool {
1594+
let ret_ty = self.type_of(scope_def_id);
1595+
match ret_ty.sty {
1596+
ty::FnDef(_, _) => {
1597+
let sig = ret_ty.fn_sig(*self);
1598+
let output = self.erase_late_bound_regions(&sig.output());
1599+
return output.is_impl_trait();
1600+
}
1601+
_ => {}
1602+
}
1603+
false
1604+
}
1605+
1606+
// Here we check if the bound region is in Impl Item.
1607+
pub fn is_bound_region_in_impl_item(
1608+
&self,
1609+
suitable_region_binding_scope: DefId,
1610+
) -> bool {
1611+
let container_id = self.associated_item(suitable_region_binding_scope)
1612+
.container
1613+
.id();
1614+
if self.impl_trait_ref(container_id).is_some() {
1615+
// For now, we do not try to target impls of traits. This is
1616+
// because this message is going to suggest that the user
1617+
// change the fn signature, but they may not be free to do so,
1618+
// since the signature must match the trait.
1619+
//
1620+
// FIXME(#42706) -- in some cases, we could do better here.
1621+
return true;
1622+
}
1623+
false
1624+
}
15481625
}
15491626

15501627
impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {

src/librustc/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub use self::sty::TyKind::*;
8080
pub use self::binding::BindingMode;
8181
pub use self::binding::BindingMode::*;
8282

83-
pub use self::context::{TyCtxt, GlobalArenas, AllArenas, tls, keep_local};
83+
pub use self::context::{TyCtxt, FreeRegionInfo, GlobalArenas, AllArenas, tls, keep_local};
8484
pub use self::context::{Lift, TypeckTables};
8585

8686
pub use self::instance::{Instance, InstanceDef};

0 commit comments

Comments
 (0)