Skip to content

Commit c1437c9

Browse files
committed
Make NiceRegionError use the InferCtxt instead of its TyCtxt
Some errors (e.g placeholder errors) have unresolved type vars so this will allow to use `resolve_type_vars_if_possible` when needed.
1 parent 1484d0d commit c1437c9

File tree

9 files changed

+48
-44
lines changed

9 files changed

+48
-44
lines changed

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

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

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

51-
let anon_reg_sub = self.tcx.is_suitable_region(sub)?;
51+
let anon_reg_sub = self.tcx().is_suitable_region(sub)?;
5252
let scope_def_id_sup = anon_reg_sup.def_id;
5353
let bregion_sup = anon_reg_sup.boundregion;
5454
let scope_def_id_sub = anon_reg_sub.def_id;
@@ -138,7 +138,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
138138
};
139139

140140

141-
struct_span_err!(self.tcx.sess, span, E0623, "lifetime mismatch")
141+
struct_span_err!(self.tcx().sess, span, E0623, "lifetime mismatch")
142142
.span_label(span_1, main_label)
143143
.span_label(span_2, String::new())
144144
.span_label(span, span_label)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
2626
region: Region<'tcx>,
2727
br: &ty::BoundRegion,
2828
) -> Option<(&hir::Ty, &hir::FnDecl)> {
29-
if let Some(anon_reg) = self.tcx.is_suitable_region(region) {
29+
if let Some(anon_reg) = self.tcx().is_suitable_region(region) {
3030
let def_id = anon_reg.def_id;
31-
if let Some(node_id) = self.tcx.hir().as_local_node_id(def_id) {
32-
let fndecl = match self.tcx.hir().get(node_id) {
31+
if let Some(node_id) = self.tcx().hir().as_local_node_id(def_id) {
32+
let fndecl = match self.tcx().hir().get(node_id) {
3333
Node::Item(&hir::Item {
3434
node: hir::ItemKind::Fn(ref fndecl, ..),
3535
..
@@ -64,7 +64,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
6464
br: &ty::BoundRegion,
6565
) -> Option<(&'gcx hir::Ty)> {
6666
let mut nested_visitor = FindNestedTypeVisitor {
67-
tcx: self.tcx,
67+
tcx: self.tcx(),
6868
bound_region: *br,
6969
found_type: None,
7070
current_index: ty::INNERMOST,

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,41 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
2222

2323
if let Some(tables) = self.in_progress_tables {
2424
let tables = tables.borrow();
25-
NiceRegionError::new(self.tcx, error.clone(), Some(&tables)).try_report().is_some()
25+
NiceRegionError::new(self, error.clone(), Some(&tables)).try_report().is_some()
2626
} else {
27-
NiceRegionError::new(self.tcx, error.clone(), None).try_report().is_some()
27+
NiceRegionError::new(self, error.clone(), None).try_report().is_some()
2828
}
2929
}
3030
}
3131

3232
pub struct NiceRegionError<'cx, 'gcx: 'tcx, 'tcx: 'cx> {
33-
tcx: TyCtxt<'cx, 'gcx, 'tcx>,
33+
infcx: &'cx InferCtxt<'cx, 'gcx, 'tcx>,
3434
error: Option<RegionResolutionError<'tcx>>,
3535
regions: Option<(Span, ty::Region<'tcx>, ty::Region<'tcx>)>,
3636
tables: Option<&'cx ty::TypeckTables<'tcx>>,
3737
}
3838

3939
impl<'cx, 'gcx, 'tcx> NiceRegionError<'cx, 'gcx, 'tcx> {
4040
pub fn new(
41-
tcx: TyCtxt<'cx, 'gcx, 'tcx>,
41+
infcx: &'cx InferCtxt<'cx, 'gcx, 'tcx>,
4242
error: RegionResolutionError<'tcx>,
4343
tables: Option<&'cx ty::TypeckTables<'tcx>>,
4444
) -> Self {
45-
Self { tcx, error: Some(error), regions: None, tables }
45+
Self { infcx, error: Some(error), regions: None, tables }
4646
}
4747

4848
pub fn new_from_span(
49-
tcx: TyCtxt<'cx, 'gcx, 'tcx>,
49+
infcx: &'cx InferCtxt<'cx, 'gcx, 'tcx>,
5050
span: Span,
5151
sub: ty::Region<'tcx>,
5252
sup: ty::Region<'tcx>,
5353
tables: Option<&'cx ty::TypeckTables<'tcx>>,
5454
) -> Self {
55-
Self { tcx, error: None, regions: Some((span, sub, sup)), tables }
55+
Self { infcx, error: None, regions: Some((span, sub, sup)), tables }
56+
}
57+
58+
fn tcx(&self) -> TyCtxt<'cx, 'gcx, 'tcx> {
59+
self.infcx.tcx
5660
}
5761

5862
pub fn try_report_from_nll(&self) -> Option<ErrorReported> {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,23 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
2424
// version new_ty of its type where the anonymous region is replaced
2525
// with the named one.//scope_def_id
2626
let (named, anon, anon_arg_info, region_info) = if self.is_named_region(sub)
27-
&& self.tcx.is_suitable_region(sup).is_some()
27+
&& self.tcx().is_suitable_region(sup).is_some()
2828
&& self.find_arg_with_region(sup, sub).is_some()
2929
{
3030
(
3131
sub,
3232
sup,
3333
self.find_arg_with_region(sup, sub).unwrap(),
34-
self.tcx.is_suitable_region(sup).unwrap(),
34+
self.tcx().is_suitable_region(sup).unwrap(),
3535
)
36-
} else if self.is_named_region(sup) && self.tcx.is_suitable_region(sub).is_some()
36+
} else if self.is_named_region(sup) && self.tcx().is_suitable_region(sub).is_some()
3737
&& self.find_arg_with_region(sub, sup).is_some()
3838
{
3939
(
4040
sup,
4141
sub,
4242
self.find_arg_with_region(sub, sup).unwrap(),
43-
self.tcx.is_suitable_region(sub).unwrap(),
43+
self.tcx().is_suitable_region(sub).unwrap(),
4444
)
4545
} else {
4646
return None; // inapplicable
@@ -97,7 +97,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
9797
};
9898

9999
struct_span_err!(
100-
self.tcx.sess,
100+
self.tcx().sess,
101101
span,
102102
E0621,
103103
"explicit lifetime required in {}",

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
4747
// closure, provide a specific message pointing this out.
4848
if let (&SubregionOrigin::BindingTypeIsNotValidAtDecl(ref external_span),
4949
&RegionKind::ReFree(ref free_region)) = (&sub_origin, sup_region) {
50-
let hir = &self.tcx.hir();
50+
let hir = &self.tcx().hir();
5151
if let Some(node_id) = hir.as_local_node_id(free_region.scope) {
5252
if let Node::Expr(Expr {
5353
node: Closure(_, _, _, closure_span, None),
5454
..
5555
}) = hir.get(node_id) {
5656
let sup_sp = sup_origin.span();
5757
let origin_sp = origin.span();
58-
let mut err = self.tcx.sess.struct_span_err(
58+
let mut err = self.tcx().sess.struct_span_err(
5959
sup_sp,
6060
"borrowed data cannot be stored outside of its closure");
6161
err.span_label(sup_sp, "cannot be stored outside of its closure");

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
3838
if expected.def_id == found.def_id =>
3939
{
4040
Some(self.try_report_placeholders_trait(
41-
Some(self.tcx.mk_region(ty::ReVar(*vid))),
41+
Some(self.tcx().mk_region(ty::ReVar(*vid))),
4242
cause,
4343
Some(sub_placeholder),
4444
Some(sup_placeholder),
@@ -62,7 +62,7 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
6262
if expected.def_id == found.def_id =>
6363
{
6464
Some(self.try_report_placeholders_trait(
65-
Some(self.tcx.mk_region(ty::ReVar(*vid))),
65+
Some(self.tcx().mk_region(ty::ReVar(*vid))),
6666
cause,
6767
Some(sub_placeholder),
6868
None,
@@ -86,7 +86,7 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
8686
if expected.def_id == found.def_id =>
8787
{
8888
Some(self.try_report_placeholders_trait(
89-
Some(self.tcx.mk_region(ty::ReVar(*vid))),
89+
Some(self.tcx().mk_region(ty::ReVar(*vid))),
9090
cause,
9191
None,
9292
Some(*sup_placeholder),
@@ -182,19 +182,19 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
182182
expected_substs: &'tcx Substs<'tcx>,
183183
actual_substs: &'tcx Substs<'tcx>,
184184
) -> ErrorReported {
185-
let mut err = self.tcx.sess.struct_span_err(
186-
cause.span(&self.tcx),
185+
let mut err = self.tcx().sess.struct_span_err(
186+
cause.span(&self.tcx()),
187187
&format!(
188188
"implementation of `{}` is not general enough",
189-
self.tcx.item_path_str(trait_def_id),
189+
self.tcx().item_path_str(trait_def_id),
190190
),
191191
);
192192

193193
match cause.code {
194194
ObligationCauseCode::ItemObligation(def_id) => {
195195
err.note(&format!(
196196
"Due to a where-clause on `{}`,",
197-
self.tcx.item_path_str(def_id),
197+
self.tcx().item_path_str(def_id),
198198
));
199199
}
200200
_ => (),
@@ -220,7 +220,7 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
220220
let mut has_sup = None;
221221
let mut has_vid = None;
222222

223-
self.tcx.for_each_free_region(&expected_trait_ref, |r| {
223+
self.tcx().for_each_free_region(&expected_trait_ref, |r| {
224224
if Some(r) == sub_placeholder && has_sub.is_none() {
225225
has_sub = Some(counter);
226226
counter += 1;
@@ -230,15 +230,15 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
230230
}
231231
});
232232

233-
self.tcx.for_each_free_region(&actual_trait_ref, |r| {
233+
self.tcx().for_each_free_region(&actual_trait_ref, |r| {
234234
if Some(r) == vid && has_vid.is_none() {
235235
has_vid = Some(counter);
236236
counter += 1;
237237
}
238238
});
239239

240240
let self_ty_has_vid = self
241-
.tcx
241+
.tcx()
242242
.any_free_region_meets(&actual_trait_ref.self_ty(), |r| Some(r) == vid);
243243

244244
RegionHighlightMode::maybe_highlighting_region(sub_placeholder, has_sub, || {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
1919
sup_r,
2020
) = error.clone()
2121
{
22-
let anon_reg_sup = self.tcx.is_suitable_region(sup_r)?;
22+
let anon_reg_sup = self.tcx().is_suitable_region(sup_r)?;
2323
if sub_r == &RegionKind::ReStatic &&
24-
self.tcx.return_type_impl_trait(anon_reg_sup.def_id).is_some()
24+
self.tcx().return_type_impl_trait(anon_reg_sup.def_id).is_some()
2525
{
2626
let sp = var_origin.span();
2727
let return_sp = sub_origin.span();
28-
let mut err = self.tcx.sess.struct_span_err(
28+
let mut err = self.tcx().sess.struct_span_err(
2929
sp,
3030
"cannot infer an appropriate lifetime",
3131
);
@@ -38,7 +38,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
3838
"...but this borrow...",
3939
);
4040

41-
let (lifetime, lt_sp_opt) = self.tcx.msg_span_from_free_region(sup_r);
41+
let (lifetime, lt_sp_opt) = self.tcx().msg_span_from_free_region(sup_r);
4242
if let Some(lifetime_sp) = lt_sp_opt {
4343
err.span_note(
4444
lifetime_sp,
@@ -52,7 +52,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
5252
}) => name.to_string(),
5353
_ => "'_".to_owned(),
5454
};
55-
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(return_sp) {
55+
if let Ok(snippet) = self.tcx().sess.source_map().span_to_snippet(return_sp) {
5656
err.span_suggestion(
5757
return_sp,
5858
&format!(

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
4444
let (id, bound_region) = match *anon_region {
4545
ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
4646
ty::ReEarlyBound(ref ebr) => (
47-
self.tcx.parent_def_id(ebr.def_id).unwrap(),
47+
self.tcx().parent_def_id(ebr.def_id).unwrap(),
4848
ty::BoundRegion::BrNamed(ebr.def_id, ebr.name),
4949
),
5050
_ => return None, // not a free region
5151
};
5252

53-
let hir = &self.tcx.hir();
53+
let hir = &self.tcx().hir();
5454
if let Some(node_id) = hir.as_local_node_id(id) {
5555
if let Some(body_id) = hir.maybe_body_owned_by(node_id) {
5656
let body = hir.body(body_id);
@@ -66,7 +66,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
6666
let arg_ty_span = hir.span(hir.hir_to_node_id(ty_hir_id));
6767
let ty = tables.node_id_to_type_opt(arg.hir_id)?;
6868
let mut found_anon_region = false;
69-
let new_arg_ty = self.tcx.fold_regions(&ty, &mut false, |r, _| {
69+
let new_arg_ty = self.tcx().fold_regions(&ty, &mut false, |r, _| {
7070
if *r == *anon_region {
7171
found_anon_region = true;
7272
replace_region
@@ -108,10 +108,10 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
108108
br: ty::BoundRegion,
109109
decl: &hir::FnDecl,
110110
) -> Option<Span> {
111-
let ret_ty = self.tcx.type_of(scope_def_id);
111+
let ret_ty = self.tcx().type_of(scope_def_id);
112112
if let ty::FnDef(_, _) = ret_ty.sty {
113-
let sig = ret_ty.fn_sig(self.tcx);
114-
let late_bound_regions = self.tcx
113+
let sig = ret_ty.fn_sig(self.tcx());
114+
let late_bound_regions = self.tcx()
115115
.collect_referenced_late_bound_regions(&sig.output());
116116
if late_bound_regions.iter().any(|r| *r == br) {
117117
return Some(decl.output.span());
@@ -126,7 +126,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
126126
// enable E0621 for it.
127127
pub(super) fn is_self_anon(&self, is_first: bool, scope_def_id: DefId) -> bool {
128128
is_first
129-
&& self.tcx
129+
&& self.tcx()
130130
.opt_associated_item(scope_def_id)
131131
.map(|i| i.method_has_self_argument) == Some(true)
132132
}

src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
243243
// Check if we can use one of the "nice region errors".
244244
if let (Some(f), Some(o)) = (self.to_error_region(fr), self.to_error_region(outlived_fr)) {
245245
let tables = infcx.tcx.typeck_tables_of(mir_def_id);
246-
let nice = NiceRegionError::new_from_span(infcx.tcx, span, o, f, Some(tables));
246+
let nice = NiceRegionError::new_from_span(infcx, span, o, f, Some(tables));
247247
if let Some(_error_reported) = nice.try_report_from_nll() {
248248
return;
249249
}

0 commit comments

Comments
 (0)