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

Implements Debug trait for types which do not support derive Debug #899

Merged
merged 4 commits into from
Aug 10, 2017

Conversation

bkchr
Copy link
Contributor

@bkchr bkchr commented Aug 10, 2017

For types that do not support derive Debug be implemented automatically by rust,
we know can generate implementations of the Debug trait. This code generation is
hidden behind the '--force-derive-debug' command-line flag.

Should solve: #875

Sorry for the extra noise in lib.rs, codegen/mod.rs etc, that was rustfmt.

For types that do not support derive Debug be implemented automatically by rust,
we know can generate implementations of the Debug trait. This code generation is
hidden behind the '--force-derive-debug' command-line flag.
@bkchr bkchr force-pushed the manual_debug_impl branch from 7fedb6f to b541f4c Compare August 10, 2017 16:13
@fitzgen
Copy link
Member

fitzgen commented Aug 10, 2017

Huh. Linking errors in bindgen-integration. But bindgen-integration wasn't touched in this patch. @emilio have you seen this before? I guess I'll try retriggering the build to see if it is a bizarre intermittent...

   Compiling bindgen-integration v0.1.0 (file:///home/travis/build/rust-lang-nursery/rust-bindgen/bindgen-integration)
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L" "/home/travis/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/tmp/bindgen/debug/build/bindgen-integration-1786b078c609d8c5/build_script_build-1786b078c609d8c5.0.o" "-o" "/tmp/bindgen/debug/build/bindgen-integration-1786b078c609d8c5/build_script_build-1786b078c609d8c5" "-Wl,--gc-sections" "-pie" "-nodefaultlibs" "-L" "/tmp/bindgen/debug/deps" "-L" "/home/travis/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/tmp/bindgen/debug/deps/libbindgen-55a46fc518ca1e4c.rlib" "/tmp/bindgen/debug/deps/libquasi-f33bbcd556ead1e9.rlib" "/tmp/bindgen/debug/deps/libpeeking_take_while-f3d42bd67235769f.rlib" "/tmp/bindgen/debug/deps/libcexpr-b8f9ee8d6ebf2c4f.rlib" "/tmp/bindgen/debug/deps/libnom-48b40d7e3820e49f.rlib" "/tmp/bindgen/debug/deps/libaster-765ffefae856852f.rlib" "/tmp/bindgen/debug/deps/libsyntex_syntax-f1fe8de5780f2132.rlib" "/tmp/bindgen/debug/deps/libbitflags-153d136e2501a57c.rlib" "/tmp/bindgen/debug/deps/liblog-daaa699d7c7fe291.rlib" "/tmp/bindgen/debug/deps/libgcc-59980d911dd87d9b.rlib" "/tmp/bindgen/debug/deps/libcfg_if-91a619aa86e87545.rlib" "/tmp/bindgen/debug/deps/libsyntex_errors-3380421bc437072b.rlib" "/tmp/bindgen/debug/deps/libunicode_xid-6089134fd5c5a09f.rlib" "/tmp/bindgen/debug/deps/libterm-2894cbc445dd1c03.rlib" "/tmp/bindgen/debug/deps/libsyntex_pos-6b3fc0c9aafb951a.rlib" "/tmp/bindgen/debug/deps/librustc_serialize-0812cd78866b93fc.rlib" "/tmp/bindgen/debug/deps/libclang_sys-ed6911a3246819a8.rlib" "/tmp/bindgen/debug/deps/libglob-eb24eb5ddd1a61cf.rlib" "/tmp/bindgen/debug/deps/libbitflags-6808a3e0b5724221.rlib" "/tmp/bindgen/debug/deps/liblibloading-63542dc5e6b407d1.rlib" "/tmp/bindgen/debug/deps/libregex-bc74dd91f554f43e.rlib" "/tmp/bindgen/debug/deps/libregex_syntax-3cac63a113b85c4b.rlib" "/tmp/bindgen/debug/deps/libthread_local-9838c888b833bc6b.rlib" "/tmp/bindgen/debug/deps/liblazy_static-76398561647a6df6.rlib" "/tmp/bindgen/debug/deps/libaho_corasick-23aab3700a33c441.rlib" "/tmp/bindgen/debug/deps/libmemchr-916dcef2effb6570.rlib" "/tmp/bindgen/debug/deps/liblibc-2236279eda551aca.rlib" "/tmp/bindgen/debug/deps/libunreachable-565b839df1e17147.rlib" "/tmp/bindgen/debug/deps/libvoid-0e7d2f20b00df729.rlib" "/tmp/bindgen/debug/deps/libutf8_ranges-7ef42ff9c561fd9c.rlib" "/home/travis/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-35ad9950c7e5074b.rlib" "/home/travis/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-fb44afc024bbc636.rlib" "/home/travis/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-14b8f3202acdad6a.rlib" "/home/travis/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librand-20a50a22d4c2b1e9.rlib" "/home/travis/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcollections-b479831207997444.rlib" "/home/travis/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-10b591f1a68dd370.rlib" "/home/travis/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc_jemalloc-28913dc5a1e63cd7.rlib" "/home/travis/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-6ecacccb5bdc4911.rlib" "/home/travis/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_unicode-f4f0ae88f5ad8ad4.rlib" "/home/travis/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-bfaa82017ca17cb2.rlib" "/home/travis/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-863b57a66ba6c3e1.rlib" "-Wl,-Bdynamic" "-l" "dl" "-l" "util" "-l" "dl" "-l" "rt" "-l" "pthread" "-l" "gcc_s" "-l" "pthread" "-l" "c" "-l" "m" "-l" "rt" "-l" "pthread" "-l" "util"
  = note: /tmp/bindgen/debug/deps/libcexpr-b8f9ee8d6ebf2c4f.rlib(cexpr-b8f9ee8d6ebf2c4f.0.o): In function `core::slice::{{impl}}::copy_from_slice<u8>':
          /checkout/src/libcore/macros.rs:(.text._ZN53_$LT$$u5b$T$u5d$$u20$as$u20$core..slice..SliceExt$GT$15copy_from_slice17hb81ef0e544a0dd55E+0x75): undefined reference to `_$LT$$u5b$T$u5d$$u20$as$u20$core..slice..SliceExt$Gt$::copy_from_slice::_MSG_FILE_LINE::hd2e8a3fe9518b348'
          collect2: error: ld returned 1 exit status
          
error: aborting due to previous error(s)
error: Could not compile `bindgen-integration`.

@fitzgen
Copy link
Member

fitzgen commented Aug 10, 2017

@bkchr thanks for this PR! I'm taking a look now

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

This is really great, thanks!

Retriggering that one build did seem to do the trick o_O

I've left a few comments below for things I'd like to see addressed before we land this.

Thanks for writing out all of those tests! Very pleased with them. 👍

kind: CompKind,
) -> Vec<ast::ImplItem> {
let struct_name = item.canonical_name(ctx);
let mut format_string = format!("{} {{{{ ", struct_name);
Copy link
Member

Choose a reason for hiding this comment

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

So. Many. Mustaches.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could remove some mustaches by doing format_string.push_str("{{"). Or just leave it as it is?

fn gen_bitfield_unit_debug_impl(
ctx: &BindgenContext,
data: &BitfieldUnit,
) -> Option<(String, Vec<TokenTree>)> {
Copy link
Member

Choose a reason for hiding this comment

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

All of these methods have pretty much the same signature:

fn (&BindgenContext, &T) -> Option<(String, Vec<TokenTree>)>;
// or sometimes
fn (&BindgenContext, &T, &U) -> Option<(String, Vec<TokenTree)>;

Let's formalize this pattern with a trait:

/// A trait for the things which we can codegen tokens that contribute towards a
/// generated `impl Debug`.
pub trait ImplDebug {
    /// Any extra parameter required by this a particular `ImplDebug` implementation. This
    /// is the `U` from the above snippet, or `()` if its not needed.
    type Extra;

    /// Generate a format string snippet to be included in the larger `impl Debug`
    /// format string, and the code to get the format string's interpolation values.
    fn impl_debug(&self, ctx: &BindgenContext, extra: &Self::Extra) -> Option<(String, Vec<TokenTree>)>;
}

And then each of these functions should be impl ImplDebug for BitfieldUnit {...} etc.

Copy link
Contributor Author

@bkchr bkchr Aug 10, 2017

Choose a reason for hiding this comment

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

Okay, will do it :) I also thought about creating a Trait, but that seemed to be to much overengineering for me.


TypeKind::Array(_, len) => {
// Generics are not required to implement Debug
if ctx.lookup_item_id_has_type_param_in_array(&item.id()) {
Copy link
Member

Choose a reason for hiding this comment

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

This should be:

if item.has_type_param_in_array(ctx) {
    ...
}

You may have to add use ir::analysis::HasTypeParamInArray; to the top of the file.

src/options.rs Outdated
@@ -55,6 +56,9 @@ pub fn builder_from_flags<I>
Arg::with_name("no-derive-debug")
.long("no-derive-debug")
.help("Avoid deriving Debug on any type."),
Arg::with_name("force-derive-debug")
.long("force-derive-debug")
Copy link
Member

Choose a reason for hiding this comment

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

Let's call this flag impl-debug.

enumerate ( ) . map (
| ( i , v ) | format ! (
"{}{:?}" , if i > 0 { ", " } else { "" } , v ) ) . collect :: <
String > ( ))
Copy link
Member

Choose a reason for hiding this comment

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

Filed #900 to investigate running rustfmt on the code we emit :)

@@ -0,0 +1,7 @@
// bindgen-flags: --opaque-type "Opaque" --force-derive-debug
Copy link
Member

Choose a reason for hiding this comment

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

Don't think the --opaque-type is needed here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ohh that is clearly a copy paste error :D

// bindgen-flags: --force-derive-debug

template<typename T, int N>
class Opaque {
Copy link
Member

Choose a reason for hiding this comment

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

It's worth adding a comment that this is opaque because of the non-type template parameter, which is easy to miss.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh nice to know, I overseved the behavior with all the other tests while developing. But I could not find the right position in the code where this case is handled.

@bkchr bkchr force-pushed the manual_debug_impl branch from a0634d6 to f7345fa Compare August 10, 2017 22:06
@bkchr
Copy link
Contributor Author

bkchr commented Aug 10, 2017

I addressed your comments :)

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

Wonderful! Thank you!

Would you like to do some more trait implementations like this? We have a bunch of issues open :)

@fitzgen
Copy link
Member

fitzgen commented Aug 10, 2017

@bors-servo r+

@bors-servo
Copy link

📌 Commit f7345fa has been approved by fitzgen

bors-servo pushed a commit that referenced this pull request Aug 10, 2017
Implements Debug trait for types which do not support derive Debug

For types that do not support derive Debug be implemented automatically by rust,
we know can generate implementations of the Debug trait. This code generation is
hidden behind the '--force-derive-debug' command-line flag.

Should solve: #875

Sorry for the extra noise in lib.rs, codegen/mod.rs etc, that was rustfmt.
@bors-servo
Copy link

⌛ Testing commit f7345fa with merge 7dd665c...

@bors-servo
Copy link

☀️ Test successful - status-travis
Approved by: fitzgen
Pushing 7dd665c to master...

@bors-servo bors-servo merged commit f7345fa into rust-lang:master Aug 10, 2017
@bkchr bkchr deleted the manual_debug_impl branch August 11, 2017 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants