Skip to content

Commit c101116

Browse files
committed
Attach TokenStream to ast::Visibility
A `Visibility` does not have outer attributes, so we only capture tokens when parsing a `macro_rules!` matcher
1 parent 55082ce commit c101116

File tree

22 files changed

+120
-50
lines changed

22 files changed

+120
-50
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2490,7 +2490,12 @@ pub enum CrateSugar {
24902490
JustCrate,
24912491
}
24922492

2493-
pub type Visibility = Spanned<VisibilityKind>;
2493+
#[derive(Clone, Encodable, Decodable, Debug)]
2494+
pub struct Visibility {
2495+
pub kind: VisibilityKind,
2496+
pub span: Span,
2497+
pub tokens: Option<TokenStream>,
2498+
}
24942499

24952500
#[derive(Clone, Encodable, Decodable, Debug)]
24962501
pub enum VisibilityKind {

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::tokenstream::*;
1414

1515
use rustc_data_structures::map_in_place::MapInPlace;
1616
use rustc_data_structures::sync::Lrc;
17-
use rustc_span::source_map::{respan, Spanned};
17+
use rustc_span::source_map::Spanned;
1818
use rustc_span::symbol::Ident;
1919
use rustc_span::Span;
2020

@@ -978,11 +978,13 @@ pub fn noop_visit_mod<T: MutVisitor>(module: &mut Mod, vis: &mut T) {
978978

979979
pub fn noop_visit_crate<T: MutVisitor>(krate: &mut Crate, vis: &mut T) {
980980
visit_clobber(krate, |Crate { module, attrs, span, proc_macros }| {
981+
let item_vis =
982+
Visibility { kind: VisibilityKind::Public, span: span.shrink_to_lo(), tokens: None };
981983
let item = P(Item {
982984
ident: Ident::invalid(),
983985
attrs,
984986
id: DUMMY_NODE_ID,
985-
vis: respan(span.shrink_to_lo(), VisibilityKind::Public),
987+
vis: item_vis,
986988
span,
987989
kind: ItemKind::Mod(module),
988990
tokens: None,
@@ -1314,13 +1316,13 @@ pub fn noop_flat_map_stmt_kind<T: MutVisitor>(
13141316
}
13151317
}
13161318

1317-
pub fn noop_visit_vis<T: MutVisitor>(Spanned { node, span }: &mut Visibility, vis: &mut T) {
1318-
match node {
1319+
pub fn noop_visit_vis<T: MutVisitor>(visibility: &mut Visibility, vis: &mut T) {
1320+
match &mut visibility.kind {
13191321
VisibilityKind::Public | VisibilityKind::Crate(_) | VisibilityKind::Inherited => {}
13201322
VisibilityKind::Restricted { path, id } => {
13211323
vis.visit_path(path);
13221324
vis.visit_id(id);
13231325
}
13241326
}
1325-
vis.visit_span(span);
1327+
vis.visit_span(&mut visibility.span);
13261328
}

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
879879
}
880880

881881
pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
882-
if let VisibilityKind::Restricted { ref path, id } = vis.node {
882+
if let VisibilityKind::Restricted { ref path, id } = vis.kind {
883883
visitor.visit_path(path, id);
884884
}
885885
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
930930
v: &Visibility,
931931
explicit_owner: Option<NodeId>,
932932
) -> hir::Visibility<'hir> {
933-
let node = match v.node {
933+
let node = match v.kind {
934934
VisibilityKind::Public => hir::VisibilityKind::Public,
935935
VisibilityKind::Crate(sugar) => hir::VisibilityKind::Crate(sugar),
936936
VisibilityKind::Restricted { ref path, id } => {

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,13 @@ impl<'a> AstValidator<'a> {
198198
}
199199

200200
fn invalid_visibility(&self, vis: &Visibility, note: Option<&str>) {
201-
if let VisibilityKind::Inherited = vis.node {
201+
if let VisibilityKind::Inherited = vis.kind {
202202
return;
203203
}
204204

205205
let mut err =
206206
struct_span_err!(self.session, vis.span, E0449, "unnecessary visibility qualifier");
207-
if vis.node.is_pub() {
207+
if vis.kind.is_pub() {
208208
err.span_label(vis.span, "`pub` not permitted here because it's implied");
209209
}
210210
if let Some(note) = note {

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
594594
}
595595

596596
fn visit_vis(&mut self, vis: &'a ast::Visibility) {
597-
if let ast::VisibilityKind::Crate(ast::CrateSugar::JustCrate) = vis.node {
597+
if let ast::VisibilityKind::Crate(ast::CrateSugar::JustCrate) = vis.kind {
598598
gate_feature_post!(
599599
&self,
600600
crate_visibility_modifier,

compiler/rustc_ast_pretty/src/pprust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,7 @@ impl<'a> State<'a> {
13591359
}
13601360

13611361
crate fn print_visibility(&mut self, vis: &ast::Visibility) {
1362-
match vis.node {
1362+
match vis.kind {
13631363
ast::VisibilityKind::Public => self.word_nbsp("pub"),
13641364
ast::VisibilityKind::Crate(sugar) => match sugar {
13651365
ast::CrateSugar::PubCrate => self.word_nbsp("pub(crate)"),

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ use rustc_ast::{GenericArg, GenericParamKind, VariantData};
187187
use rustc_attr as attr;
188188
use rustc_data_structures::map_in_place::MapInPlace;
189189
use rustc_expand::base::{Annotatable, ExtCtxt};
190-
use rustc_span::source_map::respan;
191190
use rustc_span::symbol::{kw, sym, Ident, Symbol};
192191
use rustc_span::Span;
193192

@@ -532,7 +531,11 @@ impl<'a> TraitDef<'a> {
532531
id: ast::DUMMY_NODE_ID,
533532
span: self.span,
534533
ident,
535-
vis: respan(self.span.shrink_to_lo(), ast::VisibilityKind::Inherited),
534+
vis: ast::Visibility {
535+
span: self.span.shrink_to_lo(),
536+
kind: ast::VisibilityKind::Inherited,
537+
tokens: None,
538+
},
536539
attrs: Vec::new(),
537540
kind: ast::AssocItemKind::TyAlias(
538541
ast::Defaultness::Final,
@@ -933,7 +936,11 @@ impl<'a> MethodDef<'a> {
933936
id: ast::DUMMY_NODE_ID,
934937
attrs: self.attributes.clone(),
935938
span: trait_.span,
936-
vis: respan(trait_lo_sp, ast::VisibilityKind::Inherited),
939+
vis: ast::Visibility {
940+
span: trait_lo_sp,
941+
kind: ast::VisibilityKind::Inherited,
942+
tokens: None,
943+
},
937944
ident: method_ident,
938945
kind: ast::AssocItemKind::Fn(def, sig, fn_generics, Some(body_block)),
939946
tokens: None,

compiler/rustc_builtin_macros/src/global_asm.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use rustc_ast::token;
1414
use rustc_ast::tokenstream::TokenStream;
1515
use rustc_errors::DiagnosticBuilder;
1616
use rustc_expand::base::{self, *};
17-
use rustc_span::source_map::respan;
1817
use rustc_span::symbol::Ident;
1918
use rustc_span::Span;
2019
use smallvec::smallvec;
@@ -30,7 +29,11 @@ pub fn expand_global_asm<'cx>(
3029
attrs: Vec::new(),
3130
id: ast::DUMMY_NODE_ID,
3231
kind: ast::ItemKind::GlobalAsm(P(global_asm)),
33-
vis: respan(sp.shrink_to_lo(), ast::VisibilityKind::Inherited),
32+
vis: ast::Visibility {
33+
span: sp.shrink_to_lo(),
34+
kind: ast::VisibilityKind::Inherited,
35+
tokens: None,
36+
},
3437
span: cx.with_def_site_ctxt(sp),
3538
tokens: None,
3639
})]),

compiler/rustc_builtin_macros/src/proc_macro_harness.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub fn inject(
9898

9999
impl<'a> CollectProcMacros<'a> {
100100
fn check_not_pub_in_root(&self, vis: &ast::Visibility, sp: Span) {
101-
if self.is_proc_macro_crate && self.in_root && vis.node.is_pub() {
101+
if self.is_proc_macro_crate && self.in_root && vis.kind.is_pub() {
102102
self.handler.span_err(
103103
sp,
104104
"`proc-macro` crate types currently cannot export any items other \
@@ -184,7 +184,7 @@ impl<'a> CollectProcMacros<'a> {
184184
Vec::new()
185185
};
186186

187-
if self.in_root && item.vis.node.is_pub() {
187+
if self.in_root && item.vis.kind.is_pub() {
188188
self.macros.push(ProcMacro::Derive(ProcMacroDerive {
189189
id: item.id,
190190
span: item.span,
@@ -204,7 +204,7 @@ impl<'a> CollectProcMacros<'a> {
204204
}
205205

206206
fn collect_attr_proc_macro(&mut self, item: &'a ast::Item) {
207-
if self.in_root && item.vis.node.is_pub() {
207+
if self.in_root && item.vis.kind.is_pub() {
208208
self.macros.push(ProcMacro::Def(ProcMacroDef {
209209
id: item.id,
210210
span: item.span,
@@ -223,7 +223,7 @@ impl<'a> CollectProcMacros<'a> {
223223
}
224224

225225
fn collect_bang_proc_macro(&mut self, item: &'a ast::Item) {
226-
if self.in_root && item.vis.node.is_pub() {
226+
if self.in_root && item.vis.kind.is_pub() {
227227
self.macros.push(ProcMacro::Def(ProcMacroDef {
228228
id: item.id,
229229
span: item.span,

0 commit comments

Comments
 (0)