Skip to content

Commit 27748b0

Browse files
committed
librustc: Only emit constructor functions as necessary.
1 parent 06bf73a commit 27748b0

File tree

3 files changed

+27
-62
lines changed

3 files changed

+27
-62
lines changed

src/librustc/metadata/encoder.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,7 @@ fn encode_enum_variant_info(ecx: &EncodeContext,
314314
ebml_w: &mut Encoder,
315315
id: NodeId,
316316
variants: &[P<Variant>],
317-
index: &mut Vec<entry<i64>>,
318-
generics: &ast::Generics) {
317+
index: &mut Vec<entry<i64>>) {
319318
debug!("encode_enum_variant_info(id={:?})", id);
320319

321320
let mut disr_val = 0;
@@ -343,10 +342,6 @@ fn encode_enum_variant_info(ecx: &EncodeContext,
343342
encode_stability(ebml_w, stab);
344343

345344
match variant.node.kind {
346-
ast::TupleVariantKind(ref args)
347-
if args.len() > 0 && generics.ty_params.len() == 0 => {
348-
encode_symbol(ecx, ebml_w, variant.node.id);
349-
}
350345
ast::TupleVariantKind(_) => {},
351346
ast::StructVariantKind(_) => {
352347
let fields = ty::lookup_struct_fields(ecx.tcx, def_id);
@@ -1019,7 +1014,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
10191014
encode_stability(ebml_w, stab);
10201015
ebml_w.end_tag();
10211016
}
1022-
ItemEnum(ref enum_definition, ref generics) => {
1017+
ItemEnum(ref enum_definition, _) => {
10231018
add_to_index(item, ebml_w, index);
10241019

10251020
ebml_w.start_tag(tag_items_data_item);
@@ -1046,8 +1041,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
10461041
ebml_w,
10471042
item.id,
10481043
(*enum_definition).variants.as_slice(),
1049-
index,
1050-
generics);
1044+
index);
10511045
}
10521046
ItemStruct(struct_def, _) => {
10531047
let fields = ty::lookup_struct_fields(tcx, def_id);

src/librustc/middle/trans/base.rs

+2-51
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ use std::c_str::ToCStr;
8080
use std::cell::{Cell, RefCell};
8181
use std::rc::Rc;
8282
use std::{i8, i16, i32, i64};
83-
use std::gc::Gc;
8483
use syntax::abi::{X86, X86_64, Arm, Mips, Mipsel, Rust, RustCall};
8584
use syntax::abi::{RustIntrinsic, Abi};
8685
use syntax::ast_util::{local_def, is_local};
@@ -1815,31 +1814,6 @@ fn trans_enum_variant_or_tuple_like_struct(ccx: &CrateContext,
18151814
finish_fn(&fcx, bcx, result_ty);
18161815
}
18171816

1818-
fn trans_enum_def(ccx: &CrateContext, enum_definition: &ast::EnumDef,
1819-
sp: Span, id: ast::NodeId, vi: &[Rc<ty::VariantInfo>],
1820-
i: &mut uint) {
1821-
for variant in enum_definition.variants.iter() {
1822-
let disr_val = vi[*i].disr_val;
1823-
*i += 1;
1824-
1825-
match variant.node.kind {
1826-
ast::TupleVariantKind(ref args) if args.len() > 0 => {
1827-
let llfn = get_item_val(ccx, variant.node.id);
1828-
trans_enum_variant(ccx, id, &**variant, args.as_slice(),
1829-
disr_val, &param_substs::empty(), llfn);
1830-
}
1831-
ast::TupleVariantKind(_) => {
1832-
// Nothing to do.
1833-
}
1834-
ast::StructVariantKind(struct_def) => {
1835-
trans_struct_def(ccx, struct_def);
1836-
}
1837-
}
1838-
}
1839-
1840-
enum_variant_size_lint(ccx, enum_definition, sp, id);
1841-
}
1842-
18431817
fn enum_variant_size_lint(ccx: &CrateContext, enum_def: &ast::EnumDef, sp: Span, id: ast::NodeId) {
18441818
let mut sizes = Vec::new(); // does no allocation if no pushes, thankfully
18451819

@@ -1932,12 +1906,8 @@ pub fn trans_item(ccx: &CrateContext, item: &ast::Item) {
19321906
ast::ItemMod(ref m) => {
19331907
trans_mod(ccx, m);
19341908
}
1935-
ast::ItemEnum(ref enum_definition, ref generics) => {
1936-
if !generics.is_type_parameterized() {
1937-
let vi = ty::enum_variants(ccx.tcx(), local_def(item.id));
1938-
let mut i = 0;
1939-
trans_enum_def(ccx, enum_definition, item.span, item.id, vi.as_slice(), &mut i);
1940-
}
1909+
ast::ItemEnum(ref enum_definition, _) => {
1910+
enum_variant_size_lint(ccx, enum_definition, item.span, item.id);
19411911
}
19421912
ast::ItemStatic(_, m, ref expr) => {
19431913
// Recurse on the expression to catch items in blocks
@@ -1964,11 +1934,6 @@ pub fn trans_item(ccx: &CrateContext, item: &ast::Item) {
19641934
ast::ItemForeignMod(ref foreign_mod) => {
19651935
foreign::trans_foreign_mod(ccx, foreign_mod);
19661936
}
1967-
ast::ItemStruct(struct_def, ref generics) => {
1968-
if !generics.is_type_parameterized() {
1969-
trans_struct_def(ccx, struct_def);
1970-
}
1971-
}
19721937
ast::ItemTrait(..) => {
19731938
// Inside of this trait definition, we won't be actually translating any
19741939
// functions, but the trait still needs to be walked. Otherwise default
@@ -1981,20 +1946,6 @@ pub fn trans_item(ccx: &CrateContext, item: &ast::Item) {
19811946
}
19821947
}
19831948

1984-
pub fn trans_struct_def(ccx: &CrateContext, struct_def: Gc<ast::StructDef>) {
1985-
// If this is a tuple-like struct, translate the constructor.
1986-
match struct_def.ctor_id {
1987-
// We only need to translate a constructor if there are fields;
1988-
// otherwise this is a unit-like struct.
1989-
Some(ctor_id) if struct_def.fields.len() > 0 => {
1990-
let llfndecl = get_item_val(ccx, ctor_id);
1991-
trans_tuple_struct(ccx, struct_def.fields.as_slice(),
1992-
ctor_id, &param_substs::empty(), llfndecl);
1993-
}
1994-
Some(_) | None => {}
1995-
}
1996-
}
1997-
19981949
// Translate a module. Doing this amounts to translating the items in the
19991950
// module; there ends up being no artifact (aside from linkage names) of
20001951
// separate modules in the compiled program. That's because modules exist

src/librustc/middle/trans/callee.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use arena::TypedArena;
2020
use back::abi;
2121
use back::link;
22+
use driver::session;
2223
use llvm::{ValueRef, get_param};
2324
use llvm;
2425
use metadata::csearch;
@@ -521,8 +522,27 @@ pub fn trans_fn_ref_with_vtables(
521522
}
522523
};
523524

524-
// We must monomorphise if the fn has type parameters or is a default method.
525-
let must_monomorphise = !substs.types.is_empty() || is_default;
525+
// We must monomorphise if the fn has type parameters, is a default method,
526+
// or is a named tuple constructor.
527+
let must_monomorphise = if !substs.types.is_empty() || is_default {
528+
true
529+
} else if def_id.krate == ast::LOCAL_CRATE {
530+
let map_node = session::expect(
531+
ccx.sess(),
532+
tcx.map.find(def_id.node),
533+
|| "local item should be in ast map".to_string());
534+
535+
match map_node {
536+
ast_map::NodeVariant(v) => match v.node.kind {
537+
ast::TupleVariantKind(ref args) => args.len() > 0,
538+
_ => false
539+
},
540+
ast_map::NodeStructCtor(_) => true,
541+
_ => false
542+
}
543+
} else {
544+
false
545+
};
526546

527547
// Create a monomorphic version of generic functions
528548
if must_monomorphise {

0 commit comments

Comments
 (0)