Skip to content

Commit 772c600

Browse files
committed
Auto merge of #32688 - jseyfried:ast_groundwork_for_1422, r=pnkfelix
[breaking-batch] Add support for `pub(restricted)` syntax in the AST This PR allows the AST to represent the `pub(restricted)` syntax from RFC 1422 (cc #32409). More specifically, it makes `ast::Visibility` non-`Copy` and adds two new variants, `Visibility::Crate` for `pub(crate)` and `Visitibility::Restricted { path: P<Path>, id: NodeId }` for `pub(path)`. plugin-[breaking-change] cc #31645 r? @pnkfelix
2 parents 241a9d0 + 432eb8a commit 772c600

File tree

7 files changed

+92
-69
lines changed

7 files changed

+92
-69
lines changed

src/librustc_front/lowering.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ pub fn lower_impl_item(lctx: &LoweringContext, i: &ImplItem) -> hir::ImplItem {
761761
id: i.id,
762762
name: i.ident.name,
763763
attrs: lower_attrs(lctx, &i.attrs),
764-
vis: lower_visibility(lctx, i.vis),
764+
vis: lower_visibility(lctx, &i.vis),
765765
defaultness: lower_defaultness(lctx, i.defaultness),
766766
node: match i.node {
767767
ImplItemKind::Const(ref ty, ref expr) => {
@@ -839,7 +839,7 @@ pub fn lower_item(lctx: &LoweringContext, i: &Item) -> hir::Item {
839839
name: i.ident.name,
840840
attrs: lower_attrs(lctx, &i.attrs),
841841
node: node,
842-
vis: lower_visibility(lctx, i.vis),
842+
vis: lower_visibility(lctx, &i.vis),
843843
span: i.span,
844844
}
845845
}
@@ -857,7 +857,7 @@ pub fn lower_foreign_item(lctx: &LoweringContext, i: &ForeignItem) -> hir::Forei
857857
hir::ForeignItemStatic(lower_ty(lctx, t), m)
858858
}
859859
},
860-
vis: lower_visibility(lctx, i.vis),
860+
vis: lower_visibility(lctx, &i.vis),
861861
span: i.span,
862862
}
863863
}
@@ -1706,10 +1706,11 @@ pub fn lower_capture_clause(_lctx: &LoweringContext, c: CaptureBy) -> hir::Captu
17061706
}
17071707
}
17081708

1709-
pub fn lower_visibility(_lctx: &LoweringContext, v: Visibility) -> hir::Visibility {
1710-
match v {
1709+
pub fn lower_visibility(lctx: &LoweringContext, v: &Visibility) -> hir::Visibility {
1710+
match *v {
17111711
Visibility::Public => hir::Public,
17121712
Visibility::Inherited => hir::Inherited,
1713+
_ => panic!(lctx.diagnostic().fatal("pub(restricted) is not implemented yet!"))
17131714
}
17141715
}
17151716

src/libsyntax/ast.rs

+6-13
Original file line numberDiff line numberDiff line change
@@ -1868,21 +1868,14 @@ pub struct PolyTraitRef {
18681868
pub span: Span,
18691869
}
18701870

1871-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1871+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
18721872
pub enum Visibility {
18731873
Public,
1874+
Crate,
1875+
Restricted { path: P<Path>, id: NodeId },
18741876
Inherited,
18751877
}
18761878

1877-
impl Visibility {
1878-
pub fn inherit_from(&self, parent_visibility: Visibility) -> Visibility {
1879-
match *self {
1880-
Visibility::Inherited => parent_visibility,
1881-
Visibility::Public => *self
1882-
}
1883-
}
1884-
}
1885-
18861879
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
18871880
pub struct StructField_ {
18881881
pub kind: StructFieldKind,
@@ -1902,7 +1895,7 @@ impl StructField_ {
19021895

19031896
pub type StructField = Spanned<StructField_>;
19041897

1905-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1898+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
19061899
pub enum StructFieldKind {
19071900
NamedField(Ident, Visibility),
19081901
/// Element of a tuple-like struct
@@ -1917,9 +1910,9 @@ impl StructFieldKind {
19171910
}
19181911
}
19191912

1920-
pub fn visibility(&self) -> Visibility {
1913+
pub fn visibility(&self) -> &Visibility {
19211914
match *self {
1922-
NamedField(_, vis) | UnnamedField(vis) => vis
1915+
NamedField(_, ref vis) | UnnamedField(ref vis) => vis
19231916
}
19241917
}
19251918
}

src/libsyntax/ast_util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ impl<'a, 'v, O: IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> {
247247
FnKind::ItemFn(_, generics, _, _, _, _) => {
248248
self.visit_generics_helper(generics)
249249
}
250-
FnKind::Method(_, sig, _) => {
250+
FnKind::Method(_, ref sig, _) => {
251251
self.visit_generics_helper(&sig.generics)
252252
}
253253
FnKind::Closure => {}

src/libsyntax/fold.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ pub trait Folder : Sized {
288288
noop_fold_where_predicate(where_predicate, self)
289289
}
290290

291+
fn fold_vis(&mut self, vis: Visibility) -> Visibility {
292+
noop_fold_vis(vis, self)
293+
}
294+
291295
fn new_id(&mut self, i: NodeId) -> NodeId {
292296
i
293297
}
@@ -992,7 +996,7 @@ pub fn noop_fold_impl_item<T: Folder>(i: ImplItem, folder: &mut T)
992996
id: folder.new_id(i.id),
993997
ident: folder.fold_ident(i.ident),
994998
attrs: fold_attrs(i.attrs, folder),
995-
vis: i.vis,
999+
vis: folder.fold_vis(i.vis),
9961000
defaultness: i.defaultness,
9971001
node: match i.node {
9981002
ast::ImplItemKind::Const(ty, expr) => {
@@ -1082,7 +1086,7 @@ pub fn noop_fold_item_simple<T: Folder>(Item {id, ident, attrs, node, vis, span}
10821086
ident: folder.fold_ident(ident),
10831087
attrs: fold_attrs(attrs, folder),
10841088
node: node,
1085-
vis: vis,
1089+
vis: folder.fold_vis(vis),
10861090
span: folder.new_span(span)
10871091
}
10881092
}
@@ -1100,7 +1104,7 @@ pub fn noop_fold_foreign_item<T: Folder>(ni: ForeignItem, folder: &mut T) -> For
11001104
ForeignItemKind::Static(folder.fold_ty(t), m)
11011105
}
11021106
},
1103-
vis: ni.vis,
1107+
vis: folder.fold_vis(ni.vis),
11041108
span: folder.new_span(ni.span)
11051109
}
11061110
}
@@ -1391,6 +1395,16 @@ pub fn noop_fold_stmt<T: Folder>(Spanned {node, span}: Stmt, folder: &mut T)
13911395
}
13921396
}
13931397

1398+
pub fn noop_fold_vis<T: Folder>(vis: Visibility, folder: &mut T) -> Visibility {
1399+
match vis {
1400+
Visibility::Restricted { path, id } => Visibility::Restricted {
1401+
path: path.map(|path| folder.fold_path(path)),
1402+
id: folder.new_id(id)
1403+
},
1404+
_ => vis,
1405+
}
1406+
}
1407+
13941408
#[cfg(test)]
13951409
mod tests {
13961410
use std::io;

src/libsyntax/parse/parser.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -3842,7 +3842,7 @@ impl<'a> Parser<'a> {
38423842
attrs: Vec<Attribute> ) -> PResult<'a, StructField> {
38433843
let lo = match pr {
38443844
Visibility::Inherited => self.span.lo,
3845-
Visibility::Public => self.last_span.lo,
3845+
_ => self.last_span.lo,
38463846
};
38473847
let name = self.parse_ident()?;
38483848
self.expect(&token::Colon)?;
@@ -4952,7 +4952,7 @@ impl<'a> Parser<'a> {
49524952
self.commit_expr_expecting(&expr, token::Semi)?;
49534953
(name, ast::ImplItemKind::Const(typ, expr))
49544954
} else {
4955-
let (name, inner_attrs, node) = self.parse_impl_method(vis)?;
4955+
let (name, inner_attrs, node) = self.parse_impl_method(&vis)?;
49564956
attrs.extend(inner_attrs);
49574957
(name, node)
49584958
};
@@ -4968,9 +4968,10 @@ impl<'a> Parser<'a> {
49684968
})
49694969
}
49704970

