Skip to content

Commit

Permalink
Merge pull request #1205 from mitsuhiko/bugfix/tuple-struct-flatten
Browse files Browse the repository at this point in the history
Produce error message for use of flatten in tuple structs
  • Loading branch information
dtolnay committed Apr 2, 2018
2 parents 6f6c608 + 77376f3 commit 860241a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
11 changes: 10 additions & 1 deletion serde_derive_internals/src/check.rs
Expand Up @@ -49,11 +49,20 @@ fn check_flatten(cx: &Ctxt, cont: &Container) {
cx.error("#[serde(flatten)] cannot be used within enums");
}
}
Data::Struct(_, _) => {
Data::Struct(style, _) => {
for field in cont.data.all_fields() {
if !field.attrs.flatten() {
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() {
cx.error(
"#[serde(flatten] can not be combined with \
Expand Down
16 changes: 16 additions & 0 deletions test_suite/tests/compile-fail/conflict/flatten-newtype-struct.rs
@@ -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() {}
16 changes: 16 additions & 0 deletions test_suite/tests/compile-fail/conflict/flatten-tuple-struct.rs
@@ -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() {}

0 comments on commit 860241a

Please sign in to comment.