From 3b58d1c799f5c2da75a58ba1ca6c9c49fbb5ca7c Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 22 Apr 2021 17:37:36 -0700 Subject: [PATCH] Move empty enum check to before filling in variants --- macro/src/clang.rs | 21 ++++++++++++++++++--- syntax/check.rs | 8 -------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/macro/src/clang.rs b/macro/src/clang.rs index a2e34255..23ea8ad2 100644 --- a/macro/src/clang.rs +++ b/macro/src/clang.rs @@ -2,8 +2,8 @@ use crate::syntax::attrs::OtherAttrs; use crate::syntax::namespace::Namespace; use crate::syntax::report::Errors; use crate::syntax::{Api, Discriminant, Doc, Enum, ForeignName, Pair, Variant}; -use proc_macro2::Ident; -use quote::format_ident; +use proc_macro2::{Delimiter, Group, Ident, TokenStream}; +use quote::{format_ident, quote}; use serde::Deserialize; use std::env; use std::fs; @@ -49,7 +49,15 @@ pub fn load(cx: &mut Errors, apis: &mut [Api]) { for api in apis { if let Api::Enum(enm) = api { if enm.variants_from_header { - variants_from_header.push(enm); + if enm.variants.is_empty() { + variants_from_header.push(enm); + } else { + let span = span_for_enum_error(enm); + cx.error( + span, + "enum with #![variants_from_header] must be written with no explicit variants", + ); + } } } } @@ -237,3 +245,10 @@ fn discriminant_value(mut clang: &[Node]) -> ParsedDiscriminant { } } } + +fn span_for_enum_error(enm: &Enum) -> TokenStream { + let enum_token = enm.enum_token; + let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new()); + brace_token.set_span(enm.brace_token.span); + quote!(#enum_token #brace_token) +} diff --git a/syntax/check.rs b/syntax/check.rs index 2c383370..38be4b64 100644 --- a/syntax/check.rs +++ b/syntax/check.rs @@ -357,14 +357,6 @@ fn check_api_enum(cx: &mut Check, enm: &Enum) { ); } - if enm.variants_from_header && !enm.variants.is_empty() { - let span = span_for_enum_error(enm); - cx.error( - span, - "enum with #![variants_from_header] must be written with no explicit variants", - ); - } - for derive in &enm.derives { if derive.what == Trait::Default || derive.what == Trait::ExternType { let msg = format!("derive({}) on shared enum is not supported", derive);