-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #24370 - pnkfelix:fsk-unary-panic, r=<try>
Ensure a sole string-literal passed to `panic!` is not a fmt string. To accomplish this, adds `ensure_not_fmt_string_literal!` macro that will fail the compile if its expression argument is a fmt string literal. Since this is making a certain kind of use of `panic!` illegal, it is a: [breaking-change] In particular, a panic like this: ```rust panic!("Is it stringified code: { or is it a ill-formed fmt arg? }"); ``` must be rewritten; one easy rewrite is to add parentheses: ```rust panic!(("Is it stringified code: { or is it a ill-formed fmt arg? }")); ``` ---- Fix #22932.
- Loading branch information
Showing
6 changed files
with
166 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// Issue 22932: `panic!("{}");` should not compile. | ||
|
||
pub fn f1() { panic!("this does not work {}"); | ||
//~^ ERROR panic! input cannot be format string literal | ||
} | ||
|
||
pub fn workaround_1() { | ||
panic!(("This *does* works {}")); | ||
} | ||
|
||
pub fn workaround_2() { | ||
const MSG: &'static str = "This *does* work {}"; | ||
panic!(MSG); | ||
} | ||
|
||
pub fn f2() { panic!("this does not work {"); | ||
//~^ ERROR panic! input cannot be format string literal | ||
} | ||
|
||
pub fn f3() { panic!("nor this }"); | ||
//~^ ERROR panic! input cannot be format string literal | ||
} | ||
|
||
pub fn f4() { panic!("nor this {{"); | ||
//~^ ERROR panic! input cannot be format string literal | ||
} | ||
|
||
pub fn f5() { panic!("nor this }}"); | ||
//~^ ERROR panic! input cannot be format string literal | ||
} | ||
|
||
pub fn f0_a() { | ||
ensure_not_fmt_string_literal!("`f0_a`", "this does not work {}"); | ||
//~^ ERROR `f0_a` input cannot be format string literal | ||
} | ||
|
||
pub fn f0_b() { | ||
println!(ensure_not_fmt_string_literal!("`f0_b`", "this does work")); | ||
} | ||
|
||
pub fn main() {} |