Stream: wg-traits

Topic: debug output


nikomatsakis (Feb 14 2020 at 19:46, on Zulip):

So, @Jack Huey, what I wanted to say about debug:

nikomatsakis (Feb 14 2020 at 19:46, on Zulip):

I'm not actually that keen on using Debug impls, but they are dang convenient

nikomatsakis (Feb 14 2020 at 19:47, on Zulip):

In Lark, we were experimenting with a DebugWith trait

nikomatsakis (Feb 14 2020 at 19:47, on Zulip):

that took context

nikomatsakis (Feb 14 2020 at 19:47, on Zulip):

though the design relied on specialization

nikomatsakis (Feb 14 2020 at 19:47, on Zulip):

In rustc, we use debug impls, but I think the current setup has a lot of flaws, despite us having tweaked and changed it many times

nikomatsakis (Feb 14 2020 at 19:48, on Zulip):

Some of the problems are sort of "orthogonal" but relevant -- e.g., the debug output tries to recreate "user-readable" types, but that's really complex, and it can lead to ICEs and other problems, and sometimes depends on the phase of the compiler; and what's worse, it sometimes hides info you might want when debugging

nikomatsakis (Feb 14 2020 at 19:49, on Zulip):

nikomatsakis said:

In Lark, we were experimenting with a DebugWith trait

the idea was that you would do debug!("{:?}", foo.debug_with(cx))

nikomatsakis (Feb 14 2020 at 19:49, on Zulip):

where debug_with returned a DebugWith struct that wrapped a reference to foo, and DebugWith implemented Debug

nikomatsakis (Feb 14 2020 at 19:49, on Zulip):

the nice thing was that (with specialization) you could supply different sorts of contexts to get different amounts of debug

nikomatsakis (Feb 14 2020 at 19:50, on Zulip):

anyway, we won't go that route I don't think, but I do think it'd be feasible to create a simpler variation (e.g., one that always takes a TypeInterner)

nikomatsakis (Feb 14 2020 at 19:50, on Zulip):

maybe we should do that

Jack Huey (Feb 14 2020 at 19:51, on Zulip):

Yeah, I mean, how different is that to just only implemented Debug on TyData and not Ty?

nikomatsakis (Feb 14 2020 at 19:52, on Zulip):

you want the trait because

nikomatsakis (Feb 14 2020 at 19:52, on Zulip):

you want to e.g. implement debug_with on Vec<T> where T: DebugWith

Jack Huey (Feb 14 2020 at 19:52, on Zulip):

dbg!(ty.with(cx)) vs dbg!(ty.data(cx))

Jack Huey (Feb 14 2020 at 19:52, on Zulip):

ohh

nikomatsakis (Feb 14 2020 at 19:52, on Zulip):

and other such types

nikomatsakis (Feb 14 2020 at 19:52, on Zulip):

also

Jack Huey (Feb 14 2020 at 19:52, on Zulip):

right

nikomatsakis (Feb 14 2020 at 19:52, on Zulip):

you cannot implement Debug on anything

nikomatsakis (Feb 14 2020 at 19:53, on Zulip):

because when TyData has to recursively debug types that it contains

nikomatsakis (Feb 14 2020 at 19:53, on Zulip):

it needs the context

nikomatsakis (Feb 14 2020 at 19:53, on Zulip):

so you need a custom derive

nikomatsakis (Feb 14 2020 at 19:53, on Zulip):

the only Debug impl is at the outermost level, on the DebugWith struct

nikomatsakis (Feb 14 2020 at 19:53, on Zulip):

though I agree ty.with(cx) is prob better than ty.debug_with(cx) :)

Jack Huey (Feb 14 2020 at 19:54, on Zulip):

that's a need concept

nikomatsakis (Feb 14 2020 at 19:54, on Zulip):

every byte counts when you're dumping debug output

nikomatsakis (Feb 14 2020 at 19:54, on Zulip):

that's a need concept

"neat"?

Jack Huey (Feb 14 2020 at 19:54, on Zulip):

oops, yes. neat

detrumi (Feb 14 2020 at 20:00, on Zulip):

This is quite similar to HirDisplay in rust-analyzer, which takes a HirFormatter that contains a reference to the DB

nikomatsakis (Feb 14 2020 at 20:01, on Zulip):

I have wondered if we could extract this pattern to a generic crate

nikomatsakis (Feb 14 2020 at 20:01, on Zulip):

but it's hard to do w/o speciaization

nikomatsakis (Feb 14 2020 at 20:01, on Zulip):

you wind up with a trait like DebugWith<Cx> kind of

nikomatsakis (Feb 14 2020 at 20:01, on Zulip):

and when I tried that in lark it was very painful :)

detrumi (Feb 14 2020 at 20:02, on Zulip):

We'll find a generic way at some point :slight_smile:

nikomatsakis (Feb 14 2020 at 20:02, on Zulip):

I remember just getting in these annoying corners where some types (like Vec) work with any context, but other types (like types) require specific contexts, and getting everything to fit together was annoying

nikomatsakis (Feb 14 2020 at 20:02, on Zulip):

but when I moved to a setup where everything had to work with the same kinds of context, it was much nicer

nikomatsakis (Feb 14 2020 at 20:02, on Zulip):

and everything mostly "just worked"

nikomatsakis (Feb 14 2020 at 20:02, on Zulip):

as you might expect

Last update: Feb 25 2020 at 04:25UTC