Skip to content

print vis & defaultness for nested items #69334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 33 additions & 21 deletions src/librustc_ast_pretty/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::pp::Breaks::{Consistent, Inconsistent};
use crate::pp::{self, Breaks};

use rustc_span::edition::Edition;
use rustc_span::source_map::{dummy_spanned, SourceMap, Spanned};
use rustc_span::source_map::{SourceMap, Spanned};
use rustc_span::symbol::{kw, sym};
use rustc_span::{BytePos, FileName, Span};
use syntax::ast::{self, BlockCheckMode, PatKind, RangeEnd, RangeSyntax};
Expand Down Expand Up @@ -1026,27 +1026,26 @@ impl<'a> State<'a> {
span: Span,
ident: ast::Ident,
attrs: &[Attribute],
defaultness: ast::Defaultness,
def: ast::Defaultness,
kind: &ast::AssocItemKind,
vis: &ast::Visibility,
) {
self.ann.pre(self, AnnNode::SubItem(id));
self.hardbreak_if_not_bol();
self.maybe_print_comment(span.lo());
self.print_outer_attributes(attrs);
self.print_defaultness(defaultness);
match kind {
ast::ForeignItemKind::Fn(sig, gen, body) => {
self.print_fn_full(sig, ident, gen, vis, body.as_deref(), attrs);
self.print_fn_full(sig, ident, gen, vis, def, body.as_deref(), attrs);
}
ast::ForeignItemKind::Const(ty, body) => {
self.print_item_const(ident, None, ty, body.as_deref(), vis);
self.print_item_const(ident, None, ty, body.as_deref(), vis, def);
}
ast::ForeignItemKind::Static(ty, mutbl, body) => {
self.print_item_const(ident, Some(*mutbl), ty, body.as_deref(), vis);
self.print_item_const(ident, Some(*mutbl), ty, body.as_deref(), vis, def);
}
ast::ForeignItemKind::TyAlias(generics, bounds, ty) => {
self.print_associated_type(ident, generics, bounds, ty.as_deref());
self.print_associated_type(ident, generics, bounds, ty.as_deref(), vis, def);
}
ast::ForeignItemKind::Macro(m) => {
self.print_mac(m);
Expand All @@ -1065,13 +1064,17 @@ impl<'a> State<'a> {
ty: &ast::Ty,
body: Option<&ast::Expr>,
vis: &ast::Visibility,
defaultness: ast::Defaultness,
) {
self.head("");
self.print_visibility(vis);
self.print_defaultness(defaultness);
let leading = match mutbl {
None => "const",
Some(ast::Mutability::Not) => "static",
Some(ast::Mutability::Mut) => "static mut",
};
self.head(visibility_qualified(vis, leading));
self.word_space(leading);
self.print_ident(ident);
self.word_space(":");
self.print_type(ty);
Expand All @@ -1091,7 +1094,12 @@ impl<'a> State<'a> {
generics: &ast::Generics,
bounds: &ast::GenericBounds,
ty: Option<&ast::Ty>,
vis: &ast::Visibility,
defaultness: ast::Defaultness,
) {
self.head("");
self.print_visibility(vis);
self.print_defaultness(defaultness);
self.word_space("type");
self.print_ident(ident);
self.print_generic_params(&generics.params);
Expand All @@ -1102,7 +1110,9 @@ impl<'a> State<'a> {
self.word_space("=");
self.print_type(ty);
}
self.s.word(";")
self.s.word(";");
self.end(); // end inner head-block
self.end(); // end outer head-block
}

/// Pretty-prints an item.
Expand Down Expand Up @@ -1133,13 +1143,17 @@ impl<'a> State<'a> {
self.end(); // end outer head-block
}
ast::ItemKind::Static(ref ty, mutbl, ref body) => {
self.print_item_const(item.ident, Some(mutbl), ty, body.as_deref(), &item.vis);
let def = ast::Defaultness::Final;
self.print_item_const(item.ident, Some(mutbl), ty, body.as_deref(), &item.vis, def);
}
ast::ItemKind::Const(ref ty, ref body) => {
self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis);
let def = ast::Defaultness::Final;
self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis, def);
}
ast::ItemKind::Fn(ref sig, ref gen, ref body) => {
self.print_fn_full(sig, item.ident, gen, &item.vis, body.as_deref(), &item.attrs);
let def = ast::Defaultness::Final;
let body = body.as_deref();
self.print_fn_full(sig, item.ident, gen, &item.vis, def, body, &item.attrs);
}
ast::ItemKind::Mod(ref _mod) => {
self.head(visibility_qualified(&item.vis, "mod"));
Expand Down Expand Up @@ -2370,13 +2384,16 @@ impl<'a> State<'a> {
name: ast::Ident,
generics: &ast::Generics,
vis: &ast::Visibility,
defaultness: ast::Defaultness,
body: Option<&ast::Block>,
attrs: &[ast::Attribute],
) {
if body.is_some() {
self.head("");
}
self.print_fn(&sig.decl, sig.header, Some(name), generics, vis);
self.print_visibility(vis);
self.print_defaultness(defaultness);
self.print_fn(&sig.decl, sig.header, Some(name), generics);
if let Some(body) = body {
self.nbsp();
self.print_block_with_attrs(body, attrs);
Expand All @@ -2391,10 +2408,8 @@ impl<'a> State<'a> {
header: ast::FnHeader,
name: Option<ast::Ident>,
generics: &ast::Generics,
vis: &ast::Visibility,
) {
self.print_fn_header_info(header, vis);

self.print_fn_header_info(header);
if let Some(name) = name {
self.nbsp();
self.print_ident(name);
Expand Down Expand Up @@ -2672,8 +2687,7 @@ impl<'a> State<'a> {
span: rustc_span::DUMMY_SP,
};
let header = ast::FnHeader { unsafety, ext, ..ast::FnHeader::default() };
let vis = dummy_spanned(ast::VisibilityKind::Inherited);
self.print_fn(decl, header, name, &generics, &vis);
self.print_fn(decl, header, name, &generics);
self.end();
}

Expand All @@ -2700,9 +2714,7 @@ impl<'a> State<'a> {
}
}

crate fn print_fn_header_info(&mut self, header: ast::FnHeader, vis: &ast::Visibility) {
self.s.word(visibility_qualified(vis, ""));

crate fn print_fn_header_info(&mut self, header: ast::FnHeader) {
self.print_constness(header.constness);
self.print_asyncness(header.asyncness);
self.print_unsafety(header.unsafety);
Expand Down
10 changes: 2 additions & 8 deletions src/librustc_ast_pretty/pprust/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;

use rustc_span;
use rustc_span::source_map::{dummy_spanned, respan};
use rustc_span::source_map::respan;
use syntax::ast;
use syntax::with_default_globals;

Expand All @@ -13,13 +13,7 @@ fn fun_to_string(
) -> String {
to_string(|s| {
s.head("");
s.print_fn(
decl,
header,
Some(name),
generics,
&dummy_spanned(ast::VisibilityKind::Inherited),
);
s.print_fn(decl, header, Some(name), generics);
s.end(); // Close the head box.
s.end(); // Close the outer box.
})
Expand Down
25 changes: 0 additions & 25 deletions src/test/pretty/gat-bounds.pp

This file was deleted.

1 change: 0 additions & 1 deletion src/test/pretty/gat-bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// See issue #67509.

// pretty-compare-only
// pp-exact:gat-bounds.pp

#![feature(generic_associated_types)]

Expand Down
47 changes: 47 additions & 0 deletions src/test/pretty/nested-item-vis-defaultness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Check that nested items have their visibility and `default`nesses in the right order.

// pp-exact

fn main() { }

#[cfg(FALSE)]
extern "C" {
static X: u8 ;
type X;
fn foo();
pub static X: u8 ;
pub type X;
pub fn foo();
}

#[cfg(FALSE)]
trait T {
const X: u8 ;
type X;
fn foo();
default const X: u8 ;
default type X;
default fn foo();
pub const X: u8 ;
pub type X;
pub fn foo();
pub default const X: u8 ;
pub default type X;
pub default fn foo();
}

#[cfg(FALSE)]
impl T for S {
const X: u8 ;
type X;
fn foo();
default const X: u8 ;
default type X;
default fn foo();
pub const X: u8 ;
pub type X;
pub fn foo();
pub default const X: u8 ;
pub default type X;
pub default fn foo();
}