shuffle error messages in borrowck, and prevent it from spewing too many

also, fix a few minor issues it complains about
This commit is contained in:
Niko Matsakis 2012-05-23 11:55:32 -07:00
parent 248f8826a9
commit 9773a22119
11 changed files with 74 additions and 68 deletions

View File

@ -498,6 +498,8 @@ enum check_loan_ctxt = @{
bccx: borrowck_ctxt,
req_maps: req_maps,
reported: hashmap<ast::node_id, ()>,
// Keep track of whether we're inside a ctor, so as to
// allow mutating immutable fields in the same class if
// we are in a ctor, we track the self id
@ -525,6 +527,7 @@ fn check_loans(bccx: borrowck_ctxt,
crate: @ast::crate) {
let clcx = check_loan_ctxt(@{bccx: bccx,
req_maps: req_maps,
reported: int_hash(),
mut in_ctor: false,
mut is_pure: pc_impure});
let vt = visit::mk_vt(@{visit_expr: check_loans_in_expr,
@ -641,11 +644,9 @@ impl methods for check_loan_ctxt {
/*ok*/
}
ast::impure_fn | ast::unsafe_fn {
self.bccx.span_err(
self.report_purity_error(
expr.span,
"access to non-pure functions \
prohibited in a pure context");
self.report_why_pure();
"access to non-pure functions");
}
}
}
@ -739,11 +740,9 @@ impl methods for check_loan_ctxt {
// assigned, because it is uniquely tied to this function and
// is not visible from the outside
if self.is_pure != pc_impure && cmt.lp.is_none() {
self.bccx.span_err(
self.report_purity_error(
ex.span,
#fmt["%s prohibited in a pure context",
at.ing_form(self.bccx.cmt_to_str(cmt))]);
self.report_why_pure();
at.ing_form(self.bccx.cmt_to_str(cmt)));
}
// check for a conflicting loan as well, except in the case of
@ -776,19 +775,26 @@ impl methods for check_loan_ctxt {
self.bccx.add_to_mutbl_map(cmt);
}
fn report_why_pure() {
alt self.is_pure {
fn report_purity_error(sp: span, msg: str) {
alt copy self.is_pure {
pc_impure {
self.tcx().sess.bug("report_why_pure() called when impure");
self.tcx().sess.bug("report_purity_error() called when impure");
}
pc_declaration {
// fn was declared pure; no need to report this, I think
self.tcx().sess.span_err(
sp,
#fmt["%s prohibited in pure context", msg]);
}
pc_cmt(e) {
self.tcx().sess.span_note(
e.cmt.span,
#fmt["pure context is required due to an illegal borrow: %s",
self.bccx.bckerr_code_to_str(e.code)]);
if self.reported.insert(e.cmt.id, ()) {
self.tcx().sess.span_err(
e.cmt.span,
#fmt["illegal borrow unless pure: %s",
self.bccx.bckerr_code_to_str(e.code)]);
self.tcx().sess.span_note(
sp,
#fmt["impure due to %s", msg]);
}
}
}
}

View File

@ -717,7 +717,7 @@ fn follow_import(e: env, &&sc: scopes, path: [ident], sp: span) ->
let mut dcur = lookup_in_scope_strict(e, sc, sp, path[0], ns_module);
let mut i = 1u;
loop {
alt dcur {
alt copy dcur {
some(dcur_def) {
if i == path_len { break; }
dcur =
@ -725,7 +725,7 @@ fn follow_import(e: env, &&sc: scopes, path: [ident], sp: span) ->
ns_module, outside);
i += 1u;
}
_ { break; }
none { break; }
}
}
if i == path_len {

View File

@ -235,7 +235,7 @@ fn check_main_fn_ty(ccx: @crate_ctxt,
fn check_for_main_fn(ccx: @crate_ctxt, crate: @ast::crate) {
let tcx = ccx.tcx;
if !tcx.sess.building_library {
alt tcx.sess.main_fn {
alt copy tcx.sess.main_fn {
some((id, sp)) { check_main_fn_ty(ccx, id, sp); }
none { tcx.sess.span_err(crate.span, "main function not found"); }
}

View File

@ -8,8 +8,8 @@ fn borrow_from_arg_imm_ref(&&v: ~int) {
}
fn borrow_from_arg_mut_ref(&v: ~int) {
borrow(v); //! ERROR access to non-pure functions prohibited in a pure context
//!^ NOTE pure context is required due to an illegal borrow: unique value in aliasable, mutable location
borrow(v); //! ERROR illegal borrow unless pure: unique value in aliasable, mutable location
//!^ NOTE impure due to access to non-pure functions
}
fn borrow_from_arg_move(-v: ~int) {

View File

@ -30,8 +30,8 @@ fn process(_i: int) {}
fn match_const_box_and_do_bad_things(v: &const @const option<int>) {
alt *v {
@some(i) { //! NOTE pure context is required due to an illegal borrow: enum variant in aliasable, mutable location
process(i) //! ERROR access to non-pure functions prohibited in a pure context
@some(i) { //! ERROR illegal borrow unless pure: enum variant in aliasable, mutable location
process(i) //! NOTE impure due to access to non-pure functions
}
@none {}
}

View File

@ -36,8 +36,8 @@ fn match_const_reg_unused(v: &const option<int>) {
fn match_const_reg_impure(v: &const option<int>) {
alt *v {
some(i) {impure(i)} //! ERROR access to non-pure functions prohibited in a pure context
//!^ NOTE pure context is required due to an illegal borrow: enum variant in aliasable, mutable location
some(i) {impure(i)} //! ERROR illegal borrow unless pure: enum variant in aliasable, mutable location
//!^ NOTE impure due to access to non-pure functions
none {}
}
}

View File

@ -3,13 +3,13 @@
fn impure(_i: int) {}
// check that unchecked alone does not override borrowck:
fn foo(v: &const option<int>) {
alt *v {
some(i) {
//!^ NOTE pure context is required due to an illegal borrow: enum variant in aliasable, mutable location
// check that unchecked alone does not override borrowck:
//!^ ERROR illegal borrow unless pure: enum variant in aliasable, mutable location
unchecked {
impure(i); //! ERROR access to non-pure functions prohibited in a pure context
impure(i); //! NOTE impure due to access to non-pure functions
}
}
none {

View File

@ -4,23 +4,23 @@
fn borrow(_v: &int) {}
fn box_mut(v: @mut ~int) {
borrow(*v); //! ERROR access to non-pure functions prohibited in a pure context
//!^ NOTE pure context is required due to an illegal borrow: unique value in aliasable, mutable location
borrow(*v); //! ERROR illegal borrow unless pure: unique value in aliasable, mutable location
//!^ NOTE impure due to access to non-pure functions
}
fn box_rec_mut(v: @{mut f: ~int}) {
borrow(v.f); //! ERROR access to non-pure functions prohibited in a pure context
//!^ NOTE pure context is required due to an illegal borrow: unique value in aliasable, mutable location
borrow(v.f); //! ERROR illegal borrow unless pure: unique value in aliasable, mutable location
//!^ NOTE impure due to access to non-pure functions
}
fn box_mut_rec(v: @mut {f: ~int}) {
borrow(v.f); //! ERROR access to non-pure functions prohibited in a pure context
//!^ NOTE pure context is required due to an illegal borrow: unique value in aliasable, mutable location
borrow(v.f); //! ERROR illegal borrow unless pure: unique value in aliasable, mutable location
//!^ NOTE impure due to access to non-pure functions
}
fn box_mut_recs(v: @mut {f: {g: {h: ~int}}}) {
borrow(v.f.g.h); //! ERROR access to non-pure functions prohibited in a pure context
//!^ NOTE pure context is required due to an illegal borrow: unique value in aliasable, mutable location
borrow(v.f.g.h); //! ERROR illegal borrow unless pure: unique value in aliasable, mutable location
//!^ NOTE impure due to access to non-pure functions
}
fn box_imm(v: @~int) {
@ -36,28 +36,28 @@ fn box_imm_recs(v: @{f: {g: {h: ~int}}}) {
}
fn box_const(v: @const ~int) {
borrow(*v); //! ERROR access to non-pure functions prohibited in a pure context
//!^ NOTE pure context is required due to an illegal borrow: unique value in aliasable, mutable location
borrow(*v); //! ERROR illegal borrow unless pure: unique value in aliasable, mutable location
//!^ NOTE impure due to access to non-pure functions
}
fn box_rec_const(v: @{const f: ~int}) {
borrow(v.f); //! ERROR access to non-pure functions prohibited in a pure context
//!^ NOTE pure context is required due to an illegal borrow: unique value in aliasable, mutable location
borrow(v.f); //! ERROR illegal borrow unless pure: unique value in aliasable, mutable location
//!^ NOTE impure due to access to non-pure functions
}
fn box_recs_const(v: @{f: {g: {const h: ~int}}}) {
borrow(v.f.g.h); //! ERROR access to non-pure functions prohibited in a pure context
//!^ NOTE pure context is required due to an illegal borrow: unique value in aliasable, mutable location
borrow(v.f.g.h); //! ERROR illegal borrow unless pure: unique value in aliasable, mutable location
//!^ NOTE impure due to access to non-pure functions
}
fn box_const_rec(v: @const {f: ~int}) {
borrow(v.f); //! ERROR access to non-pure functions prohibited in a pure context
//!^ NOTE pure context is required due to an illegal borrow: unique value in aliasable, mutable location
borrow(v.f); //! ERROR illegal borrow unless pure: unique value in aliasable, mutable location
//!^ NOTE impure due to access to non-pure functions
}
fn box_const_recs(v: @const {f: {g: {h: ~int}}}) {
borrow(v.f.g.h); //! ERROR access to non-pure functions prohibited in a pure context
//!^ NOTE pure context is required due to an illegal borrow: unique value in aliasable, mutable location
borrow(v.f.g.h); //! ERROR illegal borrow unless pure: unique value in aliasable, mutable location
//!^ NOTE impure due to access to non-pure functions
}
fn main() {

View File

@ -3,23 +3,23 @@
fn borrow(_v: &int) {}
fn box_mut(v: &mut ~int) {
borrow(*v); //! ERROR access to non-pure functions prohibited in a pure context
//!^ NOTE pure context is required due to an illegal borrow: unique value in aliasable, mutable location
borrow(*v); //! ERROR illegal borrow unless pure: unique value in aliasable, mutable location
//!^ NOTE impure due to access to non-pure functions
}
fn box_rec_mut(v: &{mut f: ~int}) {
borrow(v.f); //! ERROR access to non-pure functions prohibited in a pure context
//!^ NOTE pure context is required due to an illegal borrow: unique value in aliasable, mutable location
borrow(v.f); //! ERROR illegal borrow unless pure: unique value in aliasable, mutable location
//!^ NOTE impure due to access to non-pure functions
}
fn box_mut_rec(v: &mut {f: ~int}) {
borrow(v.f); //! ERROR access to non-pure functions prohibited in a pure context
//!^ NOTE pure context is required due to an illegal borrow: unique value in aliasable, mutable location
borrow(v.f); //! ERROR illegal borrow unless pure: unique value in aliasable, mutable location
//!^ NOTE impure due to access to non-pure functions
}
fn box_mut_recs(v: &mut {f: {g: {h: ~int}}}) {
borrow(v.f.g.h); //! ERROR access to non-pure functions prohibited in a pure context
//!^ NOTE pure context is required due to an illegal borrow: unique value in aliasable, mutable location
borrow(v.f.g.h); //! ERROR illegal borrow unless pure: unique value in aliasable, mutable location
//!^ NOTE impure due to access to non-pure functions
}
fn box_imm(v: &~int) {
@ -35,28 +35,28 @@ fn box_imm_recs(v: &{f: {g: {h: ~int}}}) {
}
fn box_const(v: &const ~int) {
borrow(*v); //! ERROR access to non-pure functions prohibited in a pure context
//!^ NOTE pure context is required due to an illegal borrow: unique value in aliasable, mutable location
borrow(*v); //! ERROR illegal borrow unless pure: unique value in aliasable, mutable location
//!^ NOTE impure due to access to non-pure functions
}
fn box_rec_const(v: &{const f: ~int}) {
borrow(v.f); //! ERROR access to non-pure functions prohibited in a pure context
//!^ NOTE pure context is required due to an illegal borrow: unique value in aliasable, mutable location
borrow(v.f); //! ERROR illegal borrow unless pure: unique value in aliasable, mutable location
//!^ NOTE impure due to access to non-pure functions
}
fn box_recs_const(v: &{f: {g: {const h: ~int}}}) {
borrow(v.f.g.h); //! ERROR access to non-pure functions prohibited in a pure context
//!^ NOTE pure context is required due to an illegal borrow: unique value in aliasable, mutable location
borrow(v.f.g.h); //! ERROR illegal borrow unless pure: unique value in aliasable, mutable location
//!^ NOTE impure due to access to non-pure functions
}
fn box_const_rec(v: &const {f: ~int}) {
borrow(v.f); //! ERROR access to non-pure functions prohibited in a pure context
//!^ NOTE pure context is required due to an illegal borrow: unique value in aliasable, mutable location
borrow(v.f); //! ERROR illegal borrow unless pure: unique value in aliasable, mutable location
//!^ NOTE impure due to access to non-pure functions
}
fn box_const_recs(v: &const {f: {g: {h: ~int}}}) {
borrow(v.f.g.h); //! ERROR access to non-pure functions prohibited in a pure context
//!^ NOTE pure context is required due to an illegal borrow: unique value in aliasable, mutable location
borrow(v.f.g.h); //! ERROR illegal borrow unless pure: unique value in aliasable, mutable location
//!^ NOTE impure due to access to non-pure functions
}
fn main() {

View File

@ -3,7 +3,7 @@
fn g() { }
pure fn f(_q: int) -> bool {
g(); //! ERROR access to non-pure functions prohibited in a pure context
g(); //! ERROR access to non-pure functions prohibited in pure context
ret true;
}

View File

@ -1,16 +1,16 @@
// Check that pure functions cannot modify aliased state.
pure fn modify_in_ref(&&sum: {mut f: int}) {
sum.f = 3; //! ERROR assigning to mutable field prohibited in a pure context
sum.f = 3; //! ERROR assigning to mutable field prohibited in pure context
}
pure fn modify_in_box(sum: @mut {f: int}) {
sum.f = 3; //! ERROR assigning to mutable field prohibited in a pure context
sum.f = 3; //! ERROR assigning to mutable field prohibited in pure context
}
impl foo for int {
pure fn modify_in_box_rec(sum: @{mut f: int}) {
sum.f = self; //! ERROR assigning to mutable field prohibited in a pure context
sum.f = self; //! ERROR assigning to mutable field prohibited in pure context
}
}