Skip to content

Commit

Permalink
syntax: parse const fn for free functions and inherent methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb authored and nikomatsakis committed May 21, 2015
1 parent bc6318d commit af37957
Show file tree
Hide file tree
Showing 34 changed files with 218 additions and 88 deletions.
4 changes: 2 additions & 2 deletions src/librustc/metadata/encoder.rs
Expand Up @@ -1049,7 +1049,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
encode_stability(rbml_w, stab);
rbml_w.end_tag();
}
ast::ItemFn(ref decl, _, _, ref generics, _) => {
ast::ItemFn(ref decl, _, _, _, ref generics, _) => {
add_to_index(item, rbml_w, index);
rbml_w.start_tag(tag_items_data_item);
encode_def_id(rbml_w, def_id);
Expand Down Expand Up @@ -1967,7 +1967,7 @@ fn encode_reachable_extern_fns(ecx: &EncodeContext, rbml_w: &mut Encoder) {

for id in ecx.reachable {
if let Some(ast_map::NodeItem(i)) = ecx.tcx.map.find(*id) {
if let ast::ItemFn(_, _, abi, ref generics, _) = i.node {
if let ast::ItemFn(_, _, _, abi, ref generics, _) = i.node {
if abi != abi::Rust && !generics.is_type_parameterized() {
rbml_w.wr_tagged_u32(tag_reachable_extern_fn_id, *id);
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/effect.rs
Expand Up @@ -87,8 +87,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EffectCheckVisitor<'a, 'tcx> {
block: &'v ast::Block, span: Span, _: ast::NodeId) {

let (is_item_fn, is_unsafe_fn) = match fn_kind {
visit::FkItemFn(_, _, fn_style, _, _) =>
(true, fn_style == ast::Unsafety::Unsafe),
visit::FkItemFn(_, _, unsafety, _, _) =>
(true, unsafety == ast::Unsafety::Unsafe),
visit::FkMethod(_, sig, _) =>
(true, sig.unsafety == ast::Unsafety::Unsafe),
_ => (false, false),
Expand Down
19 changes: 12 additions & 7 deletions src/librustc/middle/infer/error_reporting.rs
Expand Up @@ -158,6 +158,7 @@ trait ErrorReportingHelpers<'tcx> {
fn give_expl_lifetime_param(&self,
decl: &ast::FnDecl,
unsafety: ast::Unsafety,
constness: ast::Constness,
ident: ast::Ident,
opt_explicit_self: Option<&ast::ExplicitSelf_>,
generics: &ast::Generics,
Expand Down Expand Up @@ -826,8 +827,9 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
Some(ref node) => match *node {
ast_map::NodeItem(ref item) => {
match item.node {
ast::ItemFn(ref fn_decl, pur, _, ref gen, _) => {
Some((fn_decl, gen, pur, item.ident, None, item.span))
ast::ItemFn(ref fn_decl, unsafety, constness, _, ref gen, _) => {
Some((fn_decl, gen, unsafety, constness,
item.ident, None, item.span))
},
_ => None
}
Expand All @@ -838,6 +840,7 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
Some((&sig.decl,
&sig.generics,
sig.unsafety,
sig.constness,
item.ident,
Some(&sig.explicit_self.node),
item.span))
Expand All @@ -852,6 +855,7 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
Some((&sig.decl,
&sig.generics,
sig.unsafety,
sig.constness,
item.ident,
Some(&sig.explicit_self.node),
item.span))
Expand All @@ -863,12 +867,12 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
},
None => None
};
let (fn_decl, generics, unsafety, ident, expl_self, span)
let (fn_decl, generics, unsafety, constness, ident, expl_self, span)
= node_inner.expect("expect item fn");
let rebuilder = Rebuilder::new(self.tcx, fn_decl, expl_self,
generics, same_regions, &life_giver);
let (fn_decl, expl_self, generics) = rebuilder.rebuild();
self.give_expl_lifetime_param(&fn_decl, unsafety, ident,
self.give_expl_lifetime_param(&fn_decl, unsafety, constness, ident,
expl_self.as_ref(), &generics, span);
}
}
Expand Down Expand Up @@ -1423,12 +1427,13 @@ impl<'a, 'tcx> ErrorReportingHelpers<'tcx> for InferCtxt<'a, 'tcx> {
fn give_expl_lifetime_param(&self,
decl: &ast::FnDecl,
unsafety: ast::Unsafety,
constness: ast::Constness,
ident: ast::Ident,
opt_explicit_self: Option<&ast::ExplicitSelf_>,
generics: &ast::Generics,
span: codemap::Span) {
let suggested_fn = pprust::fun_to_string(decl, unsafety, ident,
opt_explicit_self, generics);
let suggested_fn = pprust::fun_to_string(decl, unsafety, constness, ident,
opt_explicit_self, generics);
let msg = format!("consider using an explicit lifetime \
parameter as shown: {}", suggested_fn);
self.tcx.sess.span_help(span, &msg[..]);
Expand Down Expand Up @@ -1710,7 +1715,7 @@ fn lifetimes_in_scope(tcx: &ty::ctxt,
let method_id_opt = match tcx.map.find(parent) {
Some(node) => match node {
ast_map::NodeItem(item) => match item.node {
ast::ItemFn(_, _, _, ref gen, _) => {
ast::ItemFn(_, _, _, _, ref gen, _) => {
taken.push_all(&gen.lifetimes);
None
},
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/reachable.rs
Expand Up @@ -46,7 +46,7 @@ fn item_might_be_inlined(item: &ast::Item) -> bool {

match item.node {
ast::ItemImpl(_, _, ref generics, _, _, _) |
ast::ItemFn(_, _, _, ref generics, _) => {
ast::ItemFn(_, _, _, _, ref generics, _) => {
generics_require_inlining(generics)
}
_ => false,
Expand Down Expand Up @@ -256,7 +256,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
// but all other rust-only interfaces can be private (they will not
// participate in linkage after this product is produced)
if let ast_map::NodeItem(item) = *node {
if let ast::ItemFn(_, _, abi, _, _) = item.node {
if let ast::ItemFn(_, _, _, abi, _, _) = item.node {
if abi != abi::Rust {
self.reachable_symbols.insert(search_item);
}
Expand All @@ -273,7 +273,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
match *node {
ast_map::NodeItem(item) => {
match item.node {
ast::ItemFn(_, _, _, _, ref search_block) => {
ast::ItemFn(_, _, _, _, _, ref search_block) => {
if item_might_be_inlined(&*item) {
visit::walk_block(self, &**search_block)
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/resolve_lifetime.rs
Expand Up @@ -154,7 +154,7 @@ impl<'a, 'v> Visitor<'v> for LifetimeContext<'a> {
fn visit_fn(&mut self, fk: visit::FnKind<'v>, fd: &'v ast::FnDecl,
b: &'v ast::Block, s: Span, _: ast::NodeId) {
match fk {
visit::FkItemFn(_, generics, _, _, _) => {
visit::FkItemFn(_, generics, _, _, _, _) => {
self.visit_early_late(subst::FnSpace, generics, |this| {
this.walk_fn(fk, fd, b, s)
})
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/stability.rs
Expand Up @@ -23,7 +23,7 @@ use syntax::{attr, visit};
use syntax::ast;
use syntax::ast::{Attribute, Block, Crate, DefId, FnDecl, NodeId, Variant};
use syntax::ast::{Item, Generics, StructField};
use syntax::ast_util::is_local;
use syntax::ast_util::{is_local, PostExpansionMethod};
use syntax::attr::{Stability, AttrMetaMethods};
use syntax::visit::{FnKind, Visitor};
use syntax::feature_gate::emit_feature_err;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/ty.rs
Expand Up @@ -2428,7 +2428,7 @@ impl<'a, 'tcx> ParameterEnvironment<'a, 'tcx> {
}
Some(ast_map::NodeItem(item)) => {
match item.node {
ast::ItemFn(_, _, _, _, ref body) => {
ast::ItemFn(_, _, _, _, _, ref body) => {
// We assume this is a function.
let fn_def_id = ast_util::local_def(id);
let fn_scheme = lookup_item_type(cx, fn_def_id);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_lint/builtin.rs
Expand Up @@ -977,7 +977,7 @@ impl LintPass for NonSnakeCase {
},
_ => (),
},
visit::FkItemFn(ident, _, _, _, _) => {
visit::FkItemFn(ident, _, _, _, _, _) => {
self.check_snake_case(cx, "function", &token::get_ident(ident), Some(span))
},
_ => (),
Expand Down Expand Up @@ -1853,7 +1853,7 @@ impl LintPass for UnconditionalRecursion {
ast::NodeId, ast::NodeId, ast::Ident, ast::NodeId) -> bool;

let (name, checker) = match fn_kind {
visit::FkItemFn(name, _, _, _, _) => (name, id_refers_to_this_fn as F),
visit::FkItemFn(name, _, _, _, _, _) => (name, id_refers_to_this_fn as F),
visit::FkMethod(name, _, _) => (name, id_refers_to_this_method as F),
// closures can't recur, so they don't matter.
visit::FkFnBlock => return
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -425,7 +425,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
.define_value(DefConst(local_def(item.id)), sp, modifiers);
parent.clone()
}
ItemFn(_, _, _, _, _) => {
ItemFn(_, _, _, _, _, _) => {
let name_bindings = self.add_child(name, parent, ForbidDuplicateValues, sp);

let def = DefFn(local_def(item.id), false);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_resolve/lib.rs
Expand Up @@ -1809,7 +1809,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
ItemRibKind),
|this| visit::walk_item(this, item));
}
ItemFn(_, _, _, ref generics, _) => {
ItemFn(_, _, _, _, ref generics, _) => {
self.with_type_parameter_rib(HasTypeParameters(generics,
FnSpace,
ItemRibKind),
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_trans/trans/base.rs
Expand Up @@ -1073,7 +1073,7 @@ fn build_cfg(tcx: &ty::ctxt, id: ast::NodeId) -> (ast::NodeId, Option<cfg::CFG>)
let blk = match tcx.map.find(id) {
Some(ast_map::NodeItem(i)) => {
match i.node {
ast::ItemFn(_, _, _, _, ref blk) => {
ast::ItemFn(_, _, _, _, _, ref blk) => {
blk
}
_ => tcx.sess.bug("unexpected item variant in has_nested_returns")
Expand Down Expand Up @@ -1966,7 +1966,7 @@ pub fn trans_item(ccx: &CrateContext, item: &ast::Item) {
let from_external = ccx.external_srcs().borrow().contains_key(&item.id);

match item.node {
ast::ItemFn(ref decl, _fn_style, abi, ref generics, ref body) => {
ast::ItemFn(ref decl, _, _, abi, ref generics, ref body) => {
if !generics.is_type_parameterized() {
let trans_everywhere = attr::requests_inline(&item.attrs);
// Ignore `trans_everywhere` for cross-crate inlined items
Expand Down Expand Up @@ -2336,7 +2336,7 @@ pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef {
}
}

ast::ItemFn(_, _, abi, _, _) => {
ast::ItemFn(_, _, _, abi, _, _) => {
let sym = sym();
let llfn = if abi == Rust {
register_fn(ccx, i.span, sym, i.id, ty)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/trans/inline.rs
Expand Up @@ -55,7 +55,7 @@ fn instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
trans_item(ccx, &**item);

let linkage = match item.node {
ast::ItemFn(_, _, _, ref generics, _) => {
ast::ItemFn(_, _, _, _, ref generics, _) => {
if generics.is_type_parameterized() {
// Generics have no symbol, so they can't be given any
// linkage.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/trans/monomorphize.rs
Expand Up @@ -177,7 +177,7 @@ pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
ast_map::NodeItem(i) => {
match *i {
ast::Item {
node: ast::ItemFn(ref decl, _, abi, _, ref body),
node: ast::ItemFn(ref decl, _, _, abi, _, ref body),
..
} => {
let d = mk_lldecl(abi);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -741,7 +741,7 @@ pub fn check_item_type<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>, it: &'tcx ast::Item) {
&enum_definition.variants,
it.id);
}
ast::ItemFn(_, _, _, _, _) => {} // entirely within check_item_body
ast::ItemFn(..) => {} // entirely within check_item_body
ast::ItemImpl(_, _, _, _, _, ref impl_items) => {
debug!("ItemImpl {} with id {}", token::get_ident(it.ident), it.id);
match ty::impl_trait_ref(ccx.tcx, local_def(it.id)) {
Expand Down Expand Up @@ -796,7 +796,7 @@ pub fn check_item_body<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>, it: &'tcx ast::Item) {
ty::item_path_str(ccx.tcx, local_def(it.id)));
let _indenter = indenter();
match it.node {
ast::ItemFn(ref decl, _, _, _, ref body) => {
ast::ItemFn(ref decl, _, _, _, _, ref body) => {
let fn_pty = ty::lookup_item_type(ccx.tcx, ast_util::local_def(it.id));
let param_env = ParameterEnvironment::for_item(ccx.tcx, it.id);
check_bare_fn(ccx, &**decl, &**body, it.id, it.span, fn_pty.ty, param_env);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/collect.rs
Expand Up @@ -1440,7 +1440,7 @@ fn compute_type_scheme_of_item<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
let ty = ccx.icx(&()).to_ty(&ExplicitRscope, &**t);
ty::TypeScheme { ty: ty, generics: ty::Generics::empty() }
}
ast::ItemFn(ref decl, unsafety, abi, ref generics, _) => {
ast::ItemFn(ref decl, unsafety, _, abi, ref generics, _) => {
let ty_generics = ty_generics_for_fn(ccx, generics, &ty::Generics::empty());
let tofd = astconv::ty_of_bare_fn(&ccx.icx(generics), unsafety, abi, &**decl);
let ty = ty::mk_bare_fn(tcx, Some(local_def(it.id)), tcx.mk_bare_fn(tofd));
Expand Down Expand Up @@ -1492,7 +1492,7 @@ fn convert_typed_item<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
ast::ItemStatic(..) | ast::ItemConst(..) => {
ty::GenericPredicates::empty()
}
ast::ItemFn(_, _, _, ref ast_generics, _) => {
ast::ItemFn(_, _, _, _, ref ast_generics, _) => {
ty_generic_predicates_for_fn(ccx, ast_generics, &ty::GenericPredicates::empty())
}
ast::ItemTy(_, ref generics) => {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/lib.rs
Expand Up @@ -215,7 +215,7 @@ fn check_main_fn_ty(ccx: &CrateCtxt,
match tcx.map.find(main_id) {
Some(ast_map::NodeItem(it)) => {
match it.node {
ast::ItemFn(_, _, _, ref ps, _)
ast::ItemFn(_, _, _, _, ref ps, _)
if ps.is_parameterized() => {
span_err!(ccx.tcx.sess, main_span, E0131,
"main function is not allowed to have type parameters");
Expand Down Expand Up @@ -262,7 +262,7 @@ fn check_start_fn_ty(ccx: &CrateCtxt,
match tcx.map.find(start_id) {
Some(ast_map::NodeItem(it)) => {
match it.node {
ast::ItemFn(_,_,_,ref ps,_)
ast::ItemFn(_,_,_,_,ref ps,_)
if ps.is_parameterized() => {
span_err!(tcx.sess, start_span, E0132,
"start function is not allowed to have type parameters");
Expand Down
2 changes: 2 additions & 0 deletions src/librustdoc/clean/inline.rs
Expand Up @@ -175,6 +175,7 @@ fn build_external_function(cx: &DocContext, tcx: &ty::ctxt, did: ast::DefId) ->
decl: decl,
generics: (&t.generics, &predicates, subst::FnSpace).clean(cx),
unsafety: style,
constness: ast::Constness::NotConst,
abi: abi,
}
}
Expand Down Expand Up @@ -348,6 +349,7 @@ pub fn build_impl(cx: &DocContext,
}) => {
clean::MethodItem(clean::Method {
unsafety: unsafety,
constness: ast::Constness::NotConst,
decl: decl,
self_: self_,
generics: generics,
Expand Down
9 changes: 7 additions & 2 deletions src/librustdoc/clean/mod.rs
Expand Up @@ -989,6 +989,7 @@ pub struct Method {
pub generics: Generics,
pub self_: SelfTy,
pub unsafety: ast::Unsafety,
pub constness: ast::Constness,
pub decl: FnDecl,
pub abi: abi::Abi
}
Expand All @@ -1010,7 +1011,8 @@ impl Clean<Method> for ast::MethodSig {
Method {
generics: self.generics.clean(cx),
self_: self.explicit_self.node.clean(cx),
unsafety: self.unsafety.clone(),
unsafety: self.unsafety,
constness: self.constness,
decl: decl,
abi: self.abi
}
Expand Down Expand Up @@ -1075,7 +1077,8 @@ pub struct Function {
pub decl: FnDecl,
pub generics: Generics,
pub unsafety: ast::Unsafety,
pub abi: abi::Abi
pub constness: ast::Constness,
pub abi: abi::Abi,
}

impl Clean<Item> for doctree::Function {
Expand All @@ -1091,6 +1094,7 @@ impl Clean<Item> for doctree::Function {
decl: self.decl.clean(cx),
generics: self.generics.clean(cx),
unsafety: self.unsafety,
constness: self.constness,
abi: self.abi,
}),
}
Expand Down Expand Up @@ -2453,6 +2457,7 @@ impl Clean<Item> for ast::ForeignItem {
generics: generics.clean(cx),
unsafety: ast::Unsafety::Unsafe,
abi: abi::Rust,
constness: ast::Constness::NotConst,
})
}
ast::ForeignItemStatic(ref ty, mutbl) => {
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/doctree.rs
Expand Up @@ -133,6 +133,7 @@ pub struct Function {
pub vis: ast::Visibility,
pub stab: Option<attr::Stability>,
pub unsafety: ast::Unsafety,
pub constness: ast::Constness,
pub whence: Span,
pub generics: ast::Generics,
pub abi: abi::Abi,
Expand Down

0 comments on commit af37957

Please sign in to comment.