Skip to content

Commit 834e392

Browse files
committed
Refactor note_and_explain_region to call note_and_explain_free_region
1 parent 7580826 commit 834e392

File tree

1 file changed

+92
-86
lines changed
  • src/librustc/infer/error_reporting

1 file changed

+92
-86
lines changed

src/librustc/infer/error_reporting/mod.rs

+92-86
Original file line numberDiff line numberDiff line change
@@ -87,42 +87,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
8787
prefix: &str,
8888
region: ty::Region<'tcx>,
8989
suffix: &str) {
90-
fn item_scope_tag(item: &hir::Item) -> &'static str {
91-
match item.node {
92-
hir::ItemImpl(..) => "impl",
93-
hir::ItemStruct(..) => "struct",
94-
hir::ItemUnion(..) => "union",
95-
hir::ItemEnum(..) => "enum",
96-
hir::ItemTrait(..) => "trait",
97-
hir::ItemFn(..) => "function body",
98-
_ => "item"
99-
}
100-
}
101-
102-
fn trait_item_scope_tag(item: &hir::TraitItem) -> &'static str {
103-
match item.node {
104-
hir::TraitItemKind::Method(..) => "method body",
105-
hir::TraitItemKind::Const(..) |
106-
hir::TraitItemKind::Type(..) => "associated item"
107-
}
108-
}
109-
110-
fn impl_item_scope_tag(item: &hir::ImplItem) -> &'static str {
111-
match item.node {
112-
hir::ImplItemKind::Method(..) => "method body",
113-
hir::ImplItemKind::Const(..) |
114-
hir::ImplItemKind::Type(_) => "associated item"
115-
}
116-
}
117-
118-
fn explain_span<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
119-
heading: &str, span: Span)
120-
-> (String, Option<Span>) {
121-
let lo = tcx.sess.codemap().lookup_char_pos_adj(span.lo());
122-
(format!("the {} at {}:{}", heading, lo.line, lo.col.to_usize() + 1),
123-
Some(span))
124-
}
125-
12690
let (description, span) = match *region {
12791
ty::ReScope(scope) => {
12892
let new_string;
@@ -143,9 +107,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
143107
_ => "expression",
144108
},
145109
Some(hir_map::NodeStmt(_)) => "statement",
146-
Some(hir_map::NodeItem(it)) => item_scope_tag(&it),
147-
Some(hir_map::NodeTraitItem(it)) => trait_item_scope_tag(&it),
148-
Some(hir_map::NodeImplItem(it)) => impl_item_scope_tag(&it),
110+
Some(hir_map::NodeItem(it)) => Self::item_scope_tag(&it),
111+
Some(hir_map::NodeTraitItem(it)) => Self::trait_item_scope_tag(&it),
112+
Some(hir_map::NodeImplItem(it)) => Self::impl_item_scope_tag(&it),
149113
Some(_) | None => {
150114
err.span_note(span, &unknown_scope());
151115
return;
@@ -169,57 +133,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
169133
&new_string[..]
170134
}
171135
};
172-
explain_span(self, scope_decorated_tag, span)
136+
self.explain_span(scope_decorated_tag, span)
173137
}
174138

