]>
Commit | Line | Data |
---|---|---|
ba9703b0 XL |
1 | #![feature(or_patterns)] |
2 | #![recursion_limit = "256"] | |
3 | ||
3dfed10e | 4 | use rustc_ast as ast; |
74b04a01 XL |
5 | use rustc_ast::util::parser::{self, AssocOp, Fixity}; |
6 | use rustc_ast_pretty::pp::Breaks::{Consistent, Inconsistent}; | |
7 | use rustc_ast_pretty::pp::{self, Breaks}; | |
8 | use rustc_ast_pretty::pprust::{Comments, PrintState}; | |
ba9703b0 XL |
9 | use rustc_hir as hir; |
10 | use rustc_hir::{GenericArg, GenericParam, GenericParamKind, Node}; | |
11 | use rustc_hir::{GenericBound, PatKind, RangeEnd, TraitBoundModifier}; | |
dfeec247 | 12 | use rustc_span::source_map::{SourceMap, Spanned}; |
f9f354fc | 13 | use rustc_span::symbol::{kw, Ident, IdentPrinter, Symbol}; |
dfeec247 | 14 | use rustc_span::{self, BytePos, FileName}; |
83c7162d | 15 | use rustc_target::spec::abi::Abi; |
e9174d1e | 16 | |
a1dfa0c6 | 17 | use std::borrow::Cow; |
32a655c1 | 18 | use std::cell::Cell; |
6a06907d | 19 | use std::collections::BTreeMap; |
041b39d2 | 20 | use std::vec; |
e9174d1e | 21 | |
ba9703b0 XL |
22 | pub fn id_to_string(map: &dyn rustc_hir::intravisit::Map<'_>, hir_id: hir::HirId) -> String { |
23 | to_string(&map, |s| s.print_node(map.find(hir_id).unwrap())) | |
24 | } | |
25 | ||
e9174d1e | 26 | pub enum AnnNode<'a> { |
f9f354fc | 27 | Name(&'a Symbol), |
dfeec247 XL |
28 | Block(&'a hir::Block<'a>), |
29 | Item(&'a hir::Item<'a>), | |
532ac7d7 | 30 | SubItem(hir::HirId), |
dfeec247 XL |
31 | Expr(&'a hir::Expr<'a>), |
32 | Pat(&'a hir::Pat<'a>), | |
33 | Arm(&'a hir::Arm<'a>), | |
e9174d1e SL |
34 | } |
35 | ||
32a655c1 SL |
36 | pub enum Nested { |
37 | Item(hir::ItemId), | |
38 | TraitItem(hir::TraitItemId), | |
39 | ImplItem(hir::ImplItemId), | |
fc512014 | 40 | ForeignItem(hir::ForeignItemId), |
32a655c1 | 41 | Body(hir::BodyId), |
dfeec247 | 42 | BodyParamPat(hir::BodyId, usize), |
32a655c1 SL |
43 | } |
44 | ||
e9174d1e | 45 | pub trait PpAnn { |
dfeec247 XL |
46 | fn nested(&self, _state: &mut State<'_>, _nested: Nested) {} |
47 | fn pre(&self, _state: &mut State<'_>, _node: AnnNode<'_>) {} | |
48 | fn post(&self, _state: &mut State<'_>, _node: AnnNode<'_>) {} | |
e9174d1e SL |
49 | } |
50 | ||
e9174d1e | 51 | pub struct NoAnn; |
e9174d1e | 52 | impl PpAnn for NoAnn {} |
0731742a | 53 | pub const NO_ANN: &dyn PpAnn = &NoAnn; |
e9174d1e | 54 | |
ba9703b0 | 55 | impl PpAnn for hir::Crate<'_> { |
416331ca | 56 | fn nested(&self, state: &mut State<'_>, nested: Nested) { |
32a655c1 | 57 | match nested { |
6a06907d | 58 | Nested::Item(id) => state.print_item(self.item(id)), |
32a655c1 SL |
59 | Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)), |
60 | Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)), | |
fc512014 | 61 | Nested::ForeignItem(id) => state.print_foreign_item(self.foreign_item(id)), |
32a655c1 | 62 | Nested::Body(id) => state.print_expr(&self.body(id).value), |
dfeec247 | 63 | Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat), |
32a655c1 SL |
64 | } |
65 | } | |
66 | } | |
e9174d1e | 67 | |
ba9703b0 XL |
68 | /// Identical to the `PpAnn` implementation for `hir::Crate`, |
69 | /// except it avoids creating a dependency on the whole crate. | |
70 | impl PpAnn for &dyn rustc_hir::intravisit::Map<'_> { | |
71 | fn nested(&self, state: &mut State<'_>, nested: Nested) { | |
72 | match nested { | |
6a06907d | 73 | Nested::Item(id) => state.print_item(self.item(id)), |
ba9703b0 XL |
74 | Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)), |
75 | Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)), | |
fc512014 | 76 | Nested::ForeignItem(id) => state.print_foreign_item(self.foreign_item(id)), |
ba9703b0 XL |
77 | Nested::Body(id) => state.print_expr(&self.body(id).value), |
78 | Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat), | |
79 | } | |
80 | } | |
81 | } | |
82 | ||
e9174d1e | 83 | pub struct State<'a> { |
416331ca XL |
84 | pub s: pp::Printer, |
85 | comments: Option<Comments<'a>>, | |
6a06907d | 86 | attrs: &'a BTreeMap<hir::HirId, &'a [ast::Attribute]>, |
0531ce1d | 87 | ann: &'a (dyn PpAnn + 'a), |
e9174d1e SL |
88 | } |
89 | ||
dfeec247 XL |
90 | impl<'a> State<'a> { |
91 | pub fn print_node(&mut self, node: Node<'_>) { | |
92 | match node { | |
93 | Node::Param(a) => self.print_param(&a), | |
94 | Node::Item(a) => self.print_item(&a), | |
95 | Node::ForeignItem(a) => self.print_foreign_item(&a), | |
96 | Node::TraitItem(a) => self.print_trait_item(a), | |
97 | Node::ImplItem(a) => self.print_impl_item(a), | |
98 | Node::Variant(a) => self.print_variant(&a), | |
99 | Node::AnonConst(a) => self.print_anon_const(&a), | |
100 | Node::Expr(a) => self.print_expr(&a), | |
101 | Node::Stmt(a) => self.print_stmt(&a), | |
102 | Node::PathSegment(a) => self.print_path_segment(&a), | |
103 | Node::Ty(a) => self.print_type(&a), | |
104 | Node::TraitRef(a) => self.print_trait_ref(&a), | |
105 | Node::Binding(a) | Node::Pat(a) => self.print_pat(&a), | |
106 | Node::Arm(a) => self.print_arm(&a), | |
107 | Node::Block(a) => { | |
108 | // Containing cbox, will be closed by print-block at `}`. | |
109 | self.cbox(INDENT_UNIT); | |
110 | // Head-ibox, will be closed by print-block after `{`. | |
111 | self.ibox(0); | |
112 | self.print_block(&a) | |
113 | } | |
114 | Node::Lifetime(a) => self.print_lifetime(&a), | |
115 | Node::Visibility(a) => self.print_visibility(&a), | |
116 | Node::GenericParam(_) => panic!("cannot print Node::GenericParam"), | |
6a06907d | 117 | Node::Field(_) => panic!("cannot print Node::Field"), |
dfeec247 XL |
118 | // These cases do not carry enough information in the |
119 | // `hir_map` to reconstruct their full structure for pretty | |
120 | // printing. | |
121 | Node::Ctor(..) => panic!("cannot print isolated Ctor"), | |
122 | Node::Local(a) => self.print_local_decl(&a), | |
123 | Node::MacroDef(_) => panic!("cannot print MacroDef"), | |
ba9703b0 | 124 | Node::Crate(..) => panic!("cannot print Crate"), |
dfeec247 XL |
125 | } |
126 | } | |
127 | } | |
128 | ||
416331ca XL |
129 | impl std::ops::Deref for State<'_> { |
130 | type Target = pp::Printer; | |
131 | fn deref(&self) -> &Self::Target { | |
132 | &self.s | |
b039eaaf | 133 | } |
416331ca | 134 | } |
b039eaaf | 135 | |
416331ca XL |
136 | impl std::ops::DerefMut for State<'_> { |
137 | fn deref_mut(&mut self) -> &mut Self::Target { | |
138 | &mut self.s | |
b039eaaf | 139 | } |
416331ca | 140 | } |
b039eaaf | 141 | |
416331ca XL |
142 | impl<'a> PrintState<'a> for State<'a> { |
143 | fn comments(&mut self) -> &mut Option<Comments<'a>> { | |
b039eaaf SL |
144 | &mut self.comments |
145 | } | |
146 | ||
f9f354fc | 147 | fn print_ident(&mut self, ident: Ident) { |
74b04a01 | 148 | self.s.word(IdentPrinter::for_ast_ident(ident, ident.is_raw_guess()).to_string()); |
416331ca XL |
149 | self.ann.post(self, AnnNode::Name(&ident.name)) |
150 | } | |
151 | ||
dfeec247 XL |
152 | fn print_generic_args(&mut self, _: &ast::GenericArgs, _colons_before_params: bool) { |
153 | panic!("AST generic args printed by HIR pretty-printer"); | |
b039eaaf | 154 | } |
b039eaaf SL |
155 | } |
156 | ||
416331ca | 157 | pub const INDENT_UNIT: usize = 4; |
e9174d1e SL |
158 | |
159 | /// Requires you to pass an input filename and reader so that | |
48663c56 | 160 | /// it can scan the input text for comments to copy forward. |
dfeec247 | 161 | pub fn print_crate<'a>( |
74b04a01 | 162 | sm: &'a SourceMap, |
dfeec247 XL |
163 | krate: &hir::Crate<'_>, |
164 | filename: FileName, | |
165 | input: String, | |
166 | ann: &'a dyn PpAnn, | |
167 | ) -> String { | |
6a06907d | 168 | let mut s = State::new_from_input(sm, filename, input, &krate.attrs, ann); |
e9174d1e SL |
169 | |
170 | // When printing the AST, we sometimes need to inject `#[no_std]` here. | |
171 | // Since you can't compile the HIR, it's not necessary. | |
172 | ||
6a06907d | 173 | s.print_mod(&krate.item.module, s.attrs(hir::CRATE_HIR_ID)); |
416331ca | 174 | s.print_remaining_comments(); |
041b39d2 | 175 | s.s.eof() |
e9174d1e SL |
176 | } |
177 | ||
178 | impl<'a> State<'a> { | |
dfeec247 | 179 | pub fn new_from_input( |
74b04a01 | 180 | sm: &'a SourceMap, |
dfeec247 XL |
181 | filename: FileName, |
182 | input: String, | |
6a06907d | 183 | attrs: &'a BTreeMap<hir::HirId, &[ast::Attribute]>, |
dfeec247 XL |
184 | ann: &'a dyn PpAnn, |
185 | ) -> State<'a> { | |
6a06907d XL |
186 | State { |
187 | s: pp::mk_printer(), | |
188 | comments: Some(Comments::new(sm, filename, input)), | |
189 | attrs, | |
190 | ann, | |
191 | } | |
192 | } | |
193 | ||
194 | fn attrs(&self, id: hir::HirId) -> &'a [ast::Attribute] { | |
195 | self.attrs.get(&id).map_or(&[], |la| *la) | |
e9174d1e SL |
196 | } |
197 | } | |
198 | ||
0531ce1d | 199 | pub fn to_string<F>(ann: &dyn PpAnn, f: F) -> String |
dfeec247 XL |
200 | where |
201 | F: FnOnce(&mut State<'_>), | |
e9174d1e | 202 | { |
6a06907d XL |
203 | let mut printer = |
204 | State { s: pp::mk_printer(), comments: None, attrs: &BTreeMap::default(), ann }; | |
416331ca XL |
205 | f(&mut printer); |
206 | printer.s.eof() | |
e9174d1e SL |
207 | } |
208 | ||
dfeec247 | 209 | pub fn visibility_qualified<S: Into<Cow<'static, str>>>(vis: &hir::Visibility<'_>, w: S) -> String { |
32a655c1 | 210 | to_string(NO_ANN, |s| { |
416331ca | 211 | s.print_visibility(vis); |
041b39d2 | 212 | s.s.word(w) |
e9174d1e SL |
213 | }) |
214 | } | |
215 | ||
f035d41b XL |
216 | pub fn generic_params_to_string(generic_params: &[GenericParam<'_>]) -> String { |
217 | to_string(NO_ANN, |s| s.print_generic_params(generic_params)) | |
218 | } | |
219 | ||
220 | pub fn bounds_to_string<'b>(bounds: impl IntoIterator<Item = &'b hir::GenericBound<'b>>) -> String { | |
221 | to_string(NO_ANN, |s| s.print_bounds("", bounds)) | |
222 | } | |
223 | ||
224 | pub fn param_to_string(arg: &hir::Param<'_>) -> String { | |
225 | to_string(NO_ANN, |s| s.print_param(arg)) | |
226 | } | |
227 | ||
228 | pub fn ty_to_string(ty: &hir::Ty<'_>) -> String { | |
229 | to_string(NO_ANN, |s| s.print_type(ty)) | |
230 | } | |
231 | ||
232 | pub fn path_segment_to_string(segment: &hir::PathSegment<'_>) -> String { | |
233 | to_string(NO_ANN, |s| s.print_path_segment(segment)) | |
234 | } | |
235 | ||
236 | pub fn path_to_string(segment: &hir::Path<'_>) -> String { | |
237 | to_string(NO_ANN, |s| s.print_path(segment, false)) | |
238 | } | |
239 | ||
240 | pub fn fn_to_string( | |
241 | decl: &hir::FnDecl<'_>, | |
242 | header: hir::FnHeader, | |
243 | name: Option<Symbol>, | |
244 | generics: &hir::Generics<'_>, | |
245 | vis: &hir::Visibility<'_>, | |
246 | arg_names: &[Ident], | |
247 | body_id: Option<hir::BodyId>, | |
248 | ) -> String { | |
249 | to_string(NO_ANN, |s| s.print_fn(decl, header, name, generics, vis, arg_names, body_id)) | |
250 | } | |
251 | ||
252 | pub fn enum_def_to_string( | |
253 | enum_definition: &hir::EnumDef<'_>, | |
254 | generics: &hir::Generics<'_>, | |
255 | name: Symbol, | |
256 | span: rustc_span::Span, | |
257 | visibility: &hir::Visibility<'_>, | |
258 | ) -> String { | |
259 | to_string(NO_ANN, |s| s.print_enum_def(enum_definition, generics, name, span, visibility)) | |
260 | } | |
261 | ||
e9174d1e | 262 | impl<'a> State<'a> { |
416331ca XL |
263 | pub fn cbox(&mut self, u: usize) { |
264 | self.s.cbox(u); | |
e9174d1e SL |
265 | } |
266 | ||
416331ca | 267 | pub fn nbsp(&mut self) { |
041b39d2 | 268 | self.s.word(" ") |
e9174d1e SL |
269 | } |
270 | ||
416331ca XL |
271 | pub fn word_nbsp<S: Into<Cow<'static, str>>>(&mut self, w: S) { |
272 | self.s.word(w); | |
e9174d1e SL |
273 | self.nbsp() |
274 | } | |
275 | ||
416331ca | 276 | pub fn head<S: Into<Cow<'static, str>>>(&mut self, w: S) { |
a1dfa0c6 | 277 | let w = w.into(); |
e9174d1e | 278 | // outer-box is consistent |
416331ca | 279 | self.cbox(INDENT_UNIT); |
e9174d1e | 280 | // head-box is inconsistent |
416331ca | 281 | self.ibox(w.len() + 1); |
e9174d1e SL |
282 | // keyword that starts the head |
283 | if !w.is_empty() { | |
416331ca | 284 | self.word_nbsp(w); |
e9174d1e | 285 | } |
e9174d1e SL |
286 | } |
287 | ||
416331ca XL |
288 | pub fn bopen(&mut self) { |
289 | self.s.word("{"); | |
290 | self.end(); // close the head-box | |
e9174d1e | 291 | } |
b7449926 | 292 | |
dfeec247 | 293 | pub fn bclose_maybe_open(&mut self, span: rustc_span::Span, close_box: bool) { |
416331ca XL |
294 | self.maybe_print_comment(span.hi()); |
295 | self.break_offset_if_not_bol(1, -(INDENT_UNIT as isize)); | |
296 | self.s.word("}"); | |
e9174d1e | 297 | if close_box { |
416331ca | 298 | self.end(); // close the outer-box |
e9174d1e | 299 | } |
e9174d1e | 300 | } |
b7449926 | 301 | |
dfeec247 | 302 | pub fn bclose(&mut self, span: rustc_span::Span) { |
416331ca | 303 | self.bclose_maybe_open(span, true) |
e9174d1e SL |
304 | } |
305 | ||
416331ca XL |
306 | pub fn space_if_not_bol(&mut self) { |
307 | if !self.s.is_beginning_of_line() { | |
308 | self.s.space(); | |
e9174d1e SL |
309 | } |
310 | } | |
b7449926 | 311 | |
416331ca XL |
312 | pub fn break_offset_if_not_bol(&mut self, n: usize, off: isize) { |
313 | if !self.s.is_beginning_of_line() { | |
041b39d2 | 314 | self.s.break_offset(n, off) |
29967ef6 XL |
315 | } else if off != 0 && self.s.last_token().is_hardbreak_tok() { |
316 | // We do something pretty sketchy here: tuck the nonzero | |
317 | // offset-adjustment we were going to deposit along with the | |
318 | // break into the previous hardbreak. | |
319 | self.s.replace_last_token(pp::Printer::hardbreak_tok_offset(off)); | |
e9174d1e SL |
320 | } |
321 | } | |
322 | ||
323 | // Synthesizes a comment that was not textually present in the original source | |
324 | // file. | |
416331ca XL |
325 | pub fn synth_comment(&mut self, text: String) { |
326 | self.s.word("/*"); | |
327 | self.s.space(); | |
328 | self.s.word(text); | |
329 | self.s.space(); | |
041b39d2 | 330 | self.s.word("*/") |
e9174d1e SL |
331 | } |
332 | ||
dfeec247 XL |
333 | pub fn commasep_cmnt<T, F, G>(&mut self, b: Breaks, elts: &[T], mut op: F, mut get_span: G) |
334 | where | |
335 | F: FnMut(&mut State<'_>, &T), | |
336 | G: FnMut(&T) -> rustc_span::Span, | |
e9174d1e | 337 | { |
416331ca | 338 | self.rbox(0, b); |
e9174d1e SL |
339 | let len = elts.len(); |
340 | let mut i = 0; | |
341 | for elt in elts { | |
416331ca XL |
342 | self.maybe_print_comment(get_span(elt).hi()); |
343 | op(self, elt); | |
e9174d1e SL |
344 | i += 1; |
345 | if i < len { | |
416331ca XL |
346 | self.s.word(","); |
347 | self.maybe_print_trailing_comment(get_span(elt), Some(get_span(&elts[i]).hi())); | |
348 | self.space_if_not_bol(); | |
e9174d1e SL |
349 | } |
350 | } | |
416331ca | 351 | self.end(); |
e9174d1e SL |
352 | } |
353 | ||
dfeec247 | 354 | pub fn commasep_exprs(&mut self, b: Breaks, exprs: &[hir::Expr<'_>]) { |
7453a54e | 355 | self.commasep_cmnt(b, exprs, |s, e| s.print_expr(&e), |e| e.span) |
e9174d1e SL |
356 | } |
357 | ||
dfeec247 | 358 | pub fn print_mod(&mut self, _mod: &hir::Mod<'_>, attrs: &[ast::Attribute]) { |
416331ca | 359 | self.print_inner_attributes(attrs); |
dfeec247 | 360 | for &item_id in _mod.item_ids { |
416331ca | 361 | self.ann.nested(self, Nested::Item(item_id)); |
e9174d1e | 362 | } |
e9174d1e SL |
363 | } |
364 | ||
416331ca | 365 | pub fn print_opt_lifetime(&mut self, lifetime: &hir::Lifetime) { |
32a655c1 | 366 | if !lifetime.is_elided() { |
416331ca XL |
367 | self.print_lifetime(lifetime); |
368 | self.nbsp(); | |
e9174d1e | 369 | } |
e9174d1e SL |
370 | } |
371 | ||
dfeec247 | 372 | pub fn print_type(&mut self, ty: &hir::Ty<'_>) { |
416331ca XL |
373 | self.maybe_print_comment(ty.span.lo()); |
374 | self.ibox(0); | |
e74abb32 | 375 | match ty.kind { |
8faf50e0 | 376 | hir::TyKind::Slice(ref ty) => { |
416331ca XL |
377 | self.s.word("["); |
378 | self.print_type(&ty); | |
379 | self.s.word("]"); | |
e9174d1e | 380 | } |
8faf50e0 | 381 | hir::TyKind::Ptr(ref mt) => { |
416331ca | 382 | self.s.word("*"); |
60c5eb7d | 383 | self.print_mt(mt, true); |
e9174d1e | 384 | } |
8faf50e0 | 385 | hir::TyKind::Rptr(ref lifetime, ref mt) => { |
416331ca XL |
386 | self.s.word("&"); |
387 | self.print_opt_lifetime(lifetime); | |
60c5eb7d | 388 | self.print_mt(mt, false); |
e9174d1e | 389 | } |
8faf50e0 | 390 | hir::TyKind::Never => { |
416331ca | 391 | self.s.word("!"); |
dfeec247 | 392 | } |
8faf50e0 | 393 | hir::TyKind::Tup(ref elts) => { |
416331ca XL |
394 | self.popen(); |
395 | self.commasep(Inconsistent, &elts[..], |s, ty| s.print_type(&ty)); | |
e9174d1e | 396 | if elts.len() == 1 { |
416331ca | 397 | self.s.word(","); |
e9174d1e | 398 | } |
416331ca | 399 | self.pclose(); |
e9174d1e | 400 | } |
8faf50e0 | 401 | hir::TyKind::BareFn(ref f) => { |
dfeec247 XL |
402 | self.print_ty_fn( |
403 | f.abi, | |
404 | f.unsafety, | |
405 | &f.decl, | |
406 | None, | |
407 | &f.generic_params, | |
6a06907d | 408 | f.param_names, |
dfeec247 XL |
409 | ); |
410 | } | |
f035d41b | 411 | hir::TyKind::OpaqueDef(..) => self.s.word("/*impl Trait*/"), |
dfeec247 | 412 | hir::TyKind::Path(ref qpath) => self.print_qpath(qpath, false), |
6a06907d XL |
413 | hir::TyKind::TraitObject(bounds, ref lifetime, syntax) => { |
414 | if syntax == ast::TraitObjectSyntax::Dyn { | |
415 | self.word_space("dyn"); | |
416 | } | |
32a655c1 SL |
417 | let mut first = true; |
418 | for bound in bounds { | |
32a655c1 SL |
419 | if first { |
420 | first = false; | |
421 | } else { | |
416331ca XL |
422 | self.nbsp(); |
423 | self.word_space("+"); | |
32a655c1 | 424 | } |
416331ca | 425 | self.print_poly_trait_ref(bound); |
32a655c1 SL |
426 | } |
427 | if !lifetime.is_elided() { | |
416331ca XL |
428 | self.nbsp(); |
429 | self.word_space("+"); | |
430 | self.print_lifetime(lifetime); | |
32a655c1 | 431 | } |
e9174d1e | 432 | } |
8faf50e0 | 433 | hir::TyKind::Array(ref ty, ref length) => { |
416331ca XL |
434 | self.s.word("["); |
435 | self.print_type(&ty); | |
436 | self.s.word("; "); | |
437 | self.print_anon_const(length); | |
438 | self.s.word("]"); | |
e9174d1e | 439 | } |
8faf50e0 | 440 | hir::TyKind::Typeof(ref e) => { |
416331ca XL |
441 | self.s.word("typeof("); |
442 | self.print_anon_const(e); | |
443 | self.s.word(")"); | |
e9174d1e | 444 | } |
8faf50e0 | 445 | hir::TyKind::Infer => { |
416331ca | 446 | self.s.word("_"); |
e9174d1e | 447 | } |
8faf50e0 | 448 | hir::TyKind::Err => { |
416331ca XL |
449 | self.popen(); |
450 | self.s.word("/*ERROR*/"); | |
451 | self.pclose(); | |
cc61c64b | 452 | } |
e9174d1e SL |
453 | } |
454 | self.end() | |
455 | } | |
456 | ||
dfeec247 | 457 | pub fn print_foreign_item(&mut self, item: &hir::ForeignItem<'_>) { |
416331ca XL |
458 | self.hardbreak_if_not_bol(); |
459 | self.maybe_print_comment(item.span.lo()); | |
6a06907d | 460 | self.print_outer_attributes(self.attrs(item.hir_id())); |
e74abb32 | 461 | match item.kind { |
8faf50e0 | 462 | hir::ForeignItemKind::Fn(ref decl, ref arg_names, ref generics) => { |
416331ca | 463 | self.head(""); |
dfeec247 XL |
464 | self.print_fn( |
465 | decl, | |
466 | hir::FnHeader { | |
467 | unsafety: hir::Unsafety::Normal, | |
468 | constness: hir::Constness::NotConst, | |
469 | abi: Abi::Rust, | |
470 | asyncness: hir::IsAsync::NotAsync, | |
471 | }, | |
472 | Some(item.ident.name), | |
473 | generics, | |
474 | &item.vis, | |
475 | arg_names, | |
476 | None, | |
477 | ); | |
416331ca XL |
478 | self.end(); // end head-ibox |
479 | self.s.word(";"); | |
e9174d1e SL |
480 | self.end() // end the outer fn box |
481 | } | |
8faf50e0 | 482 | hir::ForeignItemKind::Static(ref t, m) => { |
416331ca | 483 | self.head(visibility_qualified(&item.vis, "static")); |
dfeec247 | 484 | if m == hir::Mutability::Mut { |
416331ca | 485 | self.word_space("mut"); |
e9174d1e | 486 | } |
416331ca XL |
487 | self.print_ident(item.ident); |
488 | self.word_space(":"); | |
489 | self.print_type(&t); | |
490 | self.s.word(";"); | |
491 | self.end(); // end the head-ibox | |
e9174d1e SL |
492 | self.end() // end the outer cbox |
493 | } | |
8faf50e0 | 494 | hir::ForeignItemKind::Type => { |
416331ca XL |
495 | self.head(visibility_qualified(&item.vis, "type")); |
496 | self.print_ident(item.ident); | |
497 | self.s.word(";"); | |
498 | self.end(); // end the head-ibox | |
abe05a73 XL |
499 | self.end() // end the outer cbox |
500 | } | |
e9174d1e SL |
501 | } |
502 | } | |
503 | ||
dfeec247 XL |
504 | fn print_associated_const( |
505 | &mut self, | |
f9f354fc | 506 | ident: Ident, |
dfeec247 XL |
507 | ty: &hir::Ty<'_>, |
508 | default: Option<hir::BodyId>, | |
509 | vis: &hir::Visibility<'_>, | |
510 | ) { | |
416331ca XL |
511 | self.s.word(visibility_qualified(vis, "")); |
512 | self.word_space("const"); | |
513 | self.print_ident(ident); | |
514 | self.word_space(":"); | |
515 | self.print_type(ty); | |
e9174d1e | 516 | if let Some(expr) = default { |
416331ca XL |
517 | self.s.space(); |
518 | self.word_space("="); | |
519 | self.ann.nested(self, Nested::Body(expr)); | |
e9174d1e | 520 | } |
041b39d2 | 521 | self.s.word(";") |
e9174d1e SL |
522 | } |
523 | ||
dfeec247 XL |
524 | fn print_associated_type( |
525 | &mut self, | |
f9f354fc | 526 | ident: Ident, |
74b04a01 | 527 | generics: &hir::Generics<'_>, |
dfeec247 XL |
528 | bounds: Option<hir::GenericBounds<'_>>, |
529 | ty: Option<&hir::Ty<'_>>, | |
530 | ) { | |
416331ca XL |
531 | self.word_space("type"); |
532 | self.print_ident(ident); | |
74b04a01 | 533 | self.print_generic_params(&generics.params); |
e9174d1e | 534 | if let Some(bounds) = bounds { |
416331ca | 535 | self.print_bounds(":", bounds); |
e9174d1e | 536 | } |
74b04a01 | 537 | self.print_where_clause(&generics.where_clause); |
e9174d1e | 538 | if let Some(ty) = ty { |
416331ca XL |
539 | self.s.space(); |
540 | self.word_space("="); | |
541 | self.print_type(ty); | |
e9174d1e | 542 | } |
041b39d2 | 543 | self.s.word(";") |
e9174d1e SL |
544 | } |
545 | ||
416331ca XL |
546 | fn print_item_type( |
547 | &mut self, | |
dfeec247 XL |
548 | item: &hir::Item<'_>, |
549 | generics: &hir::Generics<'_>, | |
416331ca XL |
550 | inner: impl Fn(&mut Self), |
551 | ) { | |
552 | self.head(visibility_qualified(&item.vis, "type")); | |
553 | self.print_ident(item.ident); | |
554 | self.print_generic_params(&generics.params); | |
555 | self.end(); // end the inner ibox | |
556 | ||
557 | self.print_where_clause(&generics.where_clause); | |
558 | self.s.space(); | |
559 | inner(self); | |
560 | self.s.word(";"); | |
561 | self.end(); // end the outer ibox | |
562 | } | |
563 | ||
e9174d1e | 564 | /// Pretty-print an item |
dfeec247 | 565 | pub fn print_item(&mut self, item: &hir::Item<'_>) { |
416331ca XL |
566 | self.hardbreak_if_not_bol(); |
567 | self.maybe_print_comment(item.span.lo()); | |
6a06907d XL |
568 | let attrs = self.attrs(item.hir_id()); |
569 | self.print_outer_attributes(attrs); | |
416331ca | 570 | self.ann.pre(self, AnnNode::Item(item)); |
e74abb32 | 571 | match item.kind { |
8faf50e0 | 572 | hir::ItemKind::ExternCrate(orig_name) => { |
416331ca | 573 | self.head(visibility_qualified(&item.vis, "extern crate")); |
0531ce1d | 574 | if let Some(orig_name) = orig_name { |
416331ca XL |
575 | self.print_name(orig_name); |
576 | self.s.space(); | |
577 | self.s.word("as"); | |
578 | self.s.space(); | |
e9174d1e | 579 | } |
416331ca XL |
580 | self.print_ident(item.ident); |
581 | self.s.word(";"); | |
582 | self.end(); // end inner head-block | |
583 | self.end(); // end outer head-block | |
e9174d1e | 584 | } |
8faf50e0 | 585 | hir::ItemKind::Use(ref path, kind) => { |
416331ca XL |
586 | self.head(visibility_qualified(&item.vis, "use")); |
587 | self.print_path(path, false); | |
476ff2be SL |
588 | |
589 | match kind { | |
590 | hir::UseKind::Single => { | |
0731742a | 591 | if path.segments.last().unwrap().ident != item.ident { |
416331ca XL |
592 | self.s.space(); |
593 | self.word_space("as"); | |
594 | self.print_ident(item.ident); | |
476ff2be | 595 | } |
416331ca | 596 | self.s.word(";"); |
476ff2be | 597 | } |
416331ca | 598 | hir::UseKind::Glob => self.s.word("::*;"), |
dfeec247 | 599 | hir::UseKind::ListStem => self.s.word("::{};"), |
476ff2be | 600 | } |
416331ca XL |
601 | self.end(); // end inner head-block |
602 | self.end(); // end outer head-block | |
e9174d1e | 603 | } |
8faf50e0 | 604 | hir::ItemKind::Static(ref ty, m, expr) => { |
416331ca | 605 | self.head(visibility_qualified(&item.vis, "static")); |
dfeec247 | 606 | if m == hir::Mutability::Mut { |
416331ca | 607 | self.word_space("mut"); |
e9174d1e | 608 | } |
416331ca XL |
609 | self.print_ident(item.ident); |
610 | self.word_space(":"); | |
611 | self.print_type(&ty); | |
612 | self.s.space(); | |
613 | self.end(); // end the head-ibox | |
e9174d1e | 614 | |
416331ca XL |
615 | self.word_space("="); |
616 | self.ann.nested(self, Nested::Body(expr)); | |
617 | self.s.word(";"); | |
618 | self.end(); // end the outer cbox | |
e9174d1e | 619 | } |
8faf50e0 | 620 | hir::ItemKind::Const(ref ty, expr) => { |
416331ca XL |
621 | self.head(visibility_qualified(&item.vis, "const")); |
622 | self.print_ident(item.ident); | |
623 | self.word_space(":"); | |
624 | self.print_type(&ty); | |
625 | self.s.space(); | |
626 | self.end(); // end the head-ibox | |
627 | ||
628 | self.word_space("="); | |
629 | self.ann.nested(self, Nested::Body(expr)); | |
630 | self.s.word(";"); | |
631 | self.end(); // end the outer cbox | |
e9174d1e | 632 | } |
60c5eb7d | 633 | hir::ItemKind::Fn(ref sig, ref param_names, body) => { |
416331ca | 634 | self.head(""); |
dfeec247 XL |
635 | self.print_fn( |
636 | &sig.decl, | |
637 | sig.header, | |
638 | Some(item.ident.name), | |
639 | param_names, | |
640 | &item.vis, | |
641 | &[], | |
642 | Some(body), | |
643 | ); | |
416331ca XL |
644 | self.s.word(" "); |
645 | self.end(); // need to close a box | |
646 | self.end(); // need to close a box | |
647 | self.ann.nested(self, Nested::Body(body)); | |
e9174d1e | 648 | } |
8faf50e0 | 649 | hir::ItemKind::Mod(ref _mod) => { |
416331ca XL |
650 | self.head(visibility_qualified(&item.vis, "mod")); |
651 | self.print_ident(item.ident); | |
652 | self.nbsp(); | |
653 | self.bopen(); | |
6a06907d | 654 | self.print_mod(_mod, attrs); |
416331ca | 655 | self.bclose(item.span); |
e9174d1e | 656 | } |
fc512014 | 657 | hir::ItemKind::ForeignMod { abi, items } => { |
416331ca | 658 | self.head("extern"); |
fc512014 | 659 | self.word_nbsp(abi.to_string()); |
416331ca | 660 | self.bopen(); |
6a06907d | 661 | self.print_inner_attributes(self.attrs(item.hir_id())); |
fc512014 XL |
662 | for item in items { |
663 | self.ann.nested(self, Nested::ForeignItem(item.id)); | |
664 | } | |
416331ca | 665 | self.bclose(item.span); |
e9174d1e | 666 | } |
8faf50e0 | 667 | hir::ItemKind::GlobalAsm(ref ga) => { |
416331ca | 668 | self.head(visibility_qualified(&item.vis, "global asm")); |
60c5eb7d | 669 | self.s.word(ga.asm.to_string()); |
416331ca XL |
670 | self.end() |
671 | } | |
672 | hir::ItemKind::TyAlias(ref ty, ref generics) => { | |
673 | self.print_item_type(item, &generics, |state| { | |
674 | state.word_space("="); | |
675 | state.print_type(&ty); | |
676 | }); | |
677 | } | |
678 | hir::ItemKind::OpaqueTy(ref opaque_ty) => { | |
679 | self.print_item_type(item, &opaque_ty.generics, |state| { | |
680 | let mut real_bounds = Vec::with_capacity(opaque_ty.bounds.len()); | |
681 | for b in opaque_ty.bounds.iter() { | |
682 | if let GenericBound::Trait(ref ptr, hir::TraitBoundModifier::Maybe) = *b { | |
683 | state.s.space(); | |
684 | state.word_space("for ?"); | |
685 | state.print_trait_ref(&ptr.trait_ref); | |
686 | } else { | |
687 | real_bounds.push(b); | |
688 | } | |
94b46f34 | 689 | } |
416331ca XL |
690 | state.print_bounds("= impl", real_bounds); |
691 | }); | |
94b46f34 | 692 | } |
8faf50e0 | 693 | hir::ItemKind::Enum(ref enum_definition, ref params) => { |
416331ca | 694 | self.print_enum_def(enum_definition, params, item.ident.name, item.span, &item.vis); |
e9174d1e | 695 | } |
8faf50e0 | 696 | hir::ItemKind::Struct(ref struct_def, ref generics) => { |
416331ca XL |
697 | self.head(visibility_qualified(&item.vis, "struct")); |
698 | self.print_struct(struct_def, generics, item.ident.name, item.span, true); | |
e9174d1e | 699 | } |
8faf50e0 | 700 | hir::ItemKind::Union(ref struct_def, ref generics) => { |
416331ca XL |
701 | self.head(visibility_qualified(&item.vis, "union")); |
702 | self.print_struct(struct_def, generics, item.ident.name, item.span, true); | |
9e0c209e | 703 | } |
5869c6ff | 704 | hir::ItemKind::Impl(hir::Impl { |
dfeec247 XL |
705 | unsafety, |
706 | polarity, | |
707 | defaultness, | |
708 | constness, | |
ba9703b0 | 709 | defaultness_span: _, |
dfeec247 XL |
710 | ref generics, |
711 | ref of_trait, | |
712 | ref self_ty, | |
713 | items, | |
5869c6ff | 714 | }) => { |
416331ca XL |
715 | self.head(""); |
716 | self.print_visibility(&item.vis); | |
717 | self.print_defaultness(defaultness); | |
718 | self.print_unsafety(unsafety); | |
719 | self.word_nbsp("impl"); | |
e9174d1e | 720 | |
ff7c6d11 | 721 | if !generics.params.is_empty() { |
416331ca XL |
722 | self.print_generic_params(&generics.params); |
723 | self.s.space(); | |
e9174d1e SL |
724 | } |
725 | ||
74b04a01 | 726 | if constness == hir::Constness::Const { |
dfeec247 XL |
727 | self.word_nbsp("const"); |
728 | } | |
729 | ||
ba9703b0 | 730 | if let hir::ImplPolarity::Negative(_) = polarity { |
416331ca | 731 | self.s.word("!"); |
e9174d1e SL |
732 | } |
733 | ||
dfeec247 | 734 | if let Some(ref t) = of_trait { |
416331ca XL |
735 | self.print_trait_ref(t); |
736 | self.s.space(); | |
737 | self.word_space("for"); | |
e9174d1e SL |
738 | } |
739 | ||
dfeec247 | 740 | self.print_type(&self_ty); |
416331ca | 741 | self.print_where_clause(&generics.where_clause); |
e9174d1e | 742 | |
416331ca XL |
743 | self.s.space(); |
744 | self.bopen(); | |
6a06907d | 745 | self.print_inner_attributes(attrs); |
dfeec247 | 746 | for impl_item in items { |
416331ca | 747 | self.ann.nested(self, Nested::ImplItem(impl_item.id)); |
e9174d1e | 748 | } |
416331ca | 749 | self.bclose(item.span); |
e9174d1e | 750 | } |
dfeec247 | 751 | hir::ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, trait_items) => { |
416331ca XL |
752 | self.head(""); |
753 | self.print_visibility(&item.vis); | |
754 | self.print_is_auto(is_auto); | |
755 | self.print_unsafety(unsafety); | |
756 | self.word_nbsp("trait"); | |
757 | self.print_ident(item.ident); | |
758 | self.print_generic_params(&generics.params); | |
e9174d1e SL |
759 | let mut real_bounds = Vec::with_capacity(bounds.len()); |
760 | for b in bounds.iter() { | |
8faf50e0 | 761 | if let GenericBound::Trait(ref ptr, hir::TraitBoundModifier::Maybe) = *b { |
416331ca XL |
762 | self.s.space(); |
763 | self.word_space("for ?"); | |
764 | self.print_trait_ref(&ptr.trait_ref); | |
e9174d1e | 765 | } else { |
dc9dc135 | 766 | real_bounds.push(b); |
e9174d1e SL |
767 | } |
768 | } | |
416331ca XL |
769 | self.print_bounds(":", real_bounds); |
770 | self.print_where_clause(&generics.where_clause); | |
771 | self.s.word(" "); | |
772 | self.bopen(); | |
e9174d1e | 773 | for trait_item in trait_items { |
416331ca | 774 | self.ann.nested(self, Nested::TraitItem(trait_item.id)); |
e9174d1e | 775 | } |
416331ca | 776 | self.bclose(item.span); |
e9174d1e | 777 | } |
8faf50e0 | 778 | hir::ItemKind::TraitAlias(ref generics, ref bounds) => { |
416331ca XL |
779 | self.head(""); |
780 | self.print_visibility(&item.vis); | |
781 | self.word_nbsp("trait"); | |
782 | self.print_ident(item.ident); | |
783 | self.print_generic_params(&generics.params); | |
ff7c6d11 XL |
784 | let mut real_bounds = Vec::with_capacity(bounds.len()); |
785 | // FIXME(durka) this seems to be some quite outdated syntax | |
786 | for b in bounds.iter() { | |
8faf50e0 | 787 | if let GenericBound::Trait(ref ptr, hir::TraitBoundModifier::Maybe) = *b { |
416331ca XL |
788 | self.s.space(); |
789 | self.word_space("for ?"); | |
790 | self.print_trait_ref(&ptr.trait_ref); | |
ff7c6d11 | 791 | } else { |
dc9dc135 | 792 | real_bounds.push(b); |
ff7c6d11 XL |
793 | } |
794 | } | |
416331ca XL |
795 | self.nbsp(); |
796 | self.print_bounds("=", real_bounds); | |
797 | self.print_where_clause(&generics.where_clause); | |
798 | self.s.word(";"); | |
ff7c6d11 | 799 | } |
e9174d1e | 800 | } |
b7449926 | 801 | self.ann.post(self, AnnNode::Item(item)) |
e9174d1e SL |
802 | } |
803 | ||
dfeec247 | 804 | pub fn print_trait_ref(&mut self, t: &hir::TraitRef<'_>) { |
476ff2be | 805 | self.print_path(&t.path, false) |
e9174d1e SL |
806 | } |
807 | ||
dfeec247 | 808 | fn print_formal_generic_params(&mut self, generic_params: &[hir::GenericParam<'_>]) { |
ff7c6d11 | 809 | if !generic_params.is_empty() { |
416331ca XL |
810 | self.s.word("for"); |
811 | self.print_generic_params(generic_params); | |
812 | self.nbsp(); | |
e9174d1e | 813 | } |
e9174d1e SL |
814 | } |
815 | ||
dfeec247 | 816 | fn print_poly_trait_ref(&mut self, t: &hir::PolyTraitRef<'_>) { |
416331ca | 817 | self.print_formal_generic_params(&t.bound_generic_params); |
e9174d1e SL |
818 | self.print_trait_ref(&t.trait_ref) |
819 | } | |
820 | ||
dfeec247 XL |
821 | pub fn print_enum_def( |
822 | &mut self, | |
823 | enum_definition: &hir::EnumDef<'_>, | |
824 | generics: &hir::Generics<'_>, | |
f9f354fc | 825 | name: Symbol, |
dfeec247 XL |
826 | span: rustc_span::Span, |
827 | visibility: &hir::Visibility<'_>, | |
828 | ) { | |
416331ca XL |
829 | self.head(visibility_qualified(visibility, "enum")); |
830 | self.print_name(name); | |
831 | self.print_generic_params(&generics.params); | |
832 | self.print_where_clause(&generics.where_clause); | |
833 | self.s.space(); | |
e9174d1e SL |
834 | self.print_variants(&enum_definition.variants, span) |
835 | } | |
836 | ||
dfeec247 | 837 | pub fn print_variants(&mut self, variants: &[hir::Variant<'_>], span: rustc_span::Span) { |
416331ca | 838 | self.bopen(); |
e9174d1e | 839 | for v in variants { |
416331ca XL |
840 | self.space_if_not_bol(); |
841 | self.maybe_print_comment(v.span.lo()); | |
6a06907d | 842 | self.print_outer_attributes(self.attrs(v.id)); |
416331ca XL |
843 | self.ibox(INDENT_UNIT); |
844 | self.print_variant(v); | |
845 | self.s.word(","); | |
846 | self.end(); | |
847 | self.maybe_print_trailing_comment(v.span, None); | |
e9174d1e SL |
848 | } |
849 | self.bclose(span) | |
850 | } | |
851 | ||
dfeec247 | 852 | pub fn print_visibility(&mut self, vis: &hir::Visibility<'_>) { |
8faf50e0 | 853 | match vis.node { |
416331ca XL |
854 | hir::VisibilityKind::Public => self.word_nbsp("pub"), |
855 | hir::VisibilityKind::Crate(ast::CrateSugar::JustCrate) => self.word_nbsp("crate"), | |
856 | hir::VisibilityKind::Crate(ast::CrateSugar::PubCrate) => self.word_nbsp("pub(crate)"), | |
8faf50e0 | 857 | hir::VisibilityKind::Restricted { ref path, .. } => { |
416331ca | 858 | self.s.word("pub("); |
dfeec247 | 859 | if path.segments.len() == 1 && path.segments[0].ident.name == kw::Super { |
94b46f34 | 860 | // Special case: `super` can print like `pub(super)`. |
416331ca | 861 | self.s.word("super"); |
94b46f34 XL |
862 | } else { |
863 | // Everything else requires `in` at present. | |
416331ca XL |
864 | self.word_nbsp("in"); |
865 | self.print_path(path, false); | |
94b46f34 | 866 | } |
416331ca | 867 | self.word_nbsp(")"); |
32a655c1 | 868 | } |
dfeec247 | 869 | hir::VisibilityKind::Inherited => (), |
e9174d1e SL |
870 | } |
871 | } | |
872 | ||
416331ca | 873 | pub fn print_defaultness(&mut self, defaultness: hir::Defaultness) { |
7cac9316 | 874 | match defaultness { |
416331ca | 875 | hir::Defaultness::Default { .. } => self.word_nbsp("default"), |
7cac9316 XL |
876 | hir::Defaultness::Final => (), |
877 | } | |
7cac9316 XL |
878 | } |
879 | ||
dfeec247 XL |
880 | pub fn print_struct( |
881 | &mut self, | |
882 | struct_def: &hir::VariantData<'_>, | |
883 | generics: &hir::Generics<'_>, | |
f9f354fc | 884 | name: Symbol, |
dfeec247 XL |
885 | span: rustc_span::Span, |
886 | print_finalizer: bool, | |
887 | ) { | |
416331ca XL |
888 | self.print_name(name); |
889 | self.print_generic_params(&generics.params); | |
532ac7d7 XL |
890 | match struct_def { |
891 | hir::VariantData::Tuple(..) | hir::VariantData::Unit(..) => { | |
892 | if let hir::VariantData::Tuple(..) = struct_def { | |
416331ca | 893 | self.popen(); |
532ac7d7 | 894 | self.commasep(Inconsistent, struct_def.fields(), |s, field| { |
416331ca | 895 | s.maybe_print_comment(field.span.lo()); |
6a06907d | 896 | s.print_outer_attributes(s.attrs(field.hir_id)); |
416331ca | 897 | s.print_visibility(&field.vis); |
532ac7d7 | 898 | s.print_type(&field.ty) |
416331ca XL |
899 | }); |
900 | self.pclose(); | |
532ac7d7 | 901 | } |
416331ca | 902 | self.print_where_clause(&generics.where_clause); |
532ac7d7 | 903 | if print_finalizer { |
416331ca | 904 | self.s.word(";"); |
532ac7d7 | 905 | } |
416331ca | 906 | self.end(); |
532ac7d7 | 907 | self.end() // close the outer-box |
b039eaaf | 908 | } |
532ac7d7 | 909 | hir::VariantData::Struct(..) => { |
416331ca XL |
910 | self.print_where_clause(&generics.where_clause); |
911 | self.nbsp(); | |
912 | self.bopen(); | |
913 | self.hardbreak_if_not_bol(); | |
e9174d1e | 914 | |
532ac7d7 | 915 | for field in struct_def.fields() { |
416331ca XL |
916 | self.hardbreak_if_not_bol(); |
917 | self.maybe_print_comment(field.span.lo()); | |
6a06907d | 918 | self.print_outer_attributes(self.attrs(field.hir_id)); |
416331ca XL |
919 | self.print_visibility(&field.vis); |
920 | self.print_ident(field.ident); | |
921 | self.word_nbsp(":"); | |
922 | self.print_type(&field.ty); | |
923 | self.s.word(","); | |
532ac7d7 XL |
924 | } |
925 | ||
926 | self.bclose(span) | |
927 | } | |
e9174d1e SL |
928 | } |
929 | } | |
930 | ||
dfeec247 | 931 | pub fn print_variant(&mut self, v: &hir::Variant<'_>) { |
416331ca | 932 | self.head(""); |
54a0048b | 933 | let generics = hir::Generics::empty(); |
e1599b0c XL |
934 | self.print_struct(&v.data, &generics, v.ident.name, v.span, false); |
935 | if let Some(ref d) = v.disr_expr { | |
416331ca XL |
936 | self.s.space(); |
937 | self.word_space("="); | |
938 | self.print_anon_const(d); | |
e9174d1e SL |
939 | } |
940 | } | |
dfeec247 XL |
941 | pub fn print_method_sig( |
942 | &mut self, | |
f9f354fc | 943 | ident: Ident, |
dfeec247 XL |
944 | m: &hir::FnSig<'_>, |
945 | generics: &hir::Generics<'_>, | |
946 | vis: &hir::Visibility<'_>, | |
f9f354fc | 947 | arg_names: &[Ident], |
dfeec247 XL |
948 | body_id: Option<hir::BodyId>, |
949 | ) { | |
950 | self.print_fn(&m.decl, m.header, Some(ident.name), generics, vis, arg_names, body_id) | |
951 | } | |
952 | ||
953 | pub fn print_trait_item(&mut self, ti: &hir::TraitItem<'_>) { | |
6a06907d | 954 | self.ann.pre(self, AnnNode::SubItem(ti.hir_id())); |
416331ca XL |
955 | self.hardbreak_if_not_bol(); |
956 | self.maybe_print_comment(ti.span.lo()); | |
6a06907d | 957 | self.print_outer_attributes(self.attrs(ti.hir_id())); |
e74abb32 | 958 | match ti.kind { |
32a655c1 | 959 | hir::TraitItemKind::Const(ref ty, default) => { |
dfeec247 XL |
960 | let vis = |
961 | Spanned { span: rustc_span::DUMMY_SP, node: hir::VisibilityKind::Inherited }; | |
416331ca | 962 | self.print_associated_const(ti.ident, &ty, default, &vis); |
e9174d1e | 963 | } |
ba9703b0 | 964 | hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Required(ref arg_names)) => { |
dfeec247 XL |
965 | let vis = |
966 | Spanned { span: rustc_span::DUMMY_SP, node: hir::VisibilityKind::Inherited }; | |
416331ca XL |
967 | self.print_method_sig(ti.ident, sig, &ti.generics, &vis, arg_names, None); |
968 | self.s.word(";"); | |
32a655c1 | 969 | } |
ba9703b0 | 970 | hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => { |
dfeec247 XL |
971 | let vis = |
972 | Spanned { span: rustc_span::DUMMY_SP, node: hir::VisibilityKind::Inherited }; | |
416331ca XL |
973 | self.head(""); |
974 | self.print_method_sig(ti.ident, sig, &ti.generics, &vis, &[], Some(body)); | |
975 | self.nbsp(); | |
976 | self.end(); // need to close a box | |
977 | self.end(); // need to close a box | |
978 | self.ann.nested(self, Nested::Body(body)); | |
32a655c1 SL |
979 | } |
980 | hir::TraitItemKind::Type(ref bounds, ref default) => { | |
dfeec247 XL |
981 | self.print_associated_type( |
982 | ti.ident, | |
74b04a01 | 983 | &ti.generics, |
dfeec247 XL |
984 | Some(bounds), |
985 | default.as_ref().map(|ty| &**ty), | |
986 | ); | |
e9174d1e SL |
987 | } |
988 | } | |
6a06907d | 989 | self.ann.post(self, AnnNode::SubItem(ti.hir_id())) |
e9174d1e SL |
990 | } |
991 | ||
dfeec247 | 992 | pub fn print_impl_item(&mut self, ii: &hir::ImplItem<'_>) { |
6a06907d | 993 | self.ann.pre(self, AnnNode::SubItem(ii.hir_id())); |
416331ca XL |
994 | self.hardbreak_if_not_bol(); |
995 | self.maybe_print_comment(ii.span.lo()); | |
6a06907d | 996 | self.print_outer_attributes(self.attrs(ii.hir_id())); |
416331ca | 997 | self.print_defaultness(ii.defaultness); |
54a0048b | 998 | |
e74abb32 | 999 | match ii.kind { |
32a655c1 | 1000 | hir::ImplItemKind::Const(ref ty, expr) => { |
416331ca | 1001 | self.print_associated_const(ii.ident, &ty, Some(expr), &ii.vis); |
e9174d1e | 1002 | } |
ba9703b0 | 1003 | hir::ImplItemKind::Fn(ref sig, body) => { |
416331ca XL |
1004 | self.head(""); |
1005 | self.print_method_sig(ii.ident, sig, &ii.generics, &ii.vis, &[], Some(body)); | |
1006 | self.nbsp(); | |
1007 | self.end(); // need to close a box | |
1008 | self.end(); // need to close a box | |
1009 | self.ann.nested(self, Nested::Body(body)); | |
e9174d1e | 1010 | } |
416331ca | 1011 | hir::ImplItemKind::TyAlias(ref ty) => { |
74b04a01 | 1012 | self.print_associated_type(ii.ident, &ii.generics, None, Some(ty)); |
8faf50e0 | 1013 | } |
e9174d1e | 1014 | } |
6a06907d | 1015 | self.ann.post(self, AnnNode::SubItem(ii.hir_id())) |
e9174d1e SL |
1016 | } |
1017 | ||
dfeec247 | 1018 | pub fn print_local(&mut self, init: Option<&hir::Expr<'_>>, decl: impl Fn(&mut Self)) { |
416331ca XL |
1019 | self.space_if_not_bol(); |
1020 | self.ibox(INDENT_UNIT); | |
1021 | self.word_nbsp("let"); | |
48663c56 | 1022 | |
416331ca XL |
1023 | self.ibox(INDENT_UNIT); |
1024 | decl(self); | |
1025 | self.end(); | |
48663c56 XL |
1026 | |
1027 | if let Some(ref init) = init { | |
416331ca XL |
1028 | self.nbsp(); |
1029 | self.word_space("="); | |
1030 | self.print_expr(&init); | |
48663c56 XL |
1031 | } |
1032 | self.end() | |
1033 | } | |
1034 | ||
dfeec247 | 1035 | pub fn print_stmt(&mut self, st: &hir::Stmt<'_>) { |
416331ca | 1036 | self.maybe_print_comment(st.span.lo()); |
e74abb32 | 1037 | match st.kind { |
9fa01778 | 1038 | hir::StmtKind::Local(ref loc) => { |
416331ca | 1039 | self.print_local(loc.init.as_deref(), |this| this.print_local_decl(&loc)); |
9fa01778 | 1040 | } |
dfeec247 | 1041 | hir::StmtKind::Item(item) => self.ann.nested(self, Nested::Item(item)), |
9fa01778 | 1042 | hir::StmtKind::Expr(ref expr) => { |
416331ca XL |
1043 | self.space_if_not_bol(); |
1044 | self.print_expr(&expr); | |
e9174d1e | 1045 | } |
9fa01778 | 1046 | hir::StmtKind::Semi(ref expr) => { |
416331ca XL |
1047 | self.space_if_not_bol(); |
1048 | self.print_expr(&expr); | |
1049 | self.s.word(";"); | |
e9174d1e SL |
1050 | } |
1051 | } | |
e74abb32 | 1052 | if stmt_ends_with_semi(&st.kind) { |
416331ca | 1053 | self.s.word(";"); |
e9174d1e SL |
1054 | } |
1055 | self.maybe_print_trailing_comment(st.span, None) | |
1056 | } | |
1057 | ||
dfeec247 | 1058 | pub fn print_block(&mut self, blk: &hir::Block<'_>) { |
e9174d1e SL |
1059 | self.print_block_with_attrs(blk, &[]) |
1060 | } | |
1061 | ||
dfeec247 | 1062 | pub fn print_block_unclosed(&mut self, blk: &hir::Block<'_>) { |
416331ca | 1063 | self.print_block_maybe_unclosed(blk, &[], false) |
e9174d1e SL |
1064 | } |
1065 | ||
dfeec247 | 1066 | pub fn print_block_with_attrs(&mut self, blk: &hir::Block<'_>, attrs: &[ast::Attribute]) { |
416331ca | 1067 | self.print_block_maybe_unclosed(blk, attrs, true) |
e9174d1e SL |
1068 | } |
1069 | ||
dfeec247 XL |
1070 | pub fn print_block_maybe_unclosed( |
1071 | &mut self, | |
1072 | blk: &hir::Block<'_>, | |
1073 | attrs: &[ast::Attribute], | |
1074 | close_box: bool, | |
1075 | ) { | |
e9174d1e | 1076 | match blk.rules { |
ba9703b0 XL |
1077 | hir::BlockCheckMode::UnsafeBlock(..) => self.word_space("unsafe"), |
1078 | hir::BlockCheckMode::PushUnsafeBlock(..) => self.word_space("push_unsafe"), | |
1079 | hir::BlockCheckMode::PopUnsafeBlock(..) => self.word_space("pop_unsafe"), | |
1080 | hir::BlockCheckMode::DefaultBlock => (), | |
e9174d1e | 1081 | } |
416331ca XL |
1082 | self.maybe_print_comment(blk.span.lo()); |
1083 | self.ann.pre(self, AnnNode::Block(blk)); | |
1084 | self.bopen(); | |
e9174d1e | 1085 | |
416331ca | 1086 | self.print_inner_attributes(attrs); |
e9174d1e | 1087 | |
dfeec247 | 1088 | for st in blk.stmts { |
416331ca | 1089 | self.print_stmt(st); |
e9174d1e | 1090 | } |
b7449926 | 1091 | if let Some(ref expr) = blk.expr { |
416331ca XL |
1092 | self.space_if_not_bol(); |
1093 | self.print_expr(&expr); | |
1094 | self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi())); | |
e9174d1e | 1095 | } |
416331ca | 1096 | self.bclose_maybe_open(blk.span, close_box); |
b7449926 | 1097 | self.ann.post(self, AnnNode::Block(blk)) |
e9174d1e SL |
1098 | } |
1099 | ||
5869c6ff XL |
1100 | fn print_else(&mut self, els: Option<&hir::Expr<'_>>) { |
1101 | match els { | |
1102 | Some(_else) => { | |
1103 | match _else.kind { | |
1104 | // "another else-if" | |
1105 | hir::ExprKind::If(ref i, ref then, ref e) => { | |
1106 | self.cbox(INDENT_UNIT - 1); | |
1107 | self.ibox(0); | |
1108 | self.s.word(" else if "); | |
1109 | self.print_expr_as_cond(&i); | |
1110 | self.s.space(); | |
1111 | self.print_expr(&then); | |
1112 | self.print_else(e.as_ref().map(|e| &**e)) | |
1113 | } | |
1114 | // "final else" | |
1115 | hir::ExprKind::Block(ref b, _) => { | |
1116 | self.cbox(INDENT_UNIT - 1); | |
1117 | self.ibox(0); | |
1118 | self.s.word(" else "); | |
1119 | self.print_block(&b) | |
1120 | } | |
1121 | // BLEAH, constraints would be great here | |
1122 | _ => { | |
1123 | panic!("print_if saw if with weird alternative"); | |
1124 | } | |
1125 | } | |
1126 | } | |
1127 | _ => {} | |
1128 | } | |
1129 | } | |
1130 | ||
1131 | pub fn print_if( | |
1132 | &mut self, | |
1133 | test: &hir::Expr<'_>, | |
1134 | blk: &hir::Expr<'_>, | |
1135 | elseopt: Option<&hir::Expr<'_>>, | |
1136 | ) { | |
1137 | self.head("if"); | |
1138 | self.print_expr_as_cond(test); | |
1139 | self.s.space(); | |
1140 | self.print_expr(blk); | |
1141 | self.print_else(elseopt) | |
1142 | } | |
1143 | ||
416331ca | 1144 | pub fn print_anon_const(&mut self, constant: &hir::AnonConst) { |
94b46f34 XL |
1145 | self.ann.nested(self, Nested::Body(constant.body)) |
1146 | } | |
e9174d1e | 1147 | |
dfeec247 | 1148 | fn print_call_post(&mut self, args: &[hir::Expr<'_>]) { |
416331ca XL |
1149 | self.popen(); |
1150 | self.commasep_exprs(Inconsistent, args); | |
e9174d1e SL |
1151 | self.pclose() |
1152 | } | |
1153 | ||
dfeec247 | 1154 | pub fn print_expr_maybe_paren(&mut self, expr: &hir::Expr<'_>, prec: i8) { |
2c00a5a8 | 1155 | let needs_par = expr.precedence().order() < prec; |
ea8adc8c | 1156 | if needs_par { |
416331ca | 1157 | self.popen(); |
ea8adc8c | 1158 | } |
416331ca | 1159 | self.print_expr(expr); |
ea8adc8c | 1160 | if needs_par { |
416331ca | 1161 | self.pclose(); |
ea8adc8c | 1162 | } |
ea8adc8c XL |
1163 | } |
1164 | ||
1165 | /// Print an expr using syntax that's acceptable in a condition position, such as the `cond` in | |
1166 | /// `if cond { ... }`. | |
dfeec247 | 1167 | pub fn print_expr_as_cond(&mut self, expr: &hir::Expr<'_>) { |
e74abb32 | 1168 | let needs_par = match expr.kind { |
ea8adc8c XL |
1169 | // These cases need parens due to the parse error observed in #26461: `if return {}` |
1170 | // parses as the erroneous construct `if (return {})`, not `if (return) {}`. | |
dfeec247 | 1171 | hir::ExprKind::Closure(..) | hir::ExprKind::Ret(..) | hir::ExprKind::Break(..) => true, |
ea8adc8c XL |
1172 | |
1173 | _ => contains_exterior_struct_lit(expr), | |
1174 | }; | |
1175 | ||
e9174d1e | 1176 | if needs_par { |
416331ca | 1177 | self.popen(); |
e9174d1e | 1178 | } |
416331ca | 1179 | self.print_expr(expr); |
e9174d1e | 1180 | if needs_par { |
416331ca | 1181 | self.pclose(); |
e9174d1e | 1182 | } |
e9174d1e SL |
1183 | } |
1184 | ||
dfeec247 | 1185 | fn print_expr_vec(&mut self, exprs: &[hir::Expr<'_>]) { |
416331ca XL |
1186 | self.ibox(INDENT_UNIT); |
1187 | self.s.word("["); | |
1188 | self.commasep_exprs(Inconsistent, exprs); | |
1189 | self.s.word("]"); | |
e9174d1e SL |
1190 | self.end() |
1191 | } | |
1192 | ||
29967ef6 XL |
1193 | fn print_expr_anon_const(&mut self, anon_const: &hir::AnonConst) { |
1194 | self.ibox(INDENT_UNIT); | |
1195 | self.s.word_space("const"); | |
1196 | self.print_anon_const(anon_const); | |
1197 | self.end() | |
1198 | } | |
1199 | ||
dfeec247 | 1200 | fn print_expr_repeat(&mut self, element: &hir::Expr<'_>, count: &hir::AnonConst) { |
416331ca XL |
1201 | self.ibox(INDENT_UNIT); |
1202 | self.s.word("["); | |
1203 | self.print_expr(element); | |
1204 | self.word_space(";"); | |
1205 | self.print_anon_const(count); | |
1206 | self.s.word("]"); | |
e9174d1e SL |
1207 | self.end() |
1208 | } | |
1209 | ||
dfeec247 XL |
1210 | fn print_expr_struct( |
1211 | &mut self, | |
1212 | qpath: &hir::QPath<'_>, | |
6a06907d | 1213 | fields: &[hir::ExprField<'_>], |
ba9703b0 | 1214 | wth: &Option<&hir::Expr<'_>>, |
dfeec247 | 1215 | ) { |
416331ca XL |
1216 | self.print_qpath(qpath, true); |
1217 | self.s.word("{"); | |
dfeec247 XL |
1218 | self.commasep_cmnt( |
1219 | Consistent, | |
6a06907d | 1220 | fields, |
dfeec247 XL |
1221 | |s, field| { |
1222 | s.ibox(INDENT_UNIT); | |
1223 | if !field.is_shorthand { | |
1224 | s.print_ident(field.ident); | |
1225 | s.word_space(":"); | |
1226 | } | |
1227 | s.print_expr(&field.expr); | |
1228 | s.end() | |
1229 | }, | |
1230 | |f| f.span, | |
1231 | ); | |
b039eaaf SL |
1232 | match *wth { |
1233 | Some(ref expr) => { | |
416331ca | 1234 | self.ibox(INDENT_UNIT); |
b039eaaf | 1235 | if !fields.is_empty() { |
416331ca XL |
1236 | self.s.word(","); |
1237 | self.s.space(); | |
e9174d1e | 1238 | } |
416331ca XL |
1239 | self.s.word(".."); |
1240 | self.print_expr(&expr); | |
1241 | self.end(); | |
e9174d1e | 1242 | } |
dfeec247 XL |
1243 | _ => { |
1244 | if !fields.is_empty() { | |
1245 | self.s.word(",") | |
1246 | } | |
1247 | } | |
e9174d1e | 1248 | } |
416331ca | 1249 | self.s.word("}"); |
e9174d1e SL |
1250 | } |
1251 | ||
dfeec247 | 1252 | fn print_expr_tup(&mut self, exprs: &[hir::Expr<'_>]) { |
416331ca XL |
1253 | self.popen(); |
1254 | self.commasep_exprs(Inconsistent, exprs); | |
e9174d1e | 1255 | if exprs.len() == 1 { |
416331ca | 1256 | self.s.word(","); |
e9174d1e SL |
1257 | } |
1258 | self.pclose() | |
1259 | } | |
1260 | ||
dfeec247 | 1261 | fn print_expr_call(&mut self, func: &hir::Expr<'_>, args: &[hir::Expr<'_>]) { |
e74abb32 XL |
1262 | let prec = match func.kind { |
1263 | hir::ExprKind::Field(..) => parser::PREC_FORCE_PAREN, | |
1264 | _ => parser::PREC_POSTFIX, | |
1265 | }; | |
ea8adc8c | 1266 | |
416331ca | 1267 | self.print_expr_maybe_paren(func, prec); |
e9174d1e SL |
1268 | self.print_call_post(args) |
1269 | } | |
1270 | ||
dfeec247 | 1271 | fn print_expr_method_call(&mut self, segment: &hir::PathSegment<'_>, args: &[hir::Expr<'_>]) { |
e9174d1e | 1272 | let base_args = &args[1..]; |
416331ca XL |
1273 | self.print_expr_maybe_paren(&args[0], parser::PREC_POSTFIX); |
1274 | self.s.word("."); | |
1275 | self.print_ident(segment.ident); | |
8faf50e0 | 1276 | |
5869c6ff | 1277 | let generic_args = segment.args(); |
dc9dc135 | 1278 | if !generic_args.args.is_empty() || !generic_args.bindings.is_empty() { |
416331ca | 1279 | self.print_generic_args(generic_args, segment.infer_args, true); |
dc9dc135 XL |
1280 | } |
1281 | ||
e9174d1e SL |
1282 | self.print_call_post(base_args) |
1283 | } | |
1284 | ||
dfeec247 | 1285 | fn print_expr_binary(&mut self, op: hir::BinOp, lhs: &hir::Expr<'_>, rhs: &hir::Expr<'_>) { |
ea8adc8c XL |
1286 | let assoc_op = bin_op_to_assoc_op(op.node); |
1287 | let prec = assoc_op.precedence() as i8; | |
1288 | let fixity = assoc_op.fixity(); | |
1289 | ||
1290 | let (left_prec, right_prec) = match fixity { | |
1291 | Fixity::Left => (prec, prec + 1), | |
1292 | Fixity::Right => (prec + 1, prec), | |
1293 | Fixity::None => (prec + 1, prec + 1), | |
1294 | }; | |
1295 | ||
e74abb32 | 1296 | let left_prec = match (&lhs.kind, op.node) { |
abe05a73 XL |
1297 | // These cases need parens: `x as i32 < y` has the parser thinking that `i32 < y` is |
1298 | // the beginning of a path type. It starts trying to parse `x as (i32 < y ...` instead | |
1299 | // of `(x as i32) < ...`. We need to convince it _not_ to do that. | |
ba9703b0 XL |
1300 | (&hir::ExprKind::Cast { .. }, hir::BinOpKind::Lt | hir::BinOpKind::Shl) => { |
1301 | parser::PREC_FORCE_PAREN | |
1302 | } | |
abe05a73 XL |
1303 | _ => left_prec, |
1304 | }; | |
1305 | ||
416331ca XL |
1306 | self.print_expr_maybe_paren(lhs, left_prec); |
1307 | self.s.space(); | |
1308 | self.word_space(op.node.as_str()); | |
ea8adc8c | 1309 | self.print_expr_maybe_paren(rhs, right_prec) |
e9174d1e SL |
1310 | } |
1311 | ||
dfeec247 | 1312 | fn print_expr_unary(&mut self, op: hir::UnOp, expr: &hir::Expr<'_>) { |
416331ca | 1313 | self.s.word(op.as_str()); |
ea8adc8c | 1314 | self.print_expr_maybe_paren(expr, parser::PREC_PREFIX) |
e9174d1e SL |
1315 | } |
1316 | ||
dfeec247 XL |
1317 | fn print_expr_addr_of( |
1318 | &mut self, | |
1319 | kind: hir::BorrowKind, | |
1320 | mutability: hir::Mutability, | |
1321 | expr: &hir::Expr<'_>, | |
1322 | ) { | |
416331ca | 1323 | self.s.word("&"); |
60c5eb7d XL |
1324 | match kind { |
1325 | hir::BorrowKind::Ref => self.print_mutability(mutability, false), | |
1326 | hir::BorrowKind::Raw => { | |
1327 | self.word_nbsp("raw"); | |
1328 | self.print_mutability(mutability, true); | |
1329 | } | |
1330 | } | |
ea8adc8c | 1331 | self.print_expr_maybe_paren(expr, parser::PREC_PREFIX) |
e9174d1e SL |
1332 | } |
1333 | ||
416331ca XL |
1334 | fn print_literal(&mut self, lit: &hir::Lit) { |
1335 | self.maybe_print_comment(lit.span.lo()); | |
1336 | self.word(lit.node.to_lit_token().to_string()) | |
48663c56 XL |
1337 | } |
1338 | ||
dfeec247 | 1339 | pub fn print_expr(&mut self, expr: &hir::Expr<'_>) { |
416331ca | 1340 | self.maybe_print_comment(expr.span.lo()); |
6a06907d | 1341 | self.print_outer_attributes(self.attrs(expr.hir_id)); |
416331ca XL |
1342 | self.ibox(INDENT_UNIT); |
1343 | self.ann.pre(self, AnnNode::Expr(expr)); | |
e74abb32 | 1344 | match expr.kind { |
8faf50e0 | 1345 | hir::ExprKind::Box(ref expr) => { |
416331ca XL |
1346 | self.word_space("box"); |
1347 | self.print_expr_maybe_paren(expr, parser::PREC_PREFIX); | |
e9174d1e | 1348 | } |
8faf50e0 | 1349 | hir::ExprKind::Array(ref exprs) => { |
416331ca | 1350 | self.print_expr_vec(exprs); |
e9174d1e | 1351 | } |
29967ef6 XL |
1352 | hir::ExprKind::ConstBlock(ref anon_const) => { |
1353 | self.print_expr_anon_const(anon_const); | |
1354 | } | |
8faf50e0 | 1355 | hir::ExprKind::Repeat(ref element, ref count) => { |
416331ca | 1356 | self.print_expr_repeat(&element, count); |
e9174d1e | 1357 | } |
dfeec247 XL |
1358 | hir::ExprKind::Struct(ref qpath, fields, ref wth) => { |
1359 | self.print_expr_struct(qpath, fields, wth); | |
e9174d1e | 1360 | } |
8faf50e0 | 1361 | hir::ExprKind::Tup(ref exprs) => { |
416331ca | 1362 | self.print_expr_tup(exprs); |
e9174d1e | 1363 | } |
8faf50e0 | 1364 | hir::ExprKind::Call(ref func, ref args) => { |
416331ca | 1365 | self.print_expr_call(&func, args); |
e9174d1e | 1366 | } |
f035d41b | 1367 | hir::ExprKind::MethodCall(ref segment, _, ref args, _) => { |
416331ca | 1368 | self.print_expr_method_call(segment, args); |
e9174d1e | 1369 | } |
8faf50e0 | 1370 | hir::ExprKind::Binary(op, ref lhs, ref rhs) => { |
416331ca | 1371 | self.print_expr_binary(op, &lhs, &rhs); |
e9174d1e | 1372 | } |
8faf50e0 | 1373 | hir::ExprKind::Unary(op, ref expr) => { |
416331ca | 1374 | self.print_expr_unary(op, &expr); |
e9174d1e | 1375 | } |
60c5eb7d XL |
1376 | hir::ExprKind::AddrOf(k, m, ref expr) => { |
1377 | self.print_expr_addr_of(k, m, &expr); | |
e9174d1e | 1378 | } |
8faf50e0 | 1379 | hir::ExprKind::Lit(ref lit) => { |
416331ca | 1380 | self.print_literal(&lit); |
e9174d1e | 1381 | } |
8faf50e0 | 1382 | hir::ExprKind::Cast(ref expr, ref ty) => { |
ea8adc8c | 1383 | let prec = AssocOp::As.precedence() as i8; |
416331ca XL |
1384 | self.print_expr_maybe_paren(&expr, prec); |
1385 | self.s.space(); | |
1386 | self.word_space("as"); | |
1387 | self.print_type(&ty); | |
e9174d1e | 1388 | } |
8faf50e0 | 1389 | hir::ExprKind::Type(ref expr, ref ty) => { |
ea8adc8c | 1390 | let prec = AssocOp::Colon.precedence() as i8; |
416331ca XL |
1391 | self.print_expr_maybe_paren(&expr, prec); |
1392 | self.word_space(":"); | |
1393 | self.print_type(&ty); | |
9cc50fc6 | 1394 | } |
48663c56 XL |
1395 | hir::ExprKind::DropTemps(ref init) => { |
1396 | // Print `{`: | |
416331ca XL |
1397 | self.cbox(INDENT_UNIT); |
1398 | self.ibox(0); | |
1399 | self.bopen(); | |
48663c56 XL |
1400 | |
1401 | // Print `let _t = $init;`: | |
f9f354fc | 1402 | let temp = Ident::from_str("_t"); |
416331ca XL |
1403 | self.print_local(Some(init), |this| this.print_ident(temp)); |
1404 | self.s.word(";"); | |
48663c56 XL |
1405 | |
1406 | // Print `_t`: | |
416331ca XL |
1407 | self.space_if_not_bol(); |
1408 | self.print_ident(temp); | |
48663c56 XL |
1409 | |
1410 | // Print `}`: | |
416331ca | 1411 | self.bclose_maybe_open(expr.span, true); |
e9174d1e | 1412 | } |
5869c6ff XL |
1413 | hir::ExprKind::If(ref test, ref blk, ref elseopt) => { |
1414 | self.print_if(&test, &blk, elseopt.as_ref().map(|e| &**e)); | |
1415 | } | |
1416 | hir::ExprKind::Loop(ref blk, opt_label, _, _) => { | |
2c00a5a8 | 1417 | if let Some(label) = opt_label { |
416331ca XL |
1418 | self.print_ident(label.ident); |
1419 | self.word_space(":"); | |
e9174d1e | 1420 | } |
416331ca XL |
1421 | self.head("loop"); |
1422 | self.s.space(); | |
1423 | self.print_block(&blk); | |
e9174d1e | 1424 | } |
dfeec247 | 1425 | hir::ExprKind::Match(ref expr, arms, _) => { |
416331ca XL |
1426 | self.cbox(INDENT_UNIT); |
1427 | self.ibox(INDENT_UNIT); | |
1428 | self.word_nbsp("match"); | |
1429 | self.print_expr_as_cond(&expr); | |
1430 | self.s.space(); | |
1431 | self.bopen(); | |
e9174d1e | 1432 | for arm in arms { |
416331ca | 1433 | self.print_arm(arm); |
e9174d1e | 1434 | } |
416331ca | 1435 | self.bclose(expr.span); |
e9174d1e | 1436 | } |
8faf50e0 | 1437 | hir::ExprKind::Closure(capture_clause, ref decl, body, _fn_decl_span, _gen) => { |
416331ca | 1438 | self.print_capture_clause(capture_clause); |
e9174d1e | 1439 | |
e1599b0c | 1440 | self.print_closure_params(&decl, body); |
416331ca | 1441 | self.s.space(); |
e9174d1e | 1442 | |
e1599b0c | 1443 | // This is a bare expression. |
416331ca XL |
1444 | self.ann.nested(self, Nested::Body(body)); |
1445 | self.end(); // need to close a box | |
e9174d1e | 1446 | |
e1599b0c | 1447 | // A box will be closed by `print_expr`, but we didn't want an overall |
e9174d1e SL |
1448 | // wrapper so we closed the corresponding opening. so create an |
1449 | // empty box to satisfy the close. | |
416331ca | 1450 | self.ibox(0); |
e9174d1e | 1451 | } |
8faf50e0 | 1452 | hir::ExprKind::Block(ref blk, opt_label) => { |
94b46f34 | 1453 | if let Some(label) = opt_label { |
416331ca XL |
1454 | self.print_ident(label.ident); |
1455 | self.word_space(":"); | |
94b46f34 | 1456 | } |
e1599b0c | 1457 | // containing cbox, will be closed by print-block at `}` |
416331ca | 1458 | self.cbox(INDENT_UNIT); |
e1599b0c | 1459 | // head-box, will be closed by print-block after `{` |
416331ca XL |
1460 | self.ibox(0); |
1461 | self.print_block(&blk); | |
e9174d1e | 1462 | } |
dfeec247 | 1463 | hir::ExprKind::Assign(ref lhs, ref rhs, _) => { |
ea8adc8c | 1464 | let prec = AssocOp::Assign.precedence() as i8; |
416331ca XL |
1465 | self.print_expr_maybe_paren(&lhs, prec + 1); |
1466 | self.s.space(); | |
1467 | self.word_space("="); | |
1468 | self.print_expr_maybe_paren(&rhs, prec); | |
e9174d1e | 1469 | } |
8faf50e0 | 1470 | hir::ExprKind::AssignOp(op, ref lhs, ref rhs) => { |
ea8adc8c | 1471 | let prec = AssocOp::Assign.precedence() as i8; |
416331ca XL |
1472 | self.print_expr_maybe_paren(&lhs, prec + 1); |
1473 | self.s.space(); | |
1474 | self.s.word(op.node.as_str()); | |
1475 | self.word_space("="); | |
1476 | self.print_expr_maybe_paren(&rhs, prec); | |
e9174d1e | 1477 | } |
8faf50e0 | 1478 | hir::ExprKind::Field(ref expr, ident) => { |
416331ca XL |
1479 | self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX); |
1480 | self.s.word("."); | |
1481 | self.print_ident(ident); | |
e9174d1e | 1482 | } |
8faf50e0 | 1483 | hir::ExprKind::Index(ref expr, ref index) => { |
416331ca XL |
1484 | self.print_expr_maybe_paren(&expr, parser::PREC_POSTFIX); |
1485 | self.s.word("["); | |
1486 | self.print_expr(&index); | |
1487 | self.s.word("]"); | |
e9174d1e | 1488 | } |
dfeec247 | 1489 | hir::ExprKind::Path(ref qpath) => self.print_qpath(qpath, true), |
8faf50e0 | 1490 | hir::ExprKind::Break(destination, ref opt_expr) => { |
416331ca XL |
1491 | self.s.word("break"); |
1492 | self.s.space(); | |
2c00a5a8 | 1493 | if let Some(label) = destination.label { |
416331ca XL |
1494 | self.print_ident(label.ident); |
1495 | self.s.space(); | |
476ff2be SL |
1496 | } |
1497 | if let Some(ref expr) = *opt_expr { | |
416331ca XL |
1498 | self.print_expr_maybe_paren(expr, parser::PREC_JUMP); |
1499 | self.s.space(); | |
e9174d1e SL |
1500 | } |
1501 | } | |
8faf50e0 | 1502 | hir::ExprKind::Continue(destination) => { |
416331ca XL |
1503 | self.s.word("continue"); |
1504 | self.s.space(); | |
2c00a5a8 | 1505 | if let Some(label) = destination.label { |
416331ca XL |
1506 | self.print_ident(label.ident); |
1507 | self.s.space() | |
e9174d1e SL |
1508 | } |
1509 | } | |
8faf50e0 | 1510 | hir::ExprKind::Ret(ref result) => { |
416331ca | 1511 | self.s.word("return"); |
b7449926 | 1512 | if let Some(ref expr) = *result { |
416331ca XL |
1513 | self.s.word(" "); |
1514 | self.print_expr_maybe_paren(&expr, parser::PREC_JUMP); | |
e9174d1e SL |
1515 | } |
1516 | } | |
f9f354fc XL |
1517 | hir::ExprKind::InlineAsm(ref a) => { |
1518 | enum AsmArg<'a> { | |
1519 | Template(String), | |
1520 | Operand(&'a hir::InlineAsmOperand<'a>), | |
1521 | Options(ast::InlineAsmOptions), | |
1522 | } | |
1523 | ||
1524 | let mut args = vec![]; | |
1525 | args.push(AsmArg::Template(ast::InlineAsmTemplatePiece::to_string(&a.template))); | |
fc512014 | 1526 | args.extend(a.operands.iter().map(|(o, _)| AsmArg::Operand(o))); |
f9f354fc XL |
1527 | if !a.options.is_empty() { |
1528 | args.push(AsmArg::Options(a.options)); | |
1529 | } | |
1530 | ||
1531 | self.word("asm!"); | |
1532 | self.popen(); | |
1533 | self.commasep(Consistent, &args, |s, arg| match arg { | |
1534 | AsmArg::Template(template) => s.print_string(&template, ast::StrStyle::Cooked), | |
1535 | AsmArg::Operand(op) => match op { | |
1536 | hir::InlineAsmOperand::In { reg, expr } => { | |
1537 | s.word("in"); | |
1538 | s.popen(); | |
1539 | s.word(format!("{}", reg)); | |
1540 | s.pclose(); | |
1541 | s.space(); | |
1542 | s.print_expr(expr); | |
1543 | } | |
1544 | hir::InlineAsmOperand::Out { reg, late, expr } => { | |
1545 | s.word(if *late { "lateout" } else { "out" }); | |
1546 | s.popen(); | |
1547 | s.word(format!("{}", reg)); | |
1548 | s.pclose(); | |
1549 | s.space(); | |
1550 | match expr { | |
1551 | Some(expr) => s.print_expr(expr), | |
1552 | None => s.word("_"), | |
1553 | } | |
1554 | } | |
1555 | hir::InlineAsmOperand::InOut { reg, late, expr } => { | |
1556 | s.word(if *late { "inlateout" } else { "inout" }); | |
1557 | s.popen(); | |
1558 | s.word(format!("{}", reg)); | |
1559 | s.pclose(); | |
1560 | s.space(); | |
1561 | s.print_expr(expr); | |
1562 | } | |
1563 | hir::InlineAsmOperand::SplitInOut { reg, late, in_expr, out_expr } => { | |
1564 | s.word(if *late { "inlateout" } else { "inout" }); | |
1565 | s.popen(); | |
1566 | s.word(format!("{}", reg)); | |
1567 | s.pclose(); | |
1568 | s.space(); | |
1569 | s.print_expr(in_expr); | |
1570 | s.space(); | |
1571 | s.word_space("=>"); | |
1572 | match out_expr { | |
1573 | Some(out_expr) => s.print_expr(out_expr), | |
1574 | None => s.word("_"), | |
1575 | } | |
1576 | } | |
1577 | hir::InlineAsmOperand::Const { expr } => { | |
1578 | s.word("const"); | |
1579 | s.space(); | |
1580 | s.print_expr(expr); | |
1581 | } | |
1582 | hir::InlineAsmOperand::Sym { expr } => { | |
1583 | s.word("sym"); | |
1584 | s.space(); | |
1585 | s.print_expr(expr); | |
1586 | } | |
1587 | }, | |
1588 | AsmArg::Options(opts) => { | |
1589 | s.word("options"); | |
1590 | s.popen(); | |
1591 | let mut options = vec![]; | |
1592 | if opts.contains(ast::InlineAsmOptions::PURE) { | |
1593 | options.push("pure"); | |
1594 | } | |
1595 | if opts.contains(ast::InlineAsmOptions::NOMEM) { | |
1596 | options.push("nomem"); | |
1597 | } | |
1598 | if opts.contains(ast::InlineAsmOptions::READONLY) { | |
1599 | options.push("readonly"); | |
1600 | } | |
1601 | if opts.contains(ast::InlineAsmOptions::PRESERVES_FLAGS) { | |
1602 | options.push("preserves_flags"); | |
1603 | } | |
1604 | if opts.contains(ast::InlineAsmOptions::NORETURN) { | |
1605 | options.push("noreturn"); | |
1606 | } | |
1607 | if opts.contains(ast::InlineAsmOptions::NOSTACK) { | |
1608 | options.push("nostack"); | |
1609 | } | |
1610 | if opts.contains(ast::InlineAsmOptions::ATT_SYNTAX) { | |
1611 | options.push("att_syntax"); | |
1612 | } | |
1613 | s.commasep(Inconsistent, &options, |s, &opt| { | |
1614 | s.word(opt); | |
1615 | }); | |
1616 | s.pclose(); | |
1617 | } | |
1618 | }); | |
1619 | self.pclose(); | |
1620 | } | |
ba9703b0 | 1621 | hir::ExprKind::LlvmInlineAsm(ref a) => { |
60c5eb7d | 1622 | let i = &a.inner; |
ba9703b0 | 1623 | self.s.word("llvm_asm!"); |
416331ca | 1624 | self.popen(); |
3dfed10e | 1625 | self.print_symbol(i.asm, i.asm_str_style); |
416331ca | 1626 | self.word_space(":"); |
54a0048b SL |
1627 | |
1628 | let mut out_idx = 0; | |
60c5eb7d | 1629 | self.commasep(Inconsistent, &i.outputs, |s, out| { |
476ff2be SL |
1630 | let constraint = out.constraint.as_str(); |
1631 | let mut ch = constraint.chars(); | |
54a0048b SL |
1632 | match ch.next() { |
1633 | Some('=') if out.is_rw => { | |
dfeec247 | 1634 | s.print_string(&format!("+{}", ch.as_str()), ast::StrStyle::Cooked) |
92a42be0 | 1635 | } |
416331ca | 1636 | _ => s.print_string(&constraint, ast::StrStyle::Cooked), |
92a42be0 | 1637 | } |
416331ca | 1638 | s.popen(); |
60c5eb7d | 1639 | s.print_expr(&a.outputs_exprs[out_idx]); |
416331ca | 1640 | s.pclose(); |
54a0048b | 1641 | out_idx += 1; |
416331ca XL |
1642 | }); |
1643 | self.s.space(); | |
1644 | self.word_space(":"); | |
54a0048b SL |
1645 | |
1646 | let mut in_idx = 0; | |
3dfed10e XL |
1647 | self.commasep(Inconsistent, &i.inputs, |s, &co| { |
1648 | s.print_symbol(co, ast::StrStyle::Cooked); | |
416331ca | 1649 | s.popen(); |
60c5eb7d | 1650 | s.print_expr(&a.inputs_exprs[in_idx]); |
416331ca | 1651 | s.pclose(); |
54a0048b | 1652 | in_idx += 1; |
416331ca XL |
1653 | }); |
1654 | self.s.space(); | |
1655 | self.word_space(":"); | |
e9174d1e | 1656 | |
3dfed10e XL |
1657 | self.commasep(Inconsistent, &i.clobbers, |s, &co| { |
1658 | s.print_symbol(co, ast::StrStyle::Cooked); | |
416331ca | 1659 | }); |
e9174d1e | 1660 | |
92a42be0 | 1661 | let mut options = vec![]; |
60c5eb7d | 1662 | if i.volatile { |
e9174d1e SL |
1663 | options.push("volatile"); |
1664 | } | |
60c5eb7d | 1665 | if i.alignstack { |
e9174d1e SL |
1666 | options.push("alignstack"); |
1667 | } | |
ba9703b0 | 1668 | if i.dialect == ast::LlvmAsmDialect::Intel { |
e9174d1e SL |
1669 | options.push("intel"); |
1670 | } | |
1671 | ||
1672 | if !options.is_empty() { | |
416331ca XL |
1673 | self.s.space(); |
1674 | self.word_space(":"); | |
54a0048b | 1675 | self.commasep(Inconsistent, &options, |s, &co| { |
416331ca XL |
1676 | s.print_string(co, ast::StrStyle::Cooked); |
1677 | }); | |
e9174d1e SL |
1678 | } |
1679 | ||
416331ca | 1680 | self.pclose(); |
e9174d1e | 1681 | } |
dc9dc135 | 1682 | hir::ExprKind::Yield(ref expr, _) => { |
416331ca XL |
1683 | self.word_space("yield"); |
1684 | self.print_expr_maybe_paren(&expr, parser::PREC_JUMP); | |
ea8adc8c | 1685 | } |
0731742a | 1686 | hir::ExprKind::Err => { |
416331ca XL |
1687 | self.popen(); |
1688 | self.s.word("/*ERROR*/"); | |
1689 | self.pclose(); | |
0731742a | 1690 | } |
e9174d1e | 1691 | } |
416331ca | 1692 | self.ann.post(self, AnnNode::Expr(expr)); |
e9174d1e SL |
1693 | self.end() |
1694 | } | |
1695 | ||
dfeec247 | 1696 | pub fn print_local_decl(&mut self, loc: &hir::Local<'_>) { |
416331ca | 1697 | self.print_pat(&loc.pat); |
e9174d1e | 1698 | if let Some(ref ty) = loc.ty { |
416331ca XL |
1699 | self.word_space(":"); |
1700 | self.print_type(&ty); | |
e9174d1e | 1701 | } |
e9174d1e SL |
1702 | } |
1703 | ||
416331ca | 1704 | pub fn print_usize(&mut self, i: usize) { |
a1dfa0c6 | 1705 | self.s.word(i.to_string()) |
e9174d1e SL |
1706 | } |
1707 | ||
f9f354fc XL |
1708 | pub fn print_name(&mut self, name: Symbol) { |
1709 | self.print_ident(Ident::with_dummy_span(name)) | |
e9174d1e SL |
1710 | } |
1711 | ||
dfeec247 | 1712 | pub fn print_for_decl(&mut self, loc: &hir::Local<'_>, coll: &hir::Expr<'_>) { |
416331ca XL |
1713 | self.print_local_decl(loc); |
1714 | self.s.space(); | |
1715 | self.word_space("in"); | |
e9174d1e SL |
1716 | self.print_expr(coll) |
1717 | } | |
1718 | ||
dfeec247 | 1719 | pub fn print_path(&mut self, path: &hir::Path<'_>, colons_before_params: bool) { |
416331ca | 1720 | self.maybe_print_comment(path.span.lo()); |
e9174d1e | 1721 | |
32a655c1 SL |
1722 | for (i, segment) in path.segments.iter().enumerate() { |
1723 | if i > 0 { | |
416331ca | 1724 | self.s.word("::") |
e9174d1e | 1725 | } |
dc9dc135 | 1726 | if segment.ident.name != kw::PathRoot { |
416331ca | 1727 | self.print_ident(segment.ident); |
5869c6ff | 1728 | self.print_generic_args(segment.args(), segment.infer_args, colons_before_params); |
32a655c1 | 1729 | } |
e9174d1e | 1730 | } |
e9174d1e SL |
1731 | } |
1732 | ||
dfeec247 | 1733 | pub fn print_path_segment(&mut self, segment: &hir::PathSegment<'_>) { |
dc9dc135 | 1734 | if segment.ident.name != kw::PathRoot { |
416331ca | 1735 | self.print_ident(segment.ident); |
5869c6ff | 1736 | self.print_generic_args(segment.args(), segment.infer_args, false); |
13cf67c4 | 1737 | } |
13cf67c4 XL |
1738 | } |
1739 | ||
dfeec247 | 1740 | pub fn print_qpath(&mut self, qpath: &hir::QPath<'_>, colons_before_params: bool) { |
476ff2be | 1741 | match *qpath { |
dfeec247 | 1742 | hir::QPath::Resolved(None, ref path) => self.print_path(path, colons_before_params), |
476ff2be | 1743 | hir::QPath::Resolved(Some(ref qself), ref path) => { |
416331ca XL |
1744 | self.s.word("<"); |
1745 | self.print_type(qself); | |
1746 | self.s.space(); | |
1747 | self.word_space("as"); | |
476ff2be | 1748 | |
32a655c1 SL |
1749 | for (i, segment) in path.segments[..path.segments.len() - 1].iter().enumerate() { |
1750 | if i > 0 { | |
416331ca | 1751 | self.s.word("::") |
476ff2be | 1752 | } |
dc9dc135 | 1753 | if segment.ident.name != kw::PathRoot { |
416331ca | 1754 | self.print_ident(segment.ident); |
dfeec247 | 1755 | self.print_generic_args( |
5869c6ff | 1756 | segment.args(), |
dfeec247 XL |
1757 | segment.infer_args, |
1758 | colons_before_params, | |
1759 | ); | |
32a655c1 | 1760 | } |
476ff2be SL |
1761 | } |
1762 | ||
416331ca XL |
1763 | self.s.word(">"); |
1764 | self.s.word("::"); | |
476ff2be | 1765 | let item_segment = path.segments.last().unwrap(); |
416331ca | 1766 | self.print_ident(item_segment.ident); |
dfeec247 | 1767 | self.print_generic_args( |
5869c6ff | 1768 | item_segment.args(), |
dfeec247 XL |
1769 | item_segment.infer_args, |
1770 | colons_before_params, | |
1771 | ) | |
476ff2be SL |
1772 | } |
1773 | hir::QPath::TypeRelative(ref qself, ref item_segment) => { | |
e74abb32 XL |
1774 | // If we've got a compound-qualified-path, let's push an additional pair of angle |
1775 | // brackets, so that we pretty-print `<<A::B>::C>` as `<A::B>::C`, instead of just | |
1776 | // `A::B::C` (since the latter could be ambiguous to the user) | |
1777 | if let hir::TyKind::Path(hir::QPath::Resolved(None, _)) = &qself.kind { | |
1778 | self.print_type(qself); | |
1779 | } else { | |
1780 | self.s.word("<"); | |
1781 | self.print_type(qself); | |
1782 | self.s.word(">"); | |
1783 | } | |
1784 | ||
416331ca XL |
1785 | self.s.word("::"); |
1786 | self.print_ident(item_segment.ident); | |
dfeec247 | 1787 | self.print_generic_args( |
5869c6ff | 1788 | item_segment.args(), |
dfeec247 XL |
1789 | item_segment.infer_args, |
1790 | colons_before_params, | |
1791 | ) | |
476ff2be | 1792 | } |
3dfed10e XL |
1793 | hir::QPath::LangItem(lang_item, span) => { |
1794 | self.s.word("#[lang = \""); | |
1795 | self.print_ident(Ident::new(lang_item.name(), span)); | |
1796 | self.s.word("\"]"); | |
1797 | } | |
e9174d1e | 1798 | } |
e9174d1e SL |
1799 | } |
1800 | ||
dfeec247 XL |
1801 | fn print_generic_args( |
1802 | &mut self, | |
1803 | generic_args: &hir::GenericArgs<'_>, | |
1804 | infer_args: bool, | |
1805 | colons_before_params: bool, | |
1806 | ) { | |
8faf50e0 | 1807 | if generic_args.parenthesized { |
416331ca XL |
1808 | self.s.word("("); |
1809 | self.commasep(Inconsistent, generic_args.inputs(), |s, ty| s.print_type(&ty)); | |
1810 | self.s.word(")"); | |
e9174d1e | 1811 | |
416331ca XL |
1812 | self.space_if_not_bol(); |
1813 | self.word_space("->"); | |
1814 | self.print_type(generic_args.bindings[0].ty()); | |
3b2f2976 XL |
1815 | } else { |
1816 | let start = if colons_before_params { "::<" } else { "<" }; | |
1817 | let empty = Cell::new(true); | |
1818 | let start_or_comma = |this: &mut Self| { | |
1819 | if empty.get() { | |
1820 | empty.set(false); | |
1821 | this.s.word(start) | |
1822 | } else { | |
1823 | this.word_space(",") | |
e9174d1e | 1824 | } |
3b2f2976 | 1825 | }; |
e9174d1e | 1826 | |
9fa01778 XL |
1827 | let mut nonelided_generic_args: bool = false; |
1828 | let elide_lifetimes = generic_args.args.iter().all(|arg| match arg { | |
1829 | GenericArg::Lifetime(lt) => lt.is_elided(), | |
1830 | _ => { | |
1831 | nonelided_generic_args = true; | |
1832 | true | |
476ff2be | 1833 | } |
9fa01778 XL |
1834 | }); |
1835 | ||
1836 | if nonelided_generic_args { | |
416331ca | 1837 | start_or_comma(self); |
dfeec247 XL |
1838 | self.commasep( |
1839 | Inconsistent, | |
1840 | &generic_args.args, | |
1841 | |s, generic_arg| match generic_arg { | |
9fa01778 | 1842 | GenericArg::Lifetime(lt) if !elide_lifetimes => s.print_lifetime(lt), |
dfeec247 | 1843 | GenericArg::Lifetime(_) => {} |
8faf50e0 | 1844 | GenericArg::Type(ty) => s.print_type(ty), |
9fa01778 | 1845 | GenericArg::Const(ct) => s.print_anon_const(&ct.value), |
dfeec247 XL |
1846 | }, |
1847 | ); | |
3b2f2976 | 1848 | } |
e9174d1e | 1849 | |
dc9dc135 XL |
1850 | // FIXME(eddyb): this would leak into error messages (e.g., |
1851 | // "non-exhaustive patterns: `Some::<..>(_)` not covered"). | |
1852 | if infer_args && false { | |
416331ca XL |
1853 | start_or_comma(self); |
1854 | self.s.word(".."); | |
e9174d1e SL |
1855 | } |
1856 | ||
8faf50e0 | 1857 | for binding in generic_args.bindings.iter() { |
416331ca XL |
1858 | start_or_comma(self); |
1859 | self.print_ident(binding.ident); | |
5869c6ff | 1860 | self.print_generic_args(binding.gen_args, false, false); |
416331ca | 1861 | self.s.space(); |
dc9dc135 XL |
1862 | match generic_args.bindings[0].kind { |
1863 | hir::TypeBindingKind::Equality { ref ty } => { | |
416331ca XL |
1864 | self.word_space("="); |
1865 | self.print_type(ty); | |
dc9dc135 | 1866 | } |
dfeec247 | 1867 | hir::TypeBindingKind::Constraint { bounds } => { |
416331ca | 1868 | self.print_bounds(":", bounds); |
dc9dc135 XL |
1869 | } |
1870 | } | |
3b2f2976 | 1871 | } |
e9174d1e | 1872 | |
3b2f2976 | 1873 | if !empty.get() { |
416331ca | 1874 | self.s.word(">") |
e9174d1e SL |
1875 | } |
1876 | } | |
e9174d1e SL |
1877 | } |
1878 | ||
dfeec247 | 1879 | pub fn print_pat(&mut self, pat: &hir::Pat<'_>) { |
416331ca XL |
1880 | self.maybe_print_comment(pat.span.lo()); |
1881 | self.ann.pre(self, AnnNode::Pat(pat)); | |
92a42be0 SL |
1882 | // Pat isn't normalized, but the beauty of it |
1883 | // is that it doesn't matter | |
e74abb32 | 1884 | match pat.kind { |
416331ca | 1885 | PatKind::Wild => self.s.word("_"), |
532ac7d7 | 1886 | PatKind::Binding(binding_mode, _, ident, ref sub) => { |
e9174d1e | 1887 | match binding_mode { |
3b2f2976 | 1888 | hir::BindingAnnotation::Ref => { |
416331ca | 1889 | self.word_nbsp("ref"); |
dfeec247 | 1890 | self.print_mutability(hir::Mutability::Not, false); |
3b2f2976 XL |
1891 | } |
1892 | hir::BindingAnnotation::RefMut => { | |
416331ca | 1893 | self.word_nbsp("ref"); |
dfeec247 | 1894 | self.print_mutability(hir::Mutability::Mut, false); |
e9174d1e | 1895 | } |
3b2f2976 XL |
1896 | hir::BindingAnnotation::Unannotated => {} |
1897 | hir::BindingAnnotation::Mutable => { | |
416331ca | 1898 | self.word_nbsp("mut"); |
e9174d1e SL |
1899 | } |
1900 | } | |
416331ca | 1901 | self.print_ident(ident); |
3157f602 | 1902 | if let Some(ref p) = *sub { |
416331ca XL |
1903 | self.s.word("@"); |
1904 | self.print_pat(&p); | |
e9174d1e SL |
1905 | } |
1906 | } | |
476ff2be | 1907 | PatKind::TupleStruct(ref qpath, ref elts, ddpos) => { |
416331ca XL |
1908 | self.print_qpath(qpath, true); |
1909 | self.popen(); | |
3157f602 | 1910 | if let Some(ddpos) = ddpos { |
416331ca | 1911 | self.commasep(Inconsistent, &elts[..ddpos], |s, p| s.print_pat(&p)); |
3157f602 | 1912 | if ddpos != 0 { |
416331ca | 1913 | self.word_space(","); |
e9174d1e | 1914 | } |
416331ca | 1915 | self.s.word(".."); |
3157f602 | 1916 | if ddpos != elts.len() { |
416331ca XL |
1917 | self.s.word(","); |
1918 | self.commasep(Inconsistent, &elts[ddpos..], |s, p| s.print_pat(&p)); | |
3157f602 XL |
1919 | } |
1920 | } else { | |
416331ca | 1921 | self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(&p)); |
e9174d1e | 1922 | } |
416331ca | 1923 | self.pclose(); |
e9174d1e | 1924 | } |
476ff2be | 1925 | PatKind::Path(ref qpath) => { |
416331ca | 1926 | self.print_qpath(qpath, true); |
e9174d1e | 1927 | } |
476ff2be | 1928 | PatKind::Struct(ref qpath, ref fields, etc) => { |
416331ca XL |
1929 | self.print_qpath(qpath, true); |
1930 | self.nbsp(); | |
1931 | self.word_space("{"); | |
dfeec247 XL |
1932 | self.commasep_cmnt( |
1933 | Consistent, | |
1934 | &fields[..], | |
1935 | |s, f| { | |
1936 | s.cbox(INDENT_UNIT); | |
1937 | if !f.is_shorthand { | |
1938 | s.print_ident(f.ident); | |
1939 | s.word_nbsp(":"); | |
1940 | } | |
1941 | s.print_pat(&f.pat); | |
1942 | s.end() | |
1943 | }, | |
1944 | |f| f.pat.span, | |
1945 | ); | |
e9174d1e | 1946 | if etc { |
b039eaaf | 1947 | if !fields.is_empty() { |
416331ca | 1948 | self.word_space(","); |
b039eaaf | 1949 | } |
416331ca | 1950 | self.s.word(".."); |
e9174d1e | 1951 | } |
416331ca XL |
1952 | self.s.space(); |
1953 | self.s.word("}"); | |
e9174d1e | 1954 | } |
e1599b0c XL |
1955 | PatKind::Or(ref pats) => { |
1956 | self.strsep("|", true, Inconsistent, &pats[..], |s, p| s.print_pat(&p)); | |
1957 | } | |
3157f602 | 1958 | PatKind::Tuple(ref elts, ddpos) => { |
416331ca | 1959 | self.popen(); |
3157f602 | 1960 | if let Some(ddpos) = ddpos { |
416331ca | 1961 | self.commasep(Inconsistent, &elts[..ddpos], |s, p| s.print_pat(&p)); |
3157f602 | 1962 | if ddpos != 0 { |
416331ca | 1963 | self.word_space(","); |
3157f602 | 1964 | } |
416331ca | 1965 | self.s.word(".."); |
3157f602 | 1966 | if ddpos != elts.len() { |
416331ca XL |
1967 | self.s.word(","); |
1968 | self.commasep(Inconsistent, &elts[ddpos..], |s, p| s.print_pat(&p)); | |
3157f602 XL |
1969 | } |
1970 | } else { | |
416331ca | 1971 | self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(&p)); |
3157f602 | 1972 | if elts.len() == 1 { |
416331ca | 1973 | self.s.word(","); |
3157f602 | 1974 | } |
e9174d1e | 1975 | } |
416331ca | 1976 | self.pclose(); |
e9174d1e | 1977 | } |
7453a54e | 1978 | PatKind::Box(ref inner) => { |
29967ef6 | 1979 | let is_range_inner = matches!(inner.kind, PatKind::Range(..)); |
416331ca | 1980 | self.s.word("box "); |
0531ce1d | 1981 | if is_range_inner { |
416331ca | 1982 | self.popen(); |
0531ce1d | 1983 | } |
416331ca | 1984 | self.print_pat(&inner); |
0531ce1d | 1985 | if is_range_inner { |
416331ca | 1986 | self.pclose(); |
0531ce1d | 1987 | } |
e9174d1e | 1988 | } |
7453a54e | 1989 | PatKind::Ref(ref inner, mutbl) => { |
29967ef6 | 1990 | let is_range_inner = matches!(inner.kind, PatKind::Range(..)); |
416331ca | 1991 | self.s.word("&"); |
60c5eb7d | 1992 | self.s.word(mutbl.prefix_str()); |
0531ce1d | 1993 | if is_range_inner { |
416331ca | 1994 | self.popen(); |
0531ce1d | 1995 | } |
416331ca | 1996 | self.print_pat(&inner); |
0531ce1d | 1997 | if is_range_inner { |
416331ca | 1998 | self.pclose(); |
0531ce1d | 1999 | } |
e9174d1e | 2000 | } |
416331ca | 2001 | PatKind::Lit(ref e) => self.print_expr(&e), |
32a655c1 | 2002 | PatKind::Range(ref begin, ref end, ref end_kind) => { |
dfeec247 XL |
2003 | if let Some(expr) = begin { |
2004 | self.print_expr(expr); | |
2005 | self.s.space(); | |
2006 | } | |
32a655c1 | 2007 | match *end_kind { |
416331ca XL |
2008 | RangeEnd::Included => self.s.word("..."), |
2009 | RangeEnd::Excluded => self.s.word(".."), | |
32a655c1 | 2010 | } |
dfeec247 XL |
2011 | if let Some(expr) = end { |
2012 | self.print_expr(expr); | |
2013 | } | |
e9174d1e | 2014 | } |
c30ab7b3 | 2015 | PatKind::Slice(ref before, ref slice, ref after) => { |
416331ca XL |
2016 | self.s.word("["); |
2017 | self.commasep(Inconsistent, &before[..], |s, p| s.print_pat(&p)); | |
e9174d1e | 2018 | if let Some(ref p) = *slice { |
b039eaaf | 2019 | if !before.is_empty() { |
416331ca | 2020 | self.word_space(","); |
b039eaaf | 2021 | } |
e74abb32 | 2022 | if let PatKind::Wild = p.kind { |
e1599b0c | 2023 | // Print nothing. |
8faf50e0 | 2024 | } else { |
416331ca | 2025 | self.print_pat(&p); |
e9174d1e | 2026 | } |
416331ca | 2027 | self.s.word(".."); |
b039eaaf | 2028 | if !after.is_empty() { |
416331ca | 2029 | self.word_space(","); |
b039eaaf | 2030 | } |
e9174d1e | 2031 | } |
416331ca XL |
2032 | self.commasep(Inconsistent, &after[..], |s, p| s.print_pat(&p)); |
2033 | self.s.word("]"); | |
e9174d1e SL |
2034 | } |
2035 | } | |
b7449926 | 2036 | self.ann.post(self, AnnNode::Pat(pat)) |
e9174d1e SL |
2037 | } |
2038 | ||
dfeec247 | 2039 | pub fn print_param(&mut self, arg: &hir::Param<'_>) { |
6a06907d | 2040 | self.print_outer_attributes(self.attrs(arg.hir_id)); |
416331ca XL |
2041 | self.print_pat(&arg.pat); |
2042 | } | |
2043 | ||
dfeec247 | 2044 | pub fn print_arm(&mut self, arm: &hir::Arm<'_>) { |
e9174d1e SL |
2045 | // I have no idea why this check is necessary, but here it |
2046 | // is :( | |
6a06907d | 2047 | if self.attrs(arm.hir_id).is_empty() { |
416331ca | 2048 | self.s.space(); |
e9174d1e | 2049 | } |
416331ca XL |
2050 | self.cbox(INDENT_UNIT); |
2051 | self.ann.pre(self, AnnNode::Arm(arm)); | |
2052 | self.ibox(0); | |
6a06907d | 2053 | self.print_outer_attributes(&self.attrs(arm.hir_id)); |
e74abb32 | 2054 | self.print_pat(&arm.pat); |
416331ca | 2055 | self.s.space(); |
b7449926 XL |
2056 | if let Some(ref g) = arm.guard { |
2057 | match g { | |
2058 | hir::Guard::If(e) => { | |
416331ca XL |
2059 | self.word_space("if"); |
2060 | self.print_expr(&e); | |
2061 | self.s.space(); | |
b7449926 | 2062 | } |
fc512014 XL |
2063 | hir::Guard::IfLet(pat, e) => { |
2064 | self.word_nbsp("if"); | |
2065 | self.word_nbsp("let"); | |
2066 | self.print_pat(&pat); | |
2067 | self.s.space(); | |
2068 | self.word_space("="); | |
2069 | self.print_expr(&e); | |
2070 | self.s.space(); | |
2071 | } | |
b7449926 | 2072 | } |
e9174d1e | 2073 | } |
416331ca | 2074 | self.word_space("=>"); |
e9174d1e | 2075 | |
e74abb32 | 2076 | match arm.body.kind { |
8faf50e0 | 2077 | hir::ExprKind::Block(ref blk, opt_label) => { |
94b46f34 | 2078 | if let Some(label) = opt_label { |
416331ca XL |
2079 | self.print_ident(label.ident); |
2080 | self.word_space(":"); | |
94b46f34 | 2081 | } |
e9174d1e | 2082 | // the block will close the pattern's ibox |
416331ca | 2083 | self.print_block_unclosed(&blk); |
e9174d1e SL |
2084 | |
2085 | // If it is a user-provided unsafe block, print a comma after it | |
ba9703b0 XL |
2086 | if let hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::UserProvided) = blk.rules |
2087 | { | |
416331ca | 2088 | self.s.word(","); |
e9174d1e SL |
2089 | } |
2090 | } | |
2091 | _ => { | |
416331ca XL |
2092 | self.end(); // close the ibox for the pattern |
2093 | self.print_expr(&arm.body); | |
2094 | self.s.word(","); | |
e9174d1e SL |
2095 | } |
2096 | } | |
416331ca | 2097 | self.ann.post(self, AnnNode::Arm(arm)); |
e9174d1e SL |
2098 | self.end() // close enclosing cbox |
2099 | } | |
2100 | ||
dfeec247 XL |
2101 | pub fn print_fn( |
2102 | &mut self, | |
2103 | decl: &hir::FnDecl<'_>, | |
2104 | header: hir::FnHeader, | |
f9f354fc | 2105 | name: Option<Symbol>, |
dfeec247 XL |
2106 | generics: &hir::Generics<'_>, |
2107 | vis: &hir::Visibility<'_>, | |
f9f354fc | 2108 | arg_names: &[Ident], |
dfeec247 XL |
2109 | body_id: Option<hir::BodyId>, |
2110 | ) { | |
416331ca | 2111 | self.print_fn_header_info(header, vis); |
e9174d1e SL |
2112 | |
2113 | if let Some(name) = name { | |
416331ca XL |
2114 | self.nbsp(); |
2115 | self.print_name(name); | |
e9174d1e | 2116 | } |
416331ca | 2117 | self.print_generic_params(&generics.params); |
e9174d1e | 2118 | |
416331ca | 2119 | self.popen(); |
32a655c1 SL |
2120 | let mut i = 0; |
2121 | // Make sure we aren't supplied *both* `arg_names` and `body_id`. | |
2122 | assert!(arg_names.is_empty() || body_id.is_none()); | |
2123 | self.commasep(Inconsistent, &decl.inputs, |s, ty| { | |
416331ca | 2124 | s.ibox(INDENT_UNIT); |
8faf50e0 | 2125 | if let Some(arg_name) = arg_names.get(i) { |
60c5eb7d | 2126 | s.s.word(arg_name.to_string()); |
416331ca XL |
2127 | s.s.word(":"); |
2128 | s.s.space(); | |
32a655c1 | 2129 | } else if let Some(body_id) = body_id { |
e1599b0c | 2130 | s.ann.nested(s, Nested::BodyParamPat(body_id, i)); |
416331ca XL |
2131 | s.s.word(":"); |
2132 | s.s.space(); | |
32a655c1 SL |
2133 | } |
2134 | i += 1; | |
416331ca | 2135 | s.print_type(ty); |
32a655c1 | 2136 | s.end() |
416331ca | 2137 | }); |
532ac7d7 | 2138 | if decl.c_variadic { |
416331ca | 2139 | self.s.word(", ..."); |
e9174d1e | 2140 | } |
416331ca | 2141 | self.pclose(); |
e9174d1e | 2142 | |
416331ca | 2143 | self.print_fn_output(decl); |
32a655c1 | 2144 | self.print_where_clause(&generics.where_clause) |
e9174d1e SL |
2145 | } |
2146 | ||
dfeec247 | 2147 | fn print_closure_params(&mut self, decl: &hir::FnDecl<'_>, body_id: hir::BodyId) { |
416331ca | 2148 | self.s.word("|"); |
32a655c1 SL |
2149 | let mut i = 0; |
2150 | self.commasep(Inconsistent, &decl.inputs, |s, ty| { | |
416331ca | 2151 | s.ibox(INDENT_UNIT); |
32a655c1 | 2152 | |
e1599b0c | 2153 | s.ann.nested(s, Nested::BodyParamPat(body_id, i)); |
32a655c1 SL |
2154 | i += 1; |
2155 | ||
e74abb32 | 2156 | if let hir::TyKind::Infer = ty.kind { |
e1599b0c | 2157 | // Print nothing. |
8faf50e0 | 2158 | } else { |
416331ca XL |
2159 | s.s.word(":"); |
2160 | s.s.space(); | |
2161 | s.print_type(ty); | |
32a655c1 | 2162 | } |
416331ca XL |
2163 | s.end(); |
2164 | }); | |
2165 | self.s.word("|"); | |
e9174d1e | 2166 | |
ba9703b0 | 2167 | if let hir::FnRetTy::DefaultReturn(..) = decl.output { |
416331ca | 2168 | return; |
e9174d1e SL |
2169 | } |
2170 | ||
416331ca XL |
2171 | self.space_if_not_bol(); |
2172 | self.word_space("->"); | |
e9174d1e | 2173 | match decl.output { |
ba9703b0 | 2174 | hir::FnRetTy::Return(ref ty) => { |
416331ca | 2175 | self.print_type(&ty); |
ea8adc8c | 2176 | self.maybe_print_comment(ty.span.lo()) |
e9174d1e | 2177 | } |
ba9703b0 | 2178 | hir::FnRetTy::DefaultReturn(..) => unreachable!(), |
e9174d1e SL |
2179 | } |
2180 | } | |
2181 | ||
60c5eb7d | 2182 | pub fn print_capture_clause(&mut self, capture_clause: hir::CaptureBy) { |
e9174d1e | 2183 | match capture_clause { |
60c5eb7d | 2184 | hir::CaptureBy::Value => self.word_space("move"), |
dfeec247 | 2185 | hir::CaptureBy::Ref => {} |
e9174d1e SL |
2186 | } |
2187 | } | |
2188 | ||
dc9dc135 XL |
2189 | pub fn print_bounds<'b>( |
2190 | &mut self, | |
2191 | prefix: &'static str, | |
dfeec247 | 2192 | bounds: impl IntoIterator<Item = &'b hir::GenericBound<'b>>, |
416331ca | 2193 | ) { |
dc9dc135 XL |
2194 | let mut first = true; |
2195 | for bound in bounds { | |
2196 | if first { | |
416331ca | 2197 | self.s.word(prefix); |
dc9dc135 XL |
2198 | } |
2199 | if !(first && prefix.is_empty()) { | |
416331ca | 2200 | self.nbsp(); |
dc9dc135 XL |
2201 | } |
2202 | if first { | |
2203 | first = false; | |
2204 | } else { | |
416331ca | 2205 | self.word_space("+"); |
dc9dc135 | 2206 | } |
e9174d1e | 2207 | |
dc9dc135 XL |
2208 | match bound { |
2209 | GenericBound::Trait(tref, modifier) => { | |
2210 | if modifier == &TraitBoundModifier::Maybe { | |
416331ca | 2211 | self.s.word("?"); |
e9174d1e | 2212 | } |
416331ca | 2213 | self.print_poly_trait_ref(tref); |
dc9dc135 | 2214 | } |
3dfed10e XL |
2215 | GenericBound::LangItemTrait(lang_item, span, ..) => { |
2216 | self.s.word("#[lang = \""); | |
2217 | self.print_ident(Ident::new(lang_item.name(), *span)); | |
2218 | self.s.word("\"]"); | |
2219 | } | |
dc9dc135 | 2220 | GenericBound::Outlives(lt) => { |
416331ca | 2221 | self.print_lifetime(lt); |
ff7c6d11 | 2222 | } |
e9174d1e | 2223 | } |
e9174d1e SL |
2224 | } |
2225 | } | |
2226 | ||
dfeec247 | 2227 | pub fn print_generic_params(&mut self, generic_params: &[GenericParam<'_>]) { |
ff7c6d11 | 2228 | if !generic_params.is_empty() { |
416331ca | 2229 | self.s.word("<"); |
e9174d1e | 2230 | |
dfeec247 | 2231 | self.commasep(Inconsistent, generic_params, |s, param| s.print_generic_param(param)); |
e9174d1e | 2232 | |
416331ca | 2233 | self.s.word(">"); |
e9174d1e | 2234 | } |
e9174d1e SL |
2235 | } |
2236 | ||
dfeec247 | 2237 | pub fn print_generic_param(&mut self, param: &GenericParam<'_>) { |
9fa01778 | 2238 | if let GenericParamKind::Const { .. } = param.kind { |
416331ca | 2239 | self.word_space("const"); |
9fa01778 XL |
2240 | } |
2241 | ||
416331ca | 2242 | self.print_ident(param.name.ident()); |
9fa01778 | 2243 | |
8faf50e0 XL |
2244 | match param.kind { |
2245 | GenericParamKind::Lifetime { .. } => { | |
2246 | let mut sep = ":"; | |
dfeec247 | 2247 | for bound in param.bounds { |
8faf50e0 | 2248 | match bound { |
dfeec247 | 2249 | GenericBound::Outlives(ref lt) => { |
416331ca XL |
2250 | self.s.word(sep); |
2251 | self.print_lifetime(lt); | |
8faf50e0 XL |
2252 | sep = "+"; |
2253 | } | |
dfeec247 | 2254 | _ => panic!(), |
8faf50e0 XL |
2255 | } |
2256 | } | |
8faf50e0 XL |
2257 | } |
2258 | GenericParamKind::Type { ref default, .. } => { | |
dfeec247 | 2259 | self.print_bounds(":", param.bounds); |
ba9703b0 XL |
2260 | if let Some(default) = default { |
2261 | self.s.space(); | |
2262 | self.word_space("="); | |
2263 | self.print_type(&default) | |
8faf50e0 | 2264 | } |
e9174d1e | 2265 | } |
5869c6ff | 2266 | GenericParamKind::Const { ref ty, ref default } => { |
416331ca | 2267 | self.word_space(":"); |
5869c6ff XL |
2268 | self.print_type(ty); |
2269 | if let Some(ref _default) = default { | |
2270 | // FIXME(const_generics_defaults): print the `default` value here | |
2271 | } | |
9fa01778 | 2272 | } |
e9174d1e SL |
2273 | } |
2274 | } | |
2275 | ||
416331ca | 2276 | pub fn print_lifetime(&mut self, lifetime: &hir::Lifetime) { |
8faf50e0 XL |
2277 | self.print_ident(lifetime.name.ident()) |
2278 | } | |
2279 | ||
dfeec247 | 2280 | pub fn print_where_clause(&mut self, where_clause: &hir::WhereClause<'_>) { |
e9174d1e | 2281 | if where_clause.predicates.is_empty() { |
416331ca | 2282 | return; |
e9174d1e SL |
2283 | } |
2284 | ||
416331ca XL |
2285 | self.s.space(); |
2286 | self.word_space("where"); | |
e9174d1e SL |
2287 | |
2288 | for (i, predicate) in where_clause.predicates.iter().enumerate() { | |
2289 | if i != 0 { | |
416331ca | 2290 | self.word_space(","); |
e9174d1e SL |
2291 | } |
2292 | ||
2293 | match predicate { | |
5869c6ff XL |
2294 | hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate { |
2295 | bound_generic_params, | |
2296 | bounded_ty, | |
dfeec247 | 2297 | bounds, |
ff7c6d11 XL |
2298 | .. |
2299 | }) => { | |
416331ca XL |
2300 | self.print_formal_generic_params(bound_generic_params); |
2301 | self.print_type(&bounded_ty); | |
5869c6ff | 2302 | self.print_bounds(":", *bounds); |
e9174d1e | 2303 | } |
5869c6ff XL |
2304 | hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate { |
2305 | lifetime, | |
2306 | bounds, | |
dfeec247 XL |
2307 | .. |
2308 | }) => { | |
416331ca XL |
2309 | self.print_lifetime(lifetime); |
2310 | self.s.word(":"); | |
e9174d1e SL |
2311 | |
2312 | for (i, bound) in bounds.iter().enumerate() { | |
8faf50e0 XL |
2313 | match bound { |
2314 | GenericBound::Outlives(lt) => { | |
416331ca | 2315 | self.print_lifetime(lt); |
8faf50e0 | 2316 | } |
dfeec247 | 2317 | _ => panic!(), |
8faf50e0 | 2318 | } |
e9174d1e SL |
2319 | |
2320 | if i != 0 { | |
416331ca | 2321 | self.s.word(":"); |
e9174d1e SL |
2322 | } |
2323 | } | |
2324 | } | |
5869c6ff XL |
2325 | hir::WherePredicate::EqPredicate(hir::WhereEqPredicate { |
2326 | lhs_ty, rhs_ty, .. | |
dfeec247 | 2327 | }) => { |
416331ca XL |
2328 | self.print_type(lhs_ty); |
2329 | self.s.space(); | |
2330 | self.word_space("="); | |
2331 | self.print_type(rhs_ty); | |
e9174d1e SL |
2332 | } |
2333 | } | |
2334 | } | |
e9174d1e SL |
2335 | } |
2336 | ||
60c5eb7d | 2337 | pub fn print_mutability(&mut self, mutbl: hir::Mutability, print_const: bool) { |
e9174d1e | 2338 | match mutbl { |
dfeec247 XL |
2339 | hir::Mutability::Mut => self.word_nbsp("mut"), |
2340 | hir::Mutability::Not => { | |
2341 | if print_const { | |
2342 | self.word_nbsp("const") | |
2343 | } | |
2344 | } | |
e9174d1e SL |
2345 | } |
2346 | } | |
2347 | ||
dfeec247 | 2348 | pub fn print_mt(&mut self, mt: &hir::MutTy<'_>, print_const: bool) { |
60c5eb7d | 2349 | self.print_mutability(mt.mutbl, print_const); |
7453a54e | 2350 | self.print_type(&mt.ty) |
e9174d1e SL |
2351 | } |
2352 | ||
dfeec247 | 2353 | pub fn print_fn_output(&mut self, decl: &hir::FnDecl<'_>) { |
ba9703b0 | 2354 | if let hir::FnRetTy::DefaultReturn(..) = decl.output { |
416331ca | 2355 | return; |
e9174d1e SL |
2356 | } |
2357 | ||
416331ca XL |
2358 | self.space_if_not_bol(); |
2359 | self.ibox(INDENT_UNIT); | |
2360 | self.word_space("->"); | |
e9174d1e | 2361 | match decl.output { |
ba9703b0 XL |
2362 | hir::FnRetTy::DefaultReturn(..) => unreachable!(), |
2363 | hir::FnRetTy::Return(ref ty) => self.print_type(&ty), | |
e9174d1e | 2364 | } |
416331ca | 2365 | self.end(); |
e9174d1e | 2366 | |
ba9703b0 XL |
2367 | if let hir::FnRetTy::Return(ref output) = decl.output { |
2368 | self.maybe_print_comment(output.span.lo()) | |
e9174d1e SL |
2369 | } |
2370 | } | |
2371 | ||
dfeec247 XL |
2372 | pub fn print_ty_fn( |
2373 | &mut self, | |
2374 | abi: Abi, | |
2375 | unsafety: hir::Unsafety, | |
2376 | decl: &hir::FnDecl<'_>, | |
f9f354fc | 2377 | name: Option<Symbol>, |
dfeec247 | 2378 | generic_params: &[hir::GenericParam<'_>], |
f9f354fc | 2379 | arg_names: &[Ident], |
dfeec247 | 2380 | ) { |
416331ca | 2381 | self.ibox(INDENT_UNIT); |
ff7c6d11 | 2382 | if !generic_params.is_empty() { |
416331ca XL |
2383 | self.s.word("for"); |
2384 | self.print_generic_params(generic_params); | |
e9174d1e SL |
2385 | } |
2386 | let generics = hir::Generics { | |
dfeec247 XL |
2387 | params: &[], |
2388 | where_clause: hir::WhereClause { predicates: &[], span: rustc_span::DUMMY_SP }, | |
2389 | span: rustc_span::DUMMY_SP, | |
e9174d1e | 2390 | }; |
dfeec247 XL |
2391 | self.print_fn( |
2392 | decl, | |
2393 | hir::FnHeader { | |
2394 | unsafety, | |
2395 | abi, | |
2396 | constness: hir::Constness::NotConst, | |
2397 | asyncness: hir::IsAsync::NotAsync, | |
2398 | }, | |
2399 | name, | |
2400 | &generics, | |
2401 | &Spanned { span: rustc_span::DUMMY_SP, node: hir::VisibilityKind::Inherited }, | |
2402 | arg_names, | |
2403 | None, | |
2404 | ); | |
416331ca | 2405 | self.end(); |
e9174d1e SL |
2406 | } |
2407 | ||
dfeec247 XL |
2408 | pub fn maybe_print_trailing_comment( |
2409 | &mut self, | |
2410 | span: rustc_span::Span, | |
2411 | next_pos: Option<BytePos>, | |
2412 | ) { | |
416331ca XL |
2413 | if let Some(cmnts) = self.comments() { |
2414 | if let Some(cmnt) = cmnts.trailing_comment(span, next_pos) { | |
2415 | self.print_comment(&cmnt); | |
e9174d1e | 2416 | } |
e9174d1e | 2417 | } |
e9174d1e SL |
2418 | } |
2419 | ||
416331ca | 2420 | pub fn print_remaining_comments(&mut self) { |
e9174d1e SL |
2421 | // If there aren't any remaining comments, then we need to manually |
2422 | // make sure there is a line break at the end. | |
2423 | if self.next_comment().is_none() { | |
416331ca | 2424 | self.s.hardbreak(); |
e9174d1e | 2425 | } |
0531ce1d | 2426 | while let Some(ref cmnt) = self.next_comment() { |
416331ca | 2427 | self.print_comment(cmnt) |
e9174d1e | 2428 | } |
e9174d1e SL |
2429 | } |
2430 | ||
dfeec247 | 2431 | pub fn print_opt_abi_and_extern_if_nondefault(&mut self, opt_abi: Option<Abi>) { |
e9174d1e | 2432 | match opt_abi { |
dfeec247 | 2433 | Some(Abi::Rust) => {} |
e9174d1e | 2434 | Some(abi) => { |
416331ca | 2435 | self.word_nbsp("extern"); |
a1dfa0c6 | 2436 | self.word_nbsp(abi.to_string()) |
e9174d1e | 2437 | } |
dfeec247 | 2438 | None => {} |
e9174d1e SL |
2439 | } |
2440 | } | |
2441 | ||
416331ca | 2442 | pub fn print_extern_opt_abi(&mut self, opt_abi: Option<Abi>) { |
ba9703b0 XL |
2443 | if let Some(abi) = opt_abi { |
2444 | self.word_nbsp("extern"); | |
2445 | self.word_nbsp(abi.to_string()) | |
e9174d1e SL |
2446 | } |
2447 | } | |
2448 | ||
dfeec247 | 2449 | pub fn print_fn_header_info(&mut self, header: hir::FnHeader, vis: &hir::Visibility<'_>) { |
416331ca | 2450 | self.s.word(visibility_qualified(vis, "")); |
e9174d1e | 2451 | |
8faf50e0 | 2452 | match header.constness { |
e9174d1e | 2453 | hir::Constness::NotConst => {} |
416331ca | 2454 | hir::Constness::Const => self.word_nbsp("const"), |
e9174d1e SL |
2455 | } |
2456 | ||
8faf50e0 XL |
2457 | match header.asyncness { |
2458 | hir::IsAsync::NotAsync => {} | |
416331ca | 2459 | hir::IsAsync::Async => self.word_nbsp("async"), |
8faf50e0 XL |
2460 | } |
2461 | ||
416331ca | 2462 | self.print_unsafety(header.unsafety); |
8faf50e0 XL |
2463 | |
2464 | if header.abi != Abi::Rust { | |
416331ca XL |
2465 | self.word_nbsp("extern"); |
2466 | self.word_nbsp(header.abi.to_string()); | |
e9174d1e SL |
2467 | } |
2468 | ||
041b39d2 | 2469 | self.s.word("fn") |
e9174d1e SL |
2470 | } |
2471 | ||
416331ca | 2472 | pub fn print_unsafety(&mut self, s: hir::Unsafety) { |
e9174d1e | 2473 | match s { |
416331ca | 2474 | hir::Unsafety::Normal => {} |
e9174d1e SL |
2475 | hir::Unsafety::Unsafe => self.word_nbsp("unsafe"), |
2476 | } | |
2477 | } | |
abe05a73 | 2478 | |
416331ca | 2479 | pub fn print_is_auto(&mut self, s: hir::IsAuto) { |
abe05a73 XL |
2480 | match s { |
2481 | hir::IsAuto::Yes => self.word_nbsp("auto"), | |
dfeec247 | 2482 | hir::IsAuto::No => {} |
abe05a73 XL |
2483 | } |
2484 | } | |
e9174d1e SL |
2485 | } |
2486 | ||
e9174d1e SL |
2487 | /// Does this expression require a semicolon to be treated |
2488 | /// as a statement? The negation of this: 'can this expression | |
2489 | /// be used as a statement without a semicolon' -- is used | |
2490 | /// as an early-bail-out in the parser so that, for instance, | |
2491 | /// if true {...} else {...} | |
2492 | /// |x| 5 | |
2493 | /// isn't parsed as (if true {...} else {...} | x) | 5 | |
e1599b0c XL |
2494 | // |
2495 | // Duplicated from `parse::classify`, but adapted for the HIR. | |
dfeec247 | 2496 | fn expr_requires_semi_to_be_stmt(e: &hir::Expr<'_>) -> bool { |
5869c6ff XL |
2497 | !matches!( |
2498 | e.kind, | |
2499 | hir::ExprKind::If(..) | |
2500 | | hir::ExprKind::Match(..) | |
2501 | | hir::ExprKind::Block(..) | |
2502 | | hir::ExprKind::Loop(..) | |
2503 | ) | |
e9174d1e SL |
2504 | } |
2505 | ||
e1599b0c | 2506 | /// This statement requires a semicolon after it. |
e9174d1e SL |
2507 | /// note that in one case (stmt_semi), we've already |
2508 | /// seen the semicolon, and thus don't need another. | |
dfeec247 | 2509 | fn stmt_ends_with_semi(stmt: &hir::StmtKind<'_>) -> bool { |
e9174d1e | 2510 | match *stmt { |
9fa01778 XL |
2511 | hir::StmtKind::Local(_) => true, |
2512 | hir::StmtKind::Item(_) => false, | |
2513 | hir::StmtKind::Expr(ref e) => expr_requires_semi_to_be_stmt(&e), | |
2514 | hir::StmtKind::Semi(..) => false, | |
e9174d1e SL |
2515 | } |
2516 | } | |
ea8adc8c | 2517 | |
8faf50e0 | 2518 | fn bin_op_to_assoc_op(op: hir::BinOpKind) -> AssocOp { |
9fa01778 | 2519 | use crate::hir::BinOpKind::*; |
ea8adc8c | 2520 | match op { |
8faf50e0 XL |
2521 | Add => AssocOp::Add, |
2522 | Sub => AssocOp::Subtract, | |
2523 | Mul => AssocOp::Multiply, | |
2524 | Div => AssocOp::Divide, | |
2525 | Rem => AssocOp::Modulus, | |
2526 | ||
2527 | And => AssocOp::LAnd, | |
2528 | Or => AssocOp::LOr, | |
2529 | ||
2530 | BitXor => AssocOp::BitXor, | |
2531 | BitAnd => AssocOp::BitAnd, | |
2532 | BitOr => AssocOp::BitOr, | |
2533 | Shl => AssocOp::ShiftLeft, | |
2534 | Shr => AssocOp::ShiftRight, | |
2535 | ||
2536 | Eq => AssocOp::Equal, | |
2537 | Lt => AssocOp::Less, | |
2538 | Le => AssocOp::LessEqual, | |
2539 | Ne => AssocOp::NotEqual, | |
2540 | Ge => AssocOp::GreaterEqual, | |
2541 | Gt => AssocOp::Greater, | |
ea8adc8c XL |
2542 | } |
2543 | } | |
2544 | ||
e1599b0c | 2545 | /// Expressions that syntactically contain an "exterior" struct literal, i.e., not surrounded by any |
0731742a | 2546 | /// parens or other delimiters, e.g., `X { y: 1 }`, `X { y: 1 }.method()`, `foo == X { y: 1 }` and |
ea8adc8c | 2547 | /// `X { y: 1 } == foo` all do, but `(X { y: 1 }) == foo` does not. |
dfeec247 | 2548 | fn contains_exterior_struct_lit(value: &hir::Expr<'_>) -> bool { |
e74abb32 | 2549 | match value.kind { |
8faf50e0 | 2550 | hir::ExprKind::Struct(..) => true, |
ea8adc8c | 2551 | |
dfeec247 XL |
2552 | hir::ExprKind::Assign(ref lhs, ref rhs, _) |
2553 | | hir::ExprKind::AssignOp(_, ref lhs, ref rhs) | |
2554 | | hir::ExprKind::Binary(_, ref lhs, ref rhs) => { | |
e1599b0c | 2555 | // `X { y: 1 } + X { y: 2 }` |
ea8adc8c XL |
2556 | contains_exterior_struct_lit(&lhs) || contains_exterior_struct_lit(&rhs) |
2557 | } | |
dfeec247 XL |
2558 | hir::ExprKind::Unary(_, ref x) |
2559 | | hir::ExprKind::Cast(ref x, _) | |
2560 | | hir::ExprKind::Type(ref x, _) | |
2561 | | hir::ExprKind::Field(ref x, _) | |
2562 | | hir::ExprKind::Index(ref x, _) => { | |
e1599b0c | 2563 | // `&X { y: 1 }, X { y: 1 }.y` |
ea8adc8c XL |
2564 | contains_exterior_struct_lit(&x) |
2565 | } | |
2566 | ||
f035d41b | 2567 | hir::ExprKind::MethodCall(.., ref exprs, _) => { |
e1599b0c | 2568 | // `X { y: 1 }.bar(...)` |
ea8adc8c XL |
2569 | contains_exterior_struct_lit(&exprs[0]) |
2570 | } | |
2571 | ||
2572 | _ => false, | |
2573 | } | |
2574 | } |