diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2019-11-23 16:54:24 +0300 |
|---|---|---|
| committer | Mark Rousskov <mark.simulacrum@gmail.com> | 2019-12-14 10:51:51 -0500 |
| commit | a17c556bd088570c5c742eee8d36838dfa874878 (patch) | |
| tree | e70209d64f208d6e43d7db1a6590b027ae234aeb | |
| parent | e49abf25d48453cdb295e81add198331cba2d797 (diff) | |
| download | rust-a17c556bd088570c5c742eee8d36838dfa874878.tar.gz rust-a17c556bd088570c5c742eee8d36838dfa874878.zip | |
expand: Fully preserve visibilities on unnamed fields with attributes
| -rw-r--r-- | src/librustc_resolve/build_reduced_graph.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax_expand/expand.rs | 22 | ||||
| -rw-r--r-- | src/libsyntax_expand/placeholders.rs | 5 | ||||
| -rw-r--r-- | src/test/ui/attributes/unnamed-field-attributes-vis.rs | 11 |
4 files changed, 35 insertions, 5 deletions
diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index 55d2345f200..1bef810b240 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -755,7 +755,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { // NOTE: The field may be an expansion placeholder, but expansion sets correct // visibilities for unnamed field placeholders specifically, so the constructor // visibility should still be determined correctly. - let field_vis = self.resolve_visibility_speculative(&field.vis, true); + let field_vis = self.resolve_visibility(&field.vis); if ctor_vis.is_at_least(field_vis, &*self.r) { ctor_vis = field_vis; } diff --git a/src/libsyntax_expand/expand.rs b/src/libsyntax_expand/expand.rs index bdb50dbfb4f..c96b4a93dbd 100644 --- a/src/libsyntax_expand/expand.rs +++ b/src/libsyntax_expand/expand.rs @@ -85,7 +85,7 @@ macro_rules! ast_fragments { // mention some macro variable from those arguments even if it's not used. #[cfg_attr(bootstrap, allow(unused_macros))] macro _repeating($flat_map_ast_elt) {} - placeholder(AstFragmentKind::$Kind, *id).$make_ast() + placeholder(AstFragmentKind::$Kind, *id, None).$make_ast() })),)?)* _ => panic!("unexpected AST fragment kind") } @@ -274,6 +274,23 @@ pub enum InvocationKind { }, } +impl InvocationKind { + fn placeholder_visibility(&self) -> Option<ast::Visibility> { + // HACK: For unnamed fields placeholders should have the same visibility as the actual + // fields because for tuple structs/variants resolve determines visibilities of their + // constructor using these field visibilities before attributes on them are are expanded. + // The assumption is that the attribute expansion cannot change field visibilities, + // and it holds because only inert attributes are supported in this position. + match self { + InvocationKind::Attr { item: Annotatable::StructField(field), .. } | + InvocationKind::Derive { item: Annotatable::StructField(field), .. } | + InvocationKind::DeriveContainer { item: Annotatable::StructField(field), .. } + if field.ident.is_none() => Some(field.vis.clone()), + _ => None, + } + } +} + impl Invocation { pub fn span(&self) -> Span { match &self.kind { @@ -941,6 +958,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { _ => None, }; let expn_id = ExpnId::fresh(expn_data); + let vis = kind.placeholder_visibility(); self.invocations.push(Invocation { kind, fragment_kind, @@ -950,7 +968,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { ..self.cx.current_expansion.clone() }, }); - placeholder(fragment_kind, NodeId::placeholder_from_expn_id(expn_id)) + placeholder(fragment_kind, NodeId::placeholder_from_expn_id(expn_id), vis) } fn collect_bang(&mut self, mac: ast::Mac, span: Span, kind: AstFragmentKind) -> AstFragment { diff --git a/src/libsyntax_expand/placeholders.rs b/src/libsyntax_expand/placeholders.rs index e595888dae7..2d2c2cbc834 100644 --- a/src/libsyntax_expand/placeholders.rs +++ b/src/libsyntax_expand/placeholders.rs @@ -12,7 +12,8 @@ use smallvec::{smallvec, SmallVec}; use rustc_data_structures::fx::FxHashMap; -pub fn placeholder(kind: AstFragmentKind, id: ast::NodeId) -> AstFragment { +pub fn placeholder(kind: AstFragmentKind, id: ast::NodeId, vis: Option<ast::Visibility>) + -> AstFragment { fn mac_placeholder() -> ast::Mac { ast::Mac { path: ast::Path { span: DUMMY_SP, segments: Vec::new() }, @@ -26,7 +27,7 @@ pub fn placeholder(kind: AstFragmentKind, id: ast::NodeId) -> AstFragment { let ident = ast::Ident::invalid(); let attrs = Vec::new(); let generics = ast::Generics::default(); - let vis = dummy_spanned(ast::VisibilityKind::Inherited); + let vis = vis.unwrap_or_else(|| dummy_spanned(ast::VisibilityKind::Inherited)); let span = DUMMY_SP; let expr_placeholder = || P(ast::Expr { id, span, diff --git a/src/test/ui/attributes/unnamed-field-attributes-vis.rs b/src/test/ui/attributes/unnamed-field-attributes-vis.rs new file mode 100644 index 00000000000..d12155f6d81 --- /dev/null +++ b/src/test/ui/attributes/unnamed-field-attributes-vis.rs @@ -0,0 +1,11 @@ +// Unnamed fields don't lose their visibility due to non-builtin attributes on them. + +// check-pass + +mod m { + pub struct S(#[rustfmt::skip] pub u8); +} + +fn main() { + m::S(0); +} |
