Skip to content
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

Fix handling of panic calls #6310

Closed
wants to merge 7 commits into from
Closed

Conversation

camelid
Copy link
Member

@camelid camelid commented Nov 8, 2020

Fix handling of panic calls. This should make Clippy more resilient and will unblock rust-lang/rust#78343.

changelog: none

@rust-highfive
Copy link

r? @Manishearth

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Nov 8, 2020
@camelid
Copy link
Member Author

camelid commented Nov 8, 2020

r? @flip1995

@rust-highfive rust-highfive assigned flip1995 and unassigned Manishearth Nov 8, 2020
@camelid
Copy link
Member Author

camelid commented Nov 8, 2020

I'm getting this error:

error: invalid path
  --> clippy_lints/src/utils/paths.rs:84:1
   |
84 | pub const PANIC_ANY: [&str; 3] = ["std", "panicking", "panic_any"];
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `-D clippy::clippy-lints-internal` implied by `-D clippy::internal`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clippy_lints_internal

Is that because panic_any hasn't landed yet?

@camelid
Copy link
Member Author

camelid commented Nov 8, 2020

Hmm, panic_any is definitely on nightly.

They should be used through the `match_panic_call` and `match_panic_def_id`
functions to ensure consistency across Clippy.
@camelid
Copy link
Member Author

camelid commented Nov 9, 2020

Cc @m-ou-se

@camelid
Copy link
Member Author

camelid commented Nov 9, 2020

Oops, clippy is correct: the path is std::panic::panic_any, not std::panicking::panic_any ;)

@m-ou-se
Copy link
Member

m-ou-se commented Nov 9, 2020

It might be good to add a test to check that clippy now reacts to core::panic too.

@camelid
Copy link
Member Author

camelid commented Nov 9, 2020

It's a good thing you had me add that test, because the output looks wonky :)

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ultimately this PR should be opened in rust-lang/rust, to avoid the sync round trip. But we can review and test it here and just apply the patches in rust-lang/rust.

clippy_lints/src/utils/paths.rs Show resolved Hide resolved
clippy_lints/src/assertions_on_constants.rs Show resolved Hide resolved
clippy_lints/src/panic_unimplemented.rs Outdated Show resolved Hide resolved
@m-ou-se
Copy link
Member

m-ou-se commented Nov 9, 2020

Is there anything we can do in std/core to make it easier for Clippy to recognise panic/unimplemented/assert/etc.? Its detection right now seems very fragile, as it depends on the exact expansion of these macros. Those macros are going to change soon, and some will expand differently in Rust 2021. Also, assert!() might get a less predictable expansion once RFC 2011 gets implemented.

Maybe some #[rustc_diagnostic_item = ..] Clippy could use, or something in that direction?

@flip1995
Copy link
Member

flip1995 commented Nov 9, 2020

The problem is, that Clippy only sees the expanded version of macros and has no access to the pre-expansion code. So I don't think a diag item will help here. We can check if a certain span is from a specific macro expansion with is_expn_of (well normally it works, but for some reason not in the above case).

Macros are tricky, and for example, if we need macro arguments it leads to hacks like this:

/// Extract args from an assert-like macro.
/// Currently working with:
/// - `assert!`, `assert_eq!` and `assert_ne!`
/// - `debug_assert!`, `debug_assert_eq!` and `debug_assert_ne!`
/// For example:
/// `assert!(expr)` will return Some([expr])
/// `debug_assert_eq!(a, b)` will return Some([a, b])
pub fn extract_assert_macro_args<'tcx>(e: &'tcx Expr<'tcx>) -> Option<Vec<&'tcx Expr<'tcx>>> {

@camelid
Copy link
Member Author

camelid commented Nov 11, 2020

Ugh, now only core::unreachable is warning. Ideas?

@m-ou-se
Copy link
Member

m-ou-se commented Nov 11, 2020

