2012-12-03 16:48:01 -08:00
|
|
|
// 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.
|
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
//! Pulls type information out of the AST and attaches it to the document
|
2012-01-17 17:44:32 -08:00
|
|
|
|
2012-09-18 16:48:40 -07:00
|
|
|
use doc::ItemUtils;
|
2012-12-05 15:06:54 -08:00
|
|
|
use fold::Fold;
|
2012-09-05 10:41:47 -07:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::print::pprust;
|
|
|
|
use syntax::ast_map;
|
2012-09-10 15:38:28 -07:00
|
|
|
use std::map::HashMap;
|
2012-09-05 10:41:47 -07:00
|
|
|
use extract::to_str;
|
2012-01-16 15:33:06 -08:00
|
|
|
|
2012-11-19 18:00:12 -08:00
|
|
|
pub fn mk_pass() -> Pass {
|
2012-02-27 18:07:16 -08:00
|
|
|
{
|
2012-07-13 22:57:48 -07:00
|
|
|
name: ~"tystr",
|
2012-02-27 18:07:16 -08:00
|
|
|
f: run
|
|
|
|
}
|
2012-01-17 16:12:50 -08:00
|
|
|
}
|
2012-01-16 15:33:06 -08:00
|
|
|
|
2012-01-16 15:44:10 -08:00
|
|
|
fn run(
|
2012-09-18 16:48:40 -07:00
|
|
|
srv: astsrv::Srv,
|
2012-11-19 18:48:46 -08:00
|
|
|
+doc: doc::Doc
|
2012-09-18 16:48:40 -07:00
|
|
|
) -> doc::Doc {
|
2012-12-05 15:06:54 -08:00
|
|
|
let fold = Fold {
|
2012-01-24 00:14:31 -08:00
|
|
|
fold_fn: fold_fn,
|
2012-01-25 17:22:19 -08:00
|
|
|
fold_const: fold_const,
|
2012-01-26 20:59:15 -08:00
|
|
|
fold_enum: fold_enum,
|
2012-07-03 16:30:42 -07:00
|
|
|
fold_trait: fold_trait,
|
2012-02-01 22:41:41 -08:00
|
|
|
fold_impl: fold_impl,
|
2012-09-04 13:29:32 -07:00
|
|
|
fold_type: fold_type,
|
2012-09-19 14:37:43 -07:00
|
|
|
fold_struct: fold_struct,
|
2012-12-05 15:06:54 -08:00
|
|
|
.. fold::default_any_fold(srv)
|
|
|
|
};
|
2012-11-29 17:51:16 -08:00
|
|
|
(fold.fold_doc)(&fold, doc)
|
2012-01-16 15:33:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_fn(
|
2012-11-19 18:48:46 -08:00
|
|
|
fold: &fold::Fold<astsrv::Srv>,
|
|
|
|
+doc: doc::FnDoc
|
2012-09-18 16:48:40 -07:00
|
|
|
) -> doc::FnDoc {
|
2012-01-16 15:33:06 -08:00
|
|
|
|
2012-01-16 18:21:34 -08:00
|
|
|
let srv = fold.ctxt;
|
2012-01-16 15:33:06 -08:00
|
|
|
|
2012-01-30 13:05:25 -08:00
|
|
|
{
|
2012-09-04 13:29:32 -07:00
|
|
|
sig: get_fn_sig(srv, doc.id()),
|
|
|
|
.. doc
|
2012-01-16 15:33:06 -08:00
|
|
|
}
|
2012-01-18 16:09:32 -08:00
|
|
|
}
|
2012-01-16 15:33:06 -08:00
|
|
|
|
2012-09-18 16:48:40 -07:00
|
|
|
fn get_fn_sig(srv: astsrv::Srv, fn_id: doc::AstId) -> Option<~str> {
|
2012-06-30 16:19:07 -07:00
|
|
|
do astsrv::exec(srv) |ctxt| {
|
2012-08-23 15:14:56 -07:00
|
|
|
match ctxt.ast_map.get(fn_id) {
|
2012-01-19 18:09:19 -08:00
|
|
|
ast_map::node_item(@{
|
|
|
|
ident: ident,
|
2012-08-23 18:17:16 -07:00
|
|
|
node: ast::item_fn(decl, _, tys, _), _
|
2012-02-24 15:06:58 -08:00
|
|
|
}, _) |
|
2012-06-26 16:18:37 -07:00
|
|
|
ast_map::node_foreign_item(@{
|
2012-02-24 15:06:58 -08:00
|
|
|
ident: ident,
|
2012-08-23 18:17:16 -07:00
|
|
|
node: ast::foreign_item_fn(decl, _, tys), _
|
2012-08-03 19:59:04 -07:00
|
|
|
}, _, _) => {
|
2012-08-20 12:23:37 -07:00
|
|
|
Some(pprust::fun_to_str(decl, ident, tys, extract::interner()))
|
2012-01-19 18:09:19 -08:00
|
|
|
}
|
2012-08-23 15:14:56 -07:00
|
|
|
_ => fail ~"get_fn_sig: fn_id not bound to a fn item"
|
2012-01-19 18:09:19 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_fn_sig() {
|
2012-07-13 22:57:48 -07:00
|
|
|
let doc = test::mk_doc(~"fn a<T>() -> int { }");
|
2012-08-20 12:23:37 -07:00
|
|
|
assert doc.cratemod().fns()[0].sig == Some(~"fn a<T>() -> int");
|
2012-01-19 18:09:19 -08:00
|
|
|
}
|
|
|
|
|
2012-02-24 15:06:58 -08:00
|
|
|
#[test]
|
2012-06-26 16:18:37 -07:00
|
|
|
fn should_add_foreign_fn_sig() {
|
2012-07-13 22:57:48 -07:00
|
|
|
let doc = test::mk_doc(~"extern mod a { fn a<T>() -> int; }");
|
2012-08-20 12:23:37 -07:00
|
|
|
assert doc.cratemod().nmods()[0].fns[0].sig == Some(~"fn a<T>() -> int");
|
2012-02-24 15:06:58 -08:00
|
|
|
}
|
|
|
|
|
2012-01-24 00:14:31 -08:00
|
|
|
fn fold_const(
|
2012-11-19 18:48:46 -08:00
|
|
|
fold: &fold::Fold<astsrv::Srv>,
|
|
|
|
+doc: doc::ConstDoc
|
2012-09-18 16:48:40 -07:00
|
|
|
) -> doc::ConstDoc {
|
2012-01-24 00:14:31 -08:00
|
|
|
let srv = fold.ctxt;
|
|
|
|
|
2012-01-30 13:05:25 -08:00
|
|
|
{
|
2012-08-20 12:23:37 -07:00
|
|
|
sig: Some(do astsrv::exec(srv) |ctxt| {
|
2012-08-23 15:14:56 -07:00
|
|
|
match ctxt.ast_map.get(doc.id()) {
|
2012-01-24 00:14:31 -08:00
|
|
|
ast_map::node_item(@{
|
|
|
|
node: ast::item_const(ty, _), _
|
2012-08-03 19:59:04 -07:00
|
|
|
}, _) => {
|
2012-07-18 16:18:02 -07:00
|
|
|
pprust::ty_to_str(ty, extract::interner())
|
2012-01-24 00:14:31 -08:00
|
|
|
}
|
2012-08-23 15:14:56 -07:00
|
|
|
_ => fail ~"fold_const: id not bound to a const item"
|
2012-01-24 00:14:31 -08:00
|
|
|
}
|
2012-09-04 13:29:32 -07:00
|
|
|
}),
|
|
|
|
.. doc
|
2012-01-24 00:14:31 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_const_types() {
|
2012-07-13 22:57:48 -07:00
|
|
|
let doc = test::mk_doc(~"const a: bool = true;");
|
2012-08-20 12:23:37 -07:00
|
|
|
assert doc.cratemod().consts()[0].sig == Some(~"bool");
|
2012-01-25 17:22:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_enum(
|
2012-11-19 18:48:46 -08:00
|
|
|
fold: &fold::Fold<astsrv::Srv>,
|
|
|
|
+doc: doc::EnumDoc
|
2012-09-18 16:48:40 -07:00
|
|
|
) -> doc::EnumDoc {
|
2012-02-26 23:47:27 -08:00
|
|
|
let doc_id = doc.id();
|
2012-01-25 17:22:19 -08:00
|
|
|
let srv = fold.ctxt;
|
|
|
|
|
2012-01-30 13:05:25 -08:00
|
|
|
{
|
2012-07-14 03:36:35 +10:00
|
|
|
variants: do par::map(doc.variants) |variant| {
|
2012-09-28 17:17:20 -07:00
|
|
|
let variant = *variant;
|
2012-06-30 16:19:07 -07:00
|
|
|
let sig = do astsrv::exec(srv) |ctxt| {
|
2012-08-23 15:14:56 -07:00
|
|
|
match ctxt.ast_map.get(doc_id) {
|
2012-01-25 17:22:19 -08:00
|
|
|
ast_map::node_item(@{
|
2012-08-08 17:14:25 -07:00
|
|
|
node: ast::item_enum(enum_definition, _), _
|
2012-08-03 19:59:04 -07:00
|
|
|
}, _) => {
|
2012-09-21 19:37:57 -07:00
|
|
|
let ast_variant =
|
2012-08-08 17:14:25 -07:00
|
|
|
do vec::find(enum_definition.variants) |v| {
|
2012-07-18 16:18:02 -07:00
|
|
|
to_str(v.node.name) == variant.name
|
2012-09-21 19:37:57 -07:00
|
|
|
}.get();
|
2012-01-25 17:22:19 -08:00
|
|
|
|
2012-07-18 16:18:02 -07:00
|
|
|
pprust::variant_to_str(ast_variant, extract::interner())
|
2012-01-25 17:22:19 -08:00
|
|
|
}
|
2012-08-23 15:14:56 -07:00
|
|
|
_ => fail ~"enum variant not bound to an enum item"
|
2012-01-25 17:22:19 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-30 13:05:25 -08:00
|
|
|
{
|
2012-09-04 13:29:32 -07:00
|
|
|
sig: Some(sig),
|
|
|
|
.. variant
|
2012-01-25 17:22:19 -08:00
|
|
|
}
|
2012-09-04 13:29:32 -07:00
|
|
|
},
|
|
|
|
.. doc
|
2012-01-25 17:22:19 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_variant_sigs() {
|
2012-07-13 22:57:48 -07:00
|
|
|
let doc = test::mk_doc(~"enum a { b(int) }");
|
2012-08-20 12:23:37 -07:00
|
|
|
assert doc.cratemod().enums()[0].variants[0].sig == Some(~"b(int)");
|
2012-01-25 17:22:19 -08:00
|
|
|
}
|
2012-01-26 20:59:15 -08:00
|
|
|
|
2012-07-03 16:30:42 -07:00
|
|
|
fn fold_trait(
|
2012-11-19 18:48:46 -08:00
|
|
|
fold: &fold::Fold<astsrv::Srv>,
|
|
|
|
+doc: doc::TraitDoc
|
2012-09-18 16:48:40 -07:00
|
|
|
) -> doc::TraitDoc {
|
2012-01-30 15:44:21 -08:00
|
|
|
{
|
2012-09-04 13:29:32 -07:00
|
|
|
methods: merge_methods(fold.ctxt, doc.id(), doc.methods),
|
|
|
|
.. doc
|
2012-01-30 15:44:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 16:35:54 -08:00
|
|
|
fn merge_methods(
|
2012-09-18 16:48:40 -07:00
|
|
|
srv: astsrv::Srv,
|
|
|
|
item_id: doc::AstId,
|
|
|
|
docs: ~[doc::MethodDoc]
|
|
|
|
) -> ~[doc::MethodDoc] {
|
2012-07-14 03:36:35 +10:00
|
|
|
do par::map(docs) |doc| {
|
2012-01-31 16:35:54 -08:00
|
|
|
{
|
2012-09-04 13:29:32 -07:00
|
|
|
sig: get_method_sig(srv, item_id, doc.name),
|
2012-09-28 17:17:20 -07:00
|
|
|
.. *doc
|
2012-01-31 16:35:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-30 15:44:21 -08:00
|
|
|
fn get_method_sig(
|
2012-09-18 16:48:40 -07:00
|
|
|
srv: astsrv::Srv,
|
|
|
|
item_id: doc::AstId,
|
2012-07-13 22:57:48 -07:00
|
|
|
method_name: ~str
|
2012-08-20 12:23:37 -07:00
|
|
|
) -> Option<~str> {
|
2012-06-30 16:19:07 -07:00
|
|
|
do astsrv::exec(srv) |ctxt| {
|
2012-08-23 15:14:56 -07:00
|
|
|
match ctxt.ast_map.get(item_id) {
|
2012-01-30 15:44:21 -08:00
|
|
|
ast_map::node_item(@{
|
2012-08-03 15:02:01 -07:00
|
|
|
node: ast::item_trait(_, _, methods), _
|
2012-08-03 19:59:04 -07:00
|
|
|
}, _) => {
|
2012-08-23 15:14:56 -07:00
|
|
|
match vec::find(methods, |method| {
|
2012-09-27 22:20:47 -07:00
|
|
|
match *method {
|
2012-07-18 16:18:02 -07:00
|
|
|
ast::required(ty_m) => to_str(ty_m.ident) == method_name,
|
|
|
|
ast::provided(m) => to_str(m.ident) == method_name,
|
2012-07-10 13:44:20 -07:00
|
|
|
}
|
2012-06-26 13:55:56 -07:00
|
|
|
}) {
|
2012-08-20 12:23:37 -07:00
|
|
|
Some(method) => {
|
2012-08-06 12:34:08 -07:00
|
|
|
match method {
|
2012-08-03 19:59:04 -07:00
|
|
|
ast::required(ty_m) => {
|
2012-08-20 12:23:37 -07:00
|
|
|
Some(pprust::fun_to_str(
|
2012-07-10 13:44:20 -07:00
|
|
|
ty_m.decl,
|
|
|
|
ty_m.ident,
|
2012-07-18 16:18:02 -07:00
|
|
|
ty_m.tps,
|
|
|
|
extract::interner()
|
2012-07-10 13:44:20 -07:00
|
|
|
))
|
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
ast::provided(m) => {
|
2012-08-20 12:23:37 -07:00
|
|
|
Some(pprust::fun_to_str(
|
2012-07-10 13:44:20 -07:00
|
|
|
m.decl,
|
|
|
|
m.ident,
|
2012-07-18 16:18:02 -07:00
|
|
|
m.tps,
|
|
|
|
extract::interner()
|
2012-07-10 13:44:20 -07:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
2012-01-30 15:44:21 -08:00
|
|
|
}
|
2012-08-23 15:14:56 -07:00
|
|
|
_ => fail ~"method not found"
|
2012-01-31 16:35:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ast_map::node_item(@{
|
2012-11-13 19:08:01 -08:00
|
|
|
node: ast::item_impl(_, _, _, methods), _
|
2012-08-03 19:59:04 -07:00
|
|
|
}, _) => {
|
2012-11-13 19:08:01 -08:00
|
|
|
match vec::find(methods, |method| {
|
|
|
|
to_str(method.ident) == method_name
|
|
|
|
}) {
|
|
|
|
Some(method) => {
|
|
|
|
Some(pprust::fun_to_str(
|
|
|
|
method.decl,
|
|
|
|
method.ident,
|
|
|
|
method.tps,
|
|
|
|
extract::interner()
|
|
|
|
))
|
2012-01-31 16:35:54 -08:00
|
|
|
}
|
2012-11-13 19:08:01 -08:00
|
|
|
None => fail ~"method not found"
|
2012-01-30 15:44:21 -08:00
|
|
|
}
|
|
|
|
}
|
2012-08-23 15:14:56 -07:00
|
|
|
_ => fail ~"get_method_sig: item ID not bound to trait or impl"
|
2012-01-30 15:44:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-07-03 16:30:42 -07:00
|
|
|
fn should_add_trait_method_sigs() {
|
2012-07-31 10:27:51 -07:00
|
|
|
let doc = test::mk_doc(~"trait i { fn a<T>() -> int; }");
|
2012-07-03 16:30:42 -07:00
|
|
|
assert doc.cratemod().traits()[0].methods[0].sig
|
2012-08-20 12:23:37 -07:00
|
|
|
== Some(~"fn a<T>() -> int");
|
2012-01-30 15:44:21 -08:00
|
|
|
}
|
|
|
|
|
2012-01-31 16:35:54 -08:00
|
|
|
fn fold_impl(
|
2012-11-19 18:48:46 -08:00
|
|
|
fold: &fold::Fold<astsrv::Srv>,
|
|
|
|
+doc: doc::ImplDoc
|
2012-09-18 16:48:40 -07:00
|
|
|
) -> doc::ImplDoc {
|
2012-01-31 16:35:54 -08:00
|
|
|
|
|
|
|
let srv = fold.ctxt;
|
|
|
|
|
2012-07-18 09:31:53 -07:00
|
|
|
let (trait_types, self_ty) = do astsrv::exec(srv) |ctxt| {
|
2012-08-06 12:34:08 -07:00
|
|
|
match ctxt.ast_map.get(doc.id()) {
|
2012-01-31 16:35:54 -08:00
|
|
|
ast_map::node_item(@{
|
2012-09-07 15:11:26 -07:00
|
|
|
node: ast::item_impl(_, opt_trait_type, self_ty, _), _
|
2012-08-03 19:59:04 -07:00
|
|
|
}, _) => {
|
2012-09-07 15:11:26 -07:00
|
|
|
let trait_types = opt_trait_type.map_default(~[], |p| {
|
|
|
|
~[pprust::path_to_str(p.path, extract::interner())]
|
2012-06-26 13:55:56 -07:00
|
|
|
});
|
2012-08-20 12:23:37 -07:00
|
|
|
(trait_types, Some(pprust::ty_to_str(self_ty,
|
2012-07-18 16:18:02 -07:00
|
|
|
extract::interner())))
|
2012-01-31 16:35:54 -08:00
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
_ => fail ~"expected impl"
|
2012-01-31 16:35:54 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
2012-07-18 09:31:53 -07:00
|
|
|
trait_types: trait_types,
|
2012-01-31 16:35:54 -08:00
|
|
|
self_ty: self_ty,
|
2012-09-04 13:29:32 -07:00
|
|
|
methods: merge_methods(fold.ctxt, doc.id(), doc.methods),
|
|
|
|
.. doc
|
2012-01-31 16:35:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-07-18 09:31:53 -07:00
|
|
|
fn should_add_impl_trait_types() {
|
2012-08-08 17:19:06 -07:00
|
|
|
let doc = test::mk_doc(~"impl int: j { fn a<T>() { } }");
|
2012-07-18 09:31:53 -07:00
|
|
|
assert doc.cratemod().impls()[0].trait_types[0] == ~"j";
|
2012-01-31 16:35:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-07-18 09:31:53 -07:00
|
|
|
fn should_not_add_impl_trait_types_if_none() {
|
2012-08-08 17:19:06 -07:00
|
|
|
let doc = test::mk_doc(~"impl int { fn a() { } }");
|
2012-07-18 09:31:53 -07:00
|
|
|
assert vec::len(doc.cratemod().impls()[0].trait_types) == 0;
|
2012-01-31 16:35:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_impl_self_ty() {
|
2012-08-08 17:19:06 -07:00
|
|
|
let doc = test::mk_doc(~"impl int { fn a() { } }");
|
2012-08-20 12:23:37 -07:00
|
|
|
assert doc.cratemod().impls()[0].self_ty == Some(~"int");
|
2012-01-31 16:35:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_impl_method_sigs() {
|
2012-08-08 17:19:06 -07:00
|
|
|
let doc = test::mk_doc(~"impl int { fn a<T>() -> int { fail } }");
|
2012-03-18 21:30:45 -07:00
|
|
|
assert doc.cratemod().impls()[0].methods[0].sig
|
2012-08-20 12:23:37 -07:00
|
|
|
== Some(~"fn a<T>() -> int");
|
2012-01-31 16:35:54 -08:00
|
|
|
}
|
|
|
|
|
2012-02-01 22:41:41 -08:00
|
|
|
fn fold_type(
|
2012-11-19 18:48:46 -08:00
|
|
|
fold: &fold::Fold<astsrv::Srv>,
|
|
|
|
+doc: doc::TyDoc
|
2012-09-18 16:48:40 -07:00
|
|
|
) -> doc::TyDoc {
|
2012-02-01 22:41:41 -08:00
|
|
|
|
|
|
|
let srv = fold.ctxt;
|
|
|
|
|
|
|
|
{
|
2012-06-30 16:19:07 -07:00
|
|
|
sig: do astsrv::exec(srv) |ctxt| {
|
2012-08-06 12:34:08 -07:00
|
|
|
match ctxt.ast_map.get(doc.id()) {
|
2012-02-01 22:41:41 -08:00
|
|
|
ast_map::node_item(@{
|
|
|
|
ident: ident,
|
2012-07-11 10:28:30 -07:00
|
|
|
node: ast::item_ty(ty, params), _
|
2012-08-03 19:59:04 -07:00
|
|
|
}, _) => {
|
2012-08-20 12:23:37 -07:00
|
|
|
Some(fmt!(
|
2012-02-01 22:41:41 -08:00
|
|
|
"type %s%s = %s",
|
2012-07-18 16:18:02 -07:00
|
|
|
to_str(ident),
|
|
|
|
pprust::typarams_to_str(params, extract::interner()),
|
|
|
|
pprust::ty_to_str(ty, extract::interner())
|
2012-08-22 17:24:52 -07:00
|
|
|
))
|
2012-02-01 22:41:41 -08:00
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
_ => fail ~"expected type"
|
2012-02-01 22:41:41 -08:00
|
|
|
}
|
2012-09-04 13:29:32 -07:00
|
|
|
},
|
|
|
|
.. doc
|
2012-02-01 22:41:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_add_type_signatures() {
|
2012-07-13 22:57:48 -07:00
|
|
|
let doc = test::mk_doc(~"type t<T> = int;");
|
2012-08-20 12:23:37 -07:00
|
|
|
assert doc.cratemod().types()[0].sig == Some(~"type t<T> = int");
|
2012-02-01 22:41:41 -08:00
|
|
|
}
|
|
|
|
|
2012-09-19 14:37:43 -07:00
|
|
|
fn fold_struct(
|
2012-11-19 18:48:46 -08:00
|
|
|
fold: &fold::Fold<astsrv::Srv>,
|
|
|
|
+doc: doc::StructDoc
|
2012-09-19 14:37:43 -07:00
|
|
|
) -> doc::StructDoc {
|
|
|
|
let srv = fold.ctxt;
|
|
|
|
|
|
|
|
{
|
|
|
|
sig: do astsrv::exec(srv) |ctxt| {
|
|
|
|
match ctxt.ast_map.get(doc.id()) {
|
|
|
|
ast_map::node_item(item, _) => {
|
2012-09-20 15:14:20 -07:00
|
|
|
let item = strip_struct_extra_stuff(item);
|
2012-09-19 14:37:43 -07:00
|
|
|
Some(pprust::item_to_str(item,
|
|
|
|
extract::interner()))
|
|
|
|
}
|
|
|
|
_ => fail ~"not an item"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.. doc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-20 15:14:20 -07:00
|
|
|
/// Removes various things from the struct item definition that
|
|
|
|
/// shouldn't be displayed in the struct signature. Probably there
|
|
|
|
/// should be a simple pprust::struct_to_str function that does
|
|
|
|
/// what I actually want
|
|
|
|
fn strip_struct_extra_stuff(item: @ast::item) -> @ast::item {
|
2012-09-20 14:40:55 -07:00
|
|
|
let node = match item.node {
|
2012-12-10 13:47:54 -08:00
|
|
|
ast::item_struct(def, tys) => {
|
2012-09-20 14:40:55 -07:00
|
|
|
let def = @{
|
2012-09-20 15:14:20 -07:00
|
|
|
dtor: None, // Remove the drop { } block
|
2012-09-20 14:40:55 -07:00
|
|
|
.. *def
|
|
|
|
};
|
2012-12-10 13:47:54 -08:00
|
|
|
ast::item_struct(def, tys)
|
2012-09-20 14:40:55 -07:00
|
|
|
}
|
|
|
|
_ => fail ~"not a struct"
|
|
|
|
};
|
|
|
|
|
|
|
|
@{
|
2012-09-20 15:14:20 -07:00
|
|
|
attrs: ~[], // Remove the attributes
|
2012-09-20 14:40:55 -07:00
|
|
|
node: node,
|
|
|
|
.. *item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 14:37:43 -07:00
|
|
|
#[test]
|
|
|
|
fn should_add_struct_defs() {
|
|
|
|
let doc = test::mk_doc(~"struct S { field: () }");
|
|
|
|
assert doc.cratemod().structs()[0].sig.get().contains("struct S {");
|
|
|
|
}
|
|
|
|
|
2012-09-20 14:40:55 -07:00
|
|
|
#[test]
|
|
|
|
fn should_not_serialize_struct_drop_blocks() {
|
|
|
|
// All we care about are the fields
|
|
|
|
let doc = test::mk_doc(~"struct S { field: (), drop { } }");
|
|
|
|
assert !doc.cratemod().structs()[0].sig.get().contains("drop");
|
|
|
|
}
|
|
|
|
|
2012-09-20 15:14:20 -07:00
|
|
|
#[test]
|
|
|
|
fn should_not_serialize_struct_attrs() {
|
|
|
|
// All we care about are the fields
|
|
|
|
let doc = test::mk_doc(~"#[wut] struct S { field: () }");
|
|
|
|
assert !doc.cratemod().structs()[0].sig.get().contains("wut");
|
|
|
|
}
|
|
|
|
|
2012-01-31 18:32:37 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2012-09-21 18:10:45 -07:00
|
|
|
#[legacy_exports];
|
2012-09-18 16:48:40 -07:00
|
|
|
fn mk_doc(source: ~str) -> doc::Doc {
|
2012-06-30 16:19:07 -07:00
|
|
|
do astsrv::from_str(source) |srv| {
|
2012-07-13 22:57:48 -07:00
|
|
|
let doc = extract::from_srv(srv, ~"");
|
2012-02-20 21:08:19 -08:00
|
|
|
run(srv, doc)
|
|
|
|
}
|
2012-01-31 18:32:37 -08:00
|
|
|
}
|
2012-01-30 11:52:34 +01:00
|
|
|
}
|