Skip to content

Commit 1b207f1

Browse files
committed
rustc: const-qualify const fn function and method calls.
1 parent e04462f commit 1b207f1

File tree

9 files changed

+282
-62
lines changed

9 files changed

+282
-62
lines changed

src/librustc/diagnostics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ register_diagnostics! {
6767
E0018,
6868
E0019,
6969
E0020,
70+
E0021,
7071
E0022,
7172
E0109,
7273
E0110,

src/librustc/metadata/common.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,5 @@ pub const tag_codemap_filemap: uint = 0xa2;
256256
pub const tag_item_super_predicates: uint = 0xa3;
257257

258258
pub const tag_defaulted_trait: uint = 0xa4;
259+
260+
pub const tag_items_data_item_constness: uint = 0xa5;

src/librustc/metadata/csearch.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,11 @@ pub fn is_typedef(cstore: &cstore::CStore, did: ast::DefId) -> bool {
377377
decoder::is_typedef(&*cdata, did.node)
378378
}
379379

380+
pub fn is_const_fn(cstore: &cstore::CStore, did: ast::DefId) -> bool {
381+
let cdata = cstore.get_crate_data(did.krate);
382+
decoder::is_const_fn(&*cdata, did.node)
383+
}
384+
380385
pub fn get_stability(cstore: &cstore::CStore,
381386
def: ast::DefId)
382387
-> Option<attr::Stability> {

src/librustc/metadata/decoder.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,19 @@ fn item_visibility(item: rbml::Doc) -> ast::Visibility {
176176
}
177177
}
178178

179+
fn fn_constness(item: rbml::Doc) -> ast::Constness {
180+
match reader::maybe_get_doc(item, tag_items_data_item_constness) {
181+
None => ast::Constness::NotConst,
182+
Some(constness_doc) => {
183+
match reader::doc_as_u8(constness_doc) as char {
184+
'c' => ast::Constness::Const,
185+
'n' => ast::Constness::NotConst,
186+
_ => panic!("unknown constness character")
187+
}
188+
}
189+
}
190+
}
191+
179192
fn item_sort(item: rbml::Doc) -> Option<char> {
180193
let mut ret = None;
181194
reader::tagged_docs(item, tag_item_trait_item_sort, |doc| {
@@ -1451,6 +1464,14 @@ pub fn is_typedef(cdata: Cmd, id: ast::NodeId) -> bool {
14511464
}
14521465
}
14531466

1467+
pub fn is_const_fn(cdata: Cmd, id: ast::NodeId) -> bool {
1468+
let item_doc = lookup_item(id, cdata.data());
1469+
match fn_constness(item_doc) {
1470+
ast::Constness::Const => true,
1471+
ast::Constness::NotConst => false,
1472+
}
1473+
}
1474+
14541475
fn doc_generics<'tcx>(base_doc: rbml::Doc,
14551476
tcx: &ty::ctxt<'tcx>,
14561477
cdata: Cmd,

src/librustc/metadata/encoder.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,16 @@ fn encode_visibility(rbml_w: &mut Encoder, visibility: ast::Visibility) {
581581
rbml_w.wr_tagged_u8(tag_items_data_item_visibility, ch as u8);
582582
}
583583

584+
fn encode_constness(rbml_w: &mut Encoder, constness: ast::Constness) {
585+
rbml_w.start_tag(tag_items_data_item_constness);
586+
let ch = match constness {
587+
ast::Constness::Const => 'c',
588+
ast::Constness::NotConst => 'n',
589+
};
590+
rbml_w.wr_str(&ch.to_string());
591+
rbml_w.end_tag();
592+
}
593+
584594
fn encode_explicit_self(rbml_w: &mut Encoder,
585595
explicit_self: &ty::ExplicitSelfCategory) {
586596
let tag = tag_item_trait_method_explicit_self;
@@ -831,10 +841,14 @@ fn encode_info_for_method<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
831841
encode_attributes(rbml_w, &impl_item.attrs);
832842
let scheme = ty::lookup_item_type(ecx.tcx, m.def_id);
833843
let any_types = !scheme.generics.types.is_empty();
834-
if any_types || is_default_impl || attr::requests_inline(&impl_item.attrs) {
844+
let needs_inline = any_types || is_default_impl ||
845+
attr::requests_inline(&impl_item.attrs);
846+
let constness = ast_method.pe_constness();
847+
if needs_inline || constness == ast::Constness::Const {
835848
encode_inlined_item(ecx, rbml_w, IIImplItemRef(local_def(parent_id),
836849
impl_item));
837850
}
851+
encode_constness(rbml_w, constness);
838852
if !any_types {
839853
encode_symbol(ecx, rbml_w, m.def_id.node);
840854
}
@@ -1015,7 +1029,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
10151029
encode_stability(rbml_w, stab);
10161030
rbml_w.end_tag();
10171031
}
1018-
ast::ItemFn(ref decl, _, _, _, ref generics, _) => {
1032+
ast::ItemFn(ref decl, _, constness, _, ref generics, _) => {
10191033
add_to_index(item, rbml_w, index);
10201034
rbml_w.start_tag(tag_items_data_item);
10211035
encode_def_id(rbml_w, def_id);
@@ -1025,12 +1039,14 @@ fn encode_info_for_item(ecx: &EncodeContext,
10251039
encode_name(rbml_w, item.ident.name);
10261040
encode_path(rbml_w, path);
10271041
encode_attributes(rbml_w, &item.attrs);
1028-
if tps_len > 0 || attr::requests_inline(&item.attrs) {
1042+
let needs_inline = tps_len > 0 || attr::requests_inline(&item.attrs);
1043+
if needs_inline || constness == ast::Constness::Const {
10291044
encode_inlined_item(ecx, rbml_w, IIItemRef(item));
10301045
}
10311046
if tps_len == 0 {
10321047
encode_symbol(ecx, rbml_w, item.id);
10331048
}
1049+
encode_constness(rbml_w, constness);
10341050
encode_visibility(rbml_w, vis);
10351051
encode_stability(rbml_w, stab);
10361052
encode_method_argument_names(rbml_w, &**decl);

0 commit comments

Comments
 (0)