All the other tests are wrapped in let a = 2; and let b = a + 2;. I guess that might be related?

@camelid
Copy link
Member Author

camelid commented Nov 11, 2020

I don't know; I tried adding that, and it didn't change the output at all.

@m-ou-se
Copy link
Member

m-ou-se commented Nov 12, 2020

Ugh, now only core::unreachable is warning. Ideas?

std::panic expands to { $crate::begin_panic_fmt(..) } (note the {}), but core::panic expands to $crate::panicking::panic(..) (without {}). The only core macro that works is core::unreachable!(), which is the only macro that does include {} in its expansion for some reason.

Clippy seems to check specifically for that {} block.

@m-ou-se
Copy link
Member

m-ou-se commented Nov 12, 2020

This makes it work:

-            if let ExprKind::Block(ref block, _) = expr.kind;
-            if let Some(ref ex) = block.expr;
-            if let Some(params) = match_panic_call(cx, ex);
+            if let Some(params) = match_panic_call(cx, expr);

(clippy_lints/src/panic_unimplemented.rs:100)

@camelid
Copy link
Member Author

camelid commented Nov 12, 2020

@m-ou-se It worked! Wonder why the code was expecting a block... Thanks so much for helping me figure this stuff out :)

Co-authored-by: Mara Bos <m-ou.se@m-ou.se>
Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonder why the code was expecting a block...

Probably some legacy code, no one knows the reason anymore. 😄


This LGTM. It would be great, if you could do this in rustc directly, so we don't have to sync this back, before your rust PR can get merged.

# in Clippy repo
git diff master --patch > clippy.patch
sed -ie "s/--- a/--- a\/src\/tools\/clippy/g" clippy.patch
sed -ie "s/+++ b/+++ b\/src\/tools\/clippy/g" clippy.patch

# in Rust repo
git apply /path/to/clippy.patch

clippy_lints/src/panic_unimplemented.rs Outdated Show resolved Hide resolved
camelid added a commit to camelid/rust that referenced this pull request Nov 17, 2020
This should make Clippy more resilient and will unblock rust-lang#78343.

This PR is made against rust-lang/rust to avoid the need for a subtree
sync at @flip1995's suggestion in rust-lang/rust-clippy#6310.
@camelid
Copy link
Member Author

camelid commented Nov 17, 2020

Closing in favor of rust-lang/rust#79145 to avoid the sync.

@camelid camelid closed this Nov 17, 2020
m-ou-se added a commit to m-ou-se/rust that referenced this pull request Nov 18, 2020
Fix handling of panic calls

This should make Clippy more resilient and will unblock rust-lang#78343.

This PR is made against rust-lang/rust to avoid the need for a subtree
sync at `@flip1995's` suggestion in rust-lang/rust-clippy#6310.

r? `@flip1995`
cc `@m-ou-se`
m-ou-se added a commit to m-ou-se/rust that referenced this pull request Nov 18, 2020
Fix handling of panic calls

This should make Clippy more resilient and will unblock rust-lang#78343.

This PR is made against rust-lang/rust to avoid the need for a subtree
sync at ``@flip1995's`` suggestion in rust-lang/rust-clippy#6310.

r? ``@flip1995``
cc ``@m-ou-se``
flip1995 pushed a commit to flip1995/rust-clippy that referenced this pull request Nov 20, 2020
This should make Clippy more resilient and will unblock #78343.

This PR is made against rust-lang/rust to avoid the need for a subtree
sync at @flip1995's suggestion in rust-lang#6310.
flip1995 pushed a commit to flip1995/rust-clippy that referenced this pull request Nov 20, 2020
Fix handling of panic calls

This should make Clippy more resilient and will unblock #78343.

This PR is made against rust-lang/rust to avoid the need for a subtree
sync at ``@flip1995's`` suggestion in rust-lang#6310.

r? ``@flip1995``
cc ``@m-ou-se``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants