Skip to content

Commit a82d3cf

Browse files
committed
Auto merge of #69334 - Centril:nested-item-vis-def, r=petrochenkov
print vis & defaultness for nested items Fixes #69315 which was injected by #69194. r? @petrochenkov cc @alexcrichton
2 parents 436494b + 14442e0 commit a82d3cf

File tree

5 files changed

+82
-55
lines changed

5 files changed

+82
-55
lines changed

src/librustc_ast_pretty/pprust.rs

+33-21
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::pp::Breaks::{Consistent, Inconsistent};
22
use crate::pp::{self, Breaks};
33

44
use rustc_span::edition::Edition;
5-
use rustc_span::source_map::{dummy_spanned, SourceMap, Spanned};
5+
use rustc_span::source_map::{SourceMap, Spanned};
66
use rustc_span::symbol::{kw, sym};
77
use rustc_span::{BytePos, FileName, Span};
88
use syntax::ast::{self, BlockCheckMode, PatKind, RangeEnd, RangeSyntax};
@@ -1026,27 +1026,26 @@ impl<'a> State<'a> {
10261026
span: Span,
10271027
ident: ast::Ident,
10281028
attrs: &[Attribute],
1029-
defaultness: ast::Defaultness,
1029+
def: ast::Defaultness,
10301030
kind: &ast::AssocItemKind,
10311031
vis: &ast::Visibility,
10321032
) {
10331033
self.ann.pre(self, AnnNode::SubItem(id));
10341034
self.hardbreak_if_not_bol();
10351035
self.maybe_print_comment(span.lo());
10361036
self.print_outer_attributes(attrs);
1037-
self.print_defaultness(defaultness);
10381037
match kind {
10391038
ast::ForeignItemKind::Fn(sig, gen, body) => {
1040-
self.print_fn_full(sig, ident, gen, vis, body.as_deref(), attrs);
1039+
self.print_fn_full(sig, ident, gen, vis, def, body.as_deref(), attrs);
10411040
}
10421041
ast::ForeignItemKind::Const(ty, body) => {
1043-
self.print_item_const(ident, None, ty, body.as_deref(), vis);
1042+
self.print_item_const(ident, None, ty, body.as_deref(), vis, def);
10441043
}
10451044
ast::ForeignItemKind::Static(ty, mutbl, body) => {
1046-
self.print_item_const(ident, Some(*mutbl), ty, body.as_deref(), vis);
1045+
self.print_item_const(ident, Some(*mutbl), ty, body.as_deref(), vis, def);
10471046
}
10481047
ast::ForeignItemKind::TyAlias(generics, bounds, ty) => {
1049-
self.print_associated_type(ident, generics, bounds, ty.as_deref());
1048+
self.print_associated_type(ident, generics, bounds, ty.as_deref(), vis, def);
10501049
}
10511050
ast::ForeignItemKind::Macro(m) => {
10521051
self.print_mac(m);
@@ -1065,13 +1064,17 @@ impl<'a> State<'a> {
10651064
ty: &ast::Ty,
10661065
body: Option<&ast::Expr>,
10671066
vis: &ast::Visibility,
1067+
defaultness: ast::Defaultness,
10681068
) {
1069+
self.head("");
1070+
self.print_visibility(vis);
1071+
self.print_defaultness(defaultness);
10691072
let leading = match mutbl {
10701073
None => "const",
10711074
Some(ast::Mutability::Not) => "static",
10721075
Some(ast::Mutability::Mut) => "static mut",
10731076
};
1074-
self.head(visibility_qualified(vis, leading));
1077+
self.word_space(leading);
10751078
self.print_ident(ident);
10761079
self.word_space(":");
10771080
self.print_type(ty);
@@ -1091,7 +1094,12 @@ impl<'a> State<'a> {
10911094
generics: &ast::Generics,
10921095
bounds: &ast::GenericBounds,
10931096
ty: Option<&ast::Ty>,
1097+
vis: &ast::Visibility,
1098+
defaultness: ast::Defaultness,
10941099
) {
1100+
self.head("");
1101+
self.print_visibility(vis);
1102+
self.print_defaultness(defaultness);
10951103
self.word_space("type");
10961104
self.print_ident(ident);
10971105
self.print_generic_params(&generics.params);
@@ -1102,7 +1110,9 @@ impl<'a> State<'a> {
11021110
self.word_space("=");
11031111
self.print_type(ty);
11041112
}
1105-
self.s.word(";")
1113+
self.s.word(";");
1114+
self.end(); // end inner head-block
1115+
self.end(); // end outer head-block
11061116
}
11071117

11081118
/// Pretty-prints an item.
@@ -1133,13 +1143,17 @@ impl<'a> State<'a> {
11331143
self.end(); // end outer head-block
11341144
}
11351145
ast::ItemKind::Static(ref ty, mutbl, ref body) => {
1136-
self.print_item_const(item.ident, Some(mutbl), ty, body.as_deref(), &item.vis);
1146+
let def = ast::Defaultness::Final;
1147+
self.print_item_const(item.ident, Some(mutbl), ty, body.as_deref(), &item.vis, def);
11371148
}
11381149
ast::ItemKind::Const(ref ty, ref body) => {
1139-
self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis);
1150+
let def = ast::Defaultness::Final;
1151+
self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis, def);
11401152
}
11411153
ast::ItemKind::Fn(ref sig, ref gen, ref body) => {
1142-
self.print_fn_full(sig, item.ident, gen, &item.vis, body.as_deref(), &item.attrs);
1154+
let def = ast::Defaultness::Final;
1155+
let body = body.as_deref();
1156+
self.print_fn_full(sig, item.ident, gen, &item.vis, def, body, &item.attrs);
11431157
}
11441158
ast::ItemKind::Mod(ref _mod) => {
11451159
self.head(visibility_qualified(&item.vis, "mod"));
@@ -2370,13 +2384,16 @@ impl<'a> State<'a> {
23702384
name: ast::Ident,
23712385
generics: &ast::Generics,
23722386
vis: &ast::Visibility,
2387+
defaultness: ast::Defaultness,
23732388
body: Option<&ast::Block>,
23742389
attrs: &[ast::Attribute],
23752390
) {
23762391
if body.is_some() {
23772392
self.head("");
23782393
}
2379-
self.print_fn(&sig.decl, sig.header, Some(name), generics, vis);
2394+
self.print_visibility(vis);
2395+
self.print_defaultness(defaultness);
2396+
self.print_fn(&sig.decl, sig.header, Some(name), generics);
23802397
if let Some(body) = body {
23812398
self.nbsp();
23822399
self.print_block_with_attrs(body, attrs);
@@ -2391,10 +2408,8 @@ impl<'a> State<'a> {
23912408
header: ast::FnHeader,
23922409
name: Option<ast::Ident>,
23932410
generics: &ast::Generics,
2394-
vis: &ast::Visibility,
23952411
) {
2396-
self.print_fn_header_info(header, vis);
2397-
2412+
self.print_fn_header_info(header);
23982413
if let Some(name) = name {
23992414
self.nbsp();
24002415
self.print_ident(name);
@@ -2672,8 +2687,7 @@ impl<'a> State<'a> {
26722687
span: rustc_span::DUMMY_SP,
26732688
};
26742689
let header = ast::FnHeader { unsafety, ext, ..ast::FnHeader::default() };
2675-
let vis = dummy_spanned(ast::VisibilityKind::Inherited);
2676-
self.print_fn(decl, header, name, &generics, &vis);
2690+
self.print_fn(decl, header, name, &generics);
26772691
self.end();
26782692
}
26792693

@@ -2700,9 +2714,7 @@ impl<'a> State<'a> {
27002714
}
27012715
}
27022716

2703-
crate fn print_fn_header_info(&mut self, header: ast::FnHeader, vis: &ast::Visibility) {
2704-
self.s.word(visibility_qualified(vis, ""));
2705-
2717+
crate fn print_fn_header_info(&mut self, header: ast::FnHeader) {
27062718
self.print_constness(header.constness);
27072719
self.print_asyncness(header.asyncness);
27082720
self.print_unsafety(header.unsafety);

src/librustc_ast_pretty/pprust/tests.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::*;
22

33
use rustc_span;
4-
use rustc_span::source_map::{dummy_spanned, respan};
4+
use rustc_span::source_map::respan;
55
use syntax::ast;
66
use syntax::with_default_globals;
77

@@ -13,13 +13,7 @@ fn fun_to_string(
1313
) -> String {
1414
to_string(|s| {
1515
s.head("");
16-
s.print_fn(
17-
decl,
18-
header,
19-
Some(name),
20-
generics,
21-
&dummy_spanned(ast::VisibilityKind::Inherited),
22-
);
16+
s.print_fn(decl, header, Some(name), generics);
2317
s.end(); // Close the head box.
2418
s.end(); // Close the outer box.
2519
})

src/test/pretty/gat-bounds.pp

-25
This file was deleted.

src/test/pretty/gat-bounds.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// See issue #67509.
33

44
// pretty-compare-only
5-
// pp-exact:gat-bounds.pp
65

76
#![feature(generic_associated_types)]
87

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Check that nested items have their visibility and `default`nesses in the right order.
2+
3+
// pp-exact
4+
5+
fn main() { }
6+
7+
#[cfg(FALSE)]
8+
extern "C" {
9+
static X: u8 ;
10+
type X;
11+
fn foo();
12+
pub static X: u8 ;
13+
pub type X;
14+
pub fn foo();
15+
}
16+
17+
#[cfg(FALSE)]
18+
trait T {
19+
const X: u8 ;
20+
type X;
21+
fn foo();
22+
default const X: u8 ;
23+
default type X;
24+
default fn foo();
25+
pub const X: u8 ;
26+
pub type X;
27+
pub fn foo();
28+
pub default const X: u8 ;
29+
pub default type X;
30+
pub default fn foo();
31+
}
32+
33+
#[cfg(FALSE)]
34+
impl T for S {
35+
const X: u8 ;
36+
type X;
37+
fn foo();
38+
default const X: u8 ;
39+
default type X;
40+
default fn foo();
41+
pub const X: u8 ;
42+
pub type X;
43+
pub fn foo();
44+
pub default const X: u8 ;
45+
pub default type X;
46+
pub default fn foo();
47+
}

0 commit comments

Comments
 (0)