Require the parameter that will be referenced to be noted

Issue #918
This commit is contained in:
Marijn Haverbeke 2011-09-15 16:15:17 +02:00
parent 75eee8b194
commit 3abe3671bd
9 changed files with 74 additions and 38 deletions

View file

@ -383,7 +383,7 @@ tag ret_style {
noreturn; // functions with return type _|_ that always
// raise an error or exit (i.e. never return to the caller)
return_val; // everything else
return_ref(bool);
return_ref(bool, uint);
}
type _fn = {decl: fn_decl, proto: proto, body: blk};

View file

@ -215,7 +215,7 @@ fn ternary_to_if(e: @expr) -> @expr {
fn ret_by_ref(style: ret_style) -> bool {
alt style {
return_ref(_) { true }
return_ref(_, _) { true }
_ { false }
}
}

View file

@ -285,7 +285,7 @@ fn parse_ty_fn(proto: ast::proto, p: parser) -> ast::ty_ {
// FIXME: there's no syntax for this right now anyway
// auto constrs = parse_constrs(~[], p);
let constrs: [@ast::constr] = [];
let (ret_style, ret_ty) = parse_ret_ty(p);
let (ret_style, ret_ty) = parse_ret_ty(p, vec::len(inputs.node));
ret ast::ty_fn(proto, inputs.node, ret_ty, ret_style, constrs);
}
@ -437,7 +437,7 @@ fn parse_ty_postfix(orig_t: ast::ty_, p: parser, colons_before_params: bool)
}
}
fn parse_ret_ty(p: parser) -> (ast::ret_style, @ast::ty) {
fn parse_ret_ty(p: parser, n_args: uint) -> (ast::ret_style, @ast::ty) {
ret if eat(p, token::RARROW) {
let lo = p.get_lo_pos();
if eat(p, token::NOT) {
@ -445,7 +445,20 @@ fn parse_ret_ty(p: parser) -> (ast::ret_style, @ast::ty) {
} else {
let style = ast::return_val;
if eat(p, token::BINOP(token::AND)) {
style = ast::return_ref(eat(p, token::NOT));
if n_args == 0u {
p.fatal("can not return reference from argument-less fn");
}
let mut_root = eat(p, token::NOT), arg = 0u;
alt p.peek() {
token::LIT_INT(val) { p.bump(); arg = val as uint; }
_ { if n_args > 1u {
p.fatal("must specify referenced parameter");
} }
}
if arg >= n_args {
p.fatal("referenced argument does not exist");
}
style = ast::return_ref(mut_root, arg);
};
(style, parse_ty(p, false))
}
@ -1734,7 +1747,7 @@ fn parse_fn_decl(p: parser, purity: ast::purity, il: ast::inlineness) ->
p.bump();
constrs = parse_constrs(bind parse_ty_constr(inputs.node, _), p);
}
let (ret_style, ret_ty) = parse_ret_ty(p);
let (ret_style, ret_ty) = parse_ret_ty(p, vec::len(inputs.node));
ret {inputs: inputs.node,
output: ret_ty,
purity: purity,

View file

@ -1145,7 +1145,12 @@ fn print_fn_args_and_ret(s: ps, decl: ast::fn_decl, constrs: [@ast::constr]) {
space_if_not_bol(s);
word_space(s, "->");
alt decl.cf {
ast::return_ref(mut) { word(s.s, mut ? "&!" : "&"); }
ast::return_ref(mut, arg) {
word(s.s, mut ? "&!" : "&");
if vec::len(decl.inputs) > 1u {
word(s.s, std::uint::str(arg));
}
}
_ {}
}
print_type(s, decl.output);
@ -1347,7 +1352,10 @@ fn print_ty_fn(s: ps, proto: ast::proto, id: option::t<ast::ident>,
word_nbsp(s, "!");
} else {
alt cf {
ast::return_ref(mut) { word(s.s, mut ? "&!" : "&"); }
ast::return_ref(mut, arg) {
word(s.s, mut ? "&!" : "&");
if vec::len(inputs) > 1u { word(s.s, std::uint::str(arg)); }
}
_ {}
}
print_type(s, output);