Skip to content

Commit 096e8c2

Browse files
eddybnikomatsakis
authored andcommitted
syntax: parse const fn for free functions and inherent methods.
1 parent 6d718f2 commit 096e8c2

File tree

34 files changed

+218
-88
lines changed

34 files changed

+218
-88
lines changed

src/librustc/metadata/encoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
10491049
encode_stability(rbml_w, stab);
10501050
rbml_w.end_tag();
10511051
}
1052-
ast::ItemFn(ref decl, _, _, ref generics, _) => {
1052+
ast::ItemFn(ref decl, _, _, _, ref generics, _) => {
10531053
add_to_index(item, rbml_w, index);
10541054
rbml_w.start_tag(tag_items_data_item);
10551055
encode_def_id(rbml_w, def_id);
@@ -1967,7 +1967,7 @@ fn encode_reachable_extern_fns(ecx: &EncodeContext, rbml_w: &mut Encoder) {
19671967

19681968
for id in ecx.reachable {
19691969
if let Some(ast_map::NodeItem(i)) = ecx.tcx.map.find(*id) {
1970-
if let ast::ItemFn(_, _, abi, ref generics, _) = i.node {
1970+
if let ast::ItemFn(_, _, _, abi, ref generics, _) = i.node {
19711971
if abi != abi::Rust && !generics.is_type_parameterized() {
19721972
rbml_w.wr_tagged_u32(tag_reachable_extern_fn_id, *id);
19731973
}

src/librustc/middle/effect.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EffectCheckVisitor<'a, 'tcx> {
8787
block: &'v ast::Block, span: Span, _: ast::NodeId) {
8888

8989
let (is_item_fn, is_unsafe_fn) = match fn_kind {
90-
visit::FkItemFn(_, _, fn_style, _, _) =>
91-
(true, fn_style == ast::Unsafety::Unsafe),
90+
visit::FkItemFn(_, _, unsafety, _, _) =>
91+
(true, unsafety == ast::Unsafety::Unsafe),
9292
visit::FkMethod(_, sig, _) =>
9393
(true, sig.unsafety == ast::Unsafety::Unsafe),
9494
_ => (false, false),

src/librustc/middle/infer/error_reporting.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ trait ErrorReportingHelpers<'tcx> {
158158
fn give_expl_lifetime_param(&self,
159159
decl: &ast::FnDecl,
160160
unsafety: ast::Unsafety,
161+
constness: ast::Constness,
161162
ident: ast::Ident,
162163
opt_explicit_self: Option<&ast::ExplicitSelf_>,
163164
generics: &ast::Generics,
@@ -826,8 +827,9 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
826827
Some(ref node) => match *node {
827828
ast_map::NodeItem(ref item) => {
828829
match item.node {
829-
ast::ItemFn(ref fn_decl, pur, _, ref gen, _) => {
830-
Some((fn_decl, gen, pur, item.ident, None, item.span))
830+
ast::ItemFn(ref fn_decl, unsafety, constness, _, ref gen, _) => {
831+
Some((fn_decl, gen, unsafety, constness,
832+
item.ident, None, item.span))
831833
},
832834
_ => None
833835
}
@@ -838,6 +840,7 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
838840
Some((&sig.decl,
839841
&sig.generics,
840842
sig.unsafety,
843+
sig.constness,
841844
item.ident,
842845
Some(&sig.explicit_self.node),
843846
item.span))
@@ -852,6 +855,7 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
852855
Some((&sig.decl,
853856
&sig.generics,
854857
sig.unsafety,
858+
sig.constness,
855859
item.ident,
856860
Some(&sig.explicit_self.node),
857861
item.span))
@@ -863,12 +867,12 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
863867
},
864868
None => None
865869
};
866-
let (fn_decl, generics, unsafety, ident, expl_self, span)
870+
let (fn_decl, generics, unsafety, constness, ident, expl_self, span)
867871
= node_inner.expect("expect item fn");
868872
let rebuilder = Rebuilder::new(self.tcx, fn_decl, expl_self,
869873
generics, same_regions, &life_giver);
870874
let (fn_decl, expl_self, generics) = rebuilder.rebuild();
871-
self.give_expl_lifetime_param(&fn_decl, unsafety, ident,
875+
self.give_expl_lifetime_param(&fn_decl, unsafety, constness, ident,
872876
expl_self.as_ref(), &generics, span);
873877
}
874878
}
@@ -1423,12 +1427,13 @@ impl<'a, 'tcx> ErrorReportingHelpers<'tcx> for InferCtxt<'a, 'tcx> {
14231427
fn give_expl_lifetime_param(&self,
14241428
decl: &ast::FnDecl,
14251429
unsafety: ast::Unsafety,
1430+
constness: ast::Constness,
14261431
ident: ast::Ident,
14271432
opt_explicit_self: Option<&ast::ExplicitSelf_>,
14281433
generics: &ast::Generics,
14291434
span: codemap::Span) {
1430-
let suggested_fn = pprust::fun_to_string(decl, unsafety, ident,
1431-
opt_explicit_self, generics);
1435+
let suggested_fn = pprust::fun_to_string(decl, unsafety, constness, ident,
1436+
opt_explicit_self, generics);
14321437
let msg = format!("consider using an explicit lifetime \
14331438
parameter as shown: {}", suggested_fn);
14341439
self.tcx.sess.span_help(span, &msg[..]);
@@ -1710,7 +1715,7 @@ fn lifetimes_in_scope(tcx: &ty::ctxt,
17101715
let method_id_opt = match tcx.map.find(parent) {
17111716
Some(node) => match node {
17121717
ast_map::NodeItem(item) => match item.node {
1713-
ast::ItemFn(_, _, _, ref gen, _) => {
1718+
ast::ItemFn(_, _, _, _, ref gen, _) => {
17141719
taken.push_all(&gen.lifetimes);
17151720
None
17161721
},

src/librustc/middle/reachable.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn item_might_be_inlined(item: &ast::Item) -> bool {
4646

4747
match item.node {
4848
ast::ItemImpl(_, _, ref generics, _, _, _) |
49-
ast::ItemFn(_, _, _, ref generics, _) => {
49+
ast::ItemFn(_, _, _, _, ref generics, _) => {
5050
generics_require_inlining(generics)
5151
}
5252
_ => false,
@@ -256,7 +256,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
256256
// but all other rust-only interfaces can be private (they will not
257257
// participate in linkage after this product is produced)
258258
if let ast_map::NodeItem(item) = *node {
259-
if let ast::ItemFn(_, _, abi, _, _) = item.node {
259+
if let ast::ItemFn(_, _, _, abi, _, _) = item.node {
260260
if abi != abi::Rust {
261261
self.reachable_symbols.insert(search_item);
262262
}
@@ -273,7 +273,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
273273
match *node {
274274
ast_map::NodeItem(item) => {
275275
match item.node {
276-
ast::ItemFn(_, _, _, _, ref search_block) => {
276+
ast::ItemFn(_, _, _, _, _, ref search_block) => {
277277
if item_might_be_inlined(&*item) {
278278
visit::walk_block(self, &**search_block)
279279
}

src/librustc/middle/resolve_lifetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl<'a, 'v> Visitor<'v> for LifetimeContext<'a> {
154154
fn visit_fn(&mut self, fk: visit::FnKind<'v>, fd: &'v ast::FnDecl,
155155
b: &'v ast::Block, s: Span, _: ast::NodeId) {
156156
match fk {
157-
visit::FkItemFn(_, generics, _, _, _) => {
157+
visit::FkItemFn(_, generics, _, _, _, _) => {
158158
self.visit_early_late(subst::FnSpace, generics, |this| {
159159
this.walk_fn(fk, fd, b, s)
160160
})

src/librustc/middle/stability.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use syntax::{attr, visit};
2323
use syntax::ast;
2424
use syntax::ast::{Attribute, Block, Crate, DefId, FnDecl, NodeId, Variant};
2525
use syntax::ast::{Item, Generics, StructField};
26-
use syntax::ast_util::is_local;
26+
use syntax::ast_util::{is_local, PostExpansionMethod};
2727
use syntax::attr::{Stability, AttrMetaMethods};
2828
use syntax::visit::{FnKind, Visitor};
2929
use syntax::feature_gate::emit_feature_err;

src/librustc/middle/ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2428,7 +2428,7 @@ impl<'a, 'tcx> ParameterEnvironment<'a, 'tcx> {
24282428
}
24292429
Some(ast_map::NodeItem(item)) => {
24302430
match item.node {
2431-
ast::ItemFn(_, _, _, _, ref body) => {
2431+
ast::ItemFn(_, _, _, _, _, ref body) => {
24322432
// We assume this is a function.
24332433
let fn_def_id = ast_util::local_def(id);
24342434
let fn_scheme = lookup_item_type(cx, fn_def_id);

src/librustc_lint/builtin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ impl LintPass for NonSnakeCase {
977977
},
978978
_ => (),
979979
},
980-
visit::FkItemFn(ident, _, _, _, _) => {
980+
visit::FkItemFn(ident, _, _, _, _, _) => {
981981
self.check_snake_case(cx, "function", &token::get_ident(ident), Some(span))
982982
},
983983
_ => (),
@@ -1853,7 +1853,7 @@ impl LintPass for UnconditionalRecursion {
18531853
ast::NodeId, ast::NodeId, ast::Ident, ast::NodeId) -> bool;
18541854

18551855
let (name, checker) = match fn_kind {
1856-
visit::FkItemFn(name, _, _, _, _) => (name, id_refers_to_this_fn as F),
1856+
visit::FkItemFn(name, _, _, _, _, _) => (name, id_refers_to_this_fn as F),
18571857
visit::FkMethod(name, _, _) => (name, id_refers_to_this_method as F),
18581858
// closures can't recur, so they don't matter.
18591859
visit::FkFnBlock => return

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
425425
.define_value(DefConst(local_def(item.id)), sp, modifiers);
426426
parent.clone()
427427
}
428-
ItemFn(_, _, _, _, _) => {
428+
ItemFn(_, _, _, _, _, _) => {
429429
let name_bindings = self.add_child(name, parent, ForbidDuplicateValues, sp);
430430

431431
let def = DefFn(local_def(item.id), false);

src/librustc_resolve/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1809,7 +1809,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
18091809
ItemRibKind),
18101810
|this| visit::walk_item(this, item));
18111811
}
1812-
ItemFn(_, _, _, ref generics, _) => {
1812+
ItemFn(_, _, _, _, ref generics, _) => {
18131813
self.with_type_parameter_rib(HasTypeParameters(generics,
18141814
FnSpace,
18151815
ItemRibKind),

src/librustc_trans/trans/base.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ fn build_cfg(tcx: &ty::ctxt, id: ast::NodeId) -> (ast::NodeId, Option<cfg::CFG>)
10731073
let blk = match tcx.map.find(id) {
10741074
Some(ast_map::NodeItem(i)) => {
10751075
match i.node {
1076-
ast::ItemFn(_, _, _, _, ref blk) => {
1076+
ast::ItemFn(_, _, _, _, _, ref blk) => {
10771077
blk
10781078
}
10791079
_ => tcx.sess.bug("unexpected item variant in has_nested_returns")
@@ -1966,7 +1966,7 @@ pub fn trans_item(ccx: &CrateContext, item: &ast::Item) {
19661966
let from_external = ccx.external_srcs().borrow().contains_key(&item.id);
19671967

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

2339-
ast::ItemFn(_, _, abi, _, _) => {
2339+
ast::ItemFn(_, _, _, abi, _, _) => {
23402340
let sym = sym();
23412341
let llfn = if abi == Rust {
23422342
register_fn(ccx, i.span, sym, i.id, ty)

src/librustc_trans/trans/inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
5555
trans_item(ccx, &**item);
5656

5757
let linkage = match item.node {
58-
ast::ItemFn(_, _, _, ref generics, _) => {
58+
ast::ItemFn(_, _, _, _, ref generics, _) => {
5959
if generics.is_type_parameterized() {
6060
// Generics have no symbol, so they can't be given any
6161
// linkage.

src/librustc_trans/trans/monomorphize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
177177
ast_map::NodeItem(i) => {
178178
match *i {
179179
ast::Item {
180-
node: ast::ItemFn(ref decl, _, abi, _, ref body),
180+
node: ast::ItemFn(ref decl, _, _, abi, _, ref body),
181181
..
182182
} => {
183183
let d = mk_lldecl(abi);

src/librustc_typeck/check/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ pub fn check_item_type<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>, it: &'tcx ast::Item) {
741741
&enum_definition.variants,
742742
it.id);
743743
}
744-
ast::ItemFn(_, _, _, _, _) => {} // entirely within check_item_body
744+
ast::ItemFn(..) => {} // entirely within check_item_body
745745
ast::ItemImpl(_, _, _, _, _, ref impl_items) => {
746746
debug!("ItemImpl {} with id {}", token::get_ident(it.ident), it.id);
747747
match ty::impl_trait_ref(ccx.tcx, local_def(it.id)) {
@@ -796,7 +796,7 @@ pub fn check_item_body<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>, it: &'tcx ast::Item) {
796796
ty::item_path_str(ccx.tcx, local_def(it.id)));
797797
let _indenter = indenter();
798798
match it.node {
799-
ast::ItemFn(ref decl, _, _, _, ref body) => {
799+
ast::ItemFn(ref decl, _, _, _, _, ref body) => {
800800
let fn_pty = ty::lookup_item_type(ccx.tcx, ast_util::local_def(it.id));
801801
let param_env = ParameterEnvironment::for_item(ccx.tcx, it.id);
802802
check_bare_fn(ccx, &**decl, &**body, it.id, it.span, fn_pty.ty, param_env);

src/librustc_typeck/collect.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,7 @@ fn compute_type_scheme_of_item<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
14401440
let ty = ccx.icx(&()).to_ty(&ExplicitRscope, &**t);
14411441
ty::TypeScheme { ty: ty, generics: ty::Generics::empty() }
14421442
}
1443-
ast::ItemFn(ref decl, unsafety, abi, ref generics, _) => {
1443+
ast::ItemFn(ref decl, unsafety, _, abi, ref generics, _) => {
14441444
let ty_generics = ty_generics_for_fn(ccx, generics, &ty::Generics::empty());
14451445
let tofd = astconv::ty_of_bare_fn(&ccx.icx(generics), unsafety, abi, &**decl);
14461446
let ty = ty::mk_bare_fn(tcx, Some(local_def(it.id)), tcx.mk_bare_fn(tofd));
@@ -1492,7 +1492,7 @@ fn convert_typed_item<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
14921492
ast::ItemStatic(..) | ast::ItemConst(..) => {
14931493
ty::GenericPredicates::empty()
14941494
}
1495-
ast::ItemFn(_, _, _, ref ast_generics, _) => {
1495+
ast::ItemFn(_, _, _, _, ref ast_generics, _) => {
14961496
ty_generic_predicates_for_fn(ccx, ast_generics, &ty::GenericPredicates::empty())
14971497
}
14981498
ast::ItemTy(_, ref generics) => {

src/librustc_typeck/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ fn check_main_fn_ty(ccx: &CrateCtxt,
215215
match tcx.map.find(main_id) {
216216
Some(ast_map::NodeItem(it)) => {
217217
match it.node {
218-
ast::ItemFn(_, _, _, ref ps, _)
218+
ast::ItemFn(_, _, _, _, ref ps, _)
219219
if ps.is_parameterized() => {
220220
span_err!(ccx.tcx.sess, main_span, E0131,
221221
"main function is not allowed to have type parameters");
@@ -262,7 +262,7 @@ fn check_start_fn_ty(ccx: &CrateCtxt,
262262
match tcx.map.find(start_id) {
263263
Some(ast_map::NodeItem(it)) => {
264264
match it.node {
265-
ast::ItemFn(_,_,_,ref ps,_)
265+
ast::ItemFn(_,_,_,_,ref ps,_)
266266
if ps.is_parameterized() => {
267267
span_err!(tcx.sess, start_span, E0132,
268268
"start function is not allowed to have type parameters");

src/librustdoc/clean/inline.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ fn build_external_function(cx: &DocContext, tcx: &ty::ctxt, did: ast::DefId) ->
175175
decl: decl,
176176
generics: (&t.generics, &predicates, subst::FnSpace).clean(cx),
177177
unsafety: style,
178+
constness: ast::Constness::NotConst,
178179
abi: abi,
179180
}
180181
}
@@ -348,6 +349,7 @@ pub fn build_impl(cx: &DocContext,
348349
}) => {
349350
clean::MethodItem(clean::Method {
350351
unsafety: unsafety,
352+
constness: ast::Constness::NotConst,
351353
decl: decl,
352354
self_: self_,
353355
generics: generics,

src/librustdoc/clean/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,7 @@ pub struct Method {
989989
pub generics: Generics,
990990
pub self_: SelfTy,
991991
pub unsafety: ast::Unsafety,
992+
pub constness: ast::Constness,
992993
pub decl: FnDecl,
993994
pub abi: abi::Abi
994995
}
@@ -1010,7 +1011,8 @@ impl Clean<Method> for ast::MethodSig {
10101011
Method {
10111012
generics: self.generics.clean(cx),
10121013
self_: self.explicit_self.node.clean(cx),
1013-
unsafety: self.unsafety.clone(),
1014+
unsafety: self.unsafety,
1015+
constness: self.constness,
10141016
decl: decl,
10151017
abi: self.abi
10161018
}
@@ -1075,7 +1077,8 @@ pub struct Function {
10751077
pub decl: FnDecl,
10761078
pub generics: Generics,
10771079
pub unsafety: ast::Unsafety,
1078-
pub abi: abi::Abi
1080+
pub constness: ast::Constness,
1081+
pub abi: abi::Abi,
10791082
}
10801083

10811084
impl Clean<Item> for doctree::Function {
@@ -1091,6 +1094,7 @@ impl Clean<Item> for doctree::Function {
10911094
decl: self.decl.clean(cx),
10921095
generics: self.generics.clean(cx),
10931096
unsafety: self.unsafety,
1097+
constness: self.constness,
10941098
abi: self.abi,
10951099
}),
10961100
}
@@ -2453,6 +2457,7 @@ impl Clean<Item> for ast::ForeignItem {
24532457
generics: generics.clean(cx),
24542458
unsafety: ast::Unsafety::Unsafe,
24552459
abi: abi::Rust,
2460+
constness: ast::Constness::NotConst,
24562461
})
24572462
}
24582463
ast::ForeignItemStatic(ref ty, mutbl) => {

src/librustdoc/doctree.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ pub struct Function {
133133
pub vis: ast::Visibility,
134134
pub stab: Option<attr::Stability>,
135135
pub unsafety: ast::Unsafety,
136+
pub constness: ast::Constness,
136137
pub whence: Span,
137138
pub generics: ast::Generics,
138139
pub abi: abi::Abi,

0 commit comments

Comments
 (0)