diff options
| author | bors <bors@rust-lang.org> | 2015-05-24 11:12:34 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-05-24 11:12:34 +0000 |
| commit | ba0e1cd8147d452c356aacb29fb87568ca26f111 (patch) | |
| tree | 0cf03636be9210de94e30bb07467097aeb5273c7 /src/libsyntax | |
| parent | cc56c20ba4ccc12f58e1c844b77168bc4805cbb6 (diff) | |
| parent | 82ded3cd03e001e82b9dce9aedac621a5292231b (diff) | |
| download | rust-ba0e1cd8147d452c356aacb29fb87568ca26f111.tar.gz rust-ba0e1cd8147d452c356aacb29fb87568ca26f111.zip | |
Auto merge of #25609 - nikomatsakis:const-fn, r=pnkfelix
This is a port of @eddyb's `const-fn` branch. I rebased it, tweaked a few things, and added tests as well as a feature gate. The set of tests is still pretty rudimentary, I'd appreciate suggestions on new tests to write. Also, a double-check that the feature-gate covers all necessary cases. One question: currently, the feature-gate allows the *use* of const functions from stable code, just not the definition. This seems to fit our usual strategy, and implies that we might (perhaps) allow some constant functions in libstd someday, even before stabilizing const-fn, if we were willing to commit to the existence of const fns but found some details of their impl unsatisfactory. r? @pnkfelix
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 9 | ||||
| -rw-r--r-- | src/libsyntax/ast_map/blocks.rs | 55 | ||||
| -rw-r--r-- | src/libsyntax/ast_util.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/ext/build.rs | 1 | ||||
| -rw-r--r-- | src/libsyntax/ext/deriving/generic/mod.rs | 1 | ||||
| -rw-r--r-- | src/libsyntax/ext/expand.rs | 6 | ||||
| -rw-r--r-- | src/libsyntax/feature_gate.rs | 30 | ||||
| -rw-r--r-- | src/libsyntax/fold.rs | 4 | ||||
| -rw-r--r-- | src/libsyntax/parse/mod.rs | 1 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 87 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 43 | ||||
| -rw-r--r-- | src/libsyntax/test.rs | 10 | ||||
| -rw-r--r-- | src/libsyntax/visit.rs | 9 |
13 files changed, 190 insertions, 68 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index e00cb82649b..5b03b3bf038 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1215,6 +1215,7 @@ pub struct TypeField { #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub struct MethodSig { pub unsafety: Unsafety, + pub constness: Constness, pub abi: Abi, pub decl: P<FnDecl>, pub generics: Generics, @@ -1475,6 +1476,12 @@ pub enum Unsafety { Normal, } +#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] +pub enum Constness { + Const, + NotConst, +} + impl fmt::Display for Unsafety { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(match *self { @@ -1761,7 +1768,7 @@ pub enum Item_ { /// A `const` item ItemConst(P<Ty>, P<Expr>), /// A function declaration - ItemFn(P<FnDecl>, Unsafety, Abi, Generics, P<Block>), + ItemFn(P<FnDecl>, Unsafety, Constness, Abi, Generics, P<Block>), /// A module ItemMod(Mod), /// An external module diff --git a/src/libsyntax/ast_map/blocks.rs b/src/libsyntax/ast_map/blocks.rs index 36a7f3a9381..99686d54ce5 100644 --- a/src/libsyntax/ast_map/blocks.rs +++ b/src/libsyntax/ast_map/blocks.rs @@ -96,20 +96,10 @@ impl<'a> Code<'a> { /// Attempts to construct a Code from presumed FnLike or Block node input. pub fn from_node(node: Node) -> Option<Code> { - fn new(node: Node) -> FnLikeNode { FnLikeNode { node: node } } - match node { - ast_map::NodeItem(item) if item.is_fn_like() => - Some(FnLikeCode(new(node))), - ast_map::NodeTraitItem(tm) if tm.is_fn_like() => - Some(FnLikeCode(new(node))), - ast_map::NodeImplItem(_) => - Some(FnLikeCode(new(node))), - ast_map::NodeExpr(e) if e.is_fn_like() => - Some(FnLikeCode(new(node))), - ast_map::NodeBlock(block) => - Some(BlockCode(block)), - _ => - None, + if let ast_map::NodeBlock(block) = node { + Some(BlockCode(block)) + } else { + FnLikeNode::from_node(node).map(|fn_like| FnLikeCode(fn_like)) } } } @@ -120,6 +110,7 @@ struct ItemFnParts<'a> { ident: ast::Ident, decl: &'a ast::FnDecl, unsafety: ast::Unsafety, + constness: ast::Constness, abi: abi::Abi, vis: ast::Visibility, generics: &'a ast::Generics, @@ -144,6 +135,24 @@ impl<'a> ClosureParts<'a> { } impl<'a> FnLikeNode<'a> { + /// Attempts to construct a FnLikeNode from presumed FnLike node input. + pub fn from_node(node: Node) -> Option<FnLikeNode> { + let fn_like = match node { + ast_map::NodeItem(item) => item.is_fn_like(), + ast_map::NodeTraitItem(tm) => tm.is_fn_like(), + ast_map::NodeImplItem(_) => true, + ast_map::NodeExpr(e) => e.is_fn_like(), + _ => false + }; + if fn_like { + Some(FnLikeNode { + node: node + }) + } else { + None + } + } + pub fn to_fn_parts(self) -> FnParts<'a> { FnParts { decl: self.decl(), @@ -180,7 +189,7 @@ impl<'a> FnLikeNode<'a> { pub fn kind(self) -> visit::FnKind<'a> { let item = |p: ItemFnParts<'a>| -> visit::FnKind<'a> { - visit::FkItemFn(p.ident, p.generics, p.unsafety, p.abi, p.vis) + visit::FkItemFn(p.ident, p.generics, p.unsafety, p.constness, p.abi, p.vis) }; let closure = |_: ClosureParts| { visit::FkFnBlock @@ -204,10 +213,18 @@ impl<'a> FnLikeNode<'a> { { match self.node { ast_map::NodeItem(i) => match i.node { - ast::ItemFn(ref decl, unsafety, abi, ref generics, ref block) => - item_fn(ItemFnParts{ - ident: i.ident, decl: &**decl, unsafety: unsafety, body: &**block, - generics: generics, abi: abi, vis: i.vis, id: i.id, span: i.span + ast::ItemFn(ref decl, unsafety, constness, abi, ref generics, ref block) => + item_fn(ItemFnParts { + id: i.id, + ident: i.ident, + decl: &**decl, + unsafety: unsafety, + body: &**block, + generics: generics, + abi: abi, + vis: i.vis, + constness: constness, + span: i.span }), _ => panic!("item FnLikeNode that is not fn-like"), }, diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index 8471fef3487..bb8096f2770 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -440,7 +440,7 @@ impl<'a, 'v, O: IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> { self.operation.visit_id(node_id); match function_kind { - visit::FkItemFn(_, generics, _, _, _) => { + visit::FkItemFn(_, generics, _, _, _, _) => { self.visit_generics_helper(generics) } visit::FkMethod(_, sig, _) => { diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index 354a0bff749..8a80e291a53 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -1034,6 +1034,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { Vec::new(), ast::ItemFn(self.fn_decl(inputs, output), ast::Unsafety::Normal, + ast::Constness::NotConst, abi::Rust, generics, body)) diff --git a/src/libsyntax/ext/deriving/generic/mod.rs b/src/libsyntax/ext/deriving/generic/mod.rs index b9968ca9608..eab6c3ae725 100644 --- a/src/libsyntax/ext/deriving/generic/mod.rs +++ b/src/libsyntax/ext/deriving/generic/mod.rs @@ -880,6 +880,7 @@ impl<'a> MethodDef<'a> { abi: abi, explicit_self: explicit_self, unsafety: unsafety, + constness: ast::Constness::NotConst, decl: fn_decl }, body_block) }) diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 744849de445..aa74c27dc61 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -513,11 +513,12 @@ pub fn expand_item(it: P<ast::Item>, fld: &mut MacroExpander) /// Expand item_underscore fn expand_item_underscore(item: ast::Item_, fld: &mut MacroExpander) -> ast::Item_ { match item { - ast::ItemFn(decl, fn_style, abi, generics, body) => { + ast::ItemFn(decl, unsafety, constness, abi, generics, body) => { let (rewritten_fn_decl, rewritten_body) = expand_and_rename_fn_decl_and_block(decl, body, fld); let expanded_generics = fold::noop_fold_generics(generics,fld); - ast::ItemFn(rewritten_fn_decl, fn_style, abi, expanded_generics, rewritten_body) + ast::ItemFn(rewritten_fn_decl, unsafety, constness, abi, + expanded_generics, rewritten_body) } _ => noop_fold_item_underscore(item, fld) } @@ -1395,6 +1396,7 @@ fn expand_and_rename_method(sig: ast::MethodSig, body: P<ast::Block>, abi: sig.abi, explicit_self: fld.fold_explicit_self(sig.explicit_self), unsafety: sig.unsafety, + constness: sig.constness, decl: rewritten_fn_decl }, rewritten_body) } diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index bd73e6b7de7..1177ebdc531 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -155,6 +155,9 @@ const KNOWN_FEATURES: &'static [(&'static str, &'static str, Status)] = &[ // Allows the definition of associated constants in `trait` or `impl` // blocks. ("associated_consts", "1.0.0", Active), + + // Allows the definition of `const fn` functions. + ("const_fn", "1.2.0", Active), ]; // (changing above list without updating src/doc/reference.md makes @cmr sad) @@ -656,13 +659,26 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> { block: &'v ast::Block, span: Span, _node_id: NodeId) { + // check for const fn declarations + match fn_kind { + visit::FkItemFn(_, _, _, ast::Constness::Const, _, _) => { + self.gate_feature("const_fn", span, "const fn is unstable"); + } + _ => { + // stability of const fn methods are covered in + // visit_trait_item and visit_impl_item below; this is + // because default methods don't pass through this + // point. + } + } + match fn_kind { - visit::FkItemFn(_, _, _, abi, _) if abi == Abi::RustIntrinsic => { + visit::FkItemFn(_, _, _, _, abi, _) if abi == Abi::RustIntrinsic => { self.gate_feature("intrinsics", span, "intrinsics are subject to change") } - visit::FkItemFn(_, _, _, abi, _) | + visit::FkItemFn(_, _, _, _, abi, _) | visit::FkMethod(_, &ast::MethodSig { abi, .. }, _) if abi == Abi::RustCall => { self.gate_feature("unboxed_closures", span, @@ -680,6 +696,11 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> { ti.span, "associated constants are experimental") } + ast::MethodTraitItem(ref sig, _) => { + if sig.constness == ast::Constness::Const { + self.gate_feature("const_fn", ti.span, "const fn is unstable"); + } + } _ => {} } visit::walk_trait_item(self, ti); @@ -692,6 +713,11 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> { ii.span, "associated constants are experimental") } + ast::MethodImplItem(ref sig, _) => { + if sig.constness == ast::Constness::Const { + self.gate_feature("const_fn", ii.span, "const fn is unstable"); + } + } _ => {} } visit::walk_impl_item(self, ii); diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index 4bf15f509a0..7806a27c53e 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -917,10 +917,11 @@ pub fn noop_fold_item_underscore<T: Folder>(i: Item_, folder: &mut T) -> Item_ { ItemConst(t, e) => { ItemConst(folder.fold_ty(t), folder.fold_expr(e)) } - ItemFn(decl, unsafety, abi, generics, body) => { + ItemFn(decl, unsafety, constness, abi, generics, body) => { ItemFn( folder.fold_fn_decl(decl), unsafety, + constness, abi, folder.fold_generics(generics), folder.fold_block(body) @@ -1124,6 +1125,7 @@ pub fn noop_fold_method_sig<T: Folder>(sig: MethodSig, folder: &mut T) -> Method abi: sig.abi, explicit_self: folder.fold_explicit_self(sig.explicit_self), unsafety: sig.unsafety, + constness: sig.constness, decl: folder.fold_fn_decl(sig.decl) } } diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 68574560533..d6c28d41447 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -923,6 +923,7 @@ mod tests { variadic: false }), ast::Unsafety::Normal, + ast::Constness::NotConst, abi::Rust, ast::Generics{ // no idea on either of these: lifetimes: Vec::new(), diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 2b6f91bbc99..eb6420165da 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -17,7 +17,7 @@ use ast::{Public, Unsafety}; use ast::{Mod, BiAdd, Arg, Arm, Attribute, BindByRef, BindByValue}; use ast::{BiBitAnd, BiBitOr, BiBitXor, BiRem, BiLt, BiGt, Block}; use ast::{BlockCheckMode, CaptureByRef, CaptureByValue, CaptureClause}; -use ast::{ConstImplItem, ConstTraitItem, Crate, CrateConfig}; +use ast::{Constness, ConstImplItem, ConstTraitItem, Crate, CrateConfig}; use ast::{Decl, DeclItem, DeclLocal, DefaultBlock, DefaultReturn}; use ast::{UnDeref, BiDiv, EMPTY_CTXT, EnumDef, ExplicitSelf}; use ast::{Expr, Expr_, ExprAddrOf, ExprMatch, ExprAgain}; @@ -1160,7 +1160,8 @@ impl<'a> Parser<'a> { let TyParam {ident, bounds, default, ..} = try!(p.parse_ty_param()); try!(p.expect(&token::Semi)); (ident, TypeTraitItem(bounds, default)) - } else if try!(p.eat_keyword(keywords::Const)) { + } else if p.is_const_item() { + try!(p.expect_keyword(keywords::Const)); let ident = try!(p.parse_ident()); try!(p.expect(&token::Colon)); let ty = try!(p.parse_ty_sum()); @@ -1175,13 +1176,7 @@ impl<'a> Parser<'a> { }; (ident, ConstTraitItem(ty, default)) } else { - let style = try!(p.parse_unsafety()); - let abi = if try!(p.eat_keyword(keywords::Extern)) { - try!(p.parse_opt_abi()).unwrap_or(abi::C) - } else { - abi::Rust - }; - try!(p.expect_keyword(keywords::Fn)); + let (constness, unsafety, abi) = try!(p.parse_fn_front_matter()); let ident = try!(p.parse_ident()); let mut generics = try!(p.parse_generics()); @@ -1195,7 +1190,8 @@ impl<'a> Parser<'a> { generics.where_clause = try!(p.parse_where_clause()); let sig = ast::MethodSig { - unsafety: style, + unsafety: unsafety, + constness: constness, decl: d, generics: generics, abi: abi, @@ -4359,12 +4355,46 @@ impl<'a> Parser<'a> { } /// Parse an item-position function declaration. - fn parse_item_fn(&mut self, unsafety: Unsafety, abi: abi::Abi) -> PResult<ItemInfo> { + fn parse_item_fn(&mut self, + unsafety: Unsafety, + constness: Constness, + abi: abi::Abi) + -> PResult<ItemInfo> { let (ident, mut generics) = try!(self.parse_fn_header()); let decl = try!(self.parse_fn_decl(false)); generics.where_clause = try!(self.parse_where_clause()); let (inner_attrs, body) = try!(self.parse_inner_attrs_and_block()); - Ok((ident, ItemFn(decl, unsafety, abi, generics, body), Some(inner_attrs))) + Ok((ident, ItemFn(decl, unsafety, constness, abi, generics, body), Some(inner_attrs))) + } + + /// true if we are looking at `const ID`, false for things like `const fn` etc + pub fn is_const_item(&mut self) -> bool { + self.token.is_keyword(keywords::Const) && + !self.look_ahead(1, |t| t.is_keyword(keywords::Fn)) + } + + /// parses all the "front matter" for a `fn` declaration, up to + /// and including the `fn` keyword: + /// + /// - `const fn` + /// - `unsafe fn` + /// - `extern fn` + /// - etc + pub fn parse_fn_front_matter(&mut self) -> PResult<(ast::Constness, ast::Unsafety, abi::Abi)> { + let is_const_fn = try!(self.eat_keyword(keywords::Const)); + let (constness, unsafety, abi) = if is_const_fn { + (Constness::Const, Unsafety::Normal, abi::Rust) + } else { + let unsafety = try!(self.parse_unsafety()); + let abi = if try!(self.eat_keyword(keywords::Extern)) { + try!(self.parse_opt_abi()).unwrap_or(abi::C) + } else { + abi::Rust + }; + (Constness::NotConst, unsafety, abi) + }; + try!(self.expect_keyword(keywords::Fn)); + Ok((constness, unsafety, abi)) } /// Parse an impl item. @@ -4380,7 +4410,8 @@ impl<'a> Parser<'a> { let typ = try!(self.parse_ty_sum()); try!(self.expect(&token::Semi)); (name, TypeImplItem(typ)) - } else if try!(self.eat_keyword(keywords::Const)) { + } else if self.is_const_item() { + try!(self.expect_keyword(keywords::Const)); let name = try!(self.parse_ident()); try!(self.expect(&token::Colon)); let typ = try!(self.parse_ty_sum()); @@ -4445,13 +4476,7 @@ impl<'a> Parser<'a> { } Ok((token::special_idents::invalid, vec![], ast::MacImplItem(m))) } else { - let unsafety = try!(self.parse_unsafety()); - let abi = if try!(self.eat_keyword(keywords::Extern)) { - try!(self.parse_opt_abi()).unwrap_or(abi::C) - } else { - abi::Rust - }; - try!(self.expect_keyword(keywords::Fn)); + let (constness, unsafety, abi) = try!(self.parse_fn_front_matter()); let ident = try!(self.parse_ident()); let mut generics = try!(self.parse_generics()); let (explicit_self, decl) = try!(self.parse_fn_decl_with_self(|p| { @@ -4464,6 +4489,7 @@ impl<'a> Parser<'a> { abi: abi, explicit_self: explicit_self, unsafety: unsafety, + constness: constness, decl: decl }, body))) } @@ -5252,7 +5278,7 @@ impl<'a> Parser<'a> { // EXTERN FUNCTION ITEM let abi = opt_abi.unwrap_or(abi::C); let (ident, item_, extra_attrs) = - try!(self.parse_item_fn(Unsafety::Normal, abi)); + try!(self.parse_item_fn(Unsafety::Normal, Constness::NotConst, abi)); let last_span = self.last_span; let item = self.mk_item(lo, last_span.hi, @@ -5287,6 +5313,21 @@ impl<'a> Parser<'a> { return Ok(Some(item)); } if try!(self.eat_keyword(keywords::Const) ){ + if self.check_keyword(keywords::Fn) { + // CONST FUNCTION ITEM + try!(self.bump()); + let (ident, item_, extra_attrs) = + try!(self.parse_item_fn(Unsafety::Normal, Constness::Const, abi::Rust)); + let last_span = self.last_span; + let item = self.mk_item(lo, + last_span.hi, + ident, + item_, + visibility, + maybe_append(attrs, extra_attrs)); + return Ok(Some(item)); + } + // CONST ITEM if try!(self.eat_keyword(keywords::Mut) ){ let last_span = self.last_span; @@ -5340,7 +5381,7 @@ impl<'a> Parser<'a> { // FUNCTION ITEM try!(self.bump()); let (ident, item_, extra_attrs) = - try!(self.parse_item_fn(Unsafety::Normal, abi::Rust)); + try!(self.parse_item_fn(Unsafety::Normal, Constness::NotConst, abi::Rust)); let last_span = self.last_span; let item = self.mk_item(lo, last_span.hi, @@ -5361,7 +5402,7 @@ impl<'a> Parser<'a> { }; try!(self.expect_keyword(keywords::Fn)); let (ident, item_, extra_attrs) = - try!(self.parse_item_fn(Unsafety::Unsafe, abi)); + try!(self.parse_item_fn(Unsafety::Unsafe, Constness::NotConst, abi)); let last_span = self.last_span; let item = self.mk_item(lo, last_span.hi, diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index b71d65a8fb0..8958370fda5 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -378,12 +378,16 @@ pub fn ident_to_string(id: &ast::Ident) -> String { to_string(|s| s.print_ident(*id)) } -pub fn fun_to_string(decl: &ast::FnDecl, unsafety: ast::Unsafety, name: ast::Ident, - opt_explicit_self: Option<&ast::ExplicitSelf_>, - generics: &ast::Generics) -> String { +pub fn fun_to_string(decl: &ast::FnDecl, + unsafety: ast::Unsafety, + constness: ast::Constness, + name: ast::Ident, + opt_explicit_self: Option<&ast::ExplicitSelf_>, + generics: &ast::Generics) + -> String { to_string(|s| { try!(s.head("")); - try!(s.print_fn(decl, unsafety, abi::Rust, Some(name), + try!(s.print_fn(decl, unsafety, constness, abi::Rust, Some(name), generics, opt_explicit_self, ast::Inherited)); try!(s.end()); // Close the head box s.end() // Close the outer box @@ -740,7 +744,8 @@ impl<'a> State<'a> { match item.node { ast::ForeignItemFn(ref decl, ref generics) => { try!(self.head("")); - try!(self.print_fn(&**decl, ast::Unsafety::Normal, + try!(self.print_fn(decl, ast::Unsafety::Normal, + ast::Constness::NotConst, abi::Rust, Some(item.ident), generics, None, item.vis)); try!(self.end()); // end head-ibox @@ -866,11 +871,12 @@ impl<'a> State<'a> { try!(word(&mut self.s, ";")); try!(self.end()); // end the outer cbox } - ast::ItemFn(ref decl, unsafety, abi, ref typarams, ref body) => { + ast::ItemFn(ref decl, unsafety, constness, abi, ref typarams, ref body) => { try!(self.head("")); try!(self.print_fn( decl, unsafety, + constness, abi, Some(item.ident), typarams, @@ -1241,6 +1247,7 @@ impl<'a> State<'a> { -> io::Result<()> { self.print_fn(&m.decl, m.unsafety, + m.constness, m.abi, Some(ident), &m.generics, @@ -2335,12 +2342,13 @@ impl<'a> State<'a> { pub fn print_fn(&mut self, decl: &ast::FnDecl, unsafety: ast::Unsafety, + constness: ast::Constness, abi: abi::Abi, name: Option<ast::Ident>, generics: &ast::Generics, opt_explicit_self: Option<&ast::ExplicitSelf_>, vis: ast::Visibility) -> io::Result<()> { - try!(self.print_fn_header_info(unsafety, abi, vis)); + try!(self.print_fn_header_info(unsafety, constness, abi, vis)); if let Some(name) = name { try!(self.nbsp()); @@ -2726,8 +2734,13 @@ impl<'a> State<'a> { predicates: Vec::new(), }, }; - try!(self.print_fn(decl, unsafety, abi, name, - &generics, opt_explicit_self, + try!(self.print_fn(decl, + unsafety, + ast::Constness::NotConst, + abi, + name, + &generics, + opt_explicit_self, ast::Inherited)); self.end() } @@ -2976,11 +2989,17 @@ impl<'a> State<'a> { pub fn print_fn_header_info(&mut self, unsafety: ast::Unsafety, + constness: ast::Constness, abi: abi::Abi, vis: ast::Visibility) -> io::Result<()> { try!(word(&mut self.s, &visibility_qualified(vis, ""))); try!(self.print_unsafety(unsafety)); + match constness { + ast::Constness::NotConst => {} + ast::Constness::Const => try!(self.word_nbsp("const")) + } + if abi != abi::Rust { try!(self.word_nbsp("extern")); try!(self.word_nbsp(&abi.to_string())); @@ -3018,8 +3037,10 @@ mod tests { variadic: false }; let generics = ast_util::empty_generics(); - assert_eq!(fun_to_string(&decl, ast::Unsafety::Normal, abba_ident, - None, &generics), + assert_eq!(fun_to_string(&decl, ast::Unsafety::Normal, + ast::Constness::NotConst, + abba_ident, + None, &generics), "fn abba()"); } diff --git a/src/libsyntax/test.rs b/src/libsyntax/test.rs index 458e3f7f0bd..c680d5bbbdf 100644 --- a/src/libsyntax/test.rs +++ b/src/libsyntax/test.rs @@ -123,7 +123,7 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> { let i = if is_test_fn(&self.cx, &*i) || is_bench_fn(&self.cx, &*i) { match i.node { - ast::ItemFn(_, ast::Unsafety::Unsafe, _, _, _) => { + ast::ItemFn(_, ast::Unsafety::Unsafe, _, _, _, _) => { let diag = self.cx.span_diagnostic; panic!(diag.span_fatal(i.span, "unsafe functions cannot be used for tests")); } @@ -320,7 +320,7 @@ fn is_test_fn(cx: &TestCtxt, i: &ast::Item) -> bool { fn has_test_signature(i: &ast::Item) -> HasTestSignature { match &i.node { - &ast::ItemFn(ref decl, _, _, ref generics, _) => { + &ast::ItemFn(ref decl, _, _, _, ref generics, _) => { let no_output = match decl.output { ast::DefaultReturn(..) => true, ast::Return(ref t) if t.node == ast::TyTup(vec![]) => true, @@ -356,7 +356,7 @@ fn is_bench_fn(cx: &TestCtxt, i: &ast::Item) -> bool { fn has_test_signature(i: &ast::Item) -> bool { match i.node { - ast::ItemFn(ref decl, _, _, ref generics, _) => { + ast::ItemFn(ref decl, _, _, _, ref generics, _) => { let input_cnt = decl.inputs.len(); let no_output = match decl.output { ast::DefaultReturn(..) => true, @@ -469,7 +469,9 @@ fn mk_main(cx: &mut TestCtxt) -> P<ast::Item> { let main_ret_ty = ecx.ty(sp, ast::TyTup(vec![])); let main_body = ecx.block_all(sp, vec![call_test_main], None); let main = ast::ItemFn(ecx.fn_decl(vec![], main_ret_ty), - ast::Unsafety::Normal, ::abi::Rust, empty_generics(), main_body); + ast::Unsafety::Normal, + ast::Constness::NotConst, + ::abi::Rust, empty_generics(), main_body); let main = P(ast::Item { ident: token::str_to_ident("main"), attrs: vec![main_attr], diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 6cf791b10be..61fddd6bed8 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -35,7 +35,7 @@ use owned_slice::OwnedSlice; #[derive(Copy, Clone)] pub enum FnKind<'a> { /// fn foo() or extern "Abi" fn foo() - FkItemFn(Ident, &'a Generics, Unsafety, Abi, Visibility), + FkItemFn(Ident, &'a Generics, Unsafety, Constness, Abi, Visibility), /// fn foo(&self) FkMethod(Ident, &'a MethodSig, Option<Visibility>), @@ -246,8 +246,9 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) { visitor.visit_ty(&**typ); visitor.visit_expr(&**expr); } - ItemFn(ref declaration, fn_style, abi, ref generics, ref body) => { - visitor.visit_fn(FkItemFn(item.ident, generics, fn_style, abi, item.vis), + ItemFn(ref declaration, unsafety, constness, abi, ref generics, ref body) => { + visitor.visit_fn(FkItemFn(item.ident, generics, unsafety, + constness, abi, item.vis), &**declaration, &**body, item.span, @@ -604,7 +605,7 @@ pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V, walk_fn_decl(visitor, function_declaration); match function_kind { - FkItemFn(_, generics, _, _, _) => { + FkItemFn(_, generics, _, _, _, _) => { visitor.visit_generics(generics); } FkMethod(_, sig, _) => { |
