Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libsyntax: Implement assert as a macro (called fail_unless! on a transitionary basis to avoid conflicting with the keyword right now) #4582

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/libcore/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#[forbid(deprecated_pattern)];
#[warn(non_camel_case_types)];

use cast::transmute;
use cast;
use cmp::{Eq, Ord};
use iter::BaseIter;
Expand Down Expand Up @@ -477,14 +478,18 @@ pub fn shift<T>(v: &mut ~[T]) -> T unsafe {
// Memcopy the head element (the one we want) to the location we just
// popped. For the moment it unsafely exists at both the head and last
// positions
let first_slice = view(*v, 0, 1);
let last_slice = mut_view(*v, next_ln, ln);
raw::copy_memory(last_slice, first_slice, 1);
{
let first_slice = view(*v, 0, 1);
let last_slice = view(*v, next_ln, ln);
raw::copy_memory(transmute(last_slice), first_slice, 1);
}

// Memcopy everything to the left one element
let init_slice = mut_view(*v, 0, next_ln);
let tail_slice = view(*v, 1, ln);
raw::copy_memory(init_slice, tail_slice, next_ln);
{
let init_slice = view(*v, 0, next_ln);
let tail_slice = view(*v, 1, ln);
raw::copy_memory(transmute(init_slice), tail_slice, next_ln);
}

// Set the new length. Now the vector is back to normal
raw::set_len(&mut *v, next_ln);
Expand Down
22 changes: 9 additions & 13 deletions src/librustc/middle/borrowck/check_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ use core::prelude::*;
use middle::borrowck::{Loan, bckerr, borrowck_ctxt, cmt, inherent_mutability};
use middle::borrowck::{req_maps, save_and_restore};
use middle::mem_categorization::{cat_arg, cat_binding, cat_comp, cat_deref};
use middle::mem_categorization::{cat_local, cat_rvalue, cat_special, gc_ptr};
use middle::mem_categorization::{loan_path, lp_arg, lp_comp, lp_deref};
use middle::mem_categorization::{lp_local};
use middle::mem_categorization::{cat_local, cat_rvalue, cat_self};
use middle::mem_categorization::{cat_special, gc_ptr, loan_path, lp_arg};
use middle::mem_categorization::{lp_comp, lp_deref, lp_local};
use middle::ty::{CopyValue, MoveValue, ReadValue};
use middle::ty;
use util::ppaux::ty_to_str;
Expand Down Expand Up @@ -296,12 +296,11 @@ impl check_loan_ctxt {
}

match (old_loan.mutbl, new_loan.mutbl) {
(m_const, _) | (_, m_const) |
(m_mutbl, m_mutbl) | (m_imm, m_imm) => {
(m_const, _) | (_, m_const) | (m_imm, m_imm) => {
/*ok*/
}

(m_mutbl, m_imm) | (m_imm, m_mutbl) => {
(m_mutbl, m_mutbl) | (m_mutbl, m_imm) | (m_imm, m_mutbl) => {
self.bccx.span_err(
new_loan.cmt.span,
fmt!("loan of %s as %s \
Expand Down Expand Up @@ -418,8 +417,8 @@ impl check_loan_ctxt {

for self.walk_loans_of(ex.id, lp) |loan| {
match loan.mutbl {
m_mutbl | m_const => { /*ok*/ }
m_imm => {
m_const => { /*ok*/ }
m_mutbl | m_imm => {
self.bccx.span_err(
ex.span,
fmt!("%s prohibited due to outstanding loan",
Expand All @@ -445,7 +444,7 @@ impl check_loan_ctxt {
self.check_for_loan_conflicting_with_assignment(
at, ex, cmt, lp_base);
}
lp_comp(*) | lp_local(*) | lp_arg(*) | lp_deref(*) => ()
lp_comp(*) | lp_self | lp_local(*) | lp_arg(*) | lp_deref(*) => ()
}
}

Expand Down Expand Up @@ -482,16 +481,13 @@ impl check_loan_ctxt {

match cmt.cat {
// Rvalues, locals, and arguments can be moved:
cat_rvalue | cat_local(_) | cat_arg(_) => {}
cat_rvalue | cat_local(_) | cat_arg(_) | cat_self(_) => {}

// We allow moving out of static items because the old code
// did. This seems consistent with permitting moves out of
// rvalues, I guess.
cat_special(sk_static_item) => {}

// We allow moving out of explicit self only.
cat_special(sk_self) => {}

cat_deref(_, _, unsafe_ptr) => {}

// Nothing else.
Expand Down
37 changes: 34 additions & 3 deletions src/librustc/middle/borrowck/gather_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,13 +479,44 @@ impl gather_loan_ctxt {
return;
}

// Normally we wouldn't allow `re_free` here. However, in this case
// it should be sound. Below is nmatsakis' reasoning:
//
// Perhaps [this permits] a function kind of like this one here, which
// consumes one mut pointer and returns a narrower one:
//
// struct Foo { f: int }
// fn foo(p: &v/mut Foo) -> &v/mut int { &mut p.f }
//
// I think this should work fine but there is more subtlety to it than
// I at first imagined. Unfortunately it's a very important use case,
// I think, so it really ought to work. The changes you [pcwalton]
// made to permit re_free() do permit this case, I think, but I'm not
// sure what else they permit. I have to think that over a bit.
//
// Ordinarily, a loan with scope re_free wouldn't make sense, because
// you couldn't enforce it. But in this case, your function signature
// informs the caller that you demand exclusive access to p and its
// contents for the lifetime v. Since borrowed pointers are
// non-copyable, they must have (a) made a borrow which will enforce
// those conditions and then (b) given you the resulting pointer.
// Therefore, they should be respecting the loan. So it actually seems
// that it's ok in this case to have a loan with re_free, so long as
// the scope of the loan is no greater than the region pointer on
// which it is based. Neat but not something I had previously
// considered all the way through. (Note that we already rely on
// similar reasoning to permit you to return borrowed pointers into
// immutable structures, this is just the converse I suppose)

let scope_id = match scope_r {
ty::re_scope(scope_id) => scope_id,
ty::re_scope(scope_id) | ty::re_free(scope_id, _) => scope_id,
_ => {
self.bccx.tcx.sess.span_bug(
cmt.span,
fmt!("loans required but scope is scope_region is %s",
region_to_str(self.tcx(), scope_r)));
fmt!("loans required but scope is scope_region is %s \
(%?)",
region_to_str(self.tcx(), scope_r),
scope_r));
}
};

Expand Down
17 changes: 13 additions & 4 deletions src/librustc/middle/borrowck/loan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use core::prelude::*;
use middle::borrowck::{Loan, bckres, borrowck_ctxt, cmt, err_mutbl};
use middle::borrowck::{err_out_of_scope};
use middle::mem_categorization::{cat_arg, cat_binding, cat_discr, cat_comp};
use middle::mem_categorization::{cat_deref, cat_discr, cat_local};
use middle::mem_categorization::{cat_deref, cat_discr, cat_local, cat_self};
use middle::mem_categorization::{cat_special, cat_stack_upvar, comp_field};
use middle::mem_categorization::{comp_index, comp_variant, gc_ptr};
use middle::mem_categorization::{region_ptr};
Expand Down Expand Up @@ -121,7 +121,7 @@ impl LoanContext {
cmt.span,
~"rvalue with a non-none lp");
}
cat_local(local_id) | cat_arg(local_id) => {
cat_local(local_id) | cat_arg(local_id) | cat_self(local_id) => {
let local_scope_id = self.tcx().region_map.get(local_id);
self.issue_loan(cmt, ty::re_scope(local_scope_id), req_mutbl)
}
Expand Down Expand Up @@ -162,9 +162,18 @@ impl LoanContext {
// then the memory is freed.
self.loan_unstable_deref(cmt, cmt_base, req_mutbl)
}
cat_deref(cmt_base, _, region_ptr(ast::m_mutbl, region)) => {
// Mutable data can be loaned out as immutable or const. We must
// loan out the base as well as the main memory. For example,
// if someone borrows `*b`, we want to borrow `b` as immutable
// as well.
do self.loan(cmt_base, m_imm).chain |_| {
self.issue_loan(cmt, region, m_const)
}
}
cat_deref(_, _, unsafe_ptr) |
cat_deref(_, _, gc_ptr(_)) |
cat_deref(_, _, region_ptr(_)) => {
cat_deref(_, _, region_ptr(_, _)) => {
// Aliased data is simply not lendable.
self.bccx.tcx.sess.span_bug(
cmt.span,
Expand Down Expand Up @@ -251,7 +260,7 @@ impl LoanContext {
// Variant components: the base must be immutable, because
// if it is overwritten, the types of the embedded data
// could change.
do self.loan(cmt_base, m_imm).chain |_ok| {
do self.loan(cmt_base, m_imm).chain |_| {
// can use static, as in loan_stable_comp()
self.issue_loan(cmt, ty::re_static, req_mutbl)
}
Expand Down
9 changes: 6 additions & 3 deletions src/librustc/middle/borrowck/preserve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use middle::borrowck::{cmt, err_mut_uniq, err_mut_variant};
use middle::borrowck::{err_out_of_root_scope, err_out_of_scope};
use middle::borrowck::{err_root_not_permitted};
use middle::mem_categorization::{cat_arg, cat_binding, cat_comp, cat_deref};
use middle::mem_categorization::{cat_discr, cat_local, cat_special};
use middle::mem_categorization::{cat_discr, cat_local, cat_self, cat_special};
use middle::mem_categorization::{cat_stack_upvar, comp_field, comp_index};
use middle::mem_categorization::{comp_variant, gc_ptr, region_ptr};
use middle::ty;
Expand Down Expand Up @@ -90,7 +90,6 @@ priv impl &preserve_ctxt {
let _i = indenter();

match cmt.cat {
cat_special(sk_self) |
cat_special(sk_implicit_self) |
cat_special(sk_heap_upvar) => {
self.compare_scope(cmt, ty::re_scope(self.item_ub))
Expand Down Expand Up @@ -148,6 +147,10 @@ priv impl &preserve_ctxt {
let local_scope_id = self.tcx().region_map.get(local_id);
self.compare_scope(cmt, ty::re_scope(local_scope_id))
}
cat_self(local_id) => {
let local_scope_id = self.tcx().region_map.get(local_id);
self.compare_scope(cmt, ty::re_scope(local_scope_id))
}
cat_comp(cmt_base, comp_field(*)) |
cat_comp(cmt_base, comp_index(*)) |
cat_comp(cmt_base, comp_tuple) |
Expand All @@ -171,7 +174,7 @@ priv impl &preserve_ctxt {
// freed, so require imm.
self.require_imm(cmt, cmt_base, err_mut_uniq)
}
cat_deref(_, _, region_ptr(region)) => {
cat_deref(_, _, region_ptr(_, region)) => {
// References are always "stable" for lifetime `region` by
// induction (when the reference of type &MT was created,
// the memory must have been stable).
Expand Down
Loading