Skip to content

Commit 49780d2

Browse files
committed
syntax: merge things back into parse_visibility.
1 parent 30647d1 commit 49780d2

File tree

1 file changed

+25
-37
lines changed

1 file changed

+25
-37
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,13 +1423,35 @@ impl<'a> Parser<'a> {
14231423
if self.is_keyword_ahead(1, &[kw::Crate])
14241424
&& self.look_ahead(2, |t| t != &token::ModSep) // account for `pub(crate::foo)`
14251425
{
1426-
return self.parse_vis_pub_crate(lo);
1426+
// Parse `pub(crate)`.
1427+
self.bump(); // `(`
1428+
self.bump(); // `crate`
1429+
self.expect(&token::CloseDelim(token::Paren))?; // `)`
1430+
let vis = VisibilityKind::Crate(CrateSugar::PubCrate);
1431+
return Ok(respan(lo.to(self.prev_span), vis));
14271432
} else if self.is_keyword_ahead(1, &[kw::In]) {
1428-
return self.parse_vis_pub_in(lo);
1433+
// Parse `pub(in path)`.
1434+
self.bump(); // `(`
1435+
self.bump(); // `in`
1436+
let path = self.parse_path(PathStyle::Mod)?; // `path`
1437+
self.expect(&token::CloseDelim(token::Paren))?; // `)`
1438+
let vis = VisibilityKind::Restricted {
1439+
path: P(path),
1440+
id: ast::DUMMY_NODE_ID,
1441+
};
1442+
return Ok(respan(lo.to(self.prev_span), vis));
14291443
} else if self.look_ahead(2, |t| t == &token::CloseDelim(token::Paren))
14301444
&& self.is_keyword_ahead(1, &[kw::Super, kw::SelfLower])
14311445
{
1432-
return self.parse_vis_self_super(lo);
1446+
// Parse `pub(self)` or `pub(super)`.
1447+
self.bump(); // `(`
1448+
let path = self.parse_path(PathStyle::Mod)?; // `super`/`self`
1449+
self.expect(&token::CloseDelim(token::Paren))?; // `)`
1450+
let vis = VisibilityKind::Restricted {
1451+
path: P(path),
1452+
id: ast::DUMMY_NODE_ID,
1453+
};
1454+
return Ok(respan(lo.to(self.prev_span), vis));
14331455
} else if !can_take_tuple { // Provide this diagnostic if this is not a tuple struct.
14341456
self.recover_incorrect_vis_restriction()?;
14351457
// Emit diagnostic, but continue with public visibility.
@@ -1439,40 +1461,6 @@ impl<'a> Parser<'a> {
14391461
Ok(respan(lo, VisibilityKind::Public))
14401462
}
14411463

1442-
/// Parse `pub(crate)`.
1443-
fn parse_vis_pub_crate(&mut self, lo: Span) -> PResult<'a, Visibility> {
1444-
self.bump(); // `(`
1445-
self.bump(); // `crate`
1446-
self.expect(&token::CloseDelim(token::Paren))?; // `)`
1447-
Ok(respan(
1448-
lo.to(self.prev_span),
1449-
VisibilityKind::Crate(CrateSugar::PubCrate),
1450-
))
1451-
}
1452-
1453-
/// Parse `pub(in path)`.
1454-
fn parse_vis_pub_in(&mut self, lo: Span) -> PResult<'a, Visibility> {
1455-
self.bump(); // `(`
1456-
self.bump(); // `in`
1457-
let path = self.parse_path(PathStyle::Mod)?; // `path`
1458-
self.expect(&token::CloseDelim(token::Paren))?; // `)`
1459-
Ok(respan(lo.to(self.prev_span), VisibilityKind::Restricted {
1460-
path: P(path),
1461-
id: ast::DUMMY_NODE_ID,
1462-
}))
1463-
}
1464-
1465-
/// Parse `pub(self)` or `pub(super)`.
1466-
fn parse_vis_self_super(&mut self, lo: Span) -> PResult<'a, Visibility> {
1467-
self.bump(); // `(`
1468-
let path = self.parse_path(PathStyle::Mod)?; // `super`/`self`
1469-
self.expect(&token::CloseDelim(token::Paren))?; // `)`
1470-
Ok(respan(lo.to(self.prev_span), VisibilityKind::Restricted {
1471-
path: P(path),
1472-
id: ast::DUMMY_NODE_ID,
1473-
}))
1474-
}
1475-
14761464
/// Recovery for e.g. `pub(something) fn ...` or `struct X { pub(something) y: Z }`
14771465
fn recover_incorrect_vis_restriction(&mut self) -> PResult<'a, ()> {
14781466
self.bump(); // `(`

0 commit comments

Comments
 (0)