Skip to content

Commit 53ea2a4

Browse files
committed
stdlib: Use spans for #fmt errors originating in std
Issue #444
1 parent a177dc4 commit 53ea2a4

File tree

5 files changed

+34
-11
lines changed

5 files changed

+34
-11
lines changed

src/comp/front/extfmt.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,17 @@ fn expand_syntax_ext(&ext_ctxt cx,
2828
}
2929

3030
auto fmt = expr_to_str(cx, args.(0));
31+
auto fmtspan = args.(0).span;
3132

3233
log "Format string:";
3334
log fmt;
3435

35-
auto pieces = parse_fmt_string(fmt);
36+
fn parse_fmt_err_(&ext_ctxt cx, common::span sp, str msg) -> ! {
37+
cx.span_err(sp, msg);
38+
}
39+
40+
auto parse_fmt_err = bind parse_fmt_err_(cx, fmtspan, _);
41+
auto pieces = parse_fmt_string(fmt, parse_fmt_err);
3642
auto args_len = vec::len[@ast::expr](args);
3743
auto fmt_args = vec::slice[@ast::expr](args, 1u, args_len - 1u);
3844
ret pieces_to_expr(cx, sp, pieces, args);

src/lib/extfmt.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ mod ct {
7878
piece_conv(conv);
7979
}
8080

81-
fn parse_fmt_string(str s) -> vec[piece] {
81+
type error_fn = fn (str) -> !;
82+
83+
fn parse_fmt_string(str s, error_fn error) -> vec[piece] {
8284
let vec[piece] pieces = [];
8385
auto lim = str::byte_len(s);
8486
auto buf = "";
@@ -97,15 +99,14 @@ mod ct {
9799
if (str::eq(curr, "%")) {
98100
i += 1u;
99101
if (i >= lim) {
100-
log_err "unterminated conversion at end of string";
101-
fail;
102+
error("unterminated conversion at end of string");
102103
}
103104
auto curr2 = str::substr(s, i, 1u);
104105
if (str::eq(curr2, "%")) {
105106
i += 1u;
106107
} else {
107108
buf = flush_buf(buf, pieces);
108-
auto res = parse_conversion(s, i, lim);
109+
auto res = parse_conversion(s, i, lim, error);
109110
pieces += [res._0];
110111
i = res._1;
111112
}
@@ -141,12 +142,13 @@ mod ct {
141142
};
142143
}
143144

144-
fn parse_conversion(str s, uint i, uint lim) -> tup(piece, uint) {
145+
fn parse_conversion(str s, uint i, uint lim,
146+
error_fn error) -> tup(piece, uint) {
145147
auto parm = parse_parameter(s, i, lim);
146148
auto flags = parse_flags(s, parm._1, lim);
147149
auto width = parse_count(s, flags._1, lim);
148150
auto prec = parse_precision(s, width._1, lim);
149-
auto ty = parse_type(s, prec._1, lim);
151+
auto ty = parse_type(s, prec._1, lim, error);
150152
ret tup(piece_conv(rec(param = parm._0,
151153
flags = flags._0,
152154
width = width._0,
@@ -258,10 +260,9 @@ mod ct {
258260
};
259261
}
260262

261-
fn parse_type(str s, uint i, uint lim) -> tup(ty, uint) {
263+
fn parse_type(str s, uint i, uint lim, error_fn error) -> tup(ty, uint) {
262264
if (i >= lim) {
263-
log_err "missing type in conversion";
264-
fail;
265+
error("missing type in conversion");
265266
}
266267

267268
auto tstr = str::substr(s, i, 1u);
@@ -287,7 +288,8 @@ mod ct {
287288
} else if (str::eq(tstr, "o")) {
288289
ty_octal
289290
} else {
290-
log_err "unknown type in conversion";
291+
// FIXME: Shouldn't need explicit fail here. Issue #542
292+
error("unknown type in conversion: " + tstr);
291293
fail
292294
};
293295

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// error-pattern:missing type
2+
3+
fn main() {
4+
#fmt("%+");
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// error-pattern:unknown type
2+
3+
fn main() {
4+
#fmt("%w");
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// error-pattern:unterminated conversion
2+
3+
fn main() {
4+
#fmt("%");
5+
}

0 commit comments

Comments
 (0)