Factor out insert_or_error.

It appears identically as a closure in two functions.

Also change its return type from `bool` to `Option<()>` so it can be
used with `?`.
This commit is contained in:
Nicholas Nethercote 2023-10-04 18:27:18 +11:00
parent 108e541cc2
commit e49a1470ba

View File

@ -353,28 +353,28 @@ pub fn find_body_stability(
body_stab body_stab
} }
/// Read the content of a `stable`/`rustc_const_stable` attribute, and return the feature name and fn insert_or_error(sess: &Session, meta: &MetaItem, item: &mut Option<Symbol>) -> Option<()> {
/// its stability information.
fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, StabilityLevel)> {
let meta = attr.meta()?;
let MetaItem { kind: MetaItemKind::List(ref metas), .. } = meta else { return None };
let insert_or_error = |meta: &MetaItem, item: &mut Option<Symbol>| {
if item.is_some() { if item.is_some() {
handle_errors( handle_errors(
&sess.parse_sess, &sess.parse_sess,
meta.span, meta.span,
AttrError::MultipleItem(pprust::path_to_string(&meta.path)), AttrError::MultipleItem(pprust::path_to_string(&meta.path)),
); );
return false; None
} } else if let Some(v) = meta.value_str() {
if let Some(v) = meta.value_str() {
*item = Some(v); *item = Some(v);
true Some(())
} else { } else {
sess.emit_err(session_diagnostics::IncorrectMetaItem { span: meta.span }); sess.emit_err(session_diagnostics::IncorrectMetaItem { span: meta.span });
false None
} }
}; }
/// Read the content of a `stable`/`rustc_const_stable` attribute, and return the feature name and
/// its stability information.
fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, StabilityLevel)> {
let meta = attr.meta()?;
let MetaItem { kind: MetaItemKind::List(ref metas), .. } = meta else { return None };
let mut feature = None; let mut feature = None;
let mut since = None; let mut since = None;
@ -389,16 +389,8 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit
}; };
match mi.name_or_empty() { match mi.name_or_empty() {
sym::feature => { sym::feature => insert_or_error(sess, mi, &mut feature)?,
if !insert_or_error(mi, &mut feature) { sym::since => insert_or_error(sess, mi, &mut since)?,
return None;
}
}
sym::since => {
if !insert_or_error(mi, &mut since) {
return None;
}
}
_ => { _ => {
handle_errors( handle_errors(
&sess.parse_sess, &sess.parse_sess,
@ -438,23 +430,6 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit
fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, StabilityLevel)> { fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, StabilityLevel)> {
let meta = attr.meta()?; let meta = attr.meta()?;
let MetaItem { kind: MetaItemKind::List(ref metas), .. } = meta else { return None }; let MetaItem { kind: MetaItemKind::List(ref metas), .. } = meta else { return None };
let insert_or_error = |meta: &MetaItem, item: &mut Option<Symbol>| {
if item.is_some() {
handle_errors(
&sess.parse_sess,
meta.span,
AttrError::MultipleItem(pprust::path_to_string(&meta.path)),
);
return false;
}
if let Some(v) = meta.value_str() {
*item = Some(v);
true
} else {
sess.emit_err(session_diagnostics::IncorrectMetaItem { span: meta.span });
false
}
};
let mut feature = None; let mut feature = None;
let mut reason = None; let mut reason = None;
@ -473,20 +448,10 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
}; };
match mi.name_or_empty() { match mi.name_or_empty() {
sym::feature => { sym::feature => insert_or_error(sess, mi, &mut feature)?,
if !insert_or_error(mi, &mut feature) { sym::reason => insert_or_error(sess, mi, &mut reason)?,
return None;
}
}
sym::reason => {
if !insert_or_error(mi, &mut reason) {
return None;
}
}
sym::issue => { sym::issue => {
if !insert_or_error(mi, &mut issue) { insert_or_error(sess, mi, &mut issue)?;
return None;
}
// These unwraps are safe because `insert_or_error` ensures the meta item // These unwraps are safe because `insert_or_error` ensures the meta item
// is a name/value pair string literal. // is a name/value pair string literal.
@ -515,11 +480,7 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
} }
is_soft = true; is_soft = true;
} }
sym::implied_by => { sym::implied_by => insert_or_error(sess, mi, &mut implied_by)?,
if !insert_or_error(mi, &mut implied_by) {
return None;
}
}
_ => { _ => {
handle_errors( handle_errors(
&sess.parse_sess, &sess.parse_sess,