rust/src/librustc/util/ppaux.rs

989 lines
28 KiB
Rust
Raw Normal View History

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use metadata::encoder;
use middle::ty::{ReSkolemized, ReVar};
2013-10-29 09:34:11 -05:00
use middle::ty::{BoundRegion, BrAnon, BrNamed};
2013-12-08 01:55:28 -06:00
use middle::ty::{BrFresh, ctxt};
use middle::ty::{mt, t, param_ty};
2013-10-29 09:34:11 -05:00
use middle::ty::{ReFree, ReScope, ReInfer, ReStatic, Region,
ReEmpty};
use middle::ty::{ty_bool, ty_char, ty_bot, ty_box, ty_struct, ty_enum};
use middle::ty::{ty_err, ty_str, ty_vec, ty_float, ty_bare_fn, ty_closure};
use middle::ty::{ty_nil, ty_param, ty_ptr, ty_rptr, ty_self, ty_tup};
2014-04-06 05:54:41 -05:00
use middle::ty::{ty_uniq, ty_trait, ty_int, ty_uint, ty_infer};
use middle::ty;
use middle::typeck;
use std::strbuf::StrBuf;
use syntax::abi;
use syntax::ast_map;
use syntax::codemap::{Span, Pos};
2013-05-14 19:27:27 -05:00
use syntax::parse::token;
2012-09-04 13:54:36 -05:00
use syntax::print::pprust;
use syntax::{ast, ast_util};
use syntax::owned_slice::OwnedSlice;
/// Produces a string suitable for debugging output.
pub trait Repr {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str;
}
/// Produces a string suitable for showing to the user.
pub trait UserString {
2014-03-05 21:07:47 -06:00
fn user_string(&self, tcx: &ctxt) -> ~str;
}
2014-03-05 21:07:47 -06:00
pub fn note_and_explain_region(cx: &ctxt,
prefix: &str,
region: ty::Region,
suffix: &str) {
match explain_region_and_span(cx, region) {
(ref str, Some(span)) => {
cx.sess.span_note(
span,
format!("{}{}{}", prefix, *str, suffix));
}
(ref str, None) => {
cx.sess.note(
format!("{}{}{}", prefix, *str, suffix));
}
}
}
2014-03-05 21:07:47 -06:00
pub fn explain_region_and_span(cx: &ctxt, region: ty::Region)
-> (~str, Option<Span>) {
2012-08-06 14:34:08 -05:00
return match region {
2013-10-29 09:34:11 -05:00
ReScope(node_id) => {
match cx.map.find(node_id) {
Some(ast_map::NodeBlock(ref blk)) => {
explain_span(cx, "block", blk.span)
}
Some(ast_map::NodeExpr(expr)) => {
2012-08-06 14:34:08 -05:00
match expr.node {
2013-11-28 14:22:53 -06:00
ast::ExprCall(..) => explain_span(cx, "call", expr.span),
ast::ExprMethodCall(..) => {
explain_span(cx, "method call", expr.span)
},
2013-11-28 14:22:53 -06:00
ast::ExprMatch(..) => explain_span(cx, "match", expr.span),
_ => explain_span(cx, "expression", expr.span)
}
}
Some(ast_map::NodeStmt(stmt)) => {
explain_span(cx, "statement", stmt.span)
}
Some(ast_map::NodeItem(it)) if (match it.node {
ast::ItemFn(..) => true, _ => false}) => {
explain_span(cx, "function body", it.span)
}
2012-08-20 14:23:37 -05:00
Some(_) | None => {
// this really should not happen
2013-09-28 00:38:08 -05:00
(format!("unknown scope: {}. Please report a bug.", node_id),
2012-08-20 14:23:37 -05:00
None)
}
}
}
2013-10-29 09:34:11 -05:00
ReFree(ref fr) => {
let prefix = match fr.bound_region {
2013-10-29 09:34:11 -05:00
BrAnon(idx) => format!("the anonymous lifetime \\#{} defined on",
idx + 1),
2013-10-29 09:34:11 -05:00
BrFresh(_) => format!("an anonymous lifetime defined on"),
2013-09-28 00:38:08 -05:00
_ => format!("the lifetime {} as defined on",
bound_region_ptr_to_str(cx, fr.bound_region))
};
match cx.map.find(fr.scope_id) {
Some(ast_map::NodeBlock(ref blk)) => {
let (msg, opt_span) = explain_span(cx, "block", blk.span);
2013-09-28 00:38:08 -05:00
(format!("{} {}", prefix, msg), opt_span)
}
Some(ast_map::NodeItem(it)) if match it.node {
ast::ItemImpl(..) => true, _ => false} => {
let (msg, opt_span) = explain_span(cx, "impl", it.span);
(format!("{} {}", prefix, msg), opt_span)
}
2012-08-20 14:23:37 -05:00
Some(_) | None => {
// this really should not happen
2013-09-28 00:38:08 -05:00
(format!("{} node {}", prefix, fr.scope_id), None)
}
}
}
2014-04-15 20:17:48 -05:00
ReStatic => { ("the static lifetime".to_owned(), None) }
2014-04-15 20:17:48 -05:00
ReEmpty => { ("the empty lifetime".to_owned(), None) }
2013-03-15 14:24:24 -05:00
// I believe these cases should not occur (except when debugging,
// perhaps)
2013-11-28 14:22:53 -06:00
ty::ReInfer(_) | ty::ReEarlyBound(..) | ty::ReLateBound(..) => {
2013-09-28 00:38:08 -05:00
(format!("lifetime {:?}", region), None)
}
};
2014-03-05 21:07:47 -06:00
fn explain_span(cx: &ctxt, heading: &str, span: Span)
2014-03-16 13:56:24 -05:00
-> (~str, Option<Span>) {
let lo = cx.sess.codemap().lookup_char_pos_adj(span.lo);
2013-09-28 00:38:08 -05:00
(format!("the {} at {}:{}", heading,
lo.line, lo.col.to_uint()), Some(span))
}
}
2014-03-05 21:07:47 -06:00
pub fn bound_region_ptr_to_str(cx: &ctxt, br: BoundRegion) -> ~str {
bound_region_to_str(cx, "&", true, br)
}
2014-03-05 21:07:47 -06:00
pub fn bound_region_to_str(cx: &ctxt,
prefix: &str, space: bool,
2013-10-29 09:34:11 -05:00
br: BoundRegion) -> ~str {
let space_str = if space { " " } else { "" };
if cx.sess.verbose() {
return format!("{}{}{}", prefix, br.repr(cx), space_str);
}
match br {
BrNamed(_, name) => format!("{}'{}{}", prefix,
token::get_name(name), space_str),
BrAnon(_) => prefix.to_str(),
BrFresh(_) => prefix.to_str(),
}
}
// In general, if you are giving a region error message,
// you should use `explain_region()` or, better yet,
// `note_and_explain_region()`
2014-03-05 21:07:47 -06:00
pub fn region_ptr_to_str(cx: &ctxt, region: Region) -> ~str {
region_to_str(cx, "&", true, region)
}
2014-03-05 21:07:47 -06:00
pub fn region_to_str(cx: &ctxt, prefix: &str, space: bool, region: Region) -> ~str {
let space_str = if space { " " } else { "" };
if cx.sess.verbose() {
return format!("{}{}{}", prefix, region.repr(cx), space_str);
}
// These printouts are concise. They do not contain all the information
// the user might want to diagnose an error, but there is basically no way
// to fit that into a short string. Hence the recommendation to use
// `explain_region()` or `note_and_explain_region()`.
match region {
2013-10-29 09:34:11 -05:00
ty::ReScope(_) => prefix.to_str(),
ty::ReEarlyBound(_, _, name) => token::get_name(name).get().to_str(),
2013-10-29 09:34:11 -05:00
ty::ReLateBound(_, br) => bound_region_to_str(cx, prefix, space, br),
ty::ReFree(ref fr) => bound_region_to_str(cx, prefix, space, fr.bound_region),
ty::ReInfer(ReSkolemized(_, br)) => {
bound_region_to_str(cx, prefix, space, br)
}
2013-10-29 09:34:11 -05:00
ty::ReInfer(ReVar(_)) => prefix.to_str(),
ty::ReStatic => format!("{}'static{}", prefix, space_str),
ty::ReEmpty => format!("{}'<empty>{}", prefix, space_str)
}
}
pub fn mutability_to_str(m: ast::Mutability) -> ~str {
match m {
2014-04-15 20:17:48 -05:00
ast::MutMutable => "mut ".to_owned(),
ast::MutImmutable => "".to_owned(),
}
}
2014-03-05 21:07:47 -06:00
pub fn mt_to_str(cx: &ctxt, m: &mt) -> ~str {
format!("{}{}", mutability_to_str(m.mutbl), ty_to_str(cx, m.ty))
}
2014-03-05 21:07:47 -06:00
pub fn trait_store_to_str(cx: &ctxt, s: ty::TraitStore) -> ~str {
match s {
2014-04-15 20:17:48 -05:00
ty::UniqTraitStore => "~".to_owned(),
ty::RegionTraitStore(r, m) => {
format!("{}{}", region_ptr_to_str(cx, r), mutability_to_str(m))
}
}
}
pub fn vec_map_to_str<T>(ts: &[T], f: |t: &T| -> ~str) -> ~str {
let tstrs = ts.iter().map(f).collect::<Vec<~str>>();
2013-09-28 00:38:08 -05:00
format!("[{}]", tstrs.connect(", "))
2013-08-02 12:34:33 -05:00
}
2014-03-05 21:07:47 -06:00
pub fn fn_sig_to_str(cx: &ctxt, typ: &ty::FnSig) -> ~str {
format!("fn{}{} -> {}",
typ.binder_id,
typ.inputs.repr(cx),
typ.output.repr(cx))
}
2014-03-05 21:07:47 -06:00
pub fn trait_ref_to_str(cx: &ctxt, trait_ref: &ty::TraitRef) -> ~str {
trait_ref.user_string(cx)
}
2014-03-05 21:07:47 -06:00
pub fn ty_to_str(cx: &ctxt, typ: t) -> ~str {
fn fn_input_to_str(cx: &ctxt, input: ty::t) -> ~str {
2013-04-26 21:13:38 -05:00
ty_to_str(cx, input)
}
2014-03-05 21:07:47 -06:00
fn bare_fn_to_str(cx: &ctxt,
fn_style: ast::FnStyle,
abi: abi::Abi,
2013-09-01 19:50:59 -05:00
ident: Option<ast::Ident>,
sig: &ty::FnSig)
-> ~str {
let mut s = if abi == abi::Rust {
StrBuf::new()
} else {
StrBuf::from_owned_str(format!("extern {} ", abi.to_str()))
};
match fn_style {
ast::NormalFn => {}
_ => {
s.push_str(fn_style.to_str());
s.push_char(' ');
}
2012-05-25 01:44:58 -05:00
};
s.push_str("fn");
match ident {
Some(i) => {
s.push_char(' ');
s.push_str(token::get_ident(i).get());
}
_ => { }
}
push_sig_to_str(cx, &mut s, '(', ')', sig);
s.into_owned()
}
2014-03-05 21:07:47 -06:00
fn closure_to_str(cx: &ctxt, cty: &ty::ClosureTy) -> ~str {
let mut s = StrBuf::new();
match cty.store {
ty::UniqTraitStore => {}
ty::RegionTraitStore(region, _) => {
s.push_str(region_to_str(cx, "", true, region));
}
}
match cty.fn_style {
ast::NormalFn => {}
_ => {
s.push_str(cty.fn_style.to_str());
s.push_char(' ');
}
};
match cty.store {
ty::UniqTraitStore => {
assert_eq!(cty.onceness, ast::Once);
s.push_str("proc");
push_sig_to_str(cx, &mut s, '(', ')', &cty.sig);
}
ty::RegionTraitStore(..) => {
match cty.onceness {
ast::Many => {}
ast::Once => s.push_str("once ")
}
push_sig_to_str(cx, &mut s, '|', '|', &cty.sig);
}
}
if !cty.bounds.is_empty() {
s.push_str(":");
s.push_str(cty.bounds.repr(cx));
}
s.into_owned()
}
2014-03-05 21:07:47 -06:00
fn push_sig_to_str(cx: &ctxt,
s: &mut StrBuf,
bra: char,
ket: char,
sig: &ty::FnSig) {
s.push_char(bra);
let strs: Vec<~str> = sig.inputs.iter().map(|a| fn_input_to_str(cx, *a)).collect();
2013-06-10 08:25:25 -05:00
s.push_str(strs.connect(", "));
if sig.variadic {
s.push_str(", ...");
}
s.push_char(ket);
if ty::get(sig.output).sty != ty_nil {
s.push_str(" -> ");
if ty::type_is_bot(sig.output) {
s.push_char('!');
} else {
s.push_str(ty_to_str(cx, sig.output));
}
}
}
// if there is an id, print that instead of the structural type:
/*for def_id in ty::type_def_id(typ).iter() {
2012-05-04 14:33:04 -05:00
// note that this typedef cannot have type parameters
return ty::item_path_str(cx, *def_id);
}*/
// pretty print the structural type representation:
return match ty::get(typ).sty {
2014-04-15 20:17:48 -05:00
ty_nil => "()".to_owned(),
ty_bot => "!".to_owned(),
ty_bool => "bool".to_owned(),
ty_char => "char".to_owned(),
ty_int(t) => ast_util::int_ty_to_str(t, None),
ty_uint(t) => ast_util::uint_ty_to_str(t, None),
2012-08-03 21:59:04 -05:00
ty_float(t) => ast_util::float_ty_to_str(t),
2014-04-15 20:17:48 -05:00
ty_box(typ) => "@".to_owned() + ty_to_str(cx, typ),
ty_uniq(typ) => "~".to_owned() + ty_to_str(cx, typ),
ty_ptr(ref tm) => "*".to_owned() + mt_to_str(cx, tm),
ty_rptr(r, ref tm) => {
region_ptr_to_str(cx, r) + mt_to_str(cx, tm)
}
ty_tup(ref elems) => {
let strs: Vec<~str> = elems.iter().map(|elem| ty_to_str(cx, *elem)).collect();
2014-04-15 20:17:48 -05:00
"(".to_owned() + strs.connect(",") + ")"
}
ty_closure(ref f) => {
closure_to_str(cx, *f)
}
ty_bare_fn(ref f) => {
bare_fn_to_str(cx, f.fn_style, f.abi, None, &f.sig)
}
ty_infer(infer_ty) => infer_ty.to_str(),
2014-04-15 20:17:48 -05:00
ty_err => "[type error]".to_owned(),
ty_param(param_ty {idx: id, def_id: did}) => {
2014-03-20 21:49:20 -05:00
let ident = match cx.ty_param_defs.borrow().find(&did.node) {
Some(def) => token::get_ident(def.ident).get().to_str(),
// This can only happen when a type mismatch error happens and
// the actual type has more type parameters than the expected one.
None => format!("<generic \\#{}>", id)
};
if !cx.sess.verbose() {
ident
} else {
format!("{}:{:?}", ident, did)
}
}
2014-04-15 20:17:48 -05:00
ty_self(..) => "Self".to_owned(),
ty_enum(did, ref substs) | ty_struct(did, ref substs) => {
let base = ty::item_path_str(cx, did);
parameterized(cx,
base,
&substs.regions,
substs.tps.as_slice(),
did,
false)
}
ty_trait(~ty::TyTrait {
def_id: did, ref substs, store, ref bounds
}) => {
let base = ty::item_path_str(cx, did);
let ty = parameterized(cx, base, &substs.regions,
substs.tps.as_slice(), did, true);
let bound_sep = if bounds.is_empty() { "" } else { ":" };
let bound_str = bounds.repr(cx);
format!("{}{}{}{}", trait_store_to_str(cx, store), ty, bound_sep, bound_str)
}
ty_vec(ty, vs) => {
match vs {
ty::VstoreFixed(n) => {
format!("[{}, .. {}]", ty_to_str(cx, ty), n)
}
_ => {
format!("{}[{}]", vs.repr(cx), ty_to_str(cx, ty))
}
}
}
ty_str(vs) => {
match vs {
ty::VstoreFixed(n) => format!("str/{}", n),
2014-04-15 20:17:48 -05:00
ty::VstoreUniq => "~str".to_owned(),
ty::VstoreSlice(r, ()) => format!("{}str", region_ptr_to_str(cx, r))
}
}
}
}
2014-03-05 21:07:47 -06:00
pub fn parameterized(cx: &ctxt,
base: &str,
regions: &ty::RegionSubsts,
tps: &[ty::t],
did: ast::DefId,
is_trait: bool) -> ~str {
let mut strs = Vec::new();
match *regions {
ty::ErasedRegions => { }
ty::NonerasedRegions(ref regions) => {
for &r in regions.iter() {
strs.push(region_to_str(cx, "", false, r))
}
2013-06-15 22:45:48 -05:00
}
}
let generics = if is_trait {
2014-01-31 22:57:59 -06:00
ty::lookup_trait_def(cx, did).generics.clone()
} else {
ty::lookup_item_type(cx, did).generics
};
let ty_params = generics.type_param_defs();
let has_defaults = ty_params.last().map_or(false, |def| def.default.is_some());
let num_defaults = if has_defaults {
// We should have a borrowed version of substs instead of cloning.
let mut substs = ty::substs {
tps: Vec::from_slice(tps),
regions: regions.clone(),
self_ty: None
};
ty_params.iter().zip(tps.iter()).rev().take_while(|&(def, &actual)| {
substs.tps.pop();
match def.default {
Some(default) => ty::subst(cx, &substs, default) == actual,
None => false
}
}).len()
} else {
0
};
for t in tps.slice_to(tps.len() - num_defaults).iter() {
strs.push(ty_to_str(cx, *t))
}
if strs.len() > 0u {
2013-09-28 00:38:08 -05:00
format!("{}<{}>", base, strs.connect(","))
} else {
2013-09-28 00:38:08 -05:00
format!("{}", base)
}
}
2014-03-05 21:07:47 -06:00
pub fn ty_to_short_str(cx: &ctxt, typ: t) -> ~str {
let mut s = encoder::encoded_ty(cx, typ);
if s.len() >= 32u { s = s.slice(0u, 32u).to_owned(); }
2012-08-01 19:30:05 -05:00
return s;
}
impl<T:Repr> Repr for Option<T> {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
match self {
2014-04-15 20:17:48 -05:00
&None => "None".to_owned(),
2014-02-07 13:51:18 -06:00
&Some(ref t) => t.repr(tcx),
}
}
}
2014-02-07 13:51:18 -06:00
impl<T:Repr,U:Repr> Repr for Result<T,U> {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
2014-02-07 13:51:18 -06:00
match self {
&Ok(ref t) => t.repr(tcx),
&Err(ref u) => format!("Err({})", u.repr(tcx))
}
}
}
impl Repr for () {
2014-03-05 21:07:47 -06:00
fn repr(&self, _tcx: &ctxt) -> ~str {
2014-04-15 20:17:48 -05:00
"()".to_owned()
2014-02-07 13:51:18 -06:00
}
}
impl<T:Repr> Repr for @T {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
(&**self).repr(tcx)
}
}
impl<T:Repr> Repr for ~T {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
(&**self).repr(tcx)
}
}
2014-03-05 21:07:47 -06:00
fn repr_vec<T:Repr>(tcx: &ctxt, v: &[T]) -> ~str {
2013-08-02 12:34:33 -05:00
vec_map_to_str(v, |t| t.repr(tcx))
}
impl<'a, T:Repr> Repr for &'a [T] {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
repr_vec(tcx, *self)
}
}
impl<T:Repr> Repr for OwnedSlice<T> {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
repr_vec(tcx, self.as_slice())
}
}
// This is necessary to handle types like Option<~[T]>, for which
// autoderef cannot convert the &[T] handler
impl<T:Repr> Repr for Vec<T> {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
repr_vec(tcx, self.as_slice())
}
}
impl Repr for ty::TypeParameterDef {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
format!("TypeParameterDef({:?}, {})",
self.def_id,
self.bounds.repr(tcx))
}
}
impl Repr for ty::RegionParameterDef {
2014-03-05 21:07:47 -06:00
fn repr(&self, _tcx: &ctxt) -> ~str {
format!("RegionParameterDef({}, {:?})",
token::get_name(self.name),
self.def_id)
}
}
impl Repr for ty::t {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
ty_to_str(tcx, *self)
}
}
impl Repr for ty::substs {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
2013-09-28 00:38:08 -05:00
format!("substs(regions={}, self_ty={}, tps={})",
self.regions.repr(tcx),
self.self_ty.repr(tcx),
self.tps.repr(tcx))
}
}
impl Repr for ty::RegionSubsts {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
match *self {
2014-04-15 20:17:48 -05:00
ty::ErasedRegions => "erased".to_owned(),
ty::NonerasedRegions(ref regions) => regions.repr(tcx)
}
}
}
impl Repr for ty::ParamBounds {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
let mut res = Vec::new();
2013-07-25 23:53:29 -05:00
for b in self.builtin_bounds.iter() {
res.push(match b {
2014-04-15 20:17:48 -05:00
ty::BoundStatic => "'static".to_owned(),
ty::BoundSend => "Send".to_owned(),
ty::BoundSized => "Sized".to_owned(),
ty::BoundCopy => "Pod".to_owned(),
ty::BoundShare => "Share".to_owned(),
});
2013-07-25 23:53:29 -05:00
}
for t in self.trait_bounds.iter() {
res.push(t.repr(tcx));
}
2013-06-10 08:25:25 -05:00
res.connect("+")
}
}
impl Repr for ty::TraitRef {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
trait_ref_to_str(tcx, self)
}
}
impl Repr for ast::Expr {
2014-03-05 21:07:47 -06:00
fn repr(&self, _tcx: &ctxt) -> ~str {
format!("expr({}: {})", self.id, pprust::expr_to_str(self))
}
}
impl Repr for ast::Item {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
format!("item({})", tcx.map.node_to_str(self.id))
}
}
impl Repr for ast::Stmt {
2014-03-05 21:07:47 -06:00
fn repr(&self, _tcx: &ctxt) -> ~str {
format!("stmt({}: {})",
ast_util::stmt_id(self),
pprust::stmt_to_str(self))
}
}
impl Repr for ast::Pat {
2014-03-05 21:07:47 -06:00
fn repr(&self, _tcx: &ctxt) -> ~str {
2013-09-28 00:38:08 -05:00
format!("pat({}: {})",
self.id,
pprust::pat_to_str(self))
}
}
2013-10-29 09:34:11 -05:00
impl Repr for ty::BoundRegion {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
match *self {
2013-10-29 09:34:11 -05:00
ty::BrAnon(id) => format!("BrAnon({})", id),
ty::BrNamed(id, name) => format!("BrNamed({}, {})",
id.repr(tcx),
token::get_name(name)),
2013-10-29 09:34:11 -05:00
ty::BrFresh(id) => format!("BrFresh({})", id),
}
}
}
impl Repr for ty::Region {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
match *self {
ty::ReEarlyBound(id, index, name) => {
2013-10-29 09:34:11 -05:00
format!("ReEarlyBound({}, {}, {})",
id, index, token::get_name(name))
}
2013-10-29 09:34:11 -05:00
ty::ReLateBound(binder_id, ref bound_region) => {
format!("ReLateBound({}, {})",
binder_id, bound_region.repr(tcx))
}
2013-10-29 09:34:11 -05:00
ty::ReFree(ref fr) => {
format!("ReFree({}, {})",
fr.scope_id,
fr.bound_region.repr(tcx))
}
2013-10-29 09:34:11 -05:00
ty::ReScope(id) => {
format!("ReScope({})", id)
}
2013-10-29 09:34:11 -05:00
ty::ReStatic => {
format!("ReStatic")
}
2013-10-29 09:34:11 -05:00
ty::ReInfer(ReVar(ref vid)) => {
format!("ReInfer({})", vid.id)
}
2013-10-29 09:34:11 -05:00
ty::ReInfer(ReSkolemized(id, ref bound_region)) => {
format!("re_skolemized({}, {})",
id, bound_region.repr(tcx))
}
2013-10-29 09:34:11 -05:00
ty::ReEmpty => {
format!("ReEmpty")
}
}
}
}
impl Repr for ast::DefId {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
// Unfortunately, there seems to be no way to attempt to print
// a path for a def-id, so I'll just make a best effort for now
// and otherwise fallback to just printing the crate/node pair
if self.krate == ast::LOCAL_CRATE {
2013-12-27 18:09:29 -06:00
{
match tcx.map.find(self.node) {
Some(ast_map::NodeItem(..)) |
Some(ast_map::NodeForeignItem(..)) |
Some(ast_map::NodeMethod(..)) |
Some(ast_map::NodeTraitMethod(..)) |
Some(ast_map::NodeVariant(..)) |
Some(ast_map::NodeStructCtor(..)) => {
2013-12-27 18:09:29 -06:00
return format!("{:?}:{}",
*self,
ty::item_path_str(tcx, *self));
}
_ => {}
}
}
}
2013-09-28 00:38:08 -05:00
return format!("{:?}", *self);
}
}
impl Repr for ty::ty_param_bounds_and_ty {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
2013-09-28 00:38:08 -05:00
format!("ty_param_bounds_and_ty \\{generics: {}, ty: {}\\}",
self.generics.repr(tcx),
self.ty.repr(tcx))
}
}
impl Repr for ty::Generics {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
format!("Generics(type_param_defs: {}, region_param_defs: {})",
2014-01-31 22:57:59 -06:00
self.type_param_defs().repr(tcx),
self.region_param_defs().repr(tcx))
}
}
impl Repr for ty::ItemVariances {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
format!("IterVariances(self_param={}, type_params={}, region_params={})",
self.self_param.repr(tcx),
self.type_params.repr(tcx),
self.region_params.repr(tcx))
}
}
impl Repr for ty::Variance {
2014-03-05 21:07:47 -06:00
fn repr(&self, _: &ctxt) -> ~str {
self.to_str().to_owned()
}
}
impl Repr for ty::Method {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
format!("method(ident: {}, generics: {}, fty: {}, \
explicit_self: {}, vis: {}, def_id: {})",
self.ident.repr(tcx),
self.generics.repr(tcx),
self.fty.repr(tcx),
self.explicit_self.repr(tcx),
self.vis.repr(tcx),
self.def_id.repr(tcx))
}
}
impl Repr for ast::Name {
2014-03-05 21:07:47 -06:00
fn repr(&self, _tcx: &ctxt) -> ~str {
token::get_name(*self).get().to_str()
}
}
2013-09-01 19:50:59 -05:00
impl Repr for ast::Ident {
2014-03-05 21:07:47 -06:00
fn repr(&self, _tcx: &ctxt) -> ~str {
token::get_ident(*self).get().to_str()
}
}
impl Repr for ast::ExplicitSelf_ {
2014-03-05 21:07:47 -06:00
fn repr(&self, _tcx: &ctxt) -> ~str {
2013-09-28 00:38:08 -05:00
format!("{:?}", *self)
}
}
impl Repr for ast::Visibility {
2014-03-05 21:07:47 -06:00
fn repr(&self, _tcx: &ctxt) -> ~str {
2013-09-28 00:38:08 -05:00
format!("{:?}", *self)
}
}
impl Repr for ty::BareFnTy {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
format!("BareFnTy \\{fn_style: {:?}, abi: {}, sig: {}\\}",
self.fn_style,
self.abi.to_str(),
self.sig.repr(tcx))
}
}
impl Repr for ty::FnSig {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
fn_sig_to_str(tcx, self)
}
}
impl Repr for typeck::MethodCallee {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
format!("MethodCallee \\{origin: {}, ty: {}, {}\\}",
self.origin.repr(tcx),
self.ty.repr(tcx),
self.substs.repr(tcx))
}
}
impl Repr for typeck::MethodOrigin {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
match self {
&typeck::MethodStatic(def_id) => {
format!("MethodStatic({})", def_id.repr(tcx))
}
&typeck::MethodParam(ref p) => {
p.repr(tcx)
}
&typeck::MethodObject(ref p) => {
p.repr(tcx)
}
}
}
}
impl Repr for typeck::MethodParam {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
format!("MethodParam({},{:?},{:?},{:?})",
self.trait_id.repr(tcx),
self.method_num,
self.param_num,
self.bound_num)
}
}
impl Repr for typeck::MethodObject {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
format!("MethodObject({},{:?},{:?})",
self.trait_id.repr(tcx),
self.method_num,
self.real_index)
}
}
impl Repr for ty::RegionVid {
2014-03-05 21:07:47 -06:00
fn repr(&self, _tcx: &ctxt) -> ~str {
2013-09-28 00:38:08 -05:00
format!("{:?}", *self)
}
}
impl Repr for ty::TraitStore {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
trait_store_to_str(tcx, *self)
}
}
impl Repr for ty::Vstore {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
match *self {
ty::VstoreFixed(n) => format!("{}", n),
2014-04-15 20:17:48 -05:00
ty::VstoreUniq => "~".to_owned(),
ty::VstoreSlice(r, m) => {
format!("{}{}", region_ptr_to_str(tcx, r), mutability_to_str(m))
}
}
}
}
impl Repr for ty::Vstore<()> {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
match *self {
ty::VstoreFixed(n) => format!("{}", n),
2014-04-15 20:17:48 -05:00
ty::VstoreUniq => "~".to_owned(),
ty::VstoreSlice(r, ()) => region_ptr_to_str(tcx, r)
}
}
}
impl Repr for ty::BuiltinBound {
2014-03-05 21:07:47 -06:00
fn repr(&self, _tcx: &ctxt) -> ~str {
2013-09-28 00:38:08 -05:00
format!("{:?}", *self)
}
}
impl UserString for ty::BuiltinBound {
2014-03-05 21:07:47 -06:00
fn user_string(&self, _tcx: &ctxt) -> ~str {
match *self {
2014-04-15 20:17:48 -05:00
ty::BoundStatic => "'static".to_owned(),
ty::BoundSend => "Send".to_owned(),
ty::BoundSized => "Sized".to_owned(),
ty::BoundCopy => "Pod".to_owned(),
ty::BoundShare => "Share".to_owned(),
}
}
}
impl Repr for ty::BuiltinBounds {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
self.user_string(tcx)
}
}
impl Repr for Span {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
2014-03-16 13:56:24 -05:00
tcx.sess.codemap().span_to_str(*self)
}
}
impl<A:UserString> UserString for @A {
2014-03-05 21:07:47 -06:00
fn user_string(&self, tcx: &ctxt) -> ~str {
let this: &A = &**self;
this.user_string(tcx)
}
}
impl UserString for ty::BuiltinBounds {
2014-03-05 21:07:47 -06:00
fn user_string(&self, tcx: &ctxt) -> ~str {
2014-04-15 20:17:48 -05:00
if self.is_empty() { "<no-bounds>".to_owned() } else {
let mut result = Vec::new();
2013-07-25 23:53:29 -05:00
for bb in self.iter() {
result.push(bb.user_string(tcx));
2013-07-25 23:53:29 -05:00
}
2013-06-10 08:25:25 -05:00
result.connect("+")
}
}
}
impl UserString for ty::TraitRef {
2014-03-05 21:07:47 -06:00
fn user_string(&self, tcx: &ctxt) -> ~str {
let base = ty::item_path_str(tcx, self.def_id);
if tcx.sess.verbose() && self.substs.self_ty.is_some() {
2013-07-02 14:47:32 -05:00
let mut all_tps = self.substs.tps.clone();
for &t in self.substs.self_ty.iter() { all_tps.push(t); }
parameterized(tcx, base, &self.substs.regions,
all_tps.as_slice(), self.def_id, true)
} else {
parameterized(tcx, base, &self.substs.regions,
self.substs.tps.as_slice(), self.def_id, true)
}
}
}
impl UserString for ty::t {
2014-03-05 21:07:47 -06:00
fn user_string(&self, tcx: &ctxt) -> ~str {
ty_to_str(tcx, *self)
}
}
impl UserString for ast::Ident {
2014-03-05 21:07:47 -06:00
fn user_string(&self, _tcx: &ctxt) -> ~str {
token::get_name(self.name).get().to_owned()
}
}
impl Repr for abi::Abi {
2014-03-05 21:07:47 -06:00
fn repr(&self, _tcx: &ctxt) -> ~str {
self.to_str()
}
}
impl UserString for abi::Abi {
2014-03-05 21:07:47 -06:00
fn user_string(&self, _tcx: &ctxt) -> ~str {
self.to_str()
}
}
2014-02-07 13:51:18 -06:00
impl Repr for ty::UpvarId {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
2014-02-07 13:51:18 -06:00
format!("UpvarId({};`{}`;{})",
self.var_id,
ty::local_var_name_str(tcx, self.var_id),
self.closure_expr_id)
}
}
impl Repr for ast::Mutability {
2014-03-05 21:07:47 -06:00
fn repr(&self, _tcx: &ctxt) -> ~str {
2014-02-07 13:51:18 -06:00
format!("{:?}", *self)
}
}
impl Repr for ty::BorrowKind {
2014-03-05 21:07:47 -06:00
fn repr(&self, _tcx: &ctxt) -> ~str {
2014-02-07 13:51:18 -06:00
format!("{:?}", *self)
}
}
impl Repr for ty::UpvarBorrow {
2014-03-05 21:07:47 -06:00
fn repr(&self, tcx: &ctxt) -> ~str {
2014-02-07 13:51:18 -06:00
format!("UpvarBorrow({}, {})",
self.kind.repr(tcx),
self.region.repr(tcx))
}
}