-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Closed
Copy link
Labels
C-bugCategory: This is a bug.Category: This is a bug.
Description
I'm trying to write Debug impls for the yoke
crate:
Non-compiling code
use std::borrow::*;
use std::fmt;
pub trait Yokeable<'a>: 'static {
type Output: 'a;
}
impl<'a, T: 'static + ToOwned> Yokeable<'a> for Cow<'static, T> {
type Output = Cow<'a, T>;
}
pub struct Yoke<Y: for<'a> Yokeable<'a>> {
y: Y
}
impl<Y: for<'a> Yokeable<'a>> Yoke<Y> {
pub fn get<'a>(&'a self) -> &'a <Y as Yokeable<'a>>::Output {
unimplemented!()
}
}
impl<Y> fmt::Debug for Yoke<Y> where Y: for<'a> Yokeable<'a>, for<'a> <Y as Yokeable<'a>>::Output: fmt::Debug {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.get().fmt(f)
}
}
// impl<Y> fmt::Debug for Yoke<Y> where Y: for<'a> Yokeable<'a>, for<'a> &'a <Y as Yokeable<'a>>::Output: fmt::Debug {
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// self.get().fmt(f)
// }
// }
fn format_yoke(x: Yoke<Cow<'static, u8>>) {
println!("{:?}", x)
}
(playpen)
This code fails to compile:
error[E0277]: `<std::borrow::Cow<'_, u8> as Yokeable<'a>>::Output` doesn't implement `Debug`
--> src/main.rs:35:22
|
35 | println!("{:?}", x)
| ^ `<std::borrow::Cow<'_, u8> as Yokeable<'a>>::Output` cannot be formatted using `{:?}` because it doesn't implement `Debug`
|
= help: the trait `for<'a> Debug` is not implemented for `<std::borrow::Cow<'_, u8> as Yokeable<'a>>::Output`
= note: required because of the requirements on the impl of `Debug` for `Yoke<std::borrow::Cow<'_, u8>>`
= note: required by `std::fmt::Debug::fmt`
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
However, swapping the for<'a> <Y as Yokeable<'a>>::Output: fmt::Debug
bound with for<'a> &'a <Y as Yokeable<'a>>::Output: fmt::Debug
has it start compiling again.
Compiling code
use std::borrow::*;
use std::fmt;
pub trait Yokeable<'a>: 'static {
type Output: 'a;
}
impl<'a, T: 'static + ToOwned> Yokeable<'a> for Cow<'static, T> {
type Output = Cow<'a, T>;
}
pub struct Yoke<Y: for<'a> Yokeable<'a>> {
y: Y
}
impl<Y: for<'a> Yokeable<'a>> Yoke<Y> {
pub fn get<'a>(&'a self) -> &'a <Y as Yokeable<'a>>::Output {
unimplemented!()
}
}
// impl<Y> fmt::Debug for Yoke<Y> where Y: for<'a> Yokeable<'a>, for<'a> <Y as Yokeable<'a>>::Output: fmt::Debug {
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// self.get().fmt(f)
// }
// }
impl<Y> fmt::Debug for Yoke<Y> where Y: for<'a> Yokeable<'a>, for<'a> &'a <Y as Yokeable<'a>>::Output: fmt::Debug {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.get().fmt(f)
}
}
fn format_yoke(x: Yoke<Cow<'static, u8>>) {
println!("{:?}", x)
}
(playpen)
Note that Yoke::get()
does not need to exist to reproduce this bug, however it helps motivate why an HRTB bound is necessary.
It seems like both cases should successfully compile, all Cow<u8>
s implement Debug.
sffc, guswynn and demurgos
Metadata
Metadata
Assignees
Labels
C-bugCategory: This is a bug.Category: This is a bug.
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
Manishearth commentedon Jun 19, 2021
Interestingly, if you want this to work for Clone, you can just introduce
#[derive(Clone)] struct Wrap<T>(T);
and use that instead of&T
. It's probably similar for other traits that are not usingSelf
as the receiver.https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b3adf42a116484bdf1db6391fe7f3ef5
Manishearth commentedon Jun 19, 2021
I need some further trickery to make it work for
Clone
onYoke
but this works for me:Manishearth commentedon Jul 23, 2021
@jackh726 btw, #85499 seems to fix this, care to add a testcase?
jackh726 commentedon Aug 26, 2021
Fixed by #85499, closing since #56556 has a similar test
sffc commentedon Sep 23, 2021
Not completely fixed yet. Here is a different test case that still reproduces the error on nightly and beta:
https://play.rust-lang.org/?version=beta&mode=debug&edition=2018&gist=c50c84aede1fad33bfac1238424a7c2a
EDIT: Made the case a bit smaller:
https://play.rust-lang.org/?version=beta&mode=debug&edition=2018&gist=025e2d4cefe09821650352a935b0baed
10 remaining items