Skip to content

Commit 4549003

Browse files
committed
Introduce restrictions to the AST
1 parent 9c0bcb5 commit 4549003

File tree

7 files changed

+116
-5
lines changed

7 files changed

+116
-5
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use rustc_data_structures::tagged_ptr::Tag;
3232
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
3333
pub use rustc_span::AttrId;
3434
use rustc_span::source_map::{Spanned, respan};
35-
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, sym};
35+
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Ident, Span, Symbol, kw, sym};
3636
use thin_vec::{ThinVec, thin_vec};
3737

3838
pub use crate::format::*;
@@ -3316,6 +3316,42 @@ impl VisibilityKind {
33163316
}
33173317
}
33183318

3319+
#[derive(Clone, Encodable, Decodable, Debug)]
3320+
pub struct Restriction {
3321+
pub kind: RestrictionKind,
3322+
pub span: Span,
3323+
pub tokens: Option<LazyAttrTokenStream>,
3324+
}
3325+
3326+
#[derive(Clone, Encodable, Decodable, Debug)]
3327+
pub enum RestrictionKind {
3328+
Unrestricted,
3329+
Restricted { path: P<Path>, id: NodeId, shorthand: bool },
3330+
Implied,
3331+
}
3332+
3333+
impl Restriction {
3334+
pub fn unrestricted() -> Self {
3335+
Restriction { kind: RestrictionKind::Unrestricted, span: DUMMY_SP, tokens: None }
3336+
}
3337+
3338+
pub fn restricted(path: P<Path>, id: NodeId, shorthand: bool) -> Self {
3339+
Restriction {
3340+
kind: RestrictionKind::Restricted { path, id, shorthand },
3341+
span: DUMMY_SP,
3342+
tokens: None,
3343+
}
3344+
}
3345+
3346+
pub fn implied() -> Self {
3347+
Restriction { kind: RestrictionKind::Implied, span: DUMMY_SP, tokens: None }
3348+
}
3349+
3350+
pub fn with_span(self, span: Span) -> Self {
3351+
Restriction { span, ..self }
3352+
}
3353+
}
3354+
33193355
/// Field definition in a struct, variant or union.
33203356
///
33213357
/// E.g., `bar: usize` as in `struct Foo { bar: usize }`.

compiler/rustc_ast/src/ast_traits.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use crate::ptr::P;
99
use crate::tokenstream::LazyAttrTokenStream;
1010
use crate::{
1111
Arm, AssocItem, AttrItem, AttrKind, AttrVec, Attribute, Block, Crate, Expr, ExprField,
12-
FieldDef, ForeignItem, GenericParam, Item, NodeId, Param, Pat, PatField, Path, Stmt, StmtKind,
13-
Ty, Variant, Visibility, WherePredicate,
12+
FieldDef, ForeignItem, GenericParam, Item, NodeId, Param, Pat, PatField, Path, Restriction,
13+
Stmt, StmtKind, Ty, Variant, Visibility, WherePredicate,
1414
};
1515

1616
/// A trait for AST nodes having an ID.
@@ -98,7 +98,19 @@ macro_rules! impl_has_tokens_none {
9898
};
9999
}
100100

