Skip to content

Commit 7b0bafe

Browse files
committed
Don't accept none str literals for the format string in writeln
1 parent a77dfcc commit 7b0bafe

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

src/libcore/macros.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -396,18 +396,16 @@ macro_rules! write {
396396
/// ```
397397
#[macro_export]
398398
#[stable(feature = "rust1", since = "1.0.0")]
399+
#[allow_internal_unstable]
399400
macro_rules! writeln {
400401
($dst:expr) => (
401402
write!($dst, "\n")
402403
);
403404
($dst:expr,) => (
404405
writeln!($dst)
405406
);
406-
($dst:expr, $fmt:expr) => (
407-
write!($dst, concat!($fmt, "\n"))
408-
);
409-
($dst:expr, $fmt:expr, $($arg:tt)*) => (
410-
write!($dst, concat!($fmt, "\n"), $($arg)*)
407+
($dst:expr, $($arg:tt)*) => (
408+
$dst.write_fmt(format_args_nl!($($arg)*))
411409
);
412410
}
413411

src/test/ui/macros/issue-30143.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::fmt::Write;
12+
13+
fn main() {
14+
println!(0);
15+
//~^ ERROR format argument must be a string literal
16+
eprintln!('a');
17+
//~^ ERROR format argument must be a string literal
18+
let mut s = String::new();
19+
writeln!(s, true).unwrap();
20+
//~^ ERROR format argument must be a string literal
21+
}

src/test/ui/macros/issue-30143.stderr

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error: format argument must be a string literal
2+
--> $DIR/issue-30143.rs:14:14
3+
|
4+
LL | println!(0);
5+
| ^
6+
help: you might be missing a string literal to format with
7+
|
8+
LL | println!("{}", 0);
9+
| ^^^^^
10+
11+
error: format argument must be a string literal
12+
--> $DIR/issue-30143.rs:16:15
13+
|
14+
LL | eprintln!('a');
15+
| ^^^
16+
help: you might be missing a string literal to format with
17+
|
18+
LL | eprintln!("{}", 'a');
19+
| ^^^^^
20+
21+
error: format argument must be a string literal
22+
--> $DIR/issue-30143.rs:19:17
23+
|
24+
LL | writeln!(s, true).unwrap();
25+
| ^^^^
26+
help: you might be missing a string literal to format with
27+
|
28+
LL | writeln!(s, "{}", true).unwrap();
29+
| ^^^^^
30+
31+
error: aborting due to 3 previous errors
32+

0 commit comments

Comments
 (0)