-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Ensure a sole string-literal passed to panic!
is not a fmt string.
#24370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0bc20e1
Prepare for change to unary `panic!` rejecting fmt string literals.
pnkfelix a4b02ca
Ensure a sole string-literal passes to `panic!` is not a fmt string.
pnkfelix ec1af4a
revise `ensure_not_fmt_string_literal` to warn rather than error.
pnkfelix 3f85887
rename macro to make it less palatable for use outside stdlib.
pnkfelix 840afc3
bug fix.
pnkfelix f77c62b
Regression test.
pnkfelix ee540bc
allow helper macro to expand to unstable things, if necessary.
pnkfelix 4efe144
attempt to register the helper macro so that it allows unstable thing…
pnkfelix 5575da2
remove attempts to encode macro's instability from implementation.
pnkfelix 0a46037
update unit test for checking unary panic input.
pnkfelix b58359a
workaround bug in macro expansion.
pnkfelix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,75 @@ | ||
// 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. | ||
|
||
#![feature(rustc_attrs)] | ||
|
||
// Issue 22932: `panic!("{}");` should not compile. | ||
|
||
pub fn f1() { panic!("this does not work {}"); | ||
//~^ WARN unary `panic!` literal argument contains `{` | ||
//~| NOTE Is it meant to be a `format!` string? | ||
//~| HELP You can wrap the argument in parentheses to sidestep this warning | ||
} | ||
|
||
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 {"); | ||
//~^ WARN unary `panic!` literal argument contains `{` | ||
//~| NOTE Is it meant to be a `format!` string? | ||
//~| HELP You can wrap the argument in parentheses to sidestep this warning | ||
} | ||
|
||
pub fn f3() { panic!("nor this }"); | ||
//~^ WARN unary `panic!` literal argument contains `}` | ||
//~| NOTE Is it meant to be a `format!` string? | ||
//~| HELP You can wrap the argument in parentheses to sidestep this warning | ||
} | ||
|
||
pub fn f4() { panic!("nor this {{"); | ||
//~^ WARN unary `panic!` literal argument contains `{` | ||
//~| NOTE Is it meant to be a `format!` string? | ||
//~| HELP You can wrap the argument in parentheses to sidestep this warning | ||
} | ||
|
||
pub fn f5() { panic!("nor this }}"); | ||
//~^ WARN unary `panic!` literal argument contains `}` | ||
//~| NOTE Is it meant to be a `format!` string? | ||
//~| HELP You can wrap the argument in parentheses to sidestep this warning | ||
} | ||
|
||
pub fn f0_a() { | ||
__unstable_rustc_ensure_not_fmt_string_literal!("`f0_a`", "this does not work {}"); | ||
//~^ WARN `f0_a` literal argument contains `{` | ||
//~| NOTE Is it meant to be a `format!` string? | ||
//~| HELP You can wrap the argument in parentheses to sidestep this warning | ||
} | ||
|
||
pub fn f0_b() { | ||
__unstable_rustc_ensure_not_fmt_string_literal!("`f0_b`", "this does work"); | ||
} | ||
|
||
pub fn f0_c() { | ||
__unstable_rustc_ensure_not_fmt_string_literal!("`f0_c`", ("so does this {}")); | ||
} | ||
|
||
// This test is just checking that we get all the right warnings; none | ||
// of them are outright errors, so use the special `rustc_error` | ||
// attribute to force a compile error. | ||
#[rustc_error] | ||
pub fn main() { | ||
//~^ ERROR compilation successful | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this necessary to change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah wait I see the commit now, how come this was necessary to change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because otherwise I get stability warnings saying that the internals of float are unstable.
I assume this is due to a bug in either my macro, the expander, or in the way we lint stability via the chain of spans associated with the expansion. I was/am not sure if this is a deal-breaker for this change; maybe I will have to track down that bug first (and presumably let this wait until 1.x to land.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on reflection, the benefit of the warning (catching the programming mistake where one passes a format literal string to
panic!
but forgets the associated format arguments) does not warrant breaking even code like the example above.I still think the fact that my macro did not work represents a bug somewhere, perhaps in the macro system or in the stability checker.
But for now I am closing this PR; we can put in this warning later (i.e. after 1.0).