Skip to content

Commit

Permalink
remove alias analysis and replace with borrowck
Browse files Browse the repository at this point in the history
This reverts commit 7ef825b.
  • Loading branch information
nikomatsakis committed Jun 9, 2012
1 parent 1351117 commit 013fc92
Show file tree
Hide file tree
Showing 50 changed files with 27 additions and 1,133 deletions.
78 changes: 1 addition & 77 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -1382,83 +1382,7 @@ gets access to them.

## Safe references

There is one catch with this approach: sometimes the compiler can
*not* statically guarantee that the argument value at the caller side
will survive to the end of the call. Another argument might indirectly
refer to it and be used to overwrite it, or a closure might assign a
new value to it.

Fortunately, Rust tasks are single-threaded worlds, which share no
data with other tasks, and most data is immutable. This allows most
argument-passing situations to be proved safe without further
difficulty.

Take the following program:

~~~~
# fn get_really_big_record() -> int { 1 }
# fn myfunc(a: int) {}
fn main() {
let x = get_really_big_record();
myfunc(x);
}
~~~~

Here we know for sure that no one else has access to the `x` variable
in `main`, so we're good. But the call could also look like this:

~~~~
# fn myfunc(a: int, b: fn()) {}
# fn get_another_record() -> int { 1 }
# let mut x = 1;
myfunc(x, {|| x = get_another_record(); });
~~~~

Now, if `myfunc` first calls its second argument and then accesses its
first argument, it will see a different value from the one that was
passed to it.

In such a case, the compiler will insert an implicit copy of `x`,
*except* if `x` contains something mutable, in which case a copy would
result in code that behaves differently. If copying `x` might be
expensive (for example, if it holds a vector), the compiler will emit
a warning.

There are even more tricky cases, in which the Rust compiler is forced
to pessimistically assume a value will get mutated, even though it is
not sure.

~~~~
fn for_each(v: [mut @int], iter: fn(@int)) {
for v.each {|elt| iter(elt); }
}
~~~~

For all this function knows, calling `iter` (which is a closure that
might have access to the vector that's passed as `v`) could cause the
elements in the vector to be mutated, with the effect that it can not
guarantee that the boxes will live for the duration of the call. So it
has to copy them. In this case, this will happen implicitly (bumping a
reference count is considered cheap enough to not warn about it).

## The copy operator

If the `for_each` function given above were to take a vector of
`{mut a: int}` instead of `@int`, it would not be able to
implicitly copy, since if the `iter` function changes a copy of a
mutable record, the changes won't be visible in the record itself. If
we *do* want to allow copies there, we have to explicitly allow it
with the `copy` operator:

~~~~
type mutrec = {mut x: int};
fn for_each(v: [mut mutrec], iter: fn(mutrec)) {
for v.each {|elt| iter(copy elt); }
}
~~~~

Adding a `copy` operator is also the way to muffle warnings about
implicit copies.
*This system has recently changed. An explanantion is forthcoming.*

## Other uses of safe references

Expand Down
19 changes: 3 additions & 16 deletions src/rustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,6 @@ fn compile_upto(sess: session, cfg: ast::crate_cfg,
let (root_map, mutbl_map) = time(
time_passes, "borrow checking",
bind middle::borrowck::check_crate(ty_cx, method_map, crate));
let (copy_map, _ref_map) =
time(time_passes, "alias checking",
bind middle::alias::check_crate(ty_cx, crate));
time(time_passes, "kind checking",
bind kind::check_crate(ty_cx, method_map, last_use_map, crate));
time(time_passes, "lint checking",
Expand All @@ -216,7 +213,7 @@ fn compile_upto(sess: session, cfg: ast::crate_cfg,
let outputs = option::get(outputs);

let maps = {mutbl_map: mutbl_map, root_map: root_map,
copy_map: copy_map, last_use_map: last_use_map,
last_use_map: last_use_map,
impl_map: impl_map, method_map: method_map,
vtable_map: vtable_map};

Expand Down Expand Up @@ -448,14 +445,6 @@ fn build_session_options(match: getopts::match,
let sysroot_opt = getopts::opt_maybe_str(match, "sysroot");
let target_opt = getopts::opt_maybe_str(match, "target");
let save_temps = getopts::opt_present(match, "save-temps");
let borrowck = alt getopts::opt_maybe_str(match, "borrowck") {
none { 0u }
some("warn") { 1u }
some("err") { 2u }
some(_) {
early_error(demitter, "borrowck may be warn or err")
}
};
alt output_type {
// unless we're emitting huamn-readable assembly, omit comments.
link::output_type_llvm_assembly | link::output_type_assembly {}
Expand Down Expand Up @@ -504,8 +493,7 @@ fn build_session_options(match: getopts::match,
test: test,
parse_only: parse_only,
no_trans: no_trans,
debugging_opts: debugging_opts,
borrowck: borrowck};
debugging_opts: debugging_opts};
ret sopts;
}

Expand Down Expand Up @@ -582,8 +570,7 @@ fn opts() -> [getopts::opt] {
optmulti("Z"),

optmulti("cfg"), optflag("test"),
optflag("lib"), optflag("bin"), optflag("static"), optflag("gc"),
optopt("borrowck")];
optflag("lib"), optflag("bin"), optflag("static"), optflag("gc")];
}

type output_filenames = @{out_filename: str, obj_filename:str};
Expand Down
6 changes: 1 addition & 5 deletions src/rustc/driver/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ type options =
no_trans: bool,

debugging_opts: uint,

// temporary hack: 0=off,1=warn,2=err --> if 2, alias is disabled
borrowck: uint,
};

type crate_metadata = {name: str, data: [u8]};
Expand Down Expand Up @@ -181,8 +178,7 @@ fn basic_options() -> @options {
test: false,
parse_only: false,
no_trans: false,
debugging_opts: 0u,
borrowck: 0u,
debugging_opts: 0u
}
}

Expand Down
1 change: 0 additions & 1 deletion src/rustc/metadata/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ enum astencode_tag { // Reserves 0x50 -- 0x6f
tag_table_param_bounds,
tag_table_inferred_modes,
tag_table_mutbl,
tag_table_copy,
tag_table_last_use,
tag_table_spill,
tag_table_method_map,
Expand Down
Loading

0 comments on commit 013fc92

Please sign in to comment.