Merge pull request #1205 from mitsuhiko/bugfix/tuple-struct-flatten

Produce error message for use of flatten in tuple structs
This commit is contained in:
David Tolnay 2018-04-02 07:09:39 +02:00 committed by GitHub
commit 860241aa88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 1 deletions

View File

@ -49,11 +49,20 @@ fn check_flatten(cx: &Ctxt, cont: &Container) {
cx.error("#[serde(flatten)] cannot be used within enums"); cx.error("#[serde(flatten)] cannot be used within enums");
} }
} }
Data::Struct(_, _) => { Data::Struct(style, _) => {
for field in cont.data.all_fields() { for field in cont.data.all_fields() {
if !field.attrs.flatten() { if !field.attrs.flatten() {
continue; continue;
} }
match style {
Style::Tuple => {
cx.error("#[serde(flatten)] cannot be used on tuple structs");
}
Style::Newtype => {
cx.error("#[serde(flatten)] cannot be used on newtype structs");
}
_ => {}
}
if field.attrs.skip_serializing() { if field.attrs.skip_serializing() {
cx.error( cx.error(
"#[serde(flatten] can not be combined with \ "#[serde(flatten] can not be combined with \

View File

@ -0,0 +1,16 @@
// Copyright 2018 Serde Developers
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)] //~ ERROR: proc-macro derive panicked
//~^ HELP: #[serde(flatten)] cannot be used on newtype structs
struct Foo(#[serde(flatten)] HashMap<String, String>);
fn main() {}

View File

@ -0,0 +1,16 @@
// Copyright 2018 Serde Developers
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)] //~ ERROR: proc-macro derive panicked
//~^ HELP: #[serde(flatten)] cannot be used on tuple structs
struct Foo(u32, #[serde(flatten)] HashMap<String, String>);
fn main() {}