Skip to content

Commit 2ebd194

Browse files
committed
Sanitize use of ids for obj constructors.
Typeck and trans used to, by historical coincidence, use the item_obj node id, which was used to identify the obj type by the rest of the system, for the constructor function. This is now identified by the ctor id stored in the tag throughout.
1 parent 9643aed commit 2ebd194

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

src/comp/middle/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ fn encode_info_for_item(@trans::crate_ctxt cx, &ebml::writer ebml_w,
558558
encode_def_id(ebml_w, local_def(ctor_id));
559559
encode_kind(ebml_w, 'f' as u8);
560560
encode_type_param_count(ebml_w, tps);
561-
auto fn_ty = trans::node_id_type(cx, item.id);
561+
auto fn_ty = trans::node_id_type(cx, ctor_id);
562562
encode_type(cx, ebml_w, fn_ty);
563563
encode_symbol(cx, ebml_w, ctor_id);
564564
ebml::end_tag(ebml_w);

src/comp/middle/trans.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7426,7 +7426,7 @@ fn trans_dtor(@local_ctxt cx, TypeRef llself_ty, ty::t self_ty,
74267426
// trans_obj: creates an LLVM function that is the object constructor for the
74277427
// object being translated.
74287428
fn trans_obj(@local_ctxt cx, &span sp, &ast::_obj ob, ast::node_id ctor_id,
7429-
&vec[ast::ty_param] ty_params, ast::node_id type_id) {
7429+
&vec[ast::ty_param] ty_params) {
74307430
// To make a function, we have to create a function context and, inside
74317431
// that, a number of block contexts for which code is generated.
74327432

@@ -7448,9 +7448,9 @@ fn trans_obj(@local_ctxt cx, &span sp, &ast::_obj ob, ast::node_id ctor_id,
74487448
// Both regular arguments and type parameters are handled here.
74497449

74507450
create_llargs_for_fn_args(fcx, ast::proto_fn, none[ty_self_pair],
7451-
ty::ret_ty_of_fn(ccx.tcx, type_id),
7451+
ty::ret_ty_of_fn(ccx.tcx, ctor_id),
74527452
fn_args, ty_params);
7453-
let vec[ty::arg] arg_tys = arg_tys_of_fn(ccx, type_id);
7453+
let vec[ty::arg] arg_tys = arg_tys_of_fn(ccx, ctor_id);
74547454
copy_args_to_allocas(fcx, fn_args, arg_tys);
74557455
// Create the first block context in the function and keep a handle on it
74567456
// to pass to finish_fn later.
@@ -7460,7 +7460,7 @@ fn trans_obj(@local_ctxt cx, &span sp, &ast::_obj ob, ast::node_id ctor_id,
74607460
// Pick up the type of this object by looking at our own output type, that
74617461
// is, the output type of the object constructor we're building.
74627462

7463-
auto self_ty = ty::ret_ty_of_fn(ccx.tcx, type_id);
7463+
auto self_ty = ty::ret_ty_of_fn(ccx.tcx, ctor_id);
74647464
auto llself_ty = type_of(ccx, sp, self_ty);
74657465
// Set up the two-word pair that we're going to return from the object
74667466
// constructor we're building. The two elements of this pair will be a
@@ -7719,7 +7719,7 @@ fn trans_item(@local_ctxt cx, &ast::item item) {
77197719
auto sub_cx =
77207720
@rec(obj_typarams=tps, obj_fields=ob.fields
77217721
with *extend_path(cx, item.ident));
7722-
trans_obj(sub_cx, item.span, ob, ctor_id, tps, item.id);
7722+
trans_obj(sub_cx, item.span, ob, ctor_id, tps);
77237723
}
77247724
case (ast::item_mod(?m)) {
77257725
auto sub_cx =
@@ -7759,8 +7759,7 @@ fn get_pair_fn_ty(TypeRef llpairty) -> TypeRef {
77597759
}
77607760

77617761
fn decl_fn_and_pair(&@crate_ctxt ccx, &span sp, vec[str] path, str flav,
7762-
vec[ast::ty_param] ty_params, ast::node_id node_id,
7763-
ast::node_id def_id) {
7762+
vec[ast::ty_param] ty_params, ast::node_id node_id) {
77647763
auto llfty;
77657764
alt (ty::struct(ccx.tcx, node_id_type(ccx, node_id))) {
77667765
case (ty::ty_fn(?proto, ?inputs, ?output, _, _)) {
@@ -7784,7 +7783,7 @@ fn decl_fn_and_pair(&@crate_ctxt ccx, &span sp, vec[str] path, str flav,
77847783
// Declare the global constant pair that points to it.
77857784

77867785
let str ps = mangle_exported_name(ccx, path, node_id_type(ccx, node_id));
7787-
register_fn_pair(ccx, ps, llfty, llfn, def_id);
7786+
register_fn_pair(ccx, ps, llfty, llfn, node_id);
77887787
if (is_main) {
77897788
if (ccx.main_fn != none[ValueRef]) {
77907789
ccx.sess.span_fatal(sp, "multiple 'main' functions");
@@ -8046,12 +8045,11 @@ fn collect_item_2(&@crate_ctxt ccx, &@ast::item i, &vec[str] pt,
80468045
alt (i.node) {
80478046
case (ast::item_fn(?f, ?tps)) {
80488047
if (!ccx.obj_methods.contains_key(i.id)) {
8049-
decl_fn_and_pair(ccx, i.span, new_pt, "fn", tps, i.id, i.id);
8048+
decl_fn_and_pair(ccx, i.span, new_pt, "fn", tps, i.id);
80508049
}
80518050
}
80528051
case (ast::item_obj(?ob, ?tps, ?ctor_id)) {
8053-
decl_fn_and_pair(ccx, i.span, new_pt, "obj_ctor", tps, i.id,
8054-
ctor_id);
8052+
decl_fn_and_pair(ccx, i.span, new_pt, "obj_ctor", tps, ctor_id);
80558053
for (@ast::method m in ob.methods) {
80568054
ccx.obj_methods.insert(m.node.id, ());
80578055
}
@@ -8081,7 +8079,7 @@ fn collect_tag_ctor(@crate_ctxt ccx, &@ast::item i, &vec[str] pt,
80818079
if (vec::len[ast::variant_arg](variant.node.args) != 0u) {
80828080
decl_fn_and_pair(ccx, i.span,
80838081
new_pt + [variant.node.name], "tag", tps,
8084-
variant.node.id, variant.node.id);
8082+
variant.node.id);
80858083
}
80868084
}
80878085
}

src/comp/middle/typeck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ mod collect {
691691

692692
auto tpt =
693693
ty_of_obj_ctor(cx, it.ident, object, ctor_id, ty_params);
694-
write::ty_only(cx.tcx, it.id, tpt._1);
694+
write::ty_only(cx.tcx, ctor_id, tpt._1);
695695
// Write the methods into the type table.
696696
//
697697
// FIXME: Inefficient; this ends up calling

0 commit comments

Comments
 (0)