Skip to content

Commit c7082ce

Browse files
committed
Require "self" as base expression for intra-class method or field references
All field or method references within a class must begin with "self." now. A bare reference to a field or method in the same class will no longer typecheck.
1 parent c2f28e2 commit c7082ce

24 files changed

+49
-162
lines changed

src/rustc/metadata/astencode.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,6 @@ impl of tr for ast::def {
443443
ast::def_class(did) {
444444
ast::def_class(did.tr(xcx))
445445
}
446-
ast::def_class_field(did0, did1) {
447-
ast::def_class_field(did0.tr(xcx), did1.tr(xcx))
448-
}
449-
ast::def_class_method(did0, did1) {
450-
ast::def_class_method(did0.tr(xcx), did1.tr(xcx))
451-
}
452446
ast::def_region(nid) { ast::def_region(xcx.tr_id(nid)) }
453447
}
454448
}

src/rustc/middle/mutbl.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ fn check_bind(cx: @ctx, f: @expr, args: [option<@expr>]) {
321321
fn is_illegal_to_modify_def(cx: @ctx, def: def, msg: msg) -> option<str> {
322322
alt def {
323323
def_fn(_, _) | def_mod(_) | def_native_mod(_) | def_const(_) |
324-
def_use(_) | def_class_method(_,_) {
324+
def_use(_) {
325325
some("static item")
326326
}
327327
def_arg(_, m) {
@@ -354,18 +354,6 @@ fn is_illegal_to_modify_def(cx: @ctx, def: def, msg: msg) -> option<str> {
354354
}
355355

356356
def_binding(_) { some("binding") }
357-
def_class_field(parent,fld) {
358-
if option::is_none(cx.in_ctor) {
359-
/* Enforce mutability *unless* we're inside a ctor */
360-
alt ty::lookup_class_field(cx.tcx, parent, fld).mutability {
361-
class_mutable { none }
362-
class_immutable { some("immutable class field") }
363-
}
364-
}
365-
else {
366-
none
367-
}
368-
}
369357
_ { none }
370358
}
371359
}

src/rustc/middle/resolve.rs

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ enum mod_index_entry {
8888
mie_view_item(ident, node_id, span),
8989
mie_import_ident(node_id, span),
9090
mie_item(@ast::item),
91-
mie_class_item(node_id, /* parent class name */
92-
@ast::class_member), /* class member */
9391
mie_native_item(@ast::native_item),
9492
mie_enum_variant(/* variant index */uint,
9593
/*parts of enum item*/ [variant],
@@ -1017,9 +1015,6 @@ fn lookup_in_scope(e: env, sc: scopes, sp: span, name: ident, ns: namespace,
10171015
ret some(ast::def_fn(local_def(ctor.node.id),
10181016
ast::impure_fn));
10191017
}
1020-
if ns == ns_val {
1021-
ret lookup_in_class(local_def(it.id), members, name);
1022-
}
10231018
// FIXME: AST allows other items to appear in a class,
10241019
// but that might not be wise
10251020
}
@@ -1156,30 +1151,6 @@ fn lookup_in_fn(e: env, name: ident, decl: ast::fn_decl,
11561151
}
11571152
}
11581153

1159-
/*
1160-
FIXME: not sure about this code. maybe this should be handled
1161-
using the mod_index stuff
1162-
*/
1163-
fn lookup_in_class(parent_id: def_id,
1164-
members: [@class_member], name: ident)
1165-
-> option<def> {
1166-
for m in members {
1167-
alt m.node {
1168-
instance_var(v_name,_,_,id,_) {
1169-
if v_name == name {
1170-
ret some(def_class_field(parent_id, local_def(id)));
1171-
}
1172-
}
1173-
class_method(i) {
1174-
if i.ident == name {
1175-
ret some(def_class_method(parent_id, local_def(i.id)));
1176-
}
1177-
}
1178-
}
1179-
}
1180-
ret none;
1181-
}
1182-
11831154
fn lookup_in_block(e: env, name: ident, sp: span, b: ast::blk_, pos: uint,
11841155
loc_pos: uint, ns: namespace) -> option<def> {
11851156

@@ -1559,18 +1530,6 @@ fn lookup_in_mie(e: env, mie: mod_index_entry, ns: namespace) ->
15591530
}
15601531
}
15611532
}
1562-
mie_class_item(parent_id, class_item) {
1563-
alt class_item.node {
1564-
instance_var(_,_,_,id,_) {
1565-
ret some(ast::def_class_field(local_def(parent_id),
1566-
local_def(id)));
1567-
}
1568-
class_method(it) {
1569-
ret some(ast::def_class_method(local_def(parent_id),
1570-
local_def(it.id)));
1571-
}
1572-
}
1573-
}
15741533
}
15751534
ret none;
15761535
}
@@ -1648,11 +1607,6 @@ fn index_mod(md: ast::_mod) -> mod_index {
16481607
node:
16491608
item_fn(ctor.node.dec, tps, ctor.node.body),
16501609
span: ctor.node.body.span}));
1651-
// add the members
1652-
for ci in items {
1653-
add_to_index(index, class_item_ident(ci),
1654-
mie_class_item(it.id, ci));
1655-
}
16561610
}
16571611
}
16581612
}
@@ -1678,8 +1632,7 @@ fn ns_for_def(d: def) -> namespace {
16781632
ast::def_variant(_, _) { ns_val }
16791633
ast::def_fn(_, _) | ast::def_self(_) |
16801634
ast::def_const(_) | ast::def_arg(_, _) | ast::def_local(_, _) |
1681-
ast::def_upvar(_, _, _) | ast::def_self(_) |
1682-
ast::def_class_field(_,_) | ast::def_class_method(_,_) { ns_val }
1635+
ast::def_upvar(_, _, _) | ast::def_self(_) { ns_val }
16831636
ast::def_mod(_) | ast::def_native_mod(_) { ns_module }
16841637
ast::def_ty(_) | ast::def_binding(_) | ast::def_use(_) |
16851638
ast::def_ty_param(_, _) | ast::def_prim_ty(_) | ast::def_class(_)
@@ -1761,7 +1714,6 @@ fn mie_span(mie: mod_index_entry) -> span {
17611714
mie_item(item) { item.span }
17621715
mie_enum_variant(_, _, _, span) { span }
17631716
mie_native_item(item) { item.span }
1764-
mie_class_item(_,item) { item.span }
17651717
};
17661718
}
17671719

