Skip to content

Commit 36c0ee9

Browse files
committed
Auto merge of #53900 - davidtwco:issue-53771, r=nikomatsakis
NLL regresses diagnostic for impl-trait/static-return-lifetime-infered.rs Fixes #53771. r? @nikomatsakis cc @pnkfelix @estebank
2 parents b80cb47 + 18c1374 commit 36c0ee9

File tree

11 files changed

+276
-124
lines changed

11 files changed

+276
-124
lines changed

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

+2-2
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

+1-1
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

+4-4
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

+2-2
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.return_type_impl_trait(anon_reg_sup.def_id).is_some()
3333
{
3434
let sp = var_origin.span();
3535
let return_sp = sub_origin.span();

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

-79
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

+81-1
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;
@@ -872,6 +872,18 @@ impl<'tcx> CommonTypes<'tcx> {
872872
}
873873
}
874874

875+
// This struct contains information regarding the `ReFree(FreeRegion)` corresponding to a lifetime
876+
// conflict.
877+
#[derive(Debug)]
878+
pub struct FreeRegionInfo {
879+
// def id corresponding to FreeRegion
880+
pub def_id: DefId,
881+
// the bound region corresponding to FreeRegion
882+
pub boundregion: ty::BoundRegion,
883+
// checks if bound region is in Impl Item
884+
pub is_impl_item: bool,
885+
}
886+
875887
/// The central data structure of the compiler. It stores references
876888
/// to the various **arenas** and also houses the results of the
877889
/// various **compiler queries** that have been performed. See the
@@ -1571,6 +1583,74 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
15711583
}
15721584
})
15731585
}
1586+
1587+
// This method returns the DefId and the BoundRegion corresponding to the given region.
1588+
pub fn is_suitable_region(&self, region: Region<'tcx>) -> Option<FreeRegionInfo> {
1589+
let (suitable_region_binding_scope, bound_region) = match *region {
1590+
ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
1591+
ty::ReEarlyBound(ref ebr) => (
1592+
self.parent_def_id(ebr.def_id).unwrap(),
1593+
ty::BoundRegion::BrNamed(ebr.def_id, ebr.name),
1594+
),
1595+
_ => return None, // not a free region
1596+
};
1597+
1598+
let node_id = self.hir
1599+
.as_local_node_id(suitable_region_binding_scope)
1600+
.unwrap();
1601+
let is_impl_item = match self.hir.find(node_id) {
1602+
Some(Node::Item(..)) | Some(Node::TraitItem(..)) => false,
1603+
Some(Node::ImplItem(..)) => {
1604+
self.is_bound_region_in_impl_item(suitable_region_binding_scope)
1605+
}
1606+
_ => return None,
1607+
};
1608+
1609+
return Some(FreeRegionInfo {
1610+
def_id: suitable_region_binding_scope,
1611+
boundregion: bound_region,
1612+
is_impl_item: is_impl_item,
1613+
});
1614+
}
1615+
1616+
pub fn return_type_impl_trait(
1617+
&self,
1618+
scope_def_id: DefId,
1619+
) -> Option<Ty<'tcx>> {
1620+
let ret_ty = self.type_of(scope_def_id);
1621+
match ret_ty.sty {
1622+
ty::FnDef(_, _) => {
1623+
let sig = ret_ty.fn_sig(*self);
1624+
let output = self.erase_late_bound_regions(&sig.output());
1625+
if output.is_impl_trait() {
1626+
Some(output)
1627+
} else {
1628+
None
1629+
}
1630+
}
1631+
_ => None
1632+
}
1633+
}
1634+
1635+
// Here we check if the bound region is in Impl Item.
1636+
pub fn is_bound_region_in_impl_item(
1637+
&self,
1638+
suitable_region_binding_scope: DefId,
1639+
) -> bool {
1640+
let container_id = self.associated_item(suitable_region_binding_scope)
1641+
.container
1642+
.id();
1643+
if self.impl_trait_ref(container_id).is_some() {
1644+
// For now, we do not try to target impls of traits. This is
1645+
// because this message is going to suggest that the user
1646+
// change the fn signature, but they may not be free to do so,
1647+
// since the signature must match the trait.
1648+
//
1649+
// FIXME(#42706) -- in some cases, we could do better here.
1650+
return true;
1651+
}
1652+
false
1653+
}
15741654
}
15751655

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

src/librustc/ty/mod.rs

+1-1
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)