Skip to content

Commit c4e9d8f

Browse files
committed
rustc: Fail when #fmt is given too many arguments
1 parent 1b29892 commit c4e9d8f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/comp/front/extfmt.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ fn pieces_to_expr(parser p, vec[piece] pieces, vec[@ast::expr] args)
499499
auto sp = args.(0).span;
500500
auto n = 0u;
501501
auto tmp_expr = make_new_str(p, sp, "");
502+
auto nargs = vec::len[@ast::expr](args);
502503

503504
for (piece pc in pieces) {
504505
alt (pc) {
@@ -507,7 +508,7 @@ fn pieces_to_expr(parser p, vec[piece] pieces, vec[@ast::expr] args)
507508
tmp_expr = make_add_expr(p, sp, tmp_expr, s_expr);
508509
}
509510
case (piece_conv(?conv)) {
510-
if (n >= vec::len[@ast::expr](args)) {
511+
if (n >= nargs) {
511512
log_err "too many conversions in #fmt string";
512513
fail;
513514
}
@@ -524,6 +525,13 @@ fn pieces_to_expr(parser p, vec[piece] pieces, vec[@ast::expr] args)
524525
}
525526
}
526527

528+
auto expected_nargs = n + 1u; // n conversions + the fmt string
529+
if (expected_nargs < nargs) {
530+
log_err #fmt("too many arguments to #fmt. found %u, expected %u",
531+
nargs, expected_nargs);
532+
fail;
533+
}
534+
527535
// TODO: Remove this debug logging
528536
// log "dumping expanded ast:";
529537
// log pretty::print_expr(tmp_expr);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// xfail-stage0
2+
// error-pattern:too many arguments
3+
4+
use std;
5+
6+
fn main() {
7+
auto s = #fmt("%s", "test", "test");
8+
}

0 commit comments

Comments
 (0)