175139
ty::ReEarlyBound(_) |
176140
ty::ReFree(_) => {
177-
let scope = region.free_region_binding_scope(self);
178-
let node = self.hir.as_local_node_id(scope)
179-
.unwrap_or(DUMMY_NODE_ID);
180-
let unknown;
181-
let tag = match self.hir.find(node) {
182-
Some(hir_map::NodeBlock(_)) |
183-
Some(hir_map::NodeExpr(_)) => "body",
184-
Some(hir_map::NodeItem(it)) => item_scope_tag(&it),
185-
Some(hir_map::NodeTraitItem(it)) => trait_item_scope_tag(&it),
186-
Some(hir_map::NodeImplItem(it)) => impl_item_scope_tag(&it),
187-
188-
// this really should not happen, but it does:
189-
// FIXME(#27942)
190-
Some(_) => {
191-
unknown = format!("unexpected node ({}) for scope {:?}. \
192-
Please report a bug.",
193-
self.hir.node_to_string(node), scope);
194-
&unknown
195-
}
196-
None => {
197-
unknown = format!("unknown node for scope {:?}. \
198-
Please report a bug.", scope);
199-
&unknown
200-
}
201-
};
202-
let (prefix, span) = match *region {
203-
ty::ReEarlyBound(ref br) => {
204-
(format!("the lifetime {} as defined on", br.name),
205-
self.sess.codemap().def_span(self.hir.span(node)))
206-
}
207-
ty::ReFree(ref fr) => {
208-
match fr.bound_region {
209-
ty::BrAnon(idx) => {
210-
(format!("the anonymous lifetime #{} defined on", idx + 1),
211-
self.hir.span(node))
212-
}
213-
ty::BrFresh(_) => ("an anonymous lifetime defined on".to_owned(),
214-
self.hir.span(node)),
215-
_ => (format!("the lifetime {} as defined on", fr.bound_region),
216-
self.sess.codemap().def_span(self.hir.span(node))),
217-
}
218-
}
219-
_ => bug!()
220-
};
221-
let (msg, opt_span) = explain_span(self, tag, span);
222-
(format!("{} {}", prefix, msg), opt_span)
141+
self.msg_span_from_free_region(region)
223142
}
224143

225144
ty::ReStatic => ("the static lifetime".to_owned(), None),
@@ -253,6 +172,93 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
253172
err.note(&message);
254173
}
255174
}
175+
176+
fn msg_span_from_free_region(self,
177+
region: ty::Region<'tcx>)
178+
-> (String, Option<Span>) {
179+
let scope = region.free_region_binding_scope(self);
180+
let node = self.hir.as_local_node_id(scope)
181+
.unwrap_or(DUMMY_NODE_ID);
182+
let unknown;
183+
let tag = match self.hir.find(node) {
184+
Some(hir_map::NodeBlock(_)) |
185+
Some(hir_map::NodeExpr(_)) => "body",
186+
Some(hir_map::NodeItem(it)) => Self::item_scope_tag(&it),
187+
Some(hir_map::NodeTraitItem(it)) => Self::trait_item_scope_tag(&it),
188+
Some(hir_map::NodeImplItem(it)) => Self::impl_item_scope_tag(&it),
189+
190+
// this really should not happen, but it does:
191+
// FIXME(#27942)
192+
Some(_) => {
193+
unknown = format!("unexpected node ({}) for scope {:?}. \
194+
Please report a bug.",
195+
self.hir.node_to_string(node), scope);
196+
&unknown
197+
}
198+
None => {
199+
unknown = format!("unknown node for scope {:?}. \
200+
Please report a bug.", scope);
201+
&unknown
202+
}
203+
};
204+
let (prefix, span) = match *region {
205+
ty::ReEarlyBound(ref br) => {
206+
(format!("the lifetime {} as defined on", br.name),
207+
self.sess.codemap().def_span(self.hir.span(node)))
208+
}
209+
ty::ReFree(ref fr) => {
210+
match fr.bound_region {
211+
ty::BrAnon(idx) => {
212+
(format!("the anonymous lifetime #{} defined on", idx + 1),
213+
self.hir.span(node))
214+
}
215+
ty::BrFresh(_) => ("an anonymous lifetime defined on".to_owned(),
216+
self.hir.span(node)),
217+
_ => (format!("the lifetime {} as defined on", fr.bound_region),
218+
self.sess.codemap().def_span(self.hir.span(node))),
219+
}
220+
}
221+
_ => bug!()
222+
};
223+
let (msg, opt_span) = self.explain_span(tag, span);
224+
(format!("{} {}", prefix, msg), opt_span)
225+
}
226+
227+
fn item_scope_tag(item: &hir::Item) -> &'static str {
228+
match item.node {
229+
hir::ItemImpl(..) => "impl",
230+
hir::ItemStruct(..) => "struct",
231+
hir::ItemUnion(..) => "union",
232+
hir::ItemEnum(..) => "enum",
233+
hir::ItemTrait(..) => "trait",
234+
hir::ItemFn(..) => "function body",
235+
_ => "item"
236+
}
237+
}
238+
239+
fn trait_item_scope_tag(item: &hir::TraitItem) -> &'static str {
240+
match item.node {
241+
hir::TraitItemKind::Method(..) => "method body",
242+
hir::TraitItemKind::Const(..) |
243+
hir::TraitItemKind::Type(..) => "associated item"
244+
}
245+
}
246+
247+
fn impl_item_scope_tag(item: &hir::ImplItem) -> &'static str {
248+
match item.node {
249+
hir::ImplItemKind::Method(..) => "method body",
250+
hir::ImplItemKind::Const(..) |
251+
hir::ImplItemKind::Type(_) => "associated item"
252+
}
253+
}
254+
255+
fn explain_span(self,
256+
heading: &str, span: Span)
257+
-> (String, Option<Span>) {
258+
let lo = self.sess.codemap().lookup_char_pos_adj(span.lo());
259+
(format!("the {} at {}:{}", heading, lo.line, lo.col.to_usize() + 1),
260+
Some(span))
261+
}
256262
}
257263

258264
impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {

0 commit comments

Comments
 (0)