Produce error message for use of flatten within struct variant

This commit is contained in:
Armin Ronacher 2018-04-01 22:01:24 +02:00
parent 9c659d9d86
commit 302fac91a3
3 changed files with 27 additions and 1 deletions

View File

@ -68,6 +68,9 @@ impl<'a> Container<'a> {
Data::Enum(ref mut variants) => for variant in variants {
variant.attrs.rename_by_rule(attrs.rename_all());
for field in &mut variant.fields {
if field.attrs.flatten() {
has_flatten = true;
}
field.attrs.rename_by_rule(variant.attrs.rename_all());
}
},

View File

@ -45,7 +45,9 @@ fn check_getter(cx: &Ctxt, cont: &Container) {
fn check_flatten(cx: &Ctxt, cont: &Container) {
match cont.data {
Data::Enum(_) => {
assert!(!cont.attrs.has_flatten());
if cont.attrs.has_flatten() {
cx.error("#[serde(flatten)] cannot be used within enums");
}
}
Data::Struct(_, _) => {
for field in cont.data.all_fields() {

View File

@ -0,0 +1,21 @@
// 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 within enums
enum Foo {
A {
#[serde(flatten)]
fields: HashMap<String, String>,
}
}
fn main() {}