-
-
Notifications
You must be signed in to change notification settings - Fork 791
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
Generate bounds on type parameters only #456
Conversation
#[derive(Serialize)]
struct S<'a, A: 'a, B, C> {
a: Option<&'a A>,
b: B,
#[serde(skip_serializing)]
c: C,
i: i32,
} Before: impl<'a, A: 'a, B, C> Serialize for S<'a, A, B, C>
where Option<&'a A>: Serialize,
B: Serialize,
i32: Serialize { /* ... */ } After: impl<'a, A: 'a, B, C> Serialize for S<'a, A, B, C>
where A: Serialize,
B: Serialize { /* ... */ } |
Hmm I have to figure out how to make the hygiene work on nightly. |
I am done working on this for today. @oli-obk see if you can figure out a way that works with serde_macros. |
we have some pretty funky spans:
|
Yet serde_codegen is fine with it. I think serde_macros is seeing two different |
It must be more complex... If I compile |
fun fact: #[macro_use]
#[path = "../../testing/tests/macros.rs"]
mod macros;
#[path = "../../testing/tests/test_annotations.rs"]
mod test_annotations;
#[path = "../../testing/tests/test_bytes.rs"]
mod test_bytes;
#[path = "../../testing/tests/test_de.rs"]
mod test_de;
#[path = "../../testing/tests/test_gen.rs"]
mod test_gen;
#[path = "../../testing/tests/test_macros.rs"]
mod test_macros;
#[path = "../../testing/tests/test_ser.rs"]
mod test_ser; |
pretty=expanded code of #[automatically_derived]
impl <T> _serde::ser::Serialize for With<T> {
fn serialize<__S>(&self, _serializer: &mut __S)
-> ::std::result::Result<(), __S::Error> where
__S: _serde::ser::Serializer {
{
let mut state =
match _serializer.serialize_struct("With", 0 + 1) {
::std::result::Result::Ok(val) => val,
::std::result::Result::Err(err) => {
return ::std::result::Result::Err(::std::convert::From::from(err))
}
};
if !false {
match _serializer.serialize_struct_elt(&mut state,
"t", &self.t) {
::std::result::Result::Ok(val) => val,
::std::result::Result::Err(err) => {
return ::std::result::Result::Err(::std::convert::From::from(err))
}
};
}
_serializer.serialize_struct_end(state)
}
}
} obviously the bound is missing. If I use #[automatically_derived]
impl <T> _serde::ser::Serialize for With<T> where
T: _serde::ser::Serialize {
fn serialize<__S>(&self, _serializer: &mut __S)
-> ::std::result::Result<(), __S::Error> where
__S: _serde::ser::Serializer {
{
let mut state =
match _serializer.serialize_struct("With", 0 + 1)
{
::std::result::Result::Ok(val) => val,
::std::result::Result::Err(err) => {
return ::std::result::Result::Err(::std::convert::From::from(err))
}
};
if !false {
match _serializer.serialize_struct_elt(&mut state,
"t",
&self.t) {
::std::result::Result::Ok(val) => val,
::std::result::Result::Err(err) => {
return ::std::result::Result::Err(::std::convert::From::from(err))
}
};
}
_serializer.serialize_struct_end(state)
}
}
} |
include! completely messes up the identifiers' expansion info
Thanks for the fix 😃. I did not think the solution would be less hygiene instead of more hygiene. |
@dtolnay I expected |
That breaks this case: #[derive(Serialize, Deserialize)]
struct RecursiveGenericA<T> {
t: T,
b: Box<RecursiveGenericB<T>>,
}
#[derive(Serialize, Deserialize)]
enum RecursiveGenericB<T> {
T(T),
A(RecursiveGenericA<T>),
} |
@dtolnay Ah, right. We can't have nice things 😞. |
One thing I still want to do is add a hardcoded exception for PhantomData. The following should not put a bound on T: struct S<T> {
phantom: PhantomData<T>
} |
Remove #[serde(bound = "")] attributes These were fixed in serde_codegen 0.8.0 by serde-rs/serde#456. cc @nox - [ ] `./mach build -d` does not report any errors - [ ] `./mach test-tidy` does not report any errors - [x] These changes do not require tests because: the generated code continues to compile <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/12834) <!-- Reviewable:end -->
These were fixed in serde_codegen 0.8.0 by serde-rs/serde#456.
…Css deriver. Add a new `#[omit_to_css_bounds]` attribute on variants in an enum with \`#[derive(ToCss)]`. A variant `V(F1, F2, ...)` normally generates bounds on the ToCss implementation `where F1: ToCss, F2: ToCss, ...` This is problematic in following case: // Sort of what we will have for type custom CSS variable values. #[derive(ToCss)] pub enum E { V(Vec<E>) } // Sort of what we have in style_traits/values.rs. impl<T> ToCss for Vec<T> where T: ToCss { ... } The generated implementation of `ToCss` for `E` looks like impl ToCss for E where Vec<E>: ToCss { ... } ... which creates a cycle, and the compiler exits with error E0257. The `Vec<E>: ToCss` bound is not actually necessary, so with this commit we can omit its generation with `#[omit_to_css_bounds]`. It would be cool if in the future we could decide when to omit such bounds automatically. For reference, what serde did is generate bounds for type parameters used in fields only. See serde-rs/serde#456 . I tried that and it didn't seem to work with style::values::generics::BasicShape, however, maybe because Circle in that module doesn't implement ToCss.
…olnay:bound); r=nox These were fixed in serde_codegen 0.8.0 by serde-rs/serde#456. cc nox - [ ] `./mach build -d` does not report any errors - [ ] `./mach test-tidy` does not report any errors - [x] These changes do not require tests because: the generated code continues to compile Source-Repo: https://github.com/servo/servo Source-Revision: 11b853fbf1916fb71b49fefbf2ebeaccaec50ee2 UltraBlame original commit: 14f16d3626e039515989bb5b69d38f0403b822c7
…olnay:bound); r=nox These were fixed in serde_codegen 0.8.0 by serde-rs/serde#456. cc nox - [ ] `./mach build -d` does not report any errors - [ ] `./mach test-tidy` does not report any errors - [x] These changes do not require tests because: the generated code continues to compile Source-Repo: https://github.com/servo/servo Source-Revision: 11b853fbf1916fb71b49fefbf2ebeaccaec50ee2 UltraBlame original commit: 14f16d3626e039515989bb5b69d38f0403b822c7
…olnay:bound); r=nox These were fixed in serde_codegen 0.8.0 by serde-rs/serde#456. cc nox - [ ] `./mach build -d` does not report any errors - [ ] `./mach test-tidy` does not report any errors - [x] These changes do not require tests because: the generated code continues to compile Source-Repo: https://github.com/servo/servo Source-Revision: 11b853fbf1916fb71b49fefbf2ebeaccaec50ee2 UltraBlame original commit: 14f16d3626e039515989bb5b69d38f0403b822c7
Fixes #435 and fixes #436 and fixes #441 and fixes #443.