parse: extract error_on_unmatched_vis.

This commit is contained in:
Mazdak Farrokhzad 2020-02-22 03:44:24 +01:00
parent 7017058e6b
commit 9ed4c09983

View File

@ -88,16 +88,9 @@ impl<'a> Parser<'a> {
return Ok(Some(P(self.mk_item(lo, ident, kind, vis, Defaultness::Final, attrs)))); return Ok(Some(P(self.mk_item(lo, ident, kind, vis, Defaultness::Final, attrs))));
} }
// FAILURE TO PARSE ITEM // At this point, we have failed to parse an item.
if let VisibilityKind::Inherited = vis.node {
} else { self.error_on_unmatched_vis(&vis);
let vs = pprust::vis_to_string(&vis);
let vs = vs.trim_end();
self.struct_span_err(vis.span, &format!("unmatched visibility `{}`", vs))
.span_label(vis.span, "the unmatched visibility")
.help(&format!("you likely meant to define an item, e.g., `{} fn foo() {{}}`", vs))
.emit();
}
if !attributes_allowed { if !attributes_allowed {
self.recover_attrs_no_item(&attrs)?; self.recover_attrs_no_item(&attrs)?;
@ -105,6 +98,19 @@ impl<'a> Parser<'a> {
Ok(None) Ok(None)
} }
/// Error in-case a non-inherited visibility was parsed but no item followed.
fn error_on_unmatched_vis(&self, vis: &Visibility) {
if let VisibilityKind::Inherited = vis.node {
return;
}
let vs = pprust::vis_to_string(&vis);
let vs = vs.trim_end();
self.struct_span_err(vis.span, &format!("unmatched visibility `{}`", vs))
.span_label(vis.span, "the unmatched visibility")
.help(&format!("you likely meant to define an item, e.g., `{} fn foo() {{}}`", vs))
.emit();
}
/// Parses one of the items allowed by the flags. /// Parses one of the items allowed by the flags.
fn parse_item_kind( fn parse_item_kind(
&mut self, &mut self,