101-
impl_has_tokens!(AssocItem, AttrItem, Block, Expr, ForeignItem, Item, Pat, Path, Ty, Visibility);
101+
impl_has_tokens!(
102+
AssocItem,
103+
AttrItem,
104+
Block,
105+
Expr,
106+
ForeignItem,
107+
Item,
108+
Pat,
109+
Path,
110+
Restriction,
111+
Ty,
112+
Visibility
113+
);
102114
impl_has_tokens_none!(
103115
Arm,
104116
ExprField,
@@ -243,7 +255,7 @@ impl_has_attrs!(
243255
Variant,
244256
WherePredicate,
245257
);
246-
impl_has_attrs_none!(Attribute, AttrItem, Block, Pat, Path, Ty, Visibility);
258+
impl_has_attrs_none!(Attribute, AttrItem, Block, Pat, Path, Restriction, Ty, Visibility);
247259

248260
impl<T: HasAttrs> HasAttrs for P<T> {
249261
const SUPPORTS_CUSTOM_INNER_ATTRS: bool = T::SUPPORTS_CUSTOM_INNER_ATTRS;

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,10 @@ pub trait MutVisitor: Sized {
349349
walk_vis(self, vis);
350350
}
351351

352+
fn visit_restriction(&mut self, restriction: &mut Restriction) {
353+
walk_restriction(self, restriction);
354+
}
355+
352356
fn visit_id(&mut self, _id: &mut NodeId) {
353357
// Do nothing.
354358
}
@@ -1774,6 +1778,18 @@ fn walk_vis<T: MutVisitor>(vis: &mut T, visibility: &mut Visibility) {
17741778
vis.visit_span(span);
17751779
}
17761780

1781+
fn walk_restriction<T: MutVisitor>(vis: &mut T, restriction: &mut Restriction) {
1782+
let Restriction { kind, span, tokens: _ } = restriction;
1783+
match kind {
1784+
RestrictionKind::Unrestricted | RestrictionKind::Implied => {}
1785+
RestrictionKind::Restricted { path, id, shorthand: _ } => {
1786+
vis.visit_id(id);
1787+
vis.visit_path(path);
1788+
}
1789+
}
1790+
vis.visit_span(span);
1791+
}
1792+
17771793
fn walk_capture_by<T: MutVisitor>(vis: &mut T, capture_by: &mut CaptureBy) {
17781794
match capture_by {
17791795
CaptureBy::Ref => {}

compiler/rustc_ast/src/visit.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ pub trait Visitor<'ast>: Sized {
277277
fn visit_vis(&mut self, vis: &'ast Visibility) -> Self::Result {
278278
walk_vis(self, vis)
279279
}
280+
fn visit_restriction(&mut self, restriction: &'ast Restriction) -> Self::Result {
281+
walk_restriction(self, restriction)
282+
}
280283
fn visit_fn_ret_ty(&mut self, ret_ty: &'ast FnRetTy) -> Self::Result {
281284
walk_fn_ret_ty(self, ret_ty)
282285
}
@@ -1433,6 +1436,20 @@ pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) -> V::
14331436
V::Result::output()
14341437
}
14351438

1439+
pub fn walk_restriction<'a, V: Visitor<'a>>(
1440+
visitor: &mut V,
1441+
restriction: &'a Restriction,
1442+
) -> V::Result {
1443+
let Restriction { kind, span: _, tokens: _ } = restriction;
1444+
match kind {
1445+
RestrictionKind::Unrestricted | RestrictionKind::Implied => {}
1446+
RestrictionKind::Restricted { path, id, shorthand: _ } => {
1447+
try_visit!(visitor.visit_path(path, *id));
1448+
}
1449+
}
1450+
V::Result::output()
1451+
}
1452+
14361453
pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) -> V::Result {
14371454
let Attribute { kind, id: _, style: _, span: _ } = attr;
14381455
match kind {

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
10711071
Self::to_string(|s| s.print_visibility(v))
10721072
}
10731073

1074+
fn restriction_to_string(&self, kw: &'static str, r: &ast::Restriction) -> String {
1075+
Self::to_string(|s| s.print_restriction(kw, r))
1076+
}
1077+
10741078
fn block_to_string(&self, blk: &ast::Block) -> String {
10751079
Self::to_string(|s| {
10761080
let (cb, ib) = s.head("");

compiler/rustc_ast_pretty/src/pprust/state/item.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,22 @@ impl<'a> State<'a> {
473473
}
474474
}
475475

476+
// FIXME(jhpratt) make `kw` into a const generic once permitted
477+
pub(crate) fn print_restriction(&mut self, kw: &'static str, restriction: &ast::Restriction) {
478+
match restriction.kind {
479+
ast::RestrictionKind::Unrestricted => self.word_nbsp(kw),
480+
ast::RestrictionKind::Restricted { ref path, shorthand, id: _ } => {
481+
let path = Self::to_string(|s| s.print_path(path, false, 0));
482+
if shorthand {
483+
self.word_nbsp(format!("{kw}({path})"))
484+
} else {
485+
self.word_nbsp(format!("{kw}(in {path})"))
486+
}
487+
}
488+
ast::RestrictionKind::Implied => {}
489+
}
490+
}
491+
476492
fn print_defaultness(&mut self, defaultness: ast::Defaultness) {
477493
if let ast::Defaultness::Default(_) = defaultness {
478494
self.word_nbsp("default");

tests/ui/macros/stringify.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,16 @@ fn test_vis() {
793793
// Attributes are not allowed on visibilities.
794794
}
795795

796+
#[test]
797+
fn test_impl_restriction() {
798+
assert_eq!(stringify!(pub impl(crate) trait Foo {}), "pub impl(crate) trait Foo {}");
799+
assert_eq!(stringify!(pub impl(in crate) trait Foo {}), "pub impl(in crate) trait Foo {}");
800+
assert_eq!(stringify!(pub impl(super) trait Foo {}), "pub impl(super) trait Foo {}");
801+
assert_eq!(stringify!(pub impl(in super) trait Foo {}), "pub impl(in super) trait Foo {}");
802+
assert_eq!(stringify!(pub impl(self) trait Foo {}), "pub impl(self) trait Foo {}");
803+
assert_eq!(stringify!(pub impl(in self) trait Foo {}), "pub impl(in self) trait Foo {}");
804+
}
805+
796806
macro_rules! p {
797807
([$($tt:tt)*], $s:literal) => {
798808
assert_eq!(stringify!($($tt)*), $s);

0 commit comments

Comments
 (0)