Skip to content

Commit d53bf7a

Browse files
committed
Make rustc::infer::error_reporting::{note_and_explain_free_region, note_and_explain_region} free functions.
1 parent 7118e33 commit d53bf7a

File tree

3 files changed

+164
-125
lines changed

3 files changed

+164
-125
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 89 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -79,98 +79,95 @@ pub use need_type_info::TypeAnnotationNeeded;
7979

8080
pub mod nice_region_error;
8181

82-
impl<'tcx> TyCtxt<'tcx> {
83-
pub fn note_and_explain_region(
84-
self,
85-
region_scope_tree: &region::ScopeTree,
86-
err: &mut DiagnosticBuilder<'_>,
87-
prefix: &str,
88-
region: ty::Region<'tcx>,
89-
suffix: &str,
90-
) {
91-
let (description, span) = match *region {
92-
ty::ReScope(scope) => {
93-
let new_string;
94-
let unknown_scope = || {
95-
format!("{}unknown scope: {:?}{}. Please report a bug.", prefix, scope, suffix)
96-
};
97-
let span = scope.span(self, region_scope_tree);
98-
let tag = match self.hir().find(scope.hir_id(region_scope_tree)) {
99-
Some(Node::Block(_)) => "block",
100-
Some(Node::Expr(expr)) => match expr.kind {
101-
hir::ExprKind::Call(..) => "call",
102-
hir::ExprKind::MethodCall(..) => "method call",
103-
hir::ExprKind::Match(.., hir::MatchSource::IfLetDesugar { .. }) => "if let",
104-
hir::ExprKind::Match(.., hir::MatchSource::WhileLetDesugar) => "while let",
105-
hir::ExprKind::Match(.., hir::MatchSource::ForLoopDesugar) => "for",
106-
hir::ExprKind::Match(..) => "match",
107-
_ => "expression",
108-
},
109-
Some(Node::Stmt(_)) => "statement",
110-
Some(Node::Item(it)) => item_scope_tag(&it),
111-
Some(Node::TraitItem(it)) => trait_item_scope_tag(&it),
112-
Some(Node::ImplItem(it)) => impl_item_scope_tag(&it),
113-
Some(_) | None => {
114-
err.span_note(span, &unknown_scope());
115-
return;
116-
}
117-
};
118-
let scope_decorated_tag = match scope.data {
119-
region::ScopeData::Node => tag,
120-
region::ScopeData::CallSite => "scope of call-site for function",
121-
region::ScopeData::Arguments => "scope of function body",
122-
region::ScopeData::Destruction => {
123-
new_string = format!("destruction scope surrounding {}", tag);
124-
&new_string[..]
125-
}
126-
region::ScopeData::Remainder(first_statement_index) => {
127-
new_string = format!(
128-
"block suffix following statement {}",
129-
first_statement_index.index()
130-
);
131-
&new_string[..]
132-
}
133-
};
134-
explain_span(self, scope_decorated_tag, span)
135-
}
82+
pub(super) fn note_and_explain_region(
83+
tcx: TyCtxt<'tcx>,
84+
region_scope_tree: &region::ScopeTree,
85+
err: &mut DiagnosticBuilder<'_>,
86+
prefix: &str,
87+
region: ty::Region<'tcx>,
88+
suffix: &str,
89+
) {
90+
let (description, span) = match *region {
91+
ty::ReScope(scope) => {
92+
let new_string;
93+
let unknown_scope =
94+
|| format!("{}unknown scope: {:?}{}. Please report a bug.", prefix, scope, suffix);
95+
let span = scope.span(tcx, region_scope_tree);
96+
let tag = match tcx.hir().find(scope.hir_id(region_scope_tree)) {
97+
Some(Node::Block(_)) => "block",
98+
Some(Node::Expr(expr)) => match expr.kind {
99+
hir::ExprKind::Call(..) => "call",
100+
hir::ExprKind::MethodCall(..) => "method call",
101+
hir::ExprKind::Match(.., hir::MatchSource::IfLetDesugar { .. }) => "if let",
102+
hir::ExprKind::Match(.., hir::MatchSource::WhileLetDesugar) => "while let",
103+
hir::ExprKind::Match(.., hir::MatchSource::ForLoopDesugar) => "for",
104+
hir::ExprKind::Match(..) => "match",
105+
_ => "expression",
106+
},
107+
Some(Node::Stmt(_)) => "statement",
108+
Some(Node::Item(it)) => item_scope_tag(&it),
109+
Some(Node::TraitItem(it)) => trait_item_scope_tag(&it),
110+
Some(Node::ImplItem(it)) => impl_item_scope_tag(&it),
111+
Some(_) | None => {
112+
err.span_note(span, &unknown_scope());
113+
return;
114+
}
115+
};
116+
let scope_decorated_tag = match scope.data {
117+
region::ScopeData::Node => tag,
118+
region::ScopeData::CallSite => "scope of call-site for function",
119+
region::ScopeData::Arguments => "scope of function body",
120+
region::ScopeData::Destruction => {
121+
new_string = format!("destruction scope surrounding {}", tag);
122+
&new_string[..]
123+
}
124+
region::ScopeData::Remainder(first_statement_index) => {
125+
new_string = format!(
126+
"block suffix following statement {}",
127+
first_statement_index.index()
128+
);
129+
&new_string[..]
130+
}
131+
};
132+
explain_span(tcx, scope_decorated_tag, span)
133+
}
136134

137-
ty::ReEarlyBound(_) | ty::ReFree(_) | ty::ReStatic => {
138-
msg_span_from_free_region(self, region)
139-
}
135+
ty::ReEarlyBound(_) | ty::ReFree(_) | ty::ReStatic => {
136+
msg_span_from_free_region(tcx, region)
137+
}
140138

141-
ty::ReEmpty => ("the empty lifetime".to_owned(), None),
139+
ty::ReEmpty => ("the empty lifetime".to_owned(), None),
142140

143-
ty::RePlaceholder(_) => (format!("any other region"), None),
141+
ty::RePlaceholder(_) => (format!("any other region"), None),
144142

145-
// FIXME(#13998) RePlaceholder should probably print like
146-
// ReFree rather than dumping Debug output on the user.
147-
//
148-
// We shouldn't really be having unification failures with ReVar
149-
// and ReLateBound though.
150-
ty::ReVar(_) | ty::ReLateBound(..) | ty::ReErased => {
151-
(format!("lifetime {:?}", region), None)
152-
}
143+
// FIXME(#13998) RePlaceholder should probably print like
144+
// ReFree rather than dumping Debug output on the user.
145+
//
146+
// We shouldn't really be having unification failures with ReVar
147+
// and ReLateBound though.
148+
ty::ReVar(_) | ty::ReLateBound(..) | ty::ReErased => {
149+
(format!("lifetime {:?}", region), None)
150+
}
153151

154-
// We shouldn't encounter an error message with ReClosureBound.
155-
ty::ReClosureBound(..) => {
156-
bug!("encountered unexpected ReClosureBound: {:?}", region,);
157-
}
158-
};
152+
// We shouldn't encounter an error message with ReClosureBound.
153+
ty::ReClosureBound(..) => {
154+
bug!("encountered unexpected ReClosureBound: {:?}", region,);
155+
}
156+
};
159157

160-
emit_msg_span(err, prefix, description, span, suffix);
161-
}
158+
emit_msg_span(err, prefix, description, span, suffix);
159+
}
162160

163-
pub fn note_and_explain_free_region(
164-
self,
165-
err: &mut DiagnosticBuilder<'_>,
166-
prefix: &str,
167-
region: ty::Region<'tcx>,
168-
suffix: &str,
169-
) {
170-
let (description, span) = msg_span_from_free_region(self, region);
161+
pub(super) fn note_and_explain_free_region(
162+
tcx: TyCtxt<'tcx>,
163+
err: &mut DiagnosticBuilder<'_>,
164+
prefix: &str,
165+
region: ty::Region<'tcx>,
166+
suffix: &str,
167+
) {
168+
let (description, span) = msg_span_from_free_region(tcx, region);
171169

172-
emit_msg_span(err, prefix, description, span, suffix);
173-
}
170+
emit_msg_span(err, prefix, description, span, suffix);
174171
}
175172

176173
fn msg_span_from_free_region(
@@ -1719,7 +1716,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
17191716
"consider adding an explicit lifetime bound for `{}`",
17201717
bound_kind
17211718
));
1722-
self.tcx.note_and_explain_region(
1719+
note_and_explain_region(
1720+
self.tcx,
17231721
region_scope_tree,
17241722
&mut err,
17251723
&format!("{} must be valid for ", labeled_user_string),
@@ -1747,7 +1745,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
17471745
) {
17481746
let mut err = self.report_inference_failure(var_origin);
17491747

1750-
self.tcx.note_and_explain_region(
1748+
note_and_explain_region(
1749+
self.tcx,
17511750
region_scope_tree,
17521751
&mut err,
17531752
"first, the lifetime cannot outlive ",
@@ -1771,7 +1770,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
17711770
(self.values_str(&sup_trace.values), self.values_str(&sub_trace.values))
17721771
{
17731772
if sub_expected == sup_expected && sub_found == sup_found {
1774-
self.tcx.note_and_explain_region(
1773+
note_and_explain_region(
1774+
self.tcx,
17751775
region_scope_tree,
17761776
&mut err,
17771777
"...but the lifetime must also be valid for ",
@@ -1794,7 +1794,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
17941794

17951795
self.note_region_origin(&mut err, &sup_origin);
17961796

1797-
self.tcx.note_and_explain_region(
1797+
note_and_explain_region(
1798+
self.tcx,
17981799
region_scope_tree,
17991800
&mut err,
18001801
"but, the lifetime must be valid for ",

0 commit comments

Comments
 (0)