src/rustc/middle/trans/base.rs

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,22 +2177,18 @@ fn trans_local_var(cx: block, def: ast::def) -> local_var_result {
21772177
}
21782178
}
21792179

2180-
// The third argument (path) ends up getting used when the id
2181-
// refers to a field within the enclosing class, since the name
2182-
// gets turned into a record field name.
2183-
fn trans_path(cx: block, id: ast::node_id, path: @ast::path)
2180+
fn trans_path(cx: block, id: ast::node_id)
21842181
-> lval_maybe_callee {
21852182
let _icx = cx.insn_ctxt("trans_path");
21862183
alt cx.tcx().def_map.find(id) {
21872184
none { cx.sess().bug("trans_path: unbound node ID"); }
21882185
some(df) {
2189-
ret trans_var(cx, df, id, path);
2186+
ret trans_var(cx, df, id);
21902187
}
21912188
}
21922189
}
21932190

2194-
fn trans_var(cx: block, def: ast::def, id: ast::node_id, path: @ast::path)
2195-
-> lval_maybe_callee {
2191+
fn trans_var(cx: block, def: ast::def, id: ast::node_id)-> lval_maybe_callee {
21962192
let _icx = cx.insn_ctxt("trans_var");
21972193
let ccx = cx.ccx();
21982194
alt def {
@@ -2225,28 +2221,6 @@ fn trans_var(cx: block, def: ast::def, id: ast::node_id, path: @ast::path)
22252221
ret lval_no_env(cx, load_if_immediate(cx, val, tp), owned_imm);
22262222
}
22272223
}
2228-
ast::def_class_field(parent, did) {
2229-
// base is implicitly "Self"
2230-
alt cx.fcx.llself {
2231-
some(slf) {
2232-
let base = cast_self(cx, slf);
2233-
let {bcx, val, kind} = trans_rec_field_inner(cx, base,
2234-
slf.t,
2235-
path_to_ident(path), path.span);
2236-
ret lval_no_env(bcx, val, kind);
2237-
}
2238-
_ { cx.sess().bug("unbound self param in class"); }
2239-
}
2240-
}
2241-
ast::def_class_method(parent, did) {
2242-
alt cx.fcx.llself {
2243-
some(slf) {
2244-
ret {env: self_env(slf.v, slf.t, none)
2245-
with lval_static_fn(cx, did, id)};
2246-
}
2247-
none { cx.sess().bug("unbound self param in class"); }
2248-
}
2249-
}
22502224
_ {
22512225
let loc = trans_local_var(cx, def);
22522226
ret lval_no_env(cx, loc.val, loc.kind);
@@ -2323,7 +2297,7 @@ fn expr_is_lval(bcx: block, e: @ast::expr) -> bool {
23232297
fn trans_callee(bcx: block, e: @ast::expr) -> lval_maybe_callee {
23242298
let _icx = bcx.insn_ctxt("trans_callee");
23252299
alt e.node {
2326-
ast::expr_path(path) { ret trans_path(bcx, e.id, path); }
2300+
ast::expr_path(path) { ret trans_path(bcx, e.id); }
23272301
ast::expr_field(base, ident, _) {
23282302
// Lval means this is a record field, so not a method
23292303
if !expr_is_lval(bcx, e) {
@@ -2350,8 +2324,8 @@ fn trans_callee(bcx: block, e: @ast::expr) -> lval_maybe_callee {
23502324
fn trans_lval(cx: block, e: @ast::expr) -> lval_result {
23512325
let _icx = cx.insn_ctxt("trans_lval");
23522326
alt e.node {
2353-
ast::expr_path(p) {
2354-
let v = trans_path(cx, e.id, p);
2327+
ast::expr_path(_) {
2328+
let v = trans_path(cx, e.id);
23552329
ret lval_maybe_callee_to_lval(v, expr_ty(cx, e));
23562330
}
23572331
ast::expr_field(base, ident, _) {

src/rustc/middle/typeck.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,6 @@ fn ty_param_bounds_and_ty_for_def(fcx: @fn_ctxt, sp: span, defn: ast::def) ->
170170
ast::def_upvar(_, inner, _) {
171171
ret ty_param_bounds_and_ty_for_def(fcx, sp, *inner);
172172
}
173-
ast::def_class_method(_, id) | ast::def_class_field(_, id) {
174-
if id.crate != ast::local_crate {
175-
fcx.ccx.tcx.sess.span_fatal(sp,
176-
"class method or field referred to \
177-
out of scope");
178-
}
179-
alt fcx.ccx.enclosing_class.find(id.node) {
180-
some(a_ty) { ret {bounds: @[], ty: a_ty}; }
181-
_ { fcx.ccx.tcx.sess.span_fatal(sp,
182-
"class method or field referred to \
183-
out of scope"); }
184-
}
185-
}
186-
187173
_ {
188174
// FIXME: handle other names.
189175
fcx.ccx.tcx.sess.unimpl("definition variant");

src/rustc/syntax/ast.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,6 @@ enum def {
8383
@def /* closed over def */,
8484
node_id /* expr node that creates the closure */),
8585
def_class(def_id),
86-
// first def_id is for parent class
87-
def_class_field(def_id, def_id),
88-
// No purity allowed for now, I guess
89-
// (simpler this way, b/c presumably methods read mut state)
90-
def_class_method(def_id, def_id),
9186
def_region(node_id)
9287
}
9388

src/rustc/syntax/ast_util.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ fn def_id_of_def(d: def) -> def_id {
4040
def_fn(id, _) | def_mod(id) |
4141
def_native_mod(id) | def_const(id) |
4242
def_variant(_, id) | def_ty(id) | def_ty_param(id, _) |
43-
def_use(id) |
44-
def_class(id) | def_class_field(_, id) | def_class_method(_, id) { id }
45-
43+
def_use(id) | def_class(id) { id }
4644
def_arg(id, _) | def_local(id, _) | def_self(id) |
4745
def_upvar(id, _, _) | def_binding(id) | def_region(id) {
4846
local_def(id)

src/test/auxiliary/cci_class.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class cat {
77

88
let how_hungry : int;
99

10-
new(in_x : uint, in_y : int) { meows = in_x; how_hungry = in_y; }
10+
new(in_x : uint, in_y : int) { self.meows = in_x; self.how_hungry = in_y; }
1111
}
1212

1313
}

src/test/auxiliary/cci_class_2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class cat {
77

88
let how_hungry : int;
99

10-
new(in_x : uint, in_y : int) { meows = in_x; how_hungry = in_y; }
10+
new(in_x : uint, in_y : int) { self.meows = in_x; self.how_hungry = in_y; }
1111

1212
fn speak() {}
1313
}

src/test/auxiliary/cci_class_3.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ class cat {
77

88
let how_hungry : int;
99

10-
new(in_x : uint, in_y : int) { meows = in_x; how_hungry = in_y; }
10+
new(in_x : uint, in_y : int) { self.meows = in_x; self.how_hungry = in_y; }
1111

12-
fn speak() { meows += 1u; }
13-
fn meow_count() -> uint { meows }
12+
fn speak() { self.meows += 1u; }
13+
fn meow_count() -> uint { self.meows }
1414

1515
}
1616

src/test/auxiliary/cci_class_4.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ class cat {
55
let mut meows : uint;
66
fn meow() {
77
#error("Meow");
8-
meows += 1u;
9-
if meows % 5u == 0u {
10-
how_hungry += 1;
8+
self.meows += 1u;
9+
if self.meows % 5u == 0u {
10+
self.how_hungry += 1;
1111
}
1212
}
1313
}
@@ -16,14 +16,14 @@ class cat {
1616
let name : str;
1717

1818
new(in_x : uint, in_y : int, in_name: str)
19-
{ meows = in_x; how_hungry = in_y; name = in_name; }
19+
{ self.meows = in_x; self.how_hungry = in_y; self.name = in_name; }
2020

21-
fn speak() { meow(); }
21+
fn speak() { self.meow(); }
2222

2323
fn eat() -> bool {
24-
if how_hungry > 0 {
24+
if self.how_hungry > 0 {
2525
#error("OM NOM NOM");
26-
how_hungry -= 2;
26+
self.how_hungry -= 2;
2727
ret true;
2828
}
2929
else {

src/test/auxiliary/cci_class_5.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class cat {
88

99
let how_hungry : int;
1010

11-
new(in_x : uint, in_y : int) { meows = in_x; how_hungry = in_y; }
11+
new(in_x : uint, in_y : int) { self.meows = in_x; self.how_hungry = in_y; }
1212
}
1313

1414
}

src/test/compile-fail/assign-to-method.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ class cat {
66

77
let how_hungry : int;
88

9-
fn speak() { meows += 1u; }
10-
new(in_x : uint, in_y : int) { meows = in_x; how_hungry = in_y; }
9+
fn speak() { self.meows += 1u; }
10+
new(in_x : uint, in_y : int) { self.meows = in_x; self.how_hungry = in_y; }
1111
}
1212

1313
fn main() {

src/test/compile-fail/ctor-uninit-var.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ class cat {
77
let how_hungry : int;
88

99
fn eat() {
10-
how_hungry -= 5;
10+
self.how_hungry -= 5;
1111
}
1212

1313
new(in_x : uint, in_y : int) {
1414
let foo;
15-
meows = in_x + (in_y as uint);
16-
how_hungry = foo;
15+
self.meows = in_x + (in_y as uint);
16+
self.how_hungry = foo;
1717
}
1818
}
1919

src/test/compile-fail/mutable-class-fields-2.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// error-pattern:assigning to immutable class field
1+
// error-pattern:assigning to immutable field
22
class cat {
33
priv {
44
let mutable meows : uint;
@@ -7,10 +7,10 @@ class cat {
77
let how_hungry : int;
88

99
fn eat() {
10-
how_hungry -= 5;
10+
self.how_hungry -= 5;
1111
}
1212

13-
new(in_x : uint, in_y : int) { meows = in_x; how_hungry = in_y; }
13+
new(in_x : uint, in_y : int) { self.meows = in_x; self.how_hungry = in_y; }
1414
}
1515

1616
fn main() {

src/test/compile-fail/mutable-class-fields.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class cat {
66

77
let how_hungry : int;
88

9-
new(in_x : uint, in_y : int) { meows = in_x; how_hungry = in_y; }
9+
new(in_x : uint, in_y : int) { self.meows = in_x; self.how_hungry = in_y; }
1010
}
1111

1212
fn main() {

src/test/compile-fail/private-class-field.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class cat {
66

77
let how_hungry : int;
88

9-
new(in_x : uint, in_y : int) { meows = in_x; how_hungry = in_y; }
9+
new(in_x : uint, in_y : int) { self.meows = in_x; self.how_hungry = in_y; }
1010
}
1111

1212
fn main() {

0 commit comments

Comments
 (0)