4971-
fn complain_if_pub_macro(&mut self, visa: Visibility, span: Span) {
4972-
match visa {
4973-
Visibility::Public => {
4971+
fn complain_if_pub_macro(&mut self, visa: &Visibility, span: Span) {
4972+
match *visa {
4973+
Visibility::Inherited => (),
4974+
_ => {
49744975
let is_macro_rules: bool = match self.token {
49754976
token::Ident(sid, _) => sid.name == intern("macro_rules"),
49764977
_ => false,
@@ -4988,12 +4989,11 @@ impl<'a> Parser<'a> {
49884989
.emit();
49894990
}
49904991
}
4991-
Visibility::Inherited => (),
49924992
}
49934993
}
49944994

49954995
/// Parse a method or a macro invocation in a trait impl.
4996-
fn parse_impl_method(&mut self, vis: Visibility)
4996+
fn parse_impl_method(&mut self, vis: &Visibility)
49974997
-> PResult<'a, (Ident, Vec<ast::Attribute>, ast::ImplItemKind)> {
49984998
// code copied from parse_macro_use_or_failure... abstraction!
49994999
if !self.token.is_any_keyword()
@@ -5003,7 +5003,7 @@ impl<'a> Parser<'a> {
50035003
// method macro.
50045004

50055005
let last_span = self.last_span;
5006-
self.complain_if_pub_macro(vis, last_span);
5006+
self.complain_if_pub_macro(&vis, last_span);
50075007

50085008
let lo = self.span.lo;
50095009
let pth = self.parse_path(NoTypesAllowed)?;
@@ -6045,7 +6045,7 @@ impl<'a> Parser<'a> {
60456045
// MACRO INVOCATION ITEM
60466046

60476047
let last_span = self.last_span;
6048-
self.complain_if_pub_macro(visibility, last_span);
6048+
self.complain_if_pub_macro(&visibility, last_span);
60496049

60506050
let mac_lo = self.span.lo;
60516051

@@ -6096,7 +6096,7 @@ impl<'a> Parser<'a> {
60966096
// FAILURE TO PARSE ITEM
60976097
match visibility {
60986098
Visibility::Inherited => {}
6099-
Visibility::Public => {
6099+
_ => {
61006100
let last_span = self.last_span;
61016101
return Err(self.span_fatal(last_span, "unmatched visibility `pub`"));
61026102
}

0 commit comments

Comments
 (0)