2020-03-26 13:50:02 +01:00
|
|
|
#![recursion_limit = "256"]
|
|
|
|
|
2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast as ast;
|
2020-02-29 20:37:32 +03:00
|
|
|
use rustc_ast::util::parser::{self, AssocOp, Fixity};
|
2020-01-11 17:02:46 +01:00
|
|
|
use rustc_ast_pretty::pp::Breaks::{Consistent, Inconsistent};
|
|
|
|
use rustc_ast_pretty::pp::{self, Breaks};
|
2020-02-23 01:29:36 +03:00
|
|
|
use rustc_ast_pretty::pprust::{Comments, PrintState};
|
2020-03-23 20:59:19 +01:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::{GenericArg, GenericParam, GenericParamKind, Node};
|
|
|
|
use rustc_hir::{GenericBound, PatKind, RangeEnd, TraitBoundModifier};
|
2020-01-01 19:25:28 +01:00
|
|
|
use rustc_span::source_map::{SourceMap, Spanned};
|
2020-04-19 13:00:18 +02:00
|
|
|
use rustc_span::symbol::{kw, Ident, IdentPrinter, Symbol};
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::{self, BytePos, FileName};
|
2018-04-25 19:30:39 +03:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2018-11-29 11:36:58 +11:00
|
|
|
use std::borrow::Cow;
|
2017-01-09 17:46:11 +02:00
|
|
|
use std::cell::Cell;
|
2021-01-24 17:14:17 +01:00
|
|
|
use std::collections::BTreeMap;
|
2017-06-25 06:38:13 -06:00
|
|
|
use std::vec;
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2020-03-24 02:44:41 +01:00
|
|
|
pub fn id_to_string(map: &dyn rustc_hir::intravisit::Map<'_>, hir_id: hir::HirId) -> String {
|
|
|
|
to_string(&map, |s| s.print_node(map.find(hir_id).unwrap()))
|
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
pub enum AnnNode<'a> {
|
2020-04-19 13:00:18 +02:00
|
|
|
Name(&'a Symbol),
|
2019-11-29 13:43:03 +01:00
|
|
|
Block(&'a hir::Block<'a>),
|
2019-11-28 19:28:50 +01:00
|
|
|
Item(&'a hir::Item<'a>),
|
2019-02-26 11:04:58 +01:00
|
|
|
SubItem(hir::HirId),
|
2019-11-29 13:43:03 +01:00
|
|
|
Expr(&'a hir::Expr<'a>),
|
|
|
|
Pat(&'a hir::Pat<'a>),
|
|
|
|
Arm(&'a hir::Arm<'a>),
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2016-12-27 10:00:18 +02:00
|
|
|
pub enum Nested {
|
|
|
|
Item(hir::ItemId),
|
|
|
|
TraitItem(hir::TraitItemId),
|
|
|
|
ImplItem(hir::ImplItemId),
|
2020-11-11 21:57:54 +01:00
|
|
|
ForeignItem(hir::ForeignItemId),
|
2016-12-27 10:00:18 +02:00
|
|
|
Body(hir::BodyId),
|
2019-12-22 17:42:04 -05:00
|
|
|
BodyParamPat(hir::BodyId, usize),
|
2016-12-27 10:00:18 +02:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
pub trait PpAnn {
|
2019-12-22 17:42:04 -05:00
|
|
|
fn nested(&self, _state: &mut State<'_>, _nested: Nested) {}
|
|
|
|
fn pre(&self, _state: &mut State<'_>, _node: AnnNode<'_>) {}
|
|
|
|
fn post(&self, _state: &mut State<'_>, _node: AnnNode<'_>) {}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct NoAnn;
|
|
|
|
impl PpAnn for NoAnn {}
|
2018-12-04 12:46:10 +01:00
|
|
|
pub const NO_ANN: &dyn PpAnn = &NoAnn;
|
2016-12-27 10:00:18 +02:00
|
|
|
|
2020-03-23 20:59:19 +01:00
|
|
|
impl PpAnn for hir::Crate<'_> {
|
2020-03-24 02:44:41 +01:00
|
|
|
fn nested(&self, state: &mut State<'_>, nested: Nested) {
|
|
|
|
match nested {
|
2021-01-30 12:06:04 +01:00
|
|
|
Nested::Item(id) => state.print_item(self.item(id)),
|
2020-03-24 02:44:41 +01:00
|
|
|
Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)),
|
|
|
|
Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
|
2020-11-11 21:57:54 +01:00
|
|
|
Nested::ForeignItem(id) => state.print_foreign_item(self.foreign_item(id)),
|
2020-03-24 02:44:41 +01:00
|
|
|
Nested::Body(id) => state.print_expr(&self.body(id).value),
|
|
|
|
Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Identical to the `PpAnn` implementation for `hir::Crate`,
|
|
|
|
/// except it avoids creating a dependency on the whole crate.
|
|
|
|
impl PpAnn for &dyn rustc_hir::intravisit::Map<'_> {
|
2019-06-24 14:15:11 -04:00
|
|
|
fn nested(&self, state: &mut State<'_>, nested: Nested) {
|
2016-12-27 10:00:18 +02:00
|
|
|
match nested {
|
2021-01-30 12:06:04 +01:00
|
|
|
Nested::Item(id) => state.print_item(self.item(id)),
|
2016-12-27 10:00:18 +02:00
|
|
|
Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)),
|
|
|
|
Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
|
2020-11-11 21:57:54 +01:00
|
|
|
Nested::ForeignItem(id) => state.print_foreign_item(self.foreign_item(id)),
|
2016-12-27 10:00:18 +02:00
|
|
|
Nested::Body(id) => state.print_expr(&self.body(id).value),
|
2019-12-22 17:42:04 -05:00
|
|
|
Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat),
|
2016-12-27 10:00:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
|
|
|
pub struct State<'a> {
|
2019-07-05 19:10:18 -04:00
|
|
|
pub s: pp::Printer,
|
2019-07-05 18:29:15 -04:00
|
|
|
comments: Option<Comments<'a>>,
|
2021-01-24 17:14:17 +01:00
|
|
|
attrs: &'a BTreeMap<hir::HirId, &'a [ast::Attribute]>,
|
2018-02-23 09:53:00 -08:00
|
|
|
ann: &'a (dyn PpAnn + 'a),
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2020-01-02 05:18:45 +01:00
|
|
|
impl<'a> State<'a> {
|
|
|
|
pub fn print_node(&mut self, node: Node<'_>) {
|
|
|
|
match node {
|
|
|
|
Node::Param(a) => self.print_param(&a),
|
|
|
|
Node::Item(a) => self.print_item(&a),
|
|
|
|
Node::ForeignItem(a) => self.print_foreign_item(&a),
|
|
|
|
Node::TraitItem(a) => self.print_trait_item(a),
|
|
|
|
Node::ImplItem(a) => self.print_impl_item(a),
|
|
|
|
Node::Variant(a) => self.print_variant(&a),
|
|
|
|
Node::AnonConst(a) => self.print_anon_const(&a),
|
|
|
|
Node::Expr(a) => self.print_expr(&a),
|
|
|
|
Node::Stmt(a) => self.print_stmt(&a),
|
|
|
|
Node::PathSegment(a) => self.print_path_segment(&a),
|
|
|
|
Node::Ty(a) => self.print_type(&a),
|
|
|
|
Node::TraitRef(a) => self.print_trait_ref(&a),
|
|
|
|
Node::Binding(a) | Node::Pat(a) => self.print_pat(&a),
|
|
|
|
Node::Arm(a) => self.print_arm(&a),
|
|
|
|
Node::Block(a) => {
|
|
|
|
// Containing cbox, will be closed by print-block at `}`.
|
|
|
|
self.cbox(INDENT_UNIT);
|
|
|
|
// Head-ibox, will be closed by print-block after `{`.
|
|
|
|
self.ibox(0);
|
|
|
|
self.print_block(&a)
|
|
|
|
}
|
|
|
|
Node::Lifetime(a) => self.print_lifetime(&a),
|
|
|
|
Node::Visibility(a) => self.print_visibility(&a),
|
|
|
|
Node::GenericParam(_) => panic!("cannot print Node::GenericParam"),
|
2021-03-16 00:36:07 +03:00
|
|
|
Node::Field(_) => panic!("cannot print Node::Field"),
|
2020-01-02 05:18:45 +01:00
|
|
|
// These cases do not carry enough information in the
|
|
|
|
// `hir_map` to reconstruct their full structure for pretty
|
|
|
|
// printing.
|
|
|
|
Node::Ctor(..) => panic!("cannot print isolated Ctor"),
|
|
|
|
Node::Local(a) => self.print_local_decl(&a),
|
|
|
|
Node::MacroDef(_) => panic!("cannot print MacroDef"),
|
2020-02-07 16:43:36 +01:00
|
|
|
Node::Crate(..) => panic!("cannot print Crate"),
|
2020-01-02 05:18:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 09:26:50 -04:00
|
|
|
impl std::ops::Deref for State<'_> {
|
|
|
|
type Target = pp::Printer;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::DerefMut for State<'_> {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 21:58:20 +12:00
|
|
|
impl<'a> PrintState<'a> for State<'a> {
|
2019-07-05 18:29:15 -04:00
|
|
|
fn comments(&mut self) -> &mut Option<Comments<'a>> {
|
2015-09-14 21:58:20 +12:00
|
|
|
&mut self.comments
|
|
|
|
}
|
2019-07-13 19:11:07 +03:00
|
|
|
|
2020-04-19 13:00:18 +02:00
|
|
|
fn print_ident(&mut self, ident: Ident) {
|
2020-02-23 01:29:36 +03:00
|
|
|
self.s.word(IdentPrinter::for_ast_ident(ident, ident.is_raw_guess()).to_string());
|
2019-07-13 19:11:07 +03:00
|
|
|
self.ann.post(self, AnnNode::Name(&ident.name))
|
|
|
|
}
|
|
|
|
|
2020-01-02 05:18:45 +01:00
|
|
|
fn print_generic_args(&mut self, _: &ast::GenericArgs, _colons_before_params: bool) {
|
|
|
|
panic!("AST generic args printed by HIR pretty-printer");
|
2019-07-13 19:11:07 +03:00
|
|
|
}
|
2015-09-14 21:58:20 +12:00
|
|
|
}
|
|
|
|
|
2019-07-09 09:32:25 -04:00
|
|
|
pub const INDENT_UNIT: usize = 4;
|
2015-07-31 00:04:06 -07:00
|
|
|
|
|
|
|
/// Requires you to pass an input filename and reader so that
|
2019-05-09 19:04:04 +03:00
|
|
|
/// it can scan the input text for comments to copy forward.
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn print_crate<'a>(
|
2020-02-22 16:07:05 +02:00
|
|
|
sm: &'a SourceMap,
|
2019-12-22 17:42:04 -05:00
|
|
|
krate: &hir::Crate<'_>,
|
|
|
|
filename: FileName,
|
|
|
|
input: String,
|
|
|
|
ann: &'a dyn PpAnn,
|
|
|
|
) -> String {
|
2020-11-26 23:46:48 +01:00
|
|
|
let mut s = State::new_from_input(sm, filename, input, &krate.attrs, ann);
|
2015-07-31 00:04:06 -07:00
|
|
|
|
|
|
|
// When printing the AST, we sometimes need to inject `#[no_std]` here.
|
|
|
|
// Since you can't compile the HIR, it's not necessary.
|
|
|
|
|
2021-03-30 20:31:06 +02:00
|
|
|
s.print_mod(&krate.item, s.attrs(hir::CRATE_HIR_ID));
|
2019-06-24 14:15:11 -04:00
|
|
|
s.print_remaining_comments();
|
2019-07-05 19:10:18 -04:00
|
|
|
s.s.eof()
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> State<'a> {
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn new_from_input(
|
2020-02-22 16:07:05 +02:00
|
|
|
sm: &'a SourceMap,
|
2019-12-22 17:42:04 -05:00
|
|
|
filename: FileName,
|
|
|
|
input: String,
|
2021-01-24 17:14:17 +01:00
|
|
|
attrs: &'a BTreeMap<hir::HirId, &[ast::Attribute]>,
|
2019-12-22 17:42:04 -05:00
|
|
|
ann: &'a dyn PpAnn,
|
|
|
|
) -> State<'a> {
|
2020-11-26 23:46:48 +01:00
|
|
|
State {
|
|
|
|
s: pp::mk_printer(),
|
|
|
|
comments: Some(Comments::new(sm, filename, input)),
|
|
|
|
attrs,
|
|
|
|
ann,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn attrs(&self, id: hir::HirId) -> &'a [ast::Attribute] {
|
2021-01-24 17:14:17 +01:00
|
|
|
self.attrs.get(&id).map_or(&[], |la| *la)
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 09:53:00 -08:00
|
|
|
pub fn to_string<F>(ann: &dyn PpAnn, f: F) -> String
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut State<'_>),
|
2015-07-31 00:04:06 -07:00
|
|
|
{
|
2020-11-26 23:46:48 +01:00
|
|
|
let mut printer =
|
2021-01-24 17:14:17 +01:00
|
|
|
State { s: pp::mk_printer(), comments: None, attrs: &BTreeMap::default(), ann };
|
2019-07-05 19:10:18 -04:00
|
|
|
f(&mut printer);
|
|
|
|
printer.s.eof()
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
pub fn visibility_qualified<S: Into<Cow<'static, str>>>(vis: &hir::Visibility<'_>, w: S) -> String {
|
2016-12-27 10:00:18 +02:00
|
|
|
to_string(NO_ANN, |s| {
|
2019-06-24 14:15:11 -04:00
|
|
|
s.print_visibility(vis);
|
2017-06-24 21:22:42 -06:00
|
|
|
s.s.word(w)
|
2015-07-31 00:04:06 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-30 18:30:34 +01:00
|
|
|
pub fn generic_params_to_string(generic_params: &[GenericParam<'_>]) -> String {
|
|
|
|
to_string(NO_ANN, |s| s.print_generic_params(generic_params))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bounds_to_string<'b>(bounds: impl IntoIterator<Item = &'b hir::GenericBound<'b>>) -> String {
|
|
|
|
to_string(NO_ANN, |s| s.print_bounds("", bounds))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ty_to_string(ty: &hir::Ty<'_>) -> String {
|
|
|
|
to_string(NO_ANN, |s| s.print_type(ty))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn path_segment_to_string(segment: &hir::PathSegment<'_>) -> String {
|
|
|
|
to_string(NO_ANN, |s| s.print_path_segment(segment))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn path_to_string(segment: &hir::Path<'_>) -> String {
|
|
|
|
to_string(NO_ANN, |s| s.print_path(segment, false))
|
|
|
|
}
|
|
|
|
|
2020-06-06 14:21:19 +01:00
|
|
|
pub fn fn_to_string(
|
|
|
|
decl: &hir::FnDecl<'_>,
|
|
|
|
header: hir::FnHeader,
|
|
|
|
name: Option<Symbol>,
|
|
|
|
generics: &hir::Generics<'_>,
|
|
|
|
vis: &hir::Visibility<'_>,
|
|
|
|
arg_names: &[Ident],
|
|
|
|
body_id: Option<hir::BodyId>,
|
|
|
|
) -> String {
|
|
|
|
to_string(NO_ANN, |s| s.print_fn(decl, header, name, generics, vis, arg_names, body_id))
|
|
|
|
}
|
|
|
|
|
2020-06-09 23:04:58 +01:00
|
|
|
pub fn enum_def_to_string(
|
|
|
|
enum_definition: &hir::EnumDef<'_>,
|
|
|
|
generics: &hir::Generics<'_>,
|
|
|
|
name: Symbol,
|
|
|
|
span: rustc_span::Span,
|
|
|
|
visibility: &hir::Visibility<'_>,
|
|
|
|
) -> String {
|
|
|
|
to_string(NO_ANN, |s| s.print_enum_def(enum_definition, generics, name, span, visibility))
|
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
impl<'a> State<'a> {
|
2019-06-24 14:15:11 -04:00
|
|
|
pub fn cbox(&mut self, u: usize) {
|
|
|
|
self.s.cbox(u);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
pub fn nbsp(&mut self) {
|
2017-06-24 21:22:42 -06:00
|
|
|
self.s.word(" ")
|
2015-09-28 08:23:31 +13:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
pub fn word_nbsp<S: Into<Cow<'static, str>>>(&mut self, w: S) {
|
|
|
|
self.s.word(w);
|
2015-07-31 00:04:06 -07:00
|
|
|
self.nbsp()
|
|
|
|
}
|
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
pub fn head<S: Into<Cow<'static, str>>>(&mut self, w: S) {
|
2018-11-29 11:36:58 +11:00
|
|
|
let w = w.into();
|
2015-07-31 00:04:06 -07:00
|
|
|
// outer-box is consistent
|
2019-07-09 09:32:25 -04:00
|
|
|
self.cbox(INDENT_UNIT);
|
2015-07-31 00:04:06 -07:00
|
|
|
// head-box is inconsistent
|
2019-06-24 14:15:11 -04:00
|
|
|
self.ibox(w.len() + 1);
|
2015-07-31 00:04:06 -07:00
|
|
|
// keyword that starts the head
|
|
|
|
if !w.is_empty() {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_nbsp(w);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
pub fn bopen(&mut self) {
|
|
|
|
self.s.word("{");
|
|
|
|
self.end(); // close the head-box
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-12-31 20:15:40 +03:00
|
|
|
pub fn bclose_maybe_open(&mut self, span: rustc_span::Span, close_box: bool) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.maybe_print_comment(span.hi());
|
2019-07-09 09:30:08 -04:00
|
|
|
self.break_offset_if_not_bol(1, -(INDENT_UNIT as isize));
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("}");
|
2015-07-31 00:04:06 -07:00
|
|
|
if close_box {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.end(); // close the outer-box
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
2018-09-12 12:31:11 +02:00
|
|
|
|
2019-12-31 20:15:40 +03:00
|
|
|
pub fn bclose(&mut self, span: rustc_span::Span) {
|
2019-07-09 09:30:08 -04:00
|
|
|
self.bclose_maybe_open(span, true)
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
pub fn space_if_not_bol(&mut self) {
|
2019-07-09 07:42:05 -04:00
|
|
|
if !self.s.is_beginning_of_line() {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.space();
|
2015-09-28 08:23:31 +13:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-09-12 12:31:11 +02:00
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
pub fn break_offset_if_not_bol(&mut self, n: usize, off: isize) {
|
2019-07-09 07:42:05 -04:00
|
|
|
if !self.s.is_beginning_of_line() {
|
2017-06-24 21:22:42 -06:00
|
|
|
self.s.break_offset(n, off)
|
2020-10-26 21:02:48 -04:00
|
|
|
} else if off != 0 && self.s.last_token().is_hardbreak_tok() {
|
|
|
|
// We do something pretty sketchy here: tuck the nonzero
|
|
|
|
// offset-adjustment we were going to deposit along with the
|
|
|
|
// break into the previous hardbreak.
|
|
|
|
self.s.replace_last_token(pp::Printer::hardbreak_tok_offset(off));
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Synthesizes a comment that was not textually present in the original source
|
|
|
|
// file.
|
2019-06-24 14:15:11 -04:00
|
|
|
pub fn synth_comment(&mut self, text: String) {
|
|
|
|
self.s.word("/*");
|
|
|
|
self.s.space();
|
|
|
|
self.s.word(text);
|
|
|
|
self.s.space();
|
2017-06-24 21:22:42 -06:00
|
|
|
self.s.word("*/")
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn commasep_cmnt<T, F, G>(&mut self, b: Breaks, elts: &[T], mut op: F, mut get_span: G)
|
|
|
|
where
|
|
|
|
F: FnMut(&mut State<'_>, &T),
|
2019-12-31 20:15:40 +03:00
|
|
|
G: FnMut(&T) -> rustc_span::Span,
|
2015-07-31 00:04:06 -07:00
|
|
|
{
|
2019-06-24 14:15:11 -04:00
|
|
|
self.rbox(0, b);
|
2015-07-31 00:04:06 -07:00
|
|
|
let len = elts.len();
|
|
|
|
let mut i = 0;
|
|
|
|
for elt in elts {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.maybe_print_comment(get_span(elt).hi());
|
|
|
|
op(self, elt);
|
2015-07-31 00:04:06 -07:00
|
|
|
i += 1;
|
|
|
|
if i < len {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(",");
|
|
|
|
self.maybe_print_trailing_comment(get_span(elt), Some(get_span(&elts[i]).hi()));
|
|
|
|
self.space_if_not_bol();
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.end();
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
pub fn commasep_exprs(&mut self, b: Breaks, exprs: &[hir::Expr<'_>]) {
|
2016-02-08 22:50:21 +01:00
|
|
|
self.commasep_cmnt(b, exprs, |s, e| s.print_expr(&e), |e| e.span)
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-11-29 10:24:47 +01:00
|
|
|
pub fn print_mod(&mut self, _mod: &hir::Mod<'_>, attrs: &[ast::Attribute]) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_inner_attributes(attrs);
|
2019-11-29 10:24:47 +01:00
|
|
|
for &item_id in _mod.item_ids {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.ann.nested(self, Nested::Item(item_id));
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
pub fn print_opt_lifetime(&mut self, lifetime: &hir::Lifetime) {
|
2017-01-09 17:46:11 +02:00
|
|
|
if !lifetime.is_elided() {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_lifetime(lifetime);
|
|
|
|
self.nbsp();
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
pub fn print_type(&mut self, ty: &hir::Ty<'_>) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.maybe_print_comment(ty.span.lo());
|
|
|
|
self.ibox(0);
|
2019-09-26 17:25:31 +01:00
|
|
|
match ty.kind {
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::Slice(ref ty) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("[");
|
|
|
|
self.print_type(&ty);
|
|
|
|
self.s.word("]");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::Ptr(ref mt) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("*");
|
2019-11-23 14:15:49 +00:00
|
|
|
self.print_mt(mt, true);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::Rptr(ref lifetime, ref mt) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("&");
|
|
|
|
self.print_opt_lifetime(lifetime);
|
2019-11-23 14:15:49 +00:00
|
|
|
self.print_mt(mt, false);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::Never => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("!");
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::Tup(ref elts) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.popen();
|
|
|
|
self.commasep(Inconsistent, &elts[..], |s, ty| s.print_type(&ty));
|
2015-07-31 00:04:06 -07:00
|
|
|
if elts.len() == 1 {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(",");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.pclose();
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::BareFn(ref f) => {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.print_ty_fn(
|
|
|
|
f.abi,
|
|
|
|
f.unsafety,
|
|
|
|
&f.decl,
|
|
|
|
None,
|
|
|
|
&f.generic_params,
|
2021-02-16 00:30:06 +01:00
|
|
|
f.param_names,
|
2019-12-22 17:42:04 -05:00
|
|
|
);
|
|
|
|
}
|
2020-06-07 18:56:17 +01:00
|
|
|
hir::TyKind::OpaqueDef(..) => self.s.word("/*impl Trait*/"),
|
2019-12-22 17:42:04 -05:00
|
|
|
hir::TyKind::Path(ref qpath) => self.print_qpath(qpath, false),
|
2021-03-13 15:44:29 +03:00
|
|
|
hir::TyKind::TraitObject(bounds, ref lifetime, syntax) => {
|
|
|
|
if syntax == ast::TraitObjectSyntax::Dyn {
|
|
|
|
self.word_space("dyn");
|
|
|
|
}
|
2017-01-24 17:17:06 +02:00
|
|
|
let mut first = true;
|
|
|
|
for bound in bounds {
|
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
} else {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.nbsp();
|
|
|
|
self.word_space("+");
|
2017-01-24 17:17:06 +02:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_poly_trait_ref(bound);
|
2017-01-24 17:17:06 +02:00
|
|
|
}
|
|
|
|
if !lifetime.is_elided() {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.nbsp();
|
|
|
|
self.word_space("+");
|
|
|
|
self.print_lifetime(lifetime);
|
2017-01-24 17:17:06 +02:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::Array(ref ty, ref length) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("[");
|
|
|
|
self.print_type(&ty);
|
|
|
|
self.s.word("; ");
|
|
|
|
self.print_anon_const(length);
|
|
|
|
self.s.word("]");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::Typeof(ref e) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("typeof(");
|
|
|
|
self.print_anon_const(e);
|
|
|
|
self.s.word(")");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::Infer => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("_");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::Err => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.popen();
|
|
|
|
self.s.word("/*ERROR*/");
|
|
|
|
self.pclose();
|
2017-03-28 18:56:29 -07:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
self.end()
|
|
|
|
}
|
|
|
|
|
2019-11-28 20:18:29 +01:00
|
|
|
pub fn print_foreign_item(&mut self, item: &hir::ForeignItem<'_>) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.hardbreak_if_not_bol();
|
|
|
|
self.maybe_print_comment(item.span.lo());
|
2020-11-27 00:35:22 +01:00
|
|
|
self.print_outer_attributes(self.attrs(item.hir_id()));
|
2019-09-26 17:58:14 +01:00
|
|
|
match item.kind {
|
2018-07-11 22:56:44 +08:00
|
|
|
hir::ForeignItemKind::Fn(ref decl, ref arg_names, ref generics) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.head("");
|
2019-12-22 17:42:04 -05:00
|
|
|
self.print_fn(
|
|
|
|
decl,
|
|
|
|
hir::FnHeader {
|
|
|
|
unsafety: hir::Unsafety::Normal,
|
|
|
|
constness: hir::Constness::NotConst,
|
|
|
|
abi: Abi::Rust,
|
|
|
|
asyncness: hir::IsAsync::NotAsync,
|
|
|
|
},
|
|
|
|
Some(item.ident.name),
|
|
|
|
generics,
|
|
|
|
&item.vis,
|
|
|
|
arg_names,
|
|
|
|
None,
|
|
|
|
);
|
2019-06-24 14:15:11 -04:00
|
|
|
self.end(); // end head-ibox
|
|
|
|
self.s.word(";");
|
2015-07-31 00:04:06 -07:00
|
|
|
self.end() // end the outer fn box
|
|
|
|
}
|
2018-07-11 22:56:44 +08:00
|
|
|
hir::ForeignItemKind::Static(ref t, m) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.head(visibility_qualified(&item.vis, "static"));
|
2019-12-16 17:28:40 +01:00
|
|
|
if m == hir::Mutability::Mut {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_space("mut");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_ident(item.ident);
|
|
|
|
self.word_space(":");
|
|
|
|
self.print_type(&t);
|
|
|
|
self.s.word(";");
|
|
|
|
self.end(); // end the head-ibox
|
2017-09-03 19:53:58 +01:00
|
|
|
self.end() // end the outer cbox
|
|
|
|
}
|
2018-07-11 22:56:44 +08:00
|
|
|
hir::ForeignItemKind::Type => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.head(visibility_qualified(&item.vis, "type"));
|
|
|
|
self.print_ident(item.ident);
|
|
|
|
self.s.word(";");
|
|
|
|
self.end(); // end the head-ibox
|
2015-07-31 00:04:06 -07:00
|
|
|
self.end() // end the outer cbox
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn print_associated_const(
|
|
|
|
&mut self,
|
2020-04-19 13:00:18 +02:00
|
|
|
ident: Ident,
|
2019-11-30 17:46:46 +01:00
|
|
|
ty: &hir::Ty<'_>,
|
2019-12-22 17:42:04 -05:00
|
|
|
default: Option<hir::BodyId>,
|
2019-11-30 17:46:46 +01:00
|
|
|
vis: &hir::Visibility<'_>,
|
2019-12-22 17:42:04 -05:00
|
|
|
) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(visibility_qualified(vis, ""));
|
|
|
|
self.word_space("const");
|
|
|
|
self.print_ident(ident);
|
|
|
|
self.word_space(":");
|
|
|
|
self.print_type(ty);
|
2015-07-31 00:04:06 -07:00
|
|
|
if let Some(expr) = default {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.space();
|
|
|
|
self.word_space("=");
|
|
|
|
self.ann.nested(self, Nested::Body(expr));
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2017-06-24 21:22:42 -06:00
|
|
|
self.s.word(";")
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn print_associated_type(
|
|
|
|
&mut self,
|
2020-04-19 13:00:18 +02:00
|
|
|
ident: Ident,
|
2020-02-07 18:27:12 +03:00
|
|
|
generics: &hir::Generics<'_>,
|
2019-12-01 00:17:43 +01:00
|
|
|
bounds: Option<hir::GenericBounds<'_>>,
|
2019-11-30 17:46:46 +01:00
|
|
|
ty: Option<&hir::Ty<'_>>,
|
2019-12-22 17:42:04 -05:00
|
|
|
) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_space("type");
|
|
|
|
self.print_ident(ident);
|
2020-02-07 18:27:12 +03:00
|
|
|
self.print_generic_params(&generics.params);
|
2015-07-31 00:04:06 -07:00
|
|
|
if let Some(bounds) = bounds {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_bounds(":", bounds);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2020-02-07 18:27:12 +03:00
|
|
|
self.print_where_clause(&generics.where_clause);
|
2015-07-31 00:04:06 -07:00
|
|
|
if let Some(ty) = ty {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.space();
|
|
|
|
self.word_space("=");
|
|
|
|
self.print_type(ty);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2017-06-24 21:22:42 -06:00
|
|
|
self.s.word(";")
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-08-02 03:40:16 +01:00
|
|
|
fn print_item_type(
|
|
|
|
&mut self,
|
2019-11-28 19:28:50 +01:00
|
|
|
item: &hir::Item<'_>,
|
2019-11-30 17:46:46 +01:00
|
|
|
generics: &hir::Generics<'_>,
|
2019-08-02 03:40:16 +01:00
|
|
|
inner: impl Fn(&mut Self),
|
|
|
|
) {
|
|
|
|
self.head(visibility_qualified(&item.vis, "type"));
|
|
|
|
self.print_ident(item.ident);
|
|
|
|
self.print_generic_params(&generics.params);
|
|
|
|
self.end(); // end the inner ibox
|
|
|
|
|
|
|
|
self.print_where_clause(&generics.where_clause);
|
|
|
|
self.s.space();
|
|
|
|
inner(self);
|
|
|
|
self.s.word(";");
|
|
|
|
self.end(); // end the outer ibox
|
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
/// Pretty-print an item
|
2019-11-28 19:28:50 +01:00
|
|
|
pub fn print_item(&mut self, item: &hir::Item<'_>) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.hardbreak_if_not_bol();
|
|
|
|
self.maybe_print_comment(item.span.lo());
|
2021-01-24 13:17:54 +01:00
|
|
|
let attrs = self.attrs(item.hir_id());
|
|
|
|
self.print_outer_attributes(attrs);
|
2019-06-24 14:15:11 -04:00
|
|
|
self.ann.pre(self, AnnNode::Item(item));
|
2019-09-26 17:51:36 +01:00
|
|
|
match item.kind {
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::ExternCrate(orig_name) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.head(visibility_qualified(&item.vis, "extern crate"));
|
2018-03-09 18:51:48 +03:00
|
|
|
if let Some(orig_name) = orig_name {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_name(orig_name);
|
|
|
|
self.s.space();
|
|
|
|
self.s.word("as");
|
|
|
|
self.s.space();
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_ident(item.ident);
|
|
|
|
self.s.word(";");
|
|
|
|
self.end(); // end inner head-block
|
|
|
|
self.end(); // end outer head-block
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Use(ref path, kind) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.head(visibility_qualified(&item.vis, "use"));
|
|
|
|
self.print_path(path, false);
|
2016-11-24 06:11:31 +02:00
|
|
|
|
|
|
|
match kind {
|
|
|
|
hir::UseKind::Single => {
|
2018-12-01 02:47:08 +00:00
|
|
|
if path.segments.last().unwrap().ident != item.ident {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.space();
|
|
|
|
self.word_space("as");
|
|
|
|
self.print_ident(item.ident);
|
2016-11-24 06:11:31 +02:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(";");
|
2016-11-24 06:11:31 +02:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
hir::UseKind::Glob => self.s.word("::*;"),
|
2019-12-22 17:42:04 -05:00
|
|
|
hir::UseKind::ListStem => self.s.word("::{};"),
|
2016-11-24 06:11:31 +02:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.end(); // end inner head-block
|
|
|
|
self.end(); // end outer head-block
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Static(ref ty, m, expr) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.head(visibility_qualified(&item.vis, "static"));
|
2019-12-16 17:28:40 +01:00
|
|
|
if m == hir::Mutability::Mut {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_space("mut");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_ident(item.ident);
|
|
|
|
self.word_space(":");
|
|
|
|
self.print_type(&ty);
|
|
|
|
self.s.space();
|
|
|
|
self.end(); // end the head-ibox
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_space("=");
|
|
|
|
self.ann.nested(self, Nested::Body(expr));
|
|
|
|
self.s.word(";");
|
|
|
|
self.end(); // end the outer cbox
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Const(ref ty, expr) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.head(visibility_qualified(&item.vis, "const"));
|
|
|
|
self.print_ident(item.ident);
|
|
|
|
self.word_space(":");
|
|
|
|
self.print_type(&ty);
|
|
|
|
self.s.space();
|
|
|
|
self.end(); // end the head-ibox
|
|
|
|
|
|
|
|
self.word_space("=");
|
|
|
|
self.ann.nested(self, Nested::Body(expr));
|
|
|
|
self.s.word(";");
|
|
|
|
self.end(); // end the outer cbox
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-11-07 12:57:52 +01:00
|
|
|
hir::ItemKind::Fn(ref sig, ref param_names, body) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.head("");
|
2019-12-22 17:42:04 -05:00
|
|
|
self.print_fn(
|
|
|
|
&sig.decl,
|
|
|
|
sig.header,
|
|
|
|
Some(item.ident.name),
|
|
|
|
param_names,
|
|
|
|
&item.vis,
|
|
|
|
&[],
|
|
|
|
Some(body),
|
|
|
|
);
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(" ");
|
|
|
|
self.end(); // need to close a box
|
|
|
|
self.end(); // need to close a box
|
|
|
|
self.ann.nested(self, Nested::Body(body));
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Mod(ref _mod) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.head(visibility_qualified(&item.vis, "mod"));
|
|
|
|
self.print_ident(item.ident);
|
|
|
|
self.nbsp();
|
|
|
|
self.bopen();
|
2021-01-24 13:17:54 +01:00
|
|
|
self.print_mod(_mod, attrs);
|
2019-06-24 14:15:11 -04:00
|
|
|
self.bclose(item.span);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2020-11-11 22:40:09 +01:00
|
|
|
hir::ItemKind::ForeignMod { abi, items } => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.head("extern");
|
2020-11-11 22:40:09 +01:00
|
|
|
self.word_nbsp(abi.to_string());
|
2019-06-24 14:15:11 -04:00
|
|
|
self.bopen();
|
2021-01-24 13:17:54 +01:00
|
|
|
self.print_inner_attributes(self.attrs(item.hir_id()));
|
2020-11-11 22:40:09 +01:00
|
|
|
for item in items {
|
|
|
|
self.ann.nested(self, Nested::ForeignItem(item.id));
|
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.bclose(item.span);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2021-04-11 20:51:28 +01:00
|
|
|
hir::ItemKind::GlobalAsm(ref asm) => {
|
|
|
|
self.head(visibility_qualified(&item.vis, "global_asm!"));
|
|
|
|
self.print_inline_asm(asm);
|
2019-06-24 14:15:11 -04:00
|
|
|
self.end()
|
2017-03-15 21:27:40 -05:00
|
|
|
}
|
2019-08-02 11:02:08 +01:00
|
|
|
hir::ItemKind::TyAlias(ref ty, ref generics) => {
|
2019-08-02 03:40:16 +01:00
|
|
|
self.print_item_type(item, &generics, |state| {
|
|
|
|
state.word_space("=");
|
|
|
|
state.print_type(&ty);
|
|
|
|
});
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-08-02 01:14:42 +01:00
|
|
|
hir::ItemKind::OpaqueTy(ref opaque_ty) => {
|
2019-08-02 03:40:16 +01:00
|
|
|
self.print_item_type(item, &opaque_ty.generics, |state| {
|
|
|
|
let mut real_bounds = Vec::with_capacity(opaque_ty.bounds.len());
|
|
|
|
for b in opaque_ty.bounds.iter() {
|
|
|
|
if let GenericBound::Trait(ref ptr, hir::TraitBoundModifier::Maybe) = *b {
|
|
|
|
state.s.space();
|
|
|
|
state.word_space("for ?");
|
|
|
|
state.print_trait_ref(&ptr.trait_ref);
|
|
|
|
} else {
|
|
|
|
real_bounds.push(b);
|
|
|
|
}
|
2018-05-22 14:31:56 +02:00
|
|
|
}
|
2019-08-02 03:40:16 +01:00
|
|
|
state.print_bounds("= impl", real_bounds);
|
|
|
|
});
|
2018-05-22 14:31:56 +02:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Enum(ref enum_definition, ref params) => {
|
2019-08-02 03:40:16 +01:00
|
|
|
self.print_enum_def(enum_definition, params, item.ident.name, item.span, &item.vis);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Struct(ref struct_def, ref generics) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.head(visibility_qualified(&item.vis, "struct"));
|
|
|
|
self.print_struct(struct_def, generics, item.ident.name, item.span, true);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Union(ref struct_def, ref generics) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.head(visibility_qualified(&item.vis, "union"));
|
|
|
|
self.print_struct(struct_def, generics, item.ident.name, item.span, true);
|
2016-08-06 21:36:28 +03:00
|
|
|
}
|
2020-11-22 17:46:21 -05:00
|
|
|
hir::ItemKind::Impl(hir::Impl {
|
2019-12-22 17:42:04 -05:00
|
|
|
unsafety,
|
|
|
|
polarity,
|
|
|
|
defaultness,
|
2020-01-13 20:30:24 -08:00
|
|
|
constness,
|
2020-03-02 15:26:00 -08:00
|
|
|
defaultness_span: _,
|
2019-12-22 17:42:04 -05:00
|
|
|
ref generics,
|
2020-01-17 16:14:29 -08:00
|
|
|
ref of_trait,
|
|
|
|
ref self_ty,
|
|
|
|
items,
|
2020-11-22 17:46:21 -05:00
|
|
|
}) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.head("");
|
|
|
|
self.print_visibility(&item.vis);
|
|
|
|
self.print_defaultness(defaultness);
|
|
|
|
self.print_unsafety(unsafety);
|
|
|
|
self.word_nbsp("impl");
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2017-10-16 21:07:26 +02:00
|
|
|
if !generics.params.is_empty() {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_generic_params(&generics.params);
|
|
|
|
self.s.space();
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2020-02-10 15:24:53 +01:00
|
|
|
if constness == hir::Constness::Const {
|
2020-01-13 20:30:24 -08:00
|
|
|
self.word_nbsp("const");
|
|
|
|
}
|
|
|
|
|
2020-03-04 16:15:23 -08:00
|
|
|
if let hir::ImplPolarity::Negative(_) = polarity {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("!");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2020-01-17 16:14:29 -08:00
|
|
|
if let Some(ref t) = of_trait {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_trait_ref(t);
|
|
|
|
self.s.space();
|
|
|
|
self.word_space("for");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2020-01-17 16:14:29 -08:00
|
|
|
self.print_type(&self_ty);
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_where_clause(&generics.where_clause);
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.space();
|
|
|
|
self.bopen();
|
2021-01-24 13:17:54 +01:00
|
|
|
self.print_inner_attributes(attrs);
|
2020-01-17 16:14:29 -08:00
|
|
|
for impl_item in items {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.ann.nested(self, Nested::ImplItem(impl_item.id));
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.bclose(item.span);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-11-28 19:28:50 +01:00
|
|
|
hir::ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, trait_items) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.head("");
|
|
|
|
self.print_visibility(&item.vis);
|
|
|
|
self.print_is_auto(is_auto);
|
|
|
|
self.print_unsafety(unsafety);
|
|
|
|
self.word_nbsp("trait");
|
|
|
|
self.print_ident(item.ident);
|
|
|
|
self.print_generic_params(&generics.params);
|
2015-07-31 00:04:06 -07:00
|
|
|
let mut real_bounds = Vec::with_capacity(bounds.len());
|
|
|
|
for b in bounds.iter() {
|
2018-06-14 12:08:58 +01:00
|
|
|
if let GenericBound::Trait(ref ptr, hir::TraitBoundModifier::Maybe) = *b {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.space();
|
|
|
|
self.word_space("for ?");
|
|
|
|
self.print_trait_ref(&ptr.trait_ref);
|
2015-07-31 00:04:06 -07:00
|
|
|
} else {
|
2019-06-12 11:43:15 +03:00
|
|
|
real_bounds.push(b);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_bounds(":", real_bounds);
|
|
|
|
self.print_where_clause(&generics.where_clause);
|
|
|
|
self.s.word(" ");
|
|
|
|
self.bopen();
|
2015-07-31 00:04:06 -07:00
|
|
|
for trait_item in trait_items {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.ann.nested(self, Nested::TraitItem(trait_item.id));
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.bclose(item.span);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::TraitAlias(ref generics, ref bounds) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.head("");
|
|
|
|
self.print_visibility(&item.vis);
|
|
|
|
self.word_nbsp("trait");
|
|
|
|
self.print_ident(item.ident);
|
|
|
|
self.print_generic_params(&generics.params);
|
2017-10-02 12:28:16 +00:00
|
|
|
let mut real_bounds = Vec::with_capacity(bounds.len());
|
|
|
|
// FIXME(durka) this seems to be some quite outdated syntax
|
|
|
|
for b in bounds.iter() {
|
2018-06-14 12:08:58 +01:00
|
|
|
if let GenericBound::Trait(ref ptr, hir::TraitBoundModifier::Maybe) = *b {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.space();
|
|
|
|
self.word_space("for ?");
|
|
|
|
self.print_trait_ref(&ptr.trait_ref);
|
2017-10-02 12:28:16 +00:00
|
|
|
} else {
|
2019-06-12 11:43:15 +03:00
|
|
|
real_bounds.push(b);
|
2017-10-02 12:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.nbsp();
|
|
|
|
self.print_bounds("=", real_bounds);
|
|
|
|
self.print_where_clause(&generics.where_clause);
|
|
|
|
self.s.word(";");
|
2017-10-02 12:28:16 +00:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-08-22 22:05:19 +01:00
|
|
|
self.ann.post(self, AnnNode::Item(item))
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
pub fn print_trait_ref(&mut self, t: &hir::TraitRef<'_>) {
|
2016-10-27 05:17:42 +03:00
|
|
|
self.print_path(&t.path, false)
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
fn print_formal_generic_params(&mut self, generic_params: &[hir::GenericParam<'_>]) {
|
2017-10-16 21:07:26 +02:00
|
|
|
if !generic_params.is_empty() {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("for");
|
|
|
|
self.print_generic_params(generic_params);
|
|
|
|
self.nbsp();
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
fn print_poly_trait_ref(&mut self, t: &hir::PolyTraitRef<'_>) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_formal_generic_params(&t.bound_generic_params);
|
2015-07-31 00:04:06 -07:00
|
|
|
self.print_trait_ref(&t.trait_ref)
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn print_enum_def(
|
|
|
|
&mut self,
|
|
|
|
enum_definition: &hir::EnumDef<'_>,
|
2019-11-30 17:46:46 +01:00
|
|
|
generics: &hir::Generics<'_>,
|
2020-04-19 13:00:18 +02:00
|
|
|
name: Symbol,
|
2019-12-31 20:15:40 +03:00
|
|
|
span: rustc_span::Span,
|
2019-11-30 17:46:46 +01:00
|
|
|
visibility: &hir::Visibility<'_>,
|
2019-12-22 17:42:04 -05:00
|
|
|
) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.head(visibility_qualified(visibility, "enum"));
|
|
|
|
self.print_name(name);
|
|
|
|
self.print_generic_params(&generics.params);
|
|
|
|
self.print_where_clause(&generics.where_clause);
|
|
|
|
self.s.space();
|
2015-07-31 00:04:06 -07:00
|
|
|
self.print_variants(&enum_definition.variants, span)
|
|
|
|
}
|
|
|
|
|
2019-12-31 20:15:40 +03:00
|
|
|
pub fn print_variants(&mut self, variants: &[hir::Variant<'_>], span: rustc_span::Span) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.bopen();
|
2015-07-31 00:04:06 -07:00
|
|
|
for v in variants {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.space_if_not_bol();
|
|
|
|
self.maybe_print_comment(v.span.lo());
|
2020-11-27 00:07:36 +01:00
|
|
|
self.print_outer_attributes(self.attrs(v.id));
|
2019-07-09 09:32:25 -04:00
|
|
|
self.ibox(INDENT_UNIT);
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_variant(v);
|
|
|
|
self.s.word(",");
|
|
|
|
self.end();
|
|
|
|
self.maybe_print_trailing_comment(v.span, None);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
self.bclose(span)
|
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
pub fn print_visibility(&mut self, vis: &hir::Visibility<'_>) {
|
2018-06-30 20:34:18 -07:00
|
|
|
match vis.node {
|
2019-06-24 14:15:11 -04:00
|
|
|
hir::VisibilityKind::Public => self.word_nbsp("pub"),
|
|
|
|
hir::VisibilityKind::Crate(ast::CrateSugar::JustCrate) => self.word_nbsp("crate"),
|
|
|
|
hir::VisibilityKind::Crate(ast::CrateSugar::PubCrate) => self.word_nbsp("pub(crate)"),
|
2018-07-01 11:05:10 -07:00
|
|
|
hir::VisibilityKind::Restricted { ref path, .. } => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("pub(");
|
2019-12-22 17:42:04 -05:00
|
|
|
if path.segments.len() == 1 && path.segments[0].ident.name == kw::Super {
|
2018-05-25 16:53:49 -04:00
|
|
|
// Special case: `super` can print like `pub(super)`.
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("super");
|
2018-05-25 16:53:49 -04:00
|
|
|
} else {
|
|
|
|
// Everything else requires `in` at present.
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_nbsp("in");
|
|
|
|
self.print_path(path, false);
|
2018-05-25 16:53:49 -04:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_nbsp(")");
|
2016-12-27 10:00:18 +02:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
hir::VisibilityKind::Inherited => (),
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
pub fn print_defaultness(&mut self, defaultness: hir::Defaultness) {
|
2016-11-21 18:39:25 +00:00
|
|
|
match defaultness {
|
2019-06-24 14:15:11 -04:00
|
|
|
hir::Defaultness::Default { .. } => self.word_nbsp("default"),
|
2016-11-21 18:39:25 +00:00
|
|
|
hir::Defaultness::Final => (),
|
2016-11-18 17:14:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn print_struct(
|
|
|
|
&mut self,
|
|
|
|
struct_def: &hir::VariantData<'_>,
|
2019-11-30 17:46:46 +01:00
|
|
|
generics: &hir::Generics<'_>,
|
2020-04-19 13:00:18 +02:00
|
|
|
name: Symbol,
|
2019-12-31 20:15:40 +03:00
|
|
|
span: rustc_span::Span,
|
2019-12-22 17:42:04 -05:00
|
|
|
print_finalizer: bool,
|
|
|
|
) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_name(name);
|
|
|
|
self.print_generic_params(&generics.params);
|
2019-03-24 00:06:58 +03:00
|
|
|
match struct_def {
|
|
|
|
hir::VariantData::Tuple(..) | hir::VariantData::Unit(..) => {
|
|
|
|
if let hir::VariantData::Tuple(..) = struct_def {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.popen();
|
2019-03-24 00:06:58 +03:00
|
|
|
self.commasep(Inconsistent, struct_def.fields(), |s, field| {
|
2019-06-24 14:15:11 -04:00
|
|
|
s.maybe_print_comment(field.span.lo());
|
2020-11-27 00:27:34 +01:00
|
|
|
s.print_outer_attributes(s.attrs(field.hir_id));
|
2019-06-24 14:15:11 -04:00
|
|
|
s.print_visibility(&field.vis);
|
2019-03-24 00:06:58 +03:00
|
|
|
s.print_type(&field.ty)
|
2019-06-24 14:15:11 -04:00
|
|
|
});
|
|
|
|
self.pclose();
|
2019-03-24 00:06:58 +03:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_where_clause(&generics.where_clause);
|
2019-03-24 00:06:58 +03:00
|
|
|
if print_finalizer {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(";");
|
2019-03-24 00:06:58 +03:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.end();
|
2019-03-24 00:06:58 +03:00
|
|
|
self.end() // close the outer-box
|
2015-10-02 00:03:22 +03:00
|
|
|
}
|
2019-03-24 00:06:58 +03:00
|
|
|
hir::VariantData::Struct(..) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_where_clause(&generics.where_clause);
|
|
|
|
self.nbsp();
|
|
|
|
self.bopen();
|
|
|
|
self.hardbreak_if_not_bol();
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-03-24 00:06:58 +03:00
|
|
|
for field in struct_def.fields() {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.hardbreak_if_not_bol();
|
|
|
|
self.maybe_print_comment(field.span.lo());
|
2020-11-27 00:27:34 +01:00
|
|
|
self.print_outer_attributes(self.attrs(field.hir_id));
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_visibility(&field.vis);
|
|
|
|
self.print_ident(field.ident);
|
|
|
|
self.word_nbsp(":");
|
|
|
|
self.print_type(&field.ty);
|
|
|
|
self.s.word(",");
|
2019-03-24 00:06:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
self.bclose(span)
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 09:26:18 +01:00
|
|
|
pub fn print_variant(&mut self, v: &hir::Variant<'_>) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.head("");
|
2016-03-29 09:32:58 +03:00
|
|
|
let generics = hir::Generics::empty();
|
2019-08-13 21:40:21 -03:00
|
|
|
self.print_struct(&v.data, &generics, v.ident.name, v.span, false);
|
|
|
|
if let Some(ref d) = v.disr_expr {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.space();
|
|
|
|
self.word_space("=");
|
|
|
|
self.print_anon_const(d);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn print_method_sig(
|
|
|
|
&mut self,
|
2020-04-19 13:00:18 +02:00
|
|
|
ident: Ident,
|
2019-11-30 15:20:35 +01:00
|
|
|
m: &hir::FnSig<'_>,
|
2019-11-30 17:46:46 +01:00
|
|
|
generics: &hir::Generics<'_>,
|
|
|
|
vis: &hir::Visibility<'_>,
|
2020-04-19 13:00:18 +02:00
|
|
|
arg_names: &[Ident],
|
2019-12-22 17:42:04 -05:00
|
|
|
body_id: Option<hir::BodyId>,
|
|
|
|
) {
|
|
|
|
self.print_fn(&m.decl, m.header, Some(ident.name), generics, vis, arg_names, body_id)
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-11-28 21:47:10 +01:00
|
|
|
pub fn print_trait_item(&mut self, ti: &hir::TraitItem<'_>) {
|
2021-01-30 20:46:50 +01:00
|
|
|
self.ann.pre(self, AnnNode::SubItem(ti.hir_id()));
|
2019-06-24 14:15:11 -04:00
|
|
|
self.hardbreak_if_not_bol();
|
|
|
|
self.maybe_print_comment(ti.span.lo());
|
2020-11-27 09:41:53 +01:00
|
|
|
self.print_outer_attributes(self.attrs(ti.hir_id()));
|
2019-09-26 17:07:54 +01:00
|
|
|
match ti.kind {
|
2016-12-21 12:32:59 +02:00
|
|
|
hir::TraitItemKind::Const(ref ty, default) => {
|
2019-12-22 17:42:04 -05:00
|
|
|
let vis =
|
2019-12-31 20:15:40 +03:00
|
|
|
Spanned { span: rustc_span::DUMMY_SP, node: hir::VisibilityKind::Inherited };
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_associated_const(ti.ident, &ty, default, &vis);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2020-03-05 09:57:34 -06:00
|
|
|
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Required(ref arg_names)) => {
|
2019-12-22 17:42:04 -05:00
|
|
|
let vis =
|
2019-12-31 20:15:40 +03:00
|
|
|
Spanned { span: rustc_span::DUMMY_SP, node: hir::VisibilityKind::Inherited };
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_method_sig(ti.ident, sig, &ti.generics, &vis, arg_names, None);
|
|
|
|
self.s.word(";");
|
2016-12-20 22:46:11 +02:00
|
|
|
}
|
2020-03-05 09:57:34 -06:00
|
|
|
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
|
2019-12-22 17:42:04 -05:00
|
|
|
let vis =
|
2019-12-31 20:15:40 +03:00
|
|
|
Spanned { span: rustc_span::DUMMY_SP, node: hir::VisibilityKind::Inherited };
|
2019-06-24 14:15:11 -04:00
|
|
|
self.head("");
|
|
|
|
self.print_method_sig(ti.ident, sig, &ti.generics, &vis, &[], Some(body));
|
|
|
|
self.nbsp();
|
|
|
|
self.end(); // need to close a box
|
|
|
|
self.end(); // need to close a box
|
|
|
|
self.ann.nested(self, Nested::Body(body));
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2016-12-04 04:21:06 +02:00
|
|
|
hir::TraitItemKind::Type(ref bounds, ref default) => {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.print_associated_type(
|
|
|
|
ti.ident,
|
2020-02-07 18:27:12 +03:00
|
|
|
&ti.generics,
|
2019-12-22 17:42:04 -05:00
|
|
|
Some(bounds),
|
|
|
|
default.as_ref().map(|ty| &**ty),
|
|
|
|
);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
2021-01-30 20:46:50 +01:00
|
|
|
self.ann.post(self, AnnNode::SubItem(ti.hir_id()))
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-11-28 22:16:44 +01:00
|
|
|
pub fn print_impl_item(&mut self, ii: &hir::ImplItem<'_>) {
|
2021-01-30 23:25:03 +01:00
|
|
|
self.ann.pre(self, AnnNode::SubItem(ii.hir_id()));
|
2019-06-24 14:15:11 -04:00
|
|
|
self.hardbreak_if_not_bol();
|
|
|
|
self.maybe_print_comment(ii.span.lo());
|
2020-11-27 09:55:10 +01:00
|
|
|
self.print_outer_attributes(self.attrs(ii.hir_id()));
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_defaultness(ii.defaultness);
|
2016-03-13 22:55:26 -07:00
|
|
|
|
2019-09-26 16:38:13 +01:00
|
|
|
match ii.kind {
|
2016-12-21 12:32:59 +02:00
|
|
|
hir::ImplItemKind::Const(ref ty, expr) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_associated_const(ii.ident, &ty, Some(expr), &ii.vis);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2020-03-05 09:57:34 -06:00
|
|
|
hir::ImplItemKind::Fn(ref sig, body) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.head("");
|
|
|
|
self.print_method_sig(ii.ident, sig, &ii.generics, &ii.vis, &[], Some(body));
|
|
|
|
self.nbsp();
|
|
|
|
self.end(); // need to close a box
|
|
|
|
self.end(); // need to close a box
|
|
|
|
self.ann.nested(self, Nested::Body(body));
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-08-02 20:59:07 +01:00
|
|
|
hir::ImplItemKind::TyAlias(ref ty) => {
|
2020-02-07 18:27:12 +03:00
|
|
|
self.print_associated_type(ii.ident, &ii.generics, None, Some(ty));
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
2021-01-30 23:25:03 +01:00
|
|
|
self.ann.post(self, AnnNode::SubItem(ii.hir_id()))
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
pub fn print_local(&mut self, init: Option<&hir::Expr<'_>>, decl: impl Fn(&mut Self)) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.space_if_not_bol();
|
2019-07-09 09:32:25 -04:00
|
|
|
self.ibox(INDENT_UNIT);
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_nbsp("let");
|
2019-04-24 06:39:40 +02:00
|
|
|
|
2019-07-09 09:32:25 -04:00
|
|
|
self.ibox(INDENT_UNIT);
|
2019-06-24 14:15:11 -04:00
|
|
|
decl(self);
|
|
|
|
self.end();
|
2019-04-24 06:39:40 +02:00
|
|
|
|
|
|
|
if let Some(ref init) = init {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.nbsp();
|
|
|
|
self.word_space("=");
|
|
|
|
self.print_expr(&init);
|
2019-04-24 06:39:40 +02:00
|
|
|
}
|
|
|
|
self.end()
|
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
pub fn print_stmt(&mut self, st: &hir::Stmt<'_>) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.maybe_print_comment(st.span.lo());
|
2019-09-26 17:34:50 +01:00
|
|
|
match st.kind {
|
2019-01-17 10:39:24 +11:00
|
|
|
hir::StmtKind::Local(ref loc) => {
|
2019-07-06 03:35:19 +09:00
|
|
|
self.print_local(loc.init.as_deref(), |this| this.print_local_decl(&loc));
|
2019-01-17 10:39:24 +11:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
hir::StmtKind::Item(item) => self.ann.nested(self, Nested::Item(item)),
|
2019-01-17 09:45:02 +11:00
|
|
|
hir::StmtKind::Expr(ref expr) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.space_if_not_bol();
|
|
|
|
self.print_expr(&expr);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-01-17 09:45:02 +11:00
|
|
|
hir::StmtKind::Semi(ref expr) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.space_if_not_bol();
|
|
|
|
self.print_expr(&expr);
|
|
|
|
self.s.word(";");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
2019-09-26 17:34:50 +01:00
|
|
|
if stmt_ends_with_semi(&st.kind) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(";");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
self.maybe_print_trailing_comment(st.span, None)
|
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
pub fn print_block(&mut self, blk: &hir::Block<'_>) {
|
2015-07-31 00:04:06 -07:00
|
|
|
self.print_block_with_attrs(blk, &[])
|
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
pub fn print_block_unclosed(&mut self, blk: &hir::Block<'_>) {
|
2019-07-09 09:30:08 -04:00
|
|
|
self.print_block_maybe_unclosed(blk, &[], false)
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
pub fn print_block_with_attrs(&mut self, blk: &hir::Block<'_>, attrs: &[ast::Attribute]) {
|
2019-07-09 09:30:08 -04:00
|
|
|
self.print_block_maybe_unclosed(blk, attrs, true)
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn print_block_maybe_unclosed(
|
|
|
|
&mut self,
|
2019-11-29 13:43:03 +01:00
|
|
|
blk: &hir::Block<'_>,
|
2019-12-22 17:42:04 -05:00
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
close_box: bool,
|
|
|
|
) {
|
2015-07-31 00:04:06 -07:00
|
|
|
match blk.rules {
|
2020-03-23 11:32:07 +01:00
|
|
|
hir::BlockCheckMode::UnsafeBlock(..) => self.word_space("unsafe"),
|
|
|
|
hir::BlockCheckMode::PushUnsafeBlock(..) => self.word_space("push_unsafe"),
|
|
|
|
hir::BlockCheckMode::PopUnsafeBlock(..) => self.word_space("pop_unsafe"),
|
|
|
|
hir::BlockCheckMode::DefaultBlock => (),
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.maybe_print_comment(blk.span.lo());
|
|
|
|
self.ann.pre(self, AnnNode::Block(blk));
|
|
|
|
self.bopen();
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_inner_attributes(attrs);
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-11-29 14:08:03 +01:00
|
|
|
for st in blk.stmts {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_stmt(st);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-09-12 12:31:11 +02:00
|
|
|
if let Some(ref expr) = blk.expr {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.space_if_not_bol();
|
|
|
|
self.print_expr(&expr);
|
|
|
|
self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi()));
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-07-09 09:30:08 -04:00
|
|
|
self.bclose_maybe_open(blk.span, close_box);
|
2018-08-22 22:05:19 +01:00
|
|
|
self.ann.post(self, AnnNode::Block(blk))
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2021-01-01 15:38:11 -03:00
|
|
|
fn print_else(&mut self, els: Option<&hir::Expr<'_>>) {
|
|
|
|
match els {
|
2021-04-23 13:29:18 -04:00
|
|
|
Some(else_) => {
|
|
|
|
match else_.kind {
|
2021-01-01 15:38:11 -03:00
|
|
|
// "another else-if"
|
|
|
|
hir::ExprKind::If(ref i, ref then, ref e) => {
|
|
|
|
self.cbox(INDENT_UNIT - 1);
|
|
|
|
self.ibox(0);
|
|
|
|
self.s.word(" else if ");
|
|
|
|
self.print_expr_as_cond(&i);
|
|
|
|
self.s.space();
|
|
|
|
self.print_expr(&then);
|
|
|
|
self.print_else(e.as_ref().map(|e| &**e))
|
|
|
|
}
|
|
|
|
// "final else"
|
|
|
|
hir::ExprKind::Block(ref b, _) => {
|
|
|
|
self.cbox(INDENT_UNIT - 1);
|
|
|
|
self.ibox(0);
|
|
|
|
self.s.word(" else ");
|
|
|
|
self.print_block(&b)
|
|
|
|
}
|
2021-04-23 13:29:18 -04:00
|
|
|
hir::ExprKind::Match(ref expr, arms, _) => {
|
|
|
|
// else if let desugared to match
|
|
|
|
assert!(arms.len() == 2, "if let desugars to match with two arms");
|
|
|
|
|
|
|
|
self.s.word(" else ");
|
|
|
|
self.s.word("{");
|
|
|
|
|
|
|
|
self.cbox(INDENT_UNIT);
|
|
|
|
self.ibox(INDENT_UNIT);
|
|
|
|
self.word_nbsp("match");
|
|
|
|
self.print_expr_as_cond(&expr);
|
|
|
|
self.s.space();
|
|
|
|
self.bopen();
|
|
|
|
for arm in arms {
|
|
|
|
self.print_arm(arm);
|
|
|
|
}
|
|
|
|
self.bclose(expr.span);
|
|
|
|
|
|
|
|
self.s.word("}");
|
|
|
|
}
|
2021-01-01 15:38:11 -03:00
|
|
|
// BLEAH, constraints would be great here
|
|
|
|
_ => {
|
|
|
|
panic!("print_if saw if with weird alternative");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_if(
|
|
|
|
&mut self,
|
|
|
|
test: &hir::Expr<'_>,
|
|
|
|
blk: &hir::Expr<'_>,
|
|
|
|
elseopt: Option<&hir::Expr<'_>>,
|
|
|
|
) {
|
|
|
|
self.head("if");
|
|
|
|
self.print_expr_as_cond(test);
|
|
|
|
self.s.space();
|
|
|
|
self.print_expr(blk);
|
|
|
|
self.print_else(elseopt)
|
|
|
|
}
|
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
pub fn print_anon_const(&mut self, constant: &hir::AnonConst) {
|
2018-05-17 21:28:50 +03:00
|
|
|
self.ann.nested(self, Nested::Body(constant.body))
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
fn print_call_post(&mut self, args: &[hir::Expr<'_>]) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.popen();
|
|
|
|
self.commasep_exprs(Inconsistent, args);
|
2015-07-31 00:04:06 -07:00
|
|
|
self.pclose()
|
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
pub fn print_expr_maybe_paren(&mut self, expr: &hir::Expr<'_>, prec: i8) {
|
2018-01-10 11:40:16 -08:00
|
|
|
let needs_par = expr.precedence().order() < prec;
|
2017-08-14 18:27:20 -04:00
|
|
|
if needs_par {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.popen();
|
2017-08-14 18:27:20 -04:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_expr(expr);
|
2017-08-14 18:27:20 -04:00
|
|
|
if needs_par {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.pclose();
|
2017-08-14 18:27:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print an expr using syntax that's acceptable in a condition position, such as the `cond` in
|
|
|
|
/// `if cond { ... }`.
|
2019-11-29 13:43:03 +01:00
|
|
|
pub fn print_expr_as_cond(&mut self, expr: &hir::Expr<'_>) {
|
2019-09-26 14:39:48 +01:00
|
|
|
let needs_par = match expr.kind {
|
2017-08-14 18:27:20 -04:00
|
|
|
// These cases need parens due to the parse error observed in #26461: `if return {}`
|
|
|
|
// parses as the erroneous construct `if (return {})`, not `if (return) {}`.
|
2019-12-22 17:42:04 -05:00
|
|
|
hir::ExprKind::Closure(..) | hir::ExprKind::Ret(..) | hir::ExprKind::Break(..) => true,
|
2017-08-14 18:27:20 -04:00
|
|
|
|
|
|
|
_ => contains_exterior_struct_lit(expr),
|
|
|
|
};
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
if needs_par {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.popen();
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_expr(expr);
|
2015-07-31 00:04:06 -07:00
|
|
|
if needs_par {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.pclose();
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
fn print_expr_vec(&mut self, exprs: &[hir::Expr<'_>]) {
|
2019-07-09 09:32:25 -04:00
|
|
|
self.ibox(INDENT_UNIT);
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("[");
|
|
|
|
self.commasep_exprs(Inconsistent, exprs);
|
|
|
|
self.s.word("]");
|
2015-07-31 00:04:06 -07:00
|
|
|
self.end()
|
|
|
|
}
|
|
|
|
|
2020-10-06 17:51:15 -03:00
|
|
|
fn print_expr_anon_const(&mut self, anon_const: &hir::AnonConst) {
|
|
|
|
self.ibox(INDENT_UNIT);
|
|
|
|
self.s.word_space("const");
|
|
|
|
self.print_anon_const(anon_const);
|
|
|
|
self.end()
|
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
fn print_expr_repeat(&mut self, element: &hir::Expr<'_>, count: &hir::AnonConst) {
|
2019-07-09 09:32:25 -04:00
|
|
|
self.ibox(INDENT_UNIT);
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("[");
|
|
|
|
self.print_expr(element);
|
|
|
|
self.word_space(";");
|
|
|
|
self.print_anon_const(count);
|
|
|
|
self.s.word("]");
|
2015-07-31 00:04:06 -07:00
|
|
|
self.end()
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn print_expr_struct(
|
|
|
|
&mut self,
|
2019-11-30 17:46:46 +01:00
|
|
|
qpath: &hir::QPath<'_>,
|
2021-03-16 00:36:07 +03:00
|
|
|
fields: &[hir::ExprField<'_>],
|
2020-03-23 20:59:19 +01:00
|
|
|
wth: &Option<&hir::Expr<'_>>,
|
2019-12-22 17:42:04 -05:00
|
|
|
) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_qpath(qpath, true);
|
|
|
|
self.s.word("{");
|
2019-12-22 17:42:04 -05:00
|
|
|
self.commasep_cmnt(
|
|
|
|
Consistent,
|
2021-02-16 00:30:06 +01:00
|
|
|
fields,
|
2019-12-22 17:42:04 -05:00
|
|
|
|s, field| {
|
|
|
|
s.ibox(INDENT_UNIT);
|
|
|
|
if !field.is_shorthand {
|
|
|
|
s.print_ident(field.ident);
|
|
|
|
s.word_space(":");
|
|
|
|
}
|
|
|
|
s.print_expr(&field.expr);
|
|
|
|
s.end()
|
|
|
|
},
|
|
|
|
|f| f.span,
|
|
|
|
);
|
2015-09-10 22:46:52 +03:00
|
|
|
match *wth {
|
|
|
|
Some(ref expr) => {
|
2019-07-09 09:32:25 -04:00
|
|
|
self.ibox(INDENT_UNIT);
|
2015-09-10 22:46:52 +03:00
|
|
|
if !fields.is_empty() {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(",");
|
|
|
|
self.s.space();
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("..");
|
|
|
|
self.print_expr(&expr);
|
|
|
|
self.end();
|
2015-09-10 22:46:52 +03:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => {
|
|
|
|
if !fields.is_empty() {
|
|
|
|
self.s.word(",")
|
|
|
|
}
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("}");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
fn print_expr_tup(&mut self, exprs: &[hir::Expr<'_>]) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.popen();
|
|
|
|
self.commasep_exprs(Inconsistent, exprs);
|
2015-07-31 00:04:06 -07:00
|
|
|
if exprs.len() == 1 {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(",");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
self.pclose()
|
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
fn print_expr_call(&mut self, func: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
|
2019-09-26 14:39:48 +01:00
|
|
|
let prec = match func.kind {
|
|
|
|
hir::ExprKind::Field(..) => parser::PREC_FORCE_PAREN,
|
|
|
|
_ => parser::PREC_POSTFIX,
|
|
|
|
};
|
2017-08-14 18:27:20 -04:00
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_expr_maybe_paren(func, prec);
|
2015-07-31 00:04:06 -07:00
|
|
|
self.print_call_post(args)
|
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
fn print_expr_method_call(&mut self, segment: &hir::PathSegment<'_>, args: &[hir::Expr<'_>]) {
|
2015-07-31 00:04:06 -07:00
|
|
|
let base_args = &args[1..];
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_expr_maybe_paren(&args[0], parser::PREC_POSTFIX);
|
|
|
|
self.s.word(".");
|
|
|
|
self.print_ident(segment.ident);
|
2017-09-21 23:24:26 +03:00
|
|
|
|
2021-01-02 19:45:11 +01:00
|
|
|
let generic_args = segment.args();
|
2019-06-12 11:42:58 +03:00
|
|
|
if !generic_args.args.is_empty() || !generic_args.bindings.is_empty() {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_generic_args(generic_args, segment.infer_args, true);
|
2019-06-12 11:42:58 +03:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
self.print_call_post(base_args)
|
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
fn print_expr_binary(&mut self, op: hir::BinOp, lhs: &hir::Expr<'_>, rhs: &hir::Expr<'_>) {
|
2017-08-14 18:27:20 -04:00
|
|
|
let assoc_op = bin_op_to_assoc_op(op.node);
|
|
|
|
let prec = assoc_op.precedence() as i8;
|
|
|
|
let fixity = assoc_op.fixity();
|
|
|
|
|
|
|
|
let (left_prec, right_prec) = match fixity {
|
|
|
|
Fixity::Left => (prec, prec + 1),
|
|
|
|
Fixity::Right => (prec + 1, prec),
|
|
|
|
Fixity::None => (prec + 1, prec + 1),
|
|
|
|
};
|
|
|
|
|
2019-09-26 14:39:48 +01:00
|
|
|
let left_prec = match (&lhs.kind, op.node) {
|
2017-11-05 09:28:00 -08:00
|
|
|
// These cases need parens: `x as i32 < y` has the parser thinking that `i32 < y` is
|
|
|
|
// the beginning of a path type. It starts trying to parse `x as (i32 < y ...` instead
|
|
|
|
// of `(x as i32) < ...`. We need to convince it _not_ to do that.
|
2020-04-16 17:38:52 -07:00
|
|
|
(&hir::ExprKind::Cast { .. }, hir::BinOpKind::Lt | hir::BinOpKind::Shl) => {
|
|
|
|
parser::PREC_FORCE_PAREN
|
|
|
|
}
|
2017-11-05 09:28:00 -08:00
|
|
|
_ => left_prec,
|
|
|
|
};
|
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_expr_maybe_paren(lhs, left_prec);
|
|
|
|
self.s.space();
|
|
|
|
self.word_space(op.node.as_str());
|
2017-08-14 18:27:20 -04:00
|
|
|
self.print_expr_maybe_paren(rhs, right_prec)
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
fn print_expr_unary(&mut self, op: hir::UnOp, expr: &hir::Expr<'_>) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(op.as_str());
|
2017-08-14 18:27:20 -04:00
|
|
|
self.print_expr_maybe_paren(expr, parser::PREC_PREFIX)
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn print_expr_addr_of(
|
|
|
|
&mut self,
|
|
|
|
kind: hir::BorrowKind,
|
|
|
|
mutability: hir::Mutability,
|
2019-11-29 13:43:03 +01:00
|
|
|
expr: &hir::Expr<'_>,
|
2019-12-22 17:42:04 -05:00
|
|
|
) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("&");
|
2019-11-23 14:15:49 +00:00
|
|
|
match kind {
|
|
|
|
hir::BorrowKind::Ref => self.print_mutability(mutability, false),
|
|
|
|
hir::BorrowKind::Raw => {
|
|
|
|
self.word_nbsp("raw");
|
|
|
|
self.print_mutability(mutability, true);
|
|
|
|
}
|
|
|
|
}
|
2017-08-14 18:27:20 -04:00
|
|
|
self.print_expr_maybe_paren(expr, parser::PREC_PREFIX)
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
fn print_literal(&mut self, lit: &hir::Lit) {
|
|
|
|
self.maybe_print_comment(lit.span.lo());
|
2019-07-09 09:51:56 -04:00
|
|
|
self.word(lit.node.to_lit_token().to_string())
|
2019-05-09 18:04:24 +03:00
|
|
|
}
|
|
|
|
|
2021-04-11 20:51:28 +01:00
|
|
|
fn print_inline_asm(&mut self, asm: &hir::InlineAsm<'_>) {
|
|
|
|
enum AsmArg<'a> {
|
|
|
|
Template(String),
|
|
|
|
Operand(&'a hir::InlineAsmOperand<'a>),
|
|
|
|
Options(ast::InlineAsmOptions),
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut args = vec![];
|
|
|
|
args.push(AsmArg::Template(ast::InlineAsmTemplatePiece::to_string(&asm.template)));
|
|
|
|
args.extend(asm.operands.iter().map(|(o, _)| AsmArg::Operand(o)));
|
|
|
|
if !asm.options.is_empty() {
|
|
|
|
args.push(AsmArg::Options(asm.options));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.popen();
|
|
|
|
self.commasep(Consistent, &args, |s, arg| match arg {
|
|
|
|
AsmArg::Template(template) => s.print_string(&template, ast::StrStyle::Cooked),
|
|
|
|
AsmArg::Operand(op) => match op {
|
|
|
|
hir::InlineAsmOperand::In { reg, expr } => {
|
|
|
|
s.word("in");
|
|
|
|
s.popen();
|
|
|
|
s.word(format!("{}", reg));
|
|
|
|
s.pclose();
|
|
|
|
s.space();
|
|
|
|
s.print_expr(expr);
|
|
|
|
}
|
|
|
|
hir::InlineAsmOperand::Out { reg, late, expr } => {
|
|
|
|
s.word(if *late { "lateout" } else { "out" });
|
|
|
|
s.popen();
|
|
|
|
s.word(format!("{}", reg));
|
|
|
|
s.pclose();
|
|
|
|
s.space();
|
|
|
|
match expr {
|
|
|
|
Some(expr) => s.print_expr(expr),
|
|
|
|
None => s.word("_"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hir::InlineAsmOperand::InOut { reg, late, expr } => {
|
|
|
|
s.word(if *late { "inlateout" } else { "inout" });
|
|
|
|
s.popen();
|
|
|
|
s.word(format!("{}", reg));
|
|
|
|
s.pclose();
|
|
|
|
s.space();
|
|
|
|
s.print_expr(expr);
|
|
|
|
}
|
|
|
|
hir::InlineAsmOperand::SplitInOut { reg, late, in_expr, out_expr } => {
|
|
|
|
s.word(if *late { "inlateout" } else { "inout" });
|
|
|
|
s.popen();
|
|
|
|
s.word(format!("{}", reg));
|
|
|
|
s.pclose();
|
|
|
|
s.space();
|
|
|
|
s.print_expr(in_expr);
|
|
|
|
s.space();
|
|
|
|
s.word_space("=>");
|
|
|
|
match out_expr {
|
|
|
|
Some(out_expr) => s.print_expr(out_expr),
|
|
|
|
None => s.word("_"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hir::InlineAsmOperand::Const { anon_const } => {
|
|
|
|
s.word("const");
|
|
|
|
s.space();
|
|
|
|
s.print_anon_const(anon_const);
|
|
|
|
}
|
|
|
|
hir::InlineAsmOperand::Sym { expr } => {
|
|
|
|
s.word("sym");
|
|
|
|
s.space();
|
|
|
|
s.print_expr(expr);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
AsmArg::Options(opts) => {
|
|
|
|
s.word("options");
|
|
|
|
s.popen();
|
|
|
|
let mut options = vec![];
|
|
|
|
if opts.contains(ast::InlineAsmOptions::PURE) {
|
|
|
|
options.push("pure");
|
|
|
|
}
|
|
|
|
if opts.contains(ast::InlineAsmOptions::NOMEM) {
|
|
|
|
options.push("nomem");
|
|
|
|
}
|
|
|
|
if opts.contains(ast::InlineAsmOptions::READONLY) {
|
|
|
|
options.push("readonly");
|
|
|
|
}
|
|
|
|
if opts.contains(ast::InlineAsmOptions::PRESERVES_FLAGS) {
|
|
|
|
options.push("preserves_flags");
|
|
|
|
}
|
|
|
|
if opts.contains(ast::InlineAsmOptions::NORETURN) {
|
|
|
|
options.push("noreturn");
|
|
|
|
}
|
|
|
|
if opts.contains(ast::InlineAsmOptions::NOSTACK) {
|
|
|
|
options.push("nostack");
|
|
|
|
}
|
|
|
|
if opts.contains(ast::InlineAsmOptions::ATT_SYNTAX) {
|
|
|
|
options.push("att_syntax");
|
|
|
|
}
|
2021-06-24 16:25:44 +01:00
|
|
|
if opts.contains(ast::InlineAsmOptions::RAW) {
|
|
|
|
options.push("raw");
|
|
|
|
}
|
2021-04-11 20:51:28 +01:00
|
|
|
s.commasep(Inconsistent, &options, |s, &opt| {
|
|
|
|
s.word(opt);
|
|
|
|
});
|
|
|
|
s.pclose();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
self.pclose();
|
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
pub fn print_expr(&mut self, expr: &hir::Expr<'_>) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.maybe_print_comment(expr.span.lo());
|
2020-11-27 17:41:05 +01:00
|
|
|
self.print_outer_attributes(self.attrs(expr.hir_id));
|
2019-07-09 09:32:25 -04:00
|
|
|
self.ibox(INDENT_UNIT);
|
2019-06-24 14:15:11 -04:00
|
|
|
self.ann.pre(self, AnnNode::Expr(expr));
|
2019-09-26 14:39:48 +01:00
|
|
|
match expr.kind {
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Box(ref expr) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_space("box");
|
|
|
|
self.print_expr_maybe_paren(expr, parser::PREC_PREFIX);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Array(ref exprs) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_expr_vec(exprs);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2020-10-06 17:51:15 -03:00
|
|
|
hir::ExprKind::ConstBlock(ref anon_const) => {
|
|
|
|
self.print_expr_anon_const(anon_const);
|
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Repeat(ref element, ref count) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_expr_repeat(&element, count);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-11-29 14:08:03 +01:00
|
|
|
hir::ExprKind::Struct(ref qpath, fields, ref wth) => {
|
|
|
|
self.print_expr_struct(qpath, fields, wth);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Tup(ref exprs) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_expr_tup(exprs);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Call(ref func, ref args) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_expr_call(&func, args);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2020-06-09 15:34:23 -04:00
|
|
|
hir::ExprKind::MethodCall(ref segment, _, ref args, _) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_expr_method_call(segment, args);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Binary(op, ref lhs, ref rhs) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_expr_binary(op, &lhs, &rhs);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Unary(op, ref expr) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_expr_unary(op, &expr);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-11-23 14:15:49 +00:00
|
|
|
hir::ExprKind::AddrOf(k, m, ref expr) => {
|
|
|
|
self.print_expr_addr_of(k, m, &expr);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Lit(ref lit) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_literal(&lit);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Cast(ref expr, ref ty) => {
|
2017-08-14 18:27:20 -04:00
|
|
|
let prec = AssocOp::As.precedence() as i8;
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_expr_maybe_paren(&expr, prec);
|
|
|
|
self.s.space();
|
|
|
|
self.word_space("as");
|
|
|
|
self.print_type(&ty);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Type(ref expr, ref ty) => {
|
2017-08-14 18:27:20 -04:00
|
|
|
let prec = AssocOp::Colon.precedence() as i8;
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_expr_maybe_paren(&expr, prec);
|
|
|
|
self.word_space(":");
|
|
|
|
self.print_type(&ty);
|
2015-12-03 05:37:48 +03:00
|
|
|
}
|
2019-04-30 17:46:59 +02:00
|
|
|
hir::ExprKind::DropTemps(ref init) => {
|
2019-04-24 06:39:40 +02:00
|
|
|
// Print `{`:
|
2019-07-09 09:32:25 -04:00
|
|
|
self.cbox(INDENT_UNIT);
|
2019-06-24 14:15:11 -04:00
|
|
|
self.ibox(0);
|
|
|
|
self.bopen();
|
2019-04-24 06:39:40 +02:00
|
|
|
|
|
|
|
// Print `let _t = $init;`:
|
2020-04-19 13:00:18 +02:00
|
|
|
let temp = Ident::from_str("_t");
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_local(Some(init), |this| this.print_ident(temp));
|
|
|
|
self.s.word(";");
|
2019-04-24 06:39:40 +02:00
|
|
|
|
|
|
|
// Print `_t`:
|
2019-06-24 14:15:11 -04:00
|
|
|
self.space_if_not_bol();
|
|
|
|
self.print_ident(temp);
|
2019-04-24 06:39:40 +02:00
|
|
|
|
|
|
|
// Print `}`:
|
2019-07-09 09:30:08 -04:00
|
|
|
self.bclose_maybe_open(expr.span, true);
|
2019-04-24 06:39:40 +02:00
|
|
|
}
|
2021-01-01 15:38:11 -03:00
|
|
|
hir::ExprKind::If(ref test, ref blk, ref elseopt) => {
|
|
|
|
self.print_if(&test, &blk, elseopt.as_ref().map(|e| &**e));
|
|
|
|
}
|
2021-01-20 17:15:08 -08:00
|
|
|
hir::ExprKind::Loop(ref blk, opt_label, _, _) => {
|
2018-01-16 01:44:32 +03:00
|
|
|
if let Some(label) = opt_label {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_ident(label.ident);
|
|
|
|
self.word_space(":");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.head("loop");
|
|
|
|
self.s.space();
|
|
|
|
self.print_block(&blk);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-11-29 14:08:03 +01:00
|
|
|
hir::ExprKind::Match(ref expr, arms, _) => {
|
2019-07-09 09:32:25 -04:00
|
|
|
self.cbox(INDENT_UNIT);
|
2019-07-09 09:49:37 -04:00
|
|
|
self.ibox(INDENT_UNIT);
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_nbsp("match");
|
|
|
|
self.print_expr_as_cond(&expr);
|
|
|
|
self.s.space();
|
|
|
|
self.bopen();
|
2015-07-31 00:04:06 -07:00
|
|
|
for arm in arms {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_arm(arm);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-07-09 07:38:31 -04:00
|
|
|
self.bclose(expr.span);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Closure(capture_clause, ref decl, body, _fn_decl_span, _gen) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_capture_clause(capture_clause);
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-08-27 13:24:32 +02:00
|
|
|
self.print_closure_params(&decl, body);
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.space();
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
// This is a bare expression.
|
2019-06-24 14:15:11 -04:00
|
|
|
self.ann.nested(self, Nested::Body(body));
|
|
|
|
self.end(); // need to close a box
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
// A box will be closed by `print_expr`, but we didn't want an overall
|
2015-07-31 00:04:06 -07:00
|
|
|
// wrapper so we closed the corresponding opening. so create an
|
|
|
|
// empty box to satisfy the close.
|
2019-06-24 14:15:11 -04:00
|
|
|
self.ibox(0);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Block(ref blk, opt_label) => {
|
2018-04-16 05:44:39 +02:00
|
|
|
if let Some(label) = opt_label {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_ident(label.ident);
|
|
|
|
self.word_space(":");
|
2018-04-16 05:44:39 +02:00
|
|
|
}
|
2019-09-06 03:57:44 +01:00
|
|
|
// containing cbox, will be closed by print-block at `}`
|
2019-07-09 09:32:25 -04:00
|
|
|
self.cbox(INDENT_UNIT);
|
2019-09-06 03:57:44 +01:00
|
|
|
// head-box, will be closed by print-block after `{`
|
2019-06-24 14:15:11 -04:00
|
|
|
self.ibox(0);
|
|
|
|
self.print_block(&blk);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-12-22 21:08:53 +00:00
|
|
|
hir::ExprKind::Assign(ref lhs, ref rhs, _) => {
|
2017-08-14 18:27:20 -04:00
|
|
|
let prec = AssocOp::Assign.precedence() as i8;
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_expr_maybe_paren(&lhs, prec + 1);
|
|
|
|
self.s.space();
|
|
|
|
self.word_space("=");
|
|
|
|
self.print_expr_maybe_paren(&rhs, prec);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::AssignOp(op, ref lhs, ref rhs) => {
|
2017-08-14 18:27:20 -04:00
|
|
|
let prec = AssocOp::Assign.precedence() as i8;
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_expr_maybe_paren(&lhs, prec + 1);
|
|
|
|
self.s.space();
|
|
|
|
self.s.word(op.node.as_str());
|
|
|
|
self.word_space("=");
|
|
|
|
self.print_expr_maybe_paren(&rhs, prec);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Field(ref expr, ident) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX);
|
|
|
|
self.s.word(".");
|
|
|
|
self.print_ident(ident);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Index(ref expr, ref index) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_expr_maybe_paren(&expr, parser::PREC_POSTFIX);
|
|
|
|
self.s.word("[");
|
|
|
|
self.print_expr(&index);
|
|
|
|
self.s.word("]");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
hir::ExprKind::Path(ref qpath) => self.print_qpath(qpath, true),
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Break(destination, ref opt_expr) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("break");
|
|
|
|
self.s.space();
|
2018-01-16 01:44:32 +03:00
|
|
|
if let Some(label) = destination.label {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_ident(label.ident);
|
|
|
|
self.s.space();
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
Implement the `loop_break_value` feature.
This implements RFC 1624, tracking issue #37339.
- `FnCtxt` (in typeck) gets a stack of `LoopCtxt`s, which store the
currently deduced type of that loop, the desired type, and a list of
break expressions currently seen. `loop` loops get a fresh type
variable as their initial type (this logic is stolen from that for
arrays). `while` loops get `()`.
- `break {expr}` looks up the broken loop, and unifies the type of
`expr` with the type of the loop.
- `break` with no expr unifies the loop's type with `()`.
- When building MIR, `loop` loops no longer construct a `()` value at
termination of the loop; rather, the `break` expression assigns the
result of the loop. `while` loops are unchanged.
- `break` respects contexts in which expressions may not end with braced
blocks. That is, `while break { break-value } { while-body }` is
illegal; this preserves backwards compatibility.
- The RFC did not make it clear, but I chose to make `break ()` inside
of a `while` loop illegal, just in case we wanted to do anything with
that design space in the future.
This is my first time dealing with this part of rustc so I'm sure
there's plenty of problems to pick on here ^_^
2016-10-29 15:15:06 -07:00
|
|
|
if let Some(ref expr) = *opt_expr {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_expr_maybe_paren(expr, parser::PREC_JUMP);
|
|
|
|
self.s.space();
|
Implement the `loop_break_value` feature.
This implements RFC 1624, tracking issue #37339.
- `FnCtxt` (in typeck) gets a stack of `LoopCtxt`s, which store the
currently deduced type of that loop, the desired type, and a list of
break expressions currently seen. `loop` loops get a fresh type
variable as their initial type (this logic is stolen from that for
arrays). `while` loops get `()`.
- `break {expr}` looks up the broken loop, and unifies the type of
`expr` with the type of the loop.
- `break` with no expr unifies the loop's type with `()`.
- When building MIR, `loop` loops no longer construct a `()` value at
termination of the loop; rather, the `break` expression assigns the
result of the loop. `while` loops are unchanged.
- `break` respects contexts in which expressions may not end with braced
blocks. That is, `while break { break-value } { while-body }` is
illegal; this preserves backwards compatibility.
- The RFC did not make it clear, but I chose to make `break ()` inside
of a `while` loop illegal, just in case we wanted to do anything with
that design space in the future.
This is my first time dealing with this part of rustc so I'm sure
there's plenty of problems to pick on here ^_^
2016-10-29 15:15:06 -07:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Continue(destination) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("continue");
|
|
|
|
self.s.space();
|
2018-01-16 01:44:32 +03:00
|
|
|
if let Some(label) = destination.label {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_ident(label.ident);
|
|
|
|
self.s.space()
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Ret(ref result) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("return");
|
2018-09-12 12:31:11 +02:00
|
|
|
if let Some(ref expr) = *result {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(" ");
|
|
|
|
self.print_expr_maybe_paren(&expr, parser::PREC_JUMP);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
2021-04-11 20:51:28 +01:00
|
|
|
hir::ExprKind::InlineAsm(ref asm) => {
|
2020-02-12 17:32:41 +00:00
|
|
|
self.word("asm!");
|
2021-04-11 20:51:28 +01:00
|
|
|
self.print_inline_asm(asm);
|
2020-02-12 17:32:41 +00:00
|
|
|
}
|
2020-01-14 13:40:42 +00:00
|
|
|
hir::ExprKind::LlvmInlineAsm(ref a) => {
|
2019-11-18 14:43:34 +01:00
|
|
|
let i = &a.inner;
|
2020-01-14 13:40:42 +00:00
|
|
|
self.s.word("llvm_asm!");
|
2019-06-24 14:15:11 -04:00
|
|
|
self.popen();
|
2020-07-08 20:03:37 +10:00
|
|
|
self.print_symbol(i.asm, i.asm_str_style);
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_space(":");
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2016-03-09 22:17:02 +02:00
|
|
|
let mut out_idx = 0;
|
2019-11-18 14:43:34 +01:00
|
|
|
self.commasep(Inconsistent, &i.outputs, |s, out| {
|
2016-11-16 10:52:37 +00:00
|
|
|
let constraint = out.constraint.as_str();
|
|
|
|
let mut ch = constraint.chars();
|
2016-04-07 10:42:53 -07:00
|
|
|
match ch.next() {
|
|
|
|
Some('=') if out.is_rw => {
|
2019-12-22 17:42:04 -05:00
|
|
|
s.print_string(&format!("+{}", ch.as_str()), ast::StrStyle::Cooked)
|
2015-11-05 21:17:59 +00:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
_ => s.print_string(&constraint, ast::StrStyle::Cooked),
|
2015-11-05 21:17:59 +00:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
s.popen();
|
2019-11-18 14:43:34 +01:00
|
|
|
s.print_expr(&a.outputs_exprs[out_idx]);
|
2019-06-24 14:15:11 -04:00
|
|
|
s.pclose();
|
2016-03-09 22:17:02 +02:00
|
|
|
out_idx += 1;
|
2019-06-24 14:15:11 -04:00
|
|
|
});
|
|
|
|
self.s.space();
|
|
|
|
self.word_space(":");
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2016-03-09 22:17:02 +02:00
|
|
|
let mut in_idx = 0;
|
2020-07-08 20:03:37 +10:00
|
|
|
self.commasep(Inconsistent, &i.inputs, |s, &co| {
|
|
|
|
s.print_symbol(co, ast::StrStyle::Cooked);
|
2019-06-24 14:15:11 -04:00
|
|
|
s.popen();
|
2019-11-18 14:43:34 +01:00
|
|
|
s.print_expr(&a.inputs_exprs[in_idx]);
|
2019-06-24 14:15:11 -04:00
|
|
|
s.pclose();
|
2016-03-09 22:17:02 +02:00
|
|
|
in_idx += 1;
|
2019-06-24 14:15:11 -04:00
|
|
|
});
|
|
|
|
self.s.space();
|
|
|
|
self.word_space(":");
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2020-07-08 20:03:37 +10:00
|
|
|
self.commasep(Inconsistent, &i.clobbers, |s, &co| {
|
|
|
|
s.print_symbol(co, ast::StrStyle::Cooked);
|
2019-06-24 14:15:11 -04:00
|
|
|
});
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2015-11-05 21:17:59 +00:00
|
|
|
let mut options = vec![];
|
2019-11-18 14:43:34 +01:00
|
|
|
if i.volatile {
|
2015-07-31 00:04:06 -07:00
|
|
|
options.push("volatile");
|
|
|
|
}
|
2019-11-18 14:43:34 +01:00
|
|
|
if i.alignstack {
|
2015-07-31 00:04:06 -07:00
|
|
|
options.push("alignstack");
|
|
|
|
}
|
2020-01-14 13:40:42 +00:00
|
|
|
if i.dialect == ast::LlvmAsmDialect::Intel {
|
2015-07-31 00:04:06 -07:00
|
|
|
options.push("intel");
|
|
|
|
}
|
|
|
|
|
|
|
|
if !options.is_empty() {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.space();
|
|
|
|
self.word_space(":");
|
2016-03-22 22:01:37 -05:00
|
|
|
self.commasep(Inconsistent, &options, |s, &co| {
|
2019-06-24 14:15:11 -04:00
|
|
|
s.print_string(co, ast::StrStyle::Cooked);
|
|
|
|
});
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
self.pclose();
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-18 14:34:51 -07:00
|
|
|
hir::ExprKind::Yield(ref expr, _) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_space("yield");
|
|
|
|
self.print_expr_maybe_paren(&expr, parser::PREC_JUMP);
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
2018-12-17 04:57:32 +03:00
|
|
|
hir::ExprKind::Err => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.popen();
|
|
|
|
self.s.word("/*ERROR*/");
|
|
|
|
self.pclose();
|
2018-12-17 04:57:32 +03:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.ann.post(self, AnnNode::Expr(expr));
|
2015-07-31 00:04:06 -07:00
|
|
|
self.end()
|
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
pub fn print_local_decl(&mut self, loc: &hir::Local<'_>) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_pat(&loc.pat);
|
2015-07-31 00:04:06 -07:00
|
|
|
if let Some(ref ty) = loc.ty {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_space(":");
|
|
|
|
self.print_type(&ty);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 13:00:18 +02:00
|
|
|
pub fn print_name(&mut self, name: Symbol) {
|
|
|
|
self.print_ident(Ident::with_dummy_span(name))
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
pub fn print_path(&mut self, path: &hir::Path<'_>, colons_before_params: bool) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.maybe_print_comment(path.span.lo());
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2016-12-05 03:51:11 +00:00
|
|
|
for (i, segment) in path.segments.iter().enumerate() {
|
|
|
|
if i > 0 {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("::")
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-05-11 17:41:37 +03:00
|
|
|
if segment.ident.name != kw::PathRoot {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_ident(segment.ident);
|
2021-01-02 19:45:11 +01:00
|
|
|
self.print_generic_args(segment.args(), segment.infer_args, colons_before_params);
|
2016-12-05 03:51:11 +00:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
pub fn print_path_segment(&mut self, segment: &hir::PathSegment<'_>) {
|
2019-05-11 17:41:37 +03:00
|
|
|
if segment.ident.name != kw::PathRoot {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_ident(segment.ident);
|
2021-01-02 19:45:11 +01:00
|
|
|
self.print_generic_args(segment.args(), segment.infer_args, false);
|
2018-10-11 21:15:18 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
pub fn print_qpath(&mut self, qpath: &hir::QPath<'_>, colons_before_params: bool) {
|
2016-10-27 05:17:42 +03:00
|
|
|
match *qpath {
|
2019-12-22 17:42:04 -05:00
|
|
|
hir::QPath::Resolved(None, ref path) => self.print_path(path, colons_before_params),
|
2016-10-27 05:17:42 +03:00
|
|
|
hir::QPath::Resolved(Some(ref qself), ref path) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("<");
|
|
|
|
self.print_type(qself);
|
|
|
|
self.s.space();
|
|
|
|
self.word_space("as");
|
2016-10-27 05:17:42 +03:00
|
|
|
|
2016-12-05 03:51:11 +00:00
|
|
|
for (i, segment) in path.segments[..path.segments.len() - 1].iter().enumerate() {
|
|
|
|
if i > 0 {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("::")
|
2016-10-27 05:17:42 +03:00
|
|
|
}
|
2019-05-11 17:41:37 +03:00
|
|
|
if segment.ident.name != kw::PathRoot {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_ident(segment.ident);
|
2019-12-22 17:42:04 -05:00
|
|
|
self.print_generic_args(
|
2021-01-02 19:45:11 +01:00
|
|
|
segment.args(),
|
2019-12-22 17:42:04 -05:00
|
|
|
segment.infer_args,
|
|
|
|
colons_before_params,
|
|
|
|
);
|
2016-12-05 03:51:11 +00:00
|
|
|
}
|
2016-10-27 05:17:42 +03:00
|
|
|
}
|
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(">");
|
|
|
|
self.s.word("::");
|
2016-10-27 05:17:42 +03:00
|
|
|
let item_segment = path.segments.last().unwrap();
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_ident(item_segment.ident);
|
2019-12-22 17:42:04 -05:00
|
|
|
self.print_generic_args(
|
2021-01-02 19:45:11 +01:00
|
|
|
item_segment.args(),
|
2019-12-22 17:42:04 -05:00
|
|
|
item_segment.infer_args,
|
|
|
|
colons_before_params,
|
|
|
|
)
|
2016-10-27 05:17:42 +03:00
|
|
|
}
|
|
|
|
hir::QPath::TypeRelative(ref qself, ref item_segment) => {
|
2019-10-27 16:43:42 +01:00
|
|
|
// If we've got a compound-qualified-path, let's push an additional pair of angle
|
|
|
|
// brackets, so that we pretty-print `<<A::B>::C>` as `<A::B>::C`, instead of just
|
|
|
|
// `A::B::C` (since the latter could be ambiguous to the user)
|
|
|
|
if let hir::TyKind::Path(hir::QPath::Resolved(None, _)) = &qself.kind {
|
|
|
|
self.print_type(qself);
|
|
|
|
} else {
|
|
|
|
self.s.word("<");
|
|
|
|
self.print_type(qself);
|
|
|
|
self.s.word(">");
|
|
|
|
}
|
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("::");
|
|
|
|
self.print_ident(item_segment.ident);
|
2019-12-22 17:42:04 -05:00
|
|
|
self.print_generic_args(
|
2021-01-02 19:45:11 +01:00
|
|
|
item_segment.args(),
|
2019-12-22 17:42:04 -05:00
|
|
|
item_segment.infer_args,
|
|
|
|
colons_before_params,
|
|
|
|
)
|
2016-10-27 05:17:42 +03:00
|
|
|
}
|
2020-08-04 14:34:24 +01:00
|
|
|
hir::QPath::LangItem(lang_item, span) => {
|
|
|
|
self.s.word("#[lang = \"");
|
|
|
|
self.print_ident(Ident::new(lang_item.name(), span));
|
|
|
|
self.s.word("\"]");
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn print_generic_args(
|
|
|
|
&mut self,
|
2019-11-30 17:46:46 +01:00
|
|
|
generic_args: &hir::GenericArgs<'_>,
|
2019-12-22 17:42:04 -05:00
|
|
|
infer_args: bool,
|
|
|
|
colons_before_params: bool,
|
|
|
|
) {
|
2018-02-13 11:32:37 +00:00
|
|
|
if generic_args.parenthesized {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("(");
|
|
|
|
self.commasep(Inconsistent, generic_args.inputs(), |s, ty| s.print_type(&ty));
|
|
|
|
self.s.word(")");
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
self.space_if_not_bol();
|
|
|
|
self.word_space("->");
|
|
|
|
self.print_type(generic_args.bindings[0].ty());
|
2017-07-29 01:13:40 +03:00
|
|
|
} else {
|
|
|
|
let start = if colons_before_params { "::<" } else { "<" };
|
|
|
|
let empty = Cell::new(true);
|
|
|
|
let start_or_comma = |this: &mut Self| {
|
|
|
|
if empty.get() {
|
|
|
|
empty.set(false);
|
|
|
|
this.s.word(start)
|
|
|
|
} else {
|
|
|
|
this.word_space(",")
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2017-07-29 01:13:40 +03:00
|
|
|
};
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-02-15 22:21:57 +00:00
|
|
|
let mut nonelided_generic_args: bool = false;
|
|
|
|
let elide_lifetimes = generic_args.args.iter().all(|arg| match arg {
|
|
|
|
GenericArg::Lifetime(lt) => lt.is_elided(),
|
|
|
|
_ => {
|
|
|
|
nonelided_generic_args = true;
|
|
|
|
true
|
2018-05-27 01:43:03 +01:00
|
|
|
}
|
2019-02-15 22:21:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if nonelided_generic_args {
|
2019-06-24 14:15:11 -04:00
|
|
|
start_or_comma(self);
|
2019-12-22 17:42:04 -05:00
|
|
|
self.commasep(
|
|
|
|
Inconsistent,
|
|
|
|
&generic_args.args,
|
|
|
|
|s, generic_arg| match generic_arg {
|
2019-02-15 22:21:57 +00:00
|
|
|
GenericArg::Lifetime(lt) if !elide_lifetimes => s.print_lifetime(lt),
|
2019-12-22 17:42:04 -05:00
|
|
|
GenericArg::Lifetime(_) => {}
|
2018-02-13 11:32:37 +00:00
|
|
|
GenericArg::Type(ty) => s.print_type(ty),
|
2019-02-15 22:21:57 +00:00
|
|
|
GenericArg::Const(ct) => s.print_anon_const(&ct.value),
|
2019-12-22 17:42:04 -05:00
|
|
|
},
|
|
|
|
);
|
2018-02-12 21:44:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
// FIXME(eddyb): this would leak into error messages (e.g.,
|
|
|
|
// "non-exhaustive patterns: `Some::<..>(_)` not covered").
|
2019-06-07 10:18:03 +01:00
|
|
|
if infer_args && false {
|
2019-06-24 14:15:11 -04:00
|
|
|
start_or_comma(self);
|
|
|
|
self.s.word("..");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2018-02-13 11:32:37 +00:00
|
|
|
for binding in generic_args.bindings.iter() {
|
2019-06-24 14:15:11 -04:00
|
|
|
start_or_comma(self);
|
|
|
|
self.print_ident(binding.ident);
|
2020-11-30 09:23:59 +01:00
|
|
|
self.print_generic_args(binding.gen_args, false, false);
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.space();
|
2019-05-08 15:57:06 -04:00
|
|
|
match generic_args.bindings[0].kind {
|
|
|
|
hir::TypeBindingKind::Equality { ref ty } => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_space("=");
|
|
|
|
self.print_type(ty);
|
2019-05-08 15:57:06 -04:00
|
|
|
}
|
2019-12-01 00:17:43 +01:00
|
|
|
hir::TypeBindingKind::Constraint { bounds } => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_bounds(":", bounds);
|
2019-05-08 15:57:06 -04:00
|
|
|
}
|
|
|
|
}
|
2017-07-29 01:13:40 +03:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2017-07-29 01:13:40 +03:00
|
|
|
if !empty.get() {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(">")
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
pub fn print_pat(&mut self, pat: &hir::Pat<'_>) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.maybe_print_comment(pat.span.lo());
|
|
|
|
self.ann.pre(self, AnnNode::Pat(pat));
|
2015-11-05 21:17:59 +00:00
|
|
|
// Pat isn't normalized, but the beauty of it
|
|
|
|
// is that it doesn't matter
|
2019-09-26 16:18:31 +01:00
|
|
|
match pat.kind {
|
2019-06-24 14:15:11 -04:00
|
|
|
PatKind::Wild => self.s.word("_"),
|
2019-03-07 12:18:59 +01:00
|
|
|
PatKind::Binding(binding_mode, _, ident, ref sub) => {
|
2015-07-31 00:04:06 -07:00
|
|
|
match binding_mode {
|
2017-07-21 19:29:43 -04:00
|
|
|
hir::BindingAnnotation::Ref => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_nbsp("ref");
|
2019-12-16 17:28:40 +01:00
|
|
|
self.print_mutability(hir::Mutability::Not, false);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2017-07-21 19:29:43 -04:00
|
|
|
hir::BindingAnnotation::RefMut => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_nbsp("ref");
|
2019-12-16 17:28:40 +01:00
|
|
|
self.print_mutability(hir::Mutability::Mut, false);
|
2017-07-21 19:29:43 -04:00
|
|
|
}
|
|
|
|
hir::BindingAnnotation::Unannotated => {}
|
|
|
|
hir::BindingAnnotation::Mutable => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_nbsp("mut");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_ident(ident);
|
2016-06-13 22:43:30 -07:00
|
|
|
if let Some(ref p) = *sub {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("@");
|
|
|
|
self.print_pat(&p);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
2016-10-27 05:17:42 +03:00
|
|
|
PatKind::TupleStruct(ref qpath, ref elts, ddpos) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_qpath(qpath, true);
|
|
|
|
self.popen();
|
2016-03-06 15:54:44 +03:00
|
|
|
if let Some(ddpos) = ddpos {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.commasep(Inconsistent, &elts[..ddpos], |s, p| s.print_pat(&p));
|
2016-03-06 15:54:44 +03:00
|
|
|
if ddpos != 0 {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_space(",");
|
2016-03-06 15:54:44 +03:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("..");
|
2016-03-06 15:54:44 +03:00
|
|
|
if ddpos != elts.len() {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(",");
|
|
|
|
self.commasep(Inconsistent, &elts[ddpos..], |s, p| s.print_pat(&p));
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2016-03-06 15:54:44 +03:00
|
|
|
} else {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(&p));
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.pclose();
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2016-10-27 05:17:42 +03:00
|
|
|
PatKind::Path(ref qpath) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_qpath(qpath, true);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2016-10-27 05:17:42 +03:00
|
|
|
PatKind::Struct(ref qpath, ref fields, etc) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_qpath(qpath, true);
|
|
|
|
self.nbsp();
|
|
|
|
self.word_space("{");
|
2019-12-22 17:42:04 -05:00
|
|
|
self.commasep_cmnt(
|
|
|
|
Consistent,
|
|
|
|
&fields[..],
|
|
|
|
|s, f| {
|
|
|
|
s.cbox(INDENT_UNIT);
|
|
|
|
if !f.is_shorthand {
|
|
|
|
s.print_ident(f.ident);
|
|
|
|
s.word_nbsp(":");
|
|
|
|
}
|
|
|
|
s.print_pat(&f.pat);
|
|
|
|
s.end()
|
|
|
|
},
|
|
|
|
|f| f.pat.span,
|
|
|
|
);
|
2015-07-31 00:04:06 -07:00
|
|
|
if etc {
|
2015-09-28 08:23:31 +13:00
|
|
|
if !fields.is_empty() {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_space(",");
|
2015-09-28 08:23:31 +13:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("..");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.space();
|
|
|
|
self.s.word("}");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-10-19 15:40:07 +01:00
|
|
|
PatKind::Or(ref pats) => {
|
2019-07-14 01:05:52 +00:00
|
|
|
self.strsep("|", true, Inconsistent, &pats[..], |s, p| s.print_pat(&p));
|
2018-10-19 15:40:07 +01:00
|
|
|
}
|
2016-03-06 15:54:44 +03:00
|
|
|
PatKind::Tuple(ref elts, ddpos) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.popen();
|
2016-03-06 15:54:44 +03:00
|
|
|
if let Some(ddpos) = ddpos {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.commasep(Inconsistent, &elts[..ddpos], |s, p| s.print_pat(&p));
|
2016-03-06 15:54:44 +03:00
|
|
|
if ddpos != 0 {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_space(",");
|
2016-03-06 15:54:44 +03:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("..");
|
2016-03-06 15:54:44 +03:00
|
|
|
if ddpos != elts.len() {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(",");
|
|
|
|
self.commasep(Inconsistent, &elts[ddpos..], |s, p| s.print_pat(&p));
|
2016-03-06 15:54:44 +03:00
|
|
|
}
|
|
|
|
} else {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(&p));
|
2016-03-06 15:54:44 +03:00
|
|
|
if elts.len() == 1 {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(",");
|
2016-03-06 15:54:44 +03:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.pclose();
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2016-02-14 15:25:12 +03:00
|
|
|
PatKind::Box(ref inner) => {
|
2020-10-26 21:02:48 -04:00
|
|
|
let is_range_inner = matches!(inner.kind, PatKind::Range(..));
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("box ");
|
2018-03-03 06:05:54 +08:00
|
|
|
if is_range_inner {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.popen();
|
2018-03-03 06:05:54 +08:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_pat(&inner);
|
2018-03-03 06:05:54 +08:00
|
|
|
if is_range_inner {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.pclose();
|
2018-03-03 06:05:54 +08:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2016-02-14 15:25:12 +03:00
|
|
|
PatKind::Ref(ref inner, mutbl) => {
|
2020-10-26 21:02:48 -04:00
|
|
|
let is_range_inner = matches!(inner.kind, PatKind::Range(..));
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("&");
|
2019-10-30 12:55:38 -07:00
|
|
|
self.s.word(mutbl.prefix_str());
|
2018-03-03 06:05:54 +08:00
|
|
|
if is_range_inner {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.popen();
|
2018-03-03 06:05:54 +08:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_pat(&inner);
|
2018-03-03 06:05:54 +08:00
|
|
|
if is_range_inner {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.pclose();
|
2018-03-03 06:05:54 +08:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
PatKind::Lit(ref e) => self.print_expr(&e),
|
2017-01-10 22:13:53 +01:00
|
|
|
PatKind::Range(ref begin, ref end, ref end_kind) => {
|
2019-12-11 10:04:34 +01:00
|
|
|
if let Some(expr) = begin {
|
|
|
|
self.print_expr(expr);
|
|
|
|
self.s.space();
|
|
|
|
}
|
2017-01-10 22:13:53 +01:00
|
|
|
match *end_kind {
|
2019-06-24 14:15:11 -04:00
|
|
|
RangeEnd::Included => self.s.word("..."),
|
|
|
|
RangeEnd::Excluded => self.s.word(".."),
|
2017-01-10 22:13:53 +01:00
|
|
|
}
|
2019-12-11 10:04:34 +01:00
|
|
|
if let Some(expr) = end {
|
|
|
|
self.print_expr(expr);
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2016-09-20 02:14:46 +02:00
|
|
|
PatKind::Slice(ref before, ref slice, ref after) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("[");
|
|
|
|
self.commasep(Inconsistent, &before[..], |s, p| s.print_pat(&p));
|
2015-07-31 00:04:06 -07:00
|
|
|
if let Some(ref p) = *slice {
|
2015-09-28 08:23:31 +13:00
|
|
|
if !before.is_empty() {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_space(",");
|
2015-09-28 08:23:31 +13:00
|
|
|
}
|
2019-09-26 16:18:31 +01:00
|
|
|
if let PatKind::Wild = p.kind {
|
2019-09-06 03:57:44 +01:00
|
|
|
// Print nothing.
|
2018-03-21 01:58:25 +03:00
|
|
|
} else {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_pat(&p);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("..");
|
2015-09-28 08:23:31 +13:00
|
|
|
if !after.is_empty() {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_space(",");
|
2015-09-28 08:23:31 +13:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.commasep(Inconsistent, &after[..], |s, p| s.print_pat(&p));
|
|
|
|
self.s.word("]");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
2018-08-22 22:05:19 +01:00
|
|
|
self.ann.post(self, AnnNode::Pat(pat))
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
pub fn print_param(&mut self, arg: &hir::Param<'_>) {
|
2020-11-26 23:51:27 +01:00
|
|
|
self.print_outer_attributes(self.attrs(arg.hir_id));
|
2019-07-26 19:52:37 -03:00
|
|
|
self.print_pat(&arg.pat);
|
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
pub fn print_arm(&mut self, arm: &hir::Arm<'_>) {
|
2015-07-31 00:04:06 -07:00
|
|
|
// I have no idea why this check is necessary, but here it
|
|
|
|
// is :(
|
2020-11-26 23:46:48 +01:00
|
|
|
if self.attrs(arm.hir_id).is_empty() {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.space();
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-07-09 09:32:25 -04:00
|
|
|
self.cbox(INDENT_UNIT);
|
2019-06-29 15:31:43 +01:00
|
|
|
self.ann.pre(self, AnnNode::Arm(arm));
|
2019-06-24 14:15:11 -04:00
|
|
|
self.ibox(0);
|
2020-11-26 23:46:48 +01:00
|
|
|
self.print_outer_attributes(&self.attrs(arm.hir_id));
|
2019-09-07 16:00:39 +02:00
|
|
|
self.print_pat(&arm.pat);
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.space();
|
2018-08-30 12:18:11 +08:00
|
|
|
if let Some(ref g) = arm.guard {
|
|
|
|
match g {
|
|
|
|
hir::Guard::If(e) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_space("if");
|
|
|
|
self.print_expr(&e);
|
|
|
|
self.s.space();
|
2018-08-30 12:18:11 +08:00
|
|
|
}
|
2020-10-24 21:13:54 +02:00
|
|
|
hir::Guard::IfLet(pat, e) => {
|
|
|
|
self.word_nbsp("if");
|
|
|
|
self.word_nbsp("let");
|
|
|
|
self.print_pat(&pat);
|
|
|
|
self.s.space();
|
|
|
|
self.word_space("=");
|
|
|
|
self.print_expr(&e);
|
|
|
|
self.s.space();
|
|
|
|
}
|
2018-08-30 12:18:11 +08:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_space("=>");
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-09-26 14:39:48 +01:00
|
|
|
match arm.body.kind {
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Block(ref blk, opt_label) => {
|
2018-04-16 05:44:39 +02:00
|
|
|
if let Some(label) = opt_label {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_ident(label.ident);
|
|
|
|
self.word_space(":");
|
2018-04-16 05:44:39 +02:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
// the block will close the pattern's ibox
|
2019-07-09 09:30:08 -04:00
|
|
|
self.print_block_unclosed(&blk);
|
2015-07-31 00:04:06 -07:00
|
|
|
|
|
|
|
// If it is a user-provided unsafe block, print a comma after it
|
2020-03-23 11:32:07 +01:00
|
|
|
if let hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::UserProvided) = blk.rules
|
|
|
|
{
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(",");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.end(); // close the ibox for the pattern
|
|
|
|
self.print_expr(&arm.body);
|
|
|
|
self.s.word(",");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
2019-06-29 15:31:43 +01:00
|
|
|
self.ann.post(self, AnnNode::Arm(arm));
|
2015-07-31 00:04:06 -07:00
|
|
|
self.end() // close enclosing cbox
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn print_fn(
|
|
|
|
&mut self,
|
2019-11-30 17:46:46 +01:00
|
|
|
decl: &hir::FnDecl<'_>,
|
2019-12-22 17:42:04 -05:00
|
|
|
header: hir::FnHeader,
|
2020-04-19 13:00:18 +02:00
|
|
|
name: Option<Symbol>,
|
2019-11-30 17:46:46 +01:00
|
|
|
generics: &hir::Generics<'_>,
|
|
|
|
vis: &hir::Visibility<'_>,
|
2020-04-19 13:00:18 +02:00
|
|
|
arg_names: &[Ident],
|
2019-12-22 17:42:04 -05:00
|
|
|
body_id: Option<hir::BodyId>,
|
|
|
|
) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_fn_header_info(header, vis);
|
2015-07-31 00:04:06 -07:00
|
|
|
|
|
|
|
if let Some(name) = name {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.nbsp();
|
|
|
|
self.print_name(name);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_generic_params(&generics.params);
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
self.popen();
|
2016-12-20 22:46:11 +02:00
|
|
|
let mut i = 0;
|
|
|
|
// Make sure we aren't supplied *both* `arg_names` and `body_id`.
|
|
|
|
assert!(arg_names.is_empty() || body_id.is_none());
|
|
|
|
self.commasep(Inconsistent, &decl.inputs, |s, ty| {
|
2019-07-09 09:32:25 -04:00
|
|
|
s.ibox(INDENT_UNIT);
|
2018-06-10 19:33:30 +03:00
|
|
|
if let Some(arg_name) = arg_names.get(i) {
|
2019-10-22 08:31:37 +11:00
|
|
|
s.s.word(arg_name.to_string());
|
2019-06-24 14:15:11 -04:00
|
|
|
s.s.word(":");
|
|
|
|
s.s.space();
|
2016-12-27 10:00:18 +02:00
|
|
|
} else if let Some(body_id) = body_id {
|
2019-08-27 13:24:32 +02:00
|
|
|
s.ann.nested(s, Nested::BodyParamPat(body_id, i));
|
2019-06-24 14:15:11 -04:00
|
|
|
s.s.word(":");
|
|
|
|
s.s.space();
|
2016-12-20 22:46:11 +02:00
|
|
|
}
|
|
|
|
i += 1;
|
2019-06-24 14:15:11 -04:00
|
|
|
s.print_type(ty);
|
2016-12-20 22:46:11 +02:00
|
|
|
s.end()
|
2019-06-24 14:15:11 -04:00
|
|
|
});
|
2019-02-08 17:30:42 +00:00
|
|
|
if decl.c_variadic {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(", ...");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.pclose();
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_fn_output(decl);
|
2016-12-20 22:46:11 +02:00
|
|
|
self.print_where_clause(&generics.where_clause)
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
fn print_closure_params(&mut self, decl: &hir::FnDecl<'_>, body_id: hir::BodyId) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("|");
|
2016-12-20 22:46:11 +02:00
|
|
|
let mut i = 0;
|
|
|
|
self.commasep(Inconsistent, &decl.inputs, |s, ty| {
|
2019-07-09 09:32:25 -04:00
|
|
|
s.ibox(INDENT_UNIT);
|
2016-12-20 22:46:11 +02:00
|
|
|
|
2019-08-27 13:24:32 +02:00
|
|
|
s.ann.nested(s, Nested::BodyParamPat(body_id, i));
|
2016-12-20 22:46:11 +02:00
|
|
|
i += 1;
|
|
|
|
|
2019-09-26 17:25:31 +01:00
|
|
|
if let hir::TyKind::Infer = ty.kind {
|
2019-09-06 03:57:44 +01:00
|
|
|
// Print nothing.
|
2018-03-21 01:58:25 +03:00
|
|
|
} else {
|
2019-06-24 14:15:11 -04:00
|
|
|
s.s.word(":");
|
|
|
|
s.s.space();
|
|
|
|
s.print_type(ty);
|
2016-12-20 22:46:11 +02:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
s.end();
|
|
|
|
});
|
|
|
|
self.s.word("|");
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2020-03-23 11:32:07 +01:00
|
|
|
if let hir::FnRetTy::DefaultReturn(..) = decl.output {
|
2019-06-24 14:15:11 -04:00
|
|
|
return;
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
self.space_if_not_bol();
|
|
|
|
self.word_space("->");
|
2015-07-31 00:04:06 -07:00
|
|
|
match decl.output {
|
2020-03-23 11:32:07 +01:00
|
|
|
hir::FnRetTy::Return(ref ty) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_type(&ty);
|
2017-07-31 23:04:34 +03:00
|
|
|
self.maybe_print_comment(ty.span.lo())
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2020-03-23 11:32:07 +01:00
|
|
|
hir::FnRetTy::DefaultReturn(..) => unreachable!(),
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-09 18:23:11 +01:00
|
|
|
pub fn print_capture_clause(&mut self, capture_clause: hir::CaptureBy) {
|
2015-07-31 00:04:06 -07:00
|
|
|
match capture_clause {
|
2019-11-09 18:23:11 +01:00
|
|
|
hir::CaptureBy::Value => self.word_space("move"),
|
2019-12-22 17:42:04 -05:00
|
|
|
hir::CaptureBy::Ref => {}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 11:43:15 +03:00
|
|
|
pub fn print_bounds<'b>(
|
|
|
|
&mut self,
|
|
|
|
prefix: &'static str,
|
2019-11-30 17:46:46 +01:00
|
|
|
bounds: impl IntoIterator<Item = &'b hir::GenericBound<'b>>,
|
2019-06-24 14:15:11 -04:00
|
|
|
) {
|
2019-06-12 11:43:15 +03:00
|
|
|
let mut first = true;
|
|
|
|
for bound in bounds {
|
|
|
|
if first {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(prefix);
|
2019-06-12 11:43:15 +03:00
|
|
|
}
|
|
|
|
if !(first && prefix.is_empty()) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.nbsp();
|
2019-06-12 11:43:15 +03:00
|
|
|
}
|
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
} else {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_space("+");
|
2019-06-12 11:43:15 +03:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-06-12 11:43:15 +03:00
|
|
|
match bound {
|
|
|
|
GenericBound::Trait(tref, modifier) => {
|
|
|
|
if modifier == &TraitBoundModifier::Maybe {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("?");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_poly_trait_ref(tref);
|
2019-06-12 11:43:15 +03:00
|
|
|
}
|
2020-08-04 14:34:24 +01:00
|
|
|
GenericBound::LangItemTrait(lang_item, span, ..) => {
|
|
|
|
self.s.word("#[lang = \"");
|
|
|
|
self.print_ident(Ident::new(lang_item.name(), *span));
|
|
|
|
self.s.word("\"]");
|
|
|
|
}
|
2019-06-12 11:43:15 +03:00
|
|
|
GenericBound::Outlives(lt) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_lifetime(lt);
|
2017-12-19 23:40:17 +03:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
pub fn print_generic_params(&mut self, generic_params: &[GenericParam<'_>]) {
|
2017-10-16 21:07:26 +02:00
|
|
|
if !generic_params.is_empty() {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("<");
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
self.commasep(Inconsistent, generic_params, |s, param| s.print_generic_param(param));
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(">");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
pub fn print_generic_param(&mut self, param: &GenericParam<'_>) {
|
2019-02-15 22:21:57 +00:00
|
|
|
if let GenericParamKind::Const { .. } = param.kind {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_space("const");
|
2019-02-15 22:21:57 +00:00
|
|
|
}
|
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_ident(param.name.ident());
|
2019-02-15 22:21:57 +00:00
|
|
|
|
2018-05-26 00:27:54 +01:00
|
|
|
match param.kind {
|
2018-05-28 13:33:28 +01:00
|
|
|
GenericParamKind::Lifetime { .. } => {
|
2018-05-26 00:27:54 +01:00
|
|
|
let mut sep = ":";
|
2019-12-01 00:17:43 +01:00
|
|
|
for bound in param.bounds {
|
2018-05-28 13:33:28 +01:00
|
|
|
match bound {
|
2019-12-01 00:17:43 +01:00
|
|
|
GenericBound::Outlives(ref lt) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(sep);
|
|
|
|
self.print_lifetime(lt);
|
2018-05-28 13:33:28 +01:00
|
|
|
sep = "+";
|
|
|
|
}
|
2020-01-02 05:18:45 +01:00
|
|
|
_ => panic!(),
|
2018-05-28 13:33:28 +01:00
|
|
|
}
|
2018-05-26 00:27:54 +01:00
|
|
|
}
|
|
|
|
}
|
2018-05-28 13:33:28 +01:00
|
|
|
GenericParamKind::Type { ref default, .. } => {
|
2019-12-01 00:17:43 +01:00
|
|
|
self.print_bounds(":", param.bounds);
|
2020-03-22 13:36:56 +01:00
|
|
|
if let Some(default) = default {
|
|
|
|
self.s.space();
|
|
|
|
self.word_space("=");
|
|
|
|
self.print_type(&default)
|
2018-05-26 00:27:54 +01:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2020-12-31 01:58:27 +01:00
|
|
|
GenericParamKind::Const { ref ty, ref default } => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_space(":");
|
2020-12-31 01:58:27 +01:00
|
|
|
self.print_type(ty);
|
2020-08-11 00:02:45 +00:00
|
|
|
if let Some(ref default) = default {
|
|
|
|
self.s.space();
|
|
|
|
self.word_space("=");
|
|
|
|
self.print_anon_const(&default)
|
2020-12-31 01:58:27 +01:00
|
|
|
}
|
2019-02-15 22:21:57 +00:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
pub fn print_lifetime(&mut self, lifetime: &hir::Lifetime) {
|
2018-06-28 00:12:17 +03:00
|
|
|
self.print_ident(lifetime.name.ident())
|
2018-05-26 00:27:54 +01:00
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
pub fn print_where_clause(&mut self, where_clause: &hir::WhereClause<'_>) {
|
2015-07-31 00:04:06 -07:00
|
|
|
if where_clause.predicates.is_empty() {
|
2019-06-24 14:15:11 -04:00
|
|
|
return;
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.space();
|
|
|
|
self.word_space("where");
|
2015-07-31 00:04:06 -07:00
|
|
|
|
|
|
|
for (i, predicate) in where_clause.predicates.iter().enumerate() {
|
|
|
|
if i != 0 {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_space(",");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
match predicate {
|
2021-01-02 20:09:17 +01:00
|
|
|
hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
|
|
|
|
bound_generic_params,
|
|
|
|
bounded_ty,
|
2019-12-01 00:17:43 +01:00
|
|
|
bounds,
|
2017-10-16 21:07:26 +02:00
|
|
|
..
|
|
|
|
}) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_formal_generic_params(bound_generic_params);
|
|
|
|
self.print_type(&bounded_ty);
|
2021-01-02 20:09:17 +01:00
|
|
|
self.print_bounds(":", *bounds);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2021-01-02 20:09:17 +01:00
|
|
|
hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
|
|
|
|
lifetime,
|
|
|
|
bounds,
|
2019-12-22 17:42:04 -05:00
|
|
|
..
|
|
|
|
}) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_lifetime(lifetime);
|
|
|
|
self.s.word(":");
|
2015-07-31 00:04:06 -07:00
|
|
|
|
|
|
|
for (i, bound) in bounds.iter().enumerate() {
|
2018-05-28 15:23:16 +01:00
|
|
|
match bound {
|
2018-06-14 12:08:58 +01:00
|
|
|
GenericBound::Outlives(lt) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_lifetime(lt);
|
2018-05-28 15:23:16 +01:00
|
|
|
}
|
2020-01-02 05:18:45 +01:00
|
|
|
_ => panic!(),
|
2018-05-28 15:23:16 +01:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
|
|
|
if i != 0 {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(":");
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-02 20:09:17 +01:00
|
|
|
hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
|
|
|
|
lhs_ty, rhs_ty, ..
|
2019-12-22 17:42:04 -05:00
|
|
|
}) => {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_type(lhs_ty);
|
|
|
|
self.s.space();
|
|
|
|
self.word_space("=");
|
|
|
|
self.print_type(rhs_ty);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 14:15:49 +00:00
|
|
|
pub fn print_mutability(&mut self, mutbl: hir::Mutability, print_const: bool) {
|
2015-07-31 00:04:06 -07:00
|
|
|
match mutbl {
|
2019-12-16 17:28:40 +01:00
|
|
|
hir::Mutability::Mut => self.word_nbsp("mut"),
|
2019-12-22 17:42:04 -05:00
|
|
|
hir::Mutability::Not => {
|
|
|
|
if print_const {
|
|
|
|
self.word_nbsp("const")
|
|
|
|
}
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
pub fn print_mt(&mut self, mt: &hir::MutTy<'_>, print_const: bool) {
|
2019-11-23 14:15:49 +00:00
|
|
|
self.print_mutability(mt.mutbl, print_const);
|
2016-02-08 22:50:21 +01:00
|
|
|
self.print_type(&mt.ty)
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
pub fn print_fn_output(&mut self, decl: &hir::FnDecl<'_>) {
|
2020-03-23 11:32:07 +01:00
|
|
|
if let hir::FnRetTy::DefaultReturn(..) = decl.output {
|
2019-06-24 14:15:11 -04:00
|
|
|
return;
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
self.space_if_not_bol();
|
2019-07-09 09:32:25 -04:00
|
|
|
self.ibox(INDENT_UNIT);
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_space("->");
|
2015-07-31 00:04:06 -07:00
|
|
|
match decl.output {
|
2020-03-23 11:32:07 +01:00
|
|
|
hir::FnRetTy::DefaultReturn(..) => unreachable!(),
|
|
|
|
hir::FnRetTy::Return(ref ty) => self.print_type(&ty),
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2019-06-24 14:15:11 -04:00
|
|
|
self.end();
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2020-03-22 13:36:56 +01:00
|
|
|
if let hir::FnRetTy::Return(ref output) = decl.output {
|
|
|
|
self.maybe_print_comment(output.span.lo())
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn print_ty_fn(
|
|
|
|
&mut self,
|
|
|
|
abi: Abi,
|
|
|
|
unsafety: hir::Unsafety,
|
2019-11-30 17:46:46 +01:00
|
|
|
decl: &hir::FnDecl<'_>,
|
2020-04-19 13:00:18 +02:00
|
|
|
name: Option<Symbol>,
|
2019-11-30 17:46:46 +01:00
|
|
|
generic_params: &[hir::GenericParam<'_>],
|
2020-04-19 13:00:18 +02:00
|
|
|
arg_names: &[Ident],
|
2019-12-22 17:42:04 -05:00
|
|
|
) {
|
2019-07-09 09:32:25 -04:00
|
|
|
self.ibox(INDENT_UNIT);
|
2017-10-16 21:07:26 +02:00
|
|
|
if !generic_params.is_empty() {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word("for");
|
|
|
|
self.print_generic_params(generic_params);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
let generics = hir::Generics {
|
2019-12-01 17:10:12 +01:00
|
|
|
params: &[],
|
2019-12-31 20:15:40 +03:00
|
|
|
where_clause: hir::WhereClause { predicates: &[], span: rustc_span::DUMMY_SP },
|
|
|
|
span: rustc_span::DUMMY_SP,
|
2015-07-31 00:04:06 -07:00
|
|
|
};
|
2019-12-22 17:42:04 -05:00
|
|
|
self.print_fn(
|
|
|
|
decl,
|
|
|
|
hir::FnHeader {
|
|
|
|
unsafety,
|
|
|
|
abi,
|
|
|
|
constness: hir::Constness::NotConst,
|
|
|
|
asyncness: hir::IsAsync::NotAsync,
|
|
|
|
},
|
|
|
|
name,
|
|
|
|
&generics,
|
2019-12-31 20:15:40 +03:00
|
|
|
&Spanned { span: rustc_span::DUMMY_SP, node: hir::VisibilityKind::Inherited },
|
2019-12-22 17:42:04 -05:00
|
|
|
arg_names,
|
|
|
|
None,
|
|
|
|
);
|
2019-06-24 14:15:11 -04:00
|
|
|
self.end();
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn maybe_print_trailing_comment(
|
|
|
|
&mut self,
|
2019-12-31 20:15:40 +03:00
|
|
|
span: rustc_span::Span,
|
2019-12-22 17:42:04 -05:00
|
|
|
next_pos: Option<BytePos>,
|
|
|
|
) {
|
2019-07-05 18:29:15 -04:00
|
|
|
if let Some(cmnts) = self.comments() {
|
|
|
|
if let Some(cmnt) = cmnts.trailing_comment(span, next_pos) {
|
|
|
|
self.print_comment(&cmnt);
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
pub fn print_remaining_comments(&mut self) {
|
2015-07-31 00:04:06 -07:00
|
|
|
// If there aren't any remaining comments, then we need to manually
|
|
|
|
// make sure there is a line break at the end.
|
|
|
|
if self.next_comment().is_none() {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.hardbreak();
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-03-05 15:58:54 -03:00
|
|
|
while let Some(ref cmnt) = self.next_comment() {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_comment(cmnt)
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
pub fn print_fn_header_info(&mut self, header: hir::FnHeader, vis: &hir::Visibility<'_>) {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.s.word(visibility_qualified(vis, ""));
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2018-05-16 22:55:18 -07:00
|
|
|
match header.constness {
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::Constness::NotConst => {}
|
2019-06-24 14:15:11 -04:00
|
|
|
hir::Constness::Const => self.word_nbsp("const"),
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2018-05-16 22:55:18 -07:00
|
|
|
match header.asyncness {
|
|
|
|
hir::IsAsync::NotAsync => {}
|
2019-06-24 14:15:11 -04:00
|
|
|
hir::IsAsync::Async => self.word_nbsp("async"),
|
2018-05-16 22:55:18 -07:00
|
|
|
}
|
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
self.print_unsafety(header.unsafety);
|
2018-05-16 22:55:18 -07:00
|
|
|
|
|
|
|
if header.abi != Abi::Rust {
|
2019-06-24 14:15:11 -04:00
|
|
|
self.word_nbsp("extern");
|
|
|
|
self.word_nbsp(header.abi.to_string());
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2017-06-24 21:22:42 -06:00
|
|
|
self.s.word("fn")
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
pub fn print_unsafety(&mut self, s: hir::Unsafety) {
|
2015-07-31 00:04:06 -07:00
|
|
|
match s {
|
2019-06-24 14:15:11 -04:00
|
|
|
hir::Unsafety::Normal => {}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::Unsafety::Unsafe => self.word_nbsp("unsafe"),
|
|
|
|
}
|
|
|
|
}
|
2017-10-12 09:51:31 -03:00
|
|
|
|
2019-06-24 14:15:11 -04:00
|
|
|
pub fn print_is_auto(&mut self, s: hir::IsAuto) {
|
2017-10-12 09:51:31 -03:00
|
|
|
match s {
|
|
|
|
hir::IsAuto::Yes => self.word_nbsp("auto"),
|
2019-12-22 17:42:04 -05:00
|
|
|
hir::IsAuto::No => {}
|
2017-10-12 09:51:31 -03:00
|
|
|
}
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Does this expression require a semicolon to be treated
|
|
|
|
/// as a statement? The negation of this: 'can this expression
|
|
|
|
/// be used as a statement without a semicolon' -- is used
|
|
|
|
/// as an early-bail-out in the parser so that, for instance,
|
|
|
|
/// if true {...} else {...}
|
|
|
|
/// |x| 5
|
|
|
|
/// isn't parsed as (if true {...} else {...} | x) | 5
|
2019-09-06 03:57:44 +01:00
|
|
|
//
|
|
|
|
// Duplicated from `parse::classify`, but adapted for the HIR.
|
2019-11-29 13:43:03 +01:00
|
|
|
fn expr_requires_semi_to_be_stmt(e: &hir::Expr<'_>) -> bool {
|
2021-01-01 15:38:11 -03:00
|
|
|
!matches!(
|
|
|
|
e.kind,
|
|
|
|
hir::ExprKind::If(..)
|
|
|
|
| hir::ExprKind::Match(..)
|
|
|
|
| hir::ExprKind::Block(..)
|
|
|
|
| hir::ExprKind::Loop(..)
|
|
|
|
)
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// This statement requires a semicolon after it.
|
2015-07-31 00:04:06 -07:00
|
|
|
/// note that in one case (stmt_semi), we've already
|
|
|
|
/// seen the semicolon, and thus don't need another.
|
2019-11-29 13:43:03 +01:00
|
|
|
fn stmt_ends_with_semi(stmt: &hir::StmtKind<'_>) -> bool {
|
2015-07-31 00:04:06 -07:00
|
|
|
match *stmt {
|
2019-01-17 10:39:24 +11:00
|
|
|
hir::StmtKind::Local(_) => true,
|
|
|
|
hir::StmtKind::Item(_) => false,
|
|
|
|
hir::StmtKind::Expr(ref e) => expr_requires_semi_to_be_stmt(&e),
|
|
|
|
hir::StmtKind::Semi(..) => false,
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
2017-08-14 18:27:20 -04:00
|
|
|
|
2018-07-11 18:44:53 +08:00
|
|
|
fn bin_op_to_assoc_op(op: hir::BinOpKind) -> AssocOp {
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::hir::BinOpKind::*;
|
2017-08-14 18:27:20 -04:00
|
|
|
match op {
|
2018-07-11 18:44:53 +08:00
|
|
|
Add => AssocOp::Add,
|
|
|
|
Sub => AssocOp::Subtract,
|
|
|
|
Mul => AssocOp::Multiply,
|
|
|
|
Div => AssocOp::Divide,
|
|
|
|
Rem => AssocOp::Modulus,
|
|
|
|
|
|
|
|
And => AssocOp::LAnd,
|
|
|
|
Or => AssocOp::LOr,
|
|
|
|
|
|
|
|
BitXor => AssocOp::BitXor,
|
|
|
|
BitAnd => AssocOp::BitAnd,
|
|
|
|
BitOr => AssocOp::BitOr,
|
|
|
|
Shl => AssocOp::ShiftLeft,
|
|
|
|
Shr => AssocOp::ShiftRight,
|
|
|
|
|
|
|
|
Eq => AssocOp::Equal,
|
|
|
|
Lt => AssocOp::Less,
|
|
|
|
Le => AssocOp::LessEqual,
|
|
|
|
Ne => AssocOp::NotEqual,
|
|
|
|
Ge => AssocOp::GreaterEqual,
|
|
|
|
Gt => AssocOp::Greater,
|
2017-08-14 18:27:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Expressions that syntactically contain an "exterior" struct literal, i.e., not surrounded by any
|
2018-11-27 02:59:49 +00:00
|
|
|
/// parens or other delimiters, e.g., `X { y: 1 }`, `X { y: 1 }.method()`, `foo == X { y: 1 }` and
|
2017-08-14 18:27:20 -04:00
|
|
|
/// `X { y: 1 } == foo` all do, but `(X { y: 1 }) == foo` does not.
|
2019-11-29 13:43:03 +01:00
|
|
|
fn contains_exterior_struct_lit(value: &hir::Expr<'_>) -> bool {
|
2019-09-26 14:39:48 +01:00
|
|
|
match value.kind {
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Struct(..) => true,
|
2017-08-14 18:27:20 -04:00
|
|
|
|
2019-12-22 21:08:53 +00:00
|
|
|
hir::ExprKind::Assign(ref lhs, ref rhs, _)
|
2019-12-22 17:42:04 -05:00
|
|
|
| hir::ExprKind::AssignOp(_, ref lhs, ref rhs)
|
|
|
|
| hir::ExprKind::Binary(_, ref lhs, ref rhs) => {
|
2019-09-06 03:57:44 +01:00
|
|
|
// `X { y: 1 } + X { y: 2 }`
|
2017-08-14 18:27:20 -04:00
|
|
|
contains_exterior_struct_lit(&lhs) || contains_exterior_struct_lit(&rhs)
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
hir::ExprKind::Unary(_, ref x)
|
|
|
|
| hir::ExprKind::Cast(ref x, _)
|
|
|
|
| hir::ExprKind::Type(ref x, _)
|
|
|
|
| hir::ExprKind::Field(ref x, _)
|
|
|
|
| hir::ExprKind::Index(ref x, _) => {
|
2019-09-06 03:57:44 +01:00
|
|
|
// `&X { y: 1 }, X { y: 1 }.y`
|
2017-08-14 18:27:20 -04:00
|
|
|
contains_exterior_struct_lit(&x)
|
|
|
|
}
|
|
|
|
|
2020-06-09 15:34:23 -04:00
|
|
|
hir::ExprKind::MethodCall(.., ref exprs, _) => {
|
2019-09-06 03:57:44 +01:00
|
|
|
// `X { y: 1 }.bar(...)`
|
2017-08-14 18:27:20 -04:00
|
|
|
contains_exterior_struct_lit(&exprs[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|