From 3ead6de40d57ec78275ee398592baa814b5f0841 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 11 Jan 2019 23:12:29 -0800 Subject: [PATCH] Tweak incorrect discriminator value variant error --- src/libsyntax/parse/parser.rs | 20 ++++++++----- src/test/ui/parser/issue-17383.rs | 7 ++--- src/test/ui/parser/issue-17383.stderr | 6 ++-- .../ui/parser/tag-variant-disr-non-nullary.rs | 18 ++++++------ .../tag-variant-disr-non-nullary.stderr | 28 ++++++++++++++++--- 5 files changed, 53 insertions(+), 26 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index ee93696c602dc..3f736c361115a 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -6983,7 +6983,7 @@ impl<'a> Parser<'a> { fn parse_enum_def(&mut self, _generics: &ast::Generics) -> PResult<'a, EnumDef> { let mut variants = Vec::new(); let mut all_nullary = true; - let mut any_disr = None; + let mut any_disr = vec![]; while self.token != token::CloseDelim(token::Brace) { let variant_attrs = self.parse_outer_attributes()?; let vlo = self.span; @@ -7005,7 +7005,9 @@ impl<'a> Parser<'a> { id: ast::DUMMY_NODE_ID, value: self.parse_expr()?, }); - any_disr = disr_expr.as_ref().map(|c| c.value.span); + if let Some(sp) = disr_expr.as_ref().map(|c| c.value.span) { + any_disr.push(sp); + } struct_def = VariantData::Unit(ast::DUMMY_NODE_ID); } else { struct_def = VariantData::Unit(ast::DUMMY_NODE_ID); @@ -7022,11 +7024,15 @@ impl<'a> Parser<'a> { if !self.eat(&token::Comma) { break; } } self.expect(&token::CloseDelim(token::Brace))?; - match any_disr { - Some(disr_span) if !all_nullary => - self.span_err(disr_span, - "discriminator values can only be used with a field-less enum"), - _ => () + if !any_disr.is_empty() && !all_nullary { + let mut err =self.struct_span_err( + any_disr.clone(), + "discriminator values can only be used with a field-less enum", + ); + for sp in any_disr { + err.span_label(sp, "only valid in field-less enums"); + } + err.emit(); } Ok(ast::EnumDef { variants }) diff --git a/src/test/ui/parser/issue-17383.rs b/src/test/ui/parser/issue-17383.rs index 9c00289287cde..04cd43d0b1071 100644 --- a/src/test/ui/parser/issue-17383.rs +++ b/src/test/ui/parser/issue-17383.rs @@ -1,8 +1,7 @@ enum X { - A = - b'a' //~ ERROR discriminator values can only be used with a field-less enum - , - B(isize) + A = 3, + //~^ ERROR discriminator values can only be used with a field-less enum + B(usize) } fn main() {} diff --git a/src/test/ui/parser/issue-17383.stderr b/src/test/ui/parser/issue-17383.stderr index b225e11066dbc..57caa3372a629 100644 --- a/src/test/ui/parser/issue-17383.stderr +++ b/src/test/ui/parser/issue-17383.stderr @@ -1,8 +1,8 @@ error: discriminator values can only be used with a field-less enum - --> $DIR/issue-17383.rs:3:9 + --> $DIR/issue-17383.rs:2:9 | -LL | b'a' //~ ERROR discriminator values can only be used with a field-less enum - | ^^^^ +LL | A = 3, + | ^ only valid in field-less enums error: aborting due to previous error diff --git a/src/test/ui/parser/tag-variant-disr-non-nullary.rs b/src/test/ui/parser/tag-variant-disr-non-nullary.rs index 35a36cbbf91c6..8f5453d06f37f 100644 --- a/src/test/ui/parser/tag-variant-disr-non-nullary.rs +++ b/src/test/ui/parser/tag-variant-disr-non-nullary.rs @@ -1,12 +1,14 @@ -//error-pattern: discriminator values can only be used with a field-less enum -enum color { - red = 0xff0000, - green = 0x00ff00, - blue = 0x0000ff, - black = 0x000000, - white = 0xffffff, - other (str), +enum Color { + Red = 0xff0000, + //~^ ERROR discriminator values can only be used with a field-less enum + Green = 0x00ff00, + Blue = 0x0000ff, + Black = 0x000000, + White = 0xffffff, + Other (str), + //~^ ERROR the size for values of type + // the above is kept in order to verify that we get beyond parse errors } fn main() {} diff --git a/src/test/ui/parser/tag-variant-disr-non-nullary.stderr b/src/test/ui/parser/tag-variant-disr-non-nullary.stderr index aa45ea4ac26e9..7088352f8033a 100644 --- a/src/test/ui/parser/tag-variant-disr-non-nullary.stderr +++ b/src/test/ui/parser/tag-variant-disr-non-nullary.stderr @@ -1,8 +1,28 @@ error: discriminator values can only be used with a field-less enum - --> $DIR/tag-variant-disr-non-nullary.rs:8:13 + --> $DIR/tag-variant-disr-non-nullary.rs:3:11 | -LL | white = 0xffffff, - | ^^^^^^^^ +LL | Red = 0xff0000, + | ^^^^^^^^ only valid in field-less enums +LL | //~^ ERROR discriminator values can only be used with a field-less enum +LL | Green = 0x00ff00, + | ^^^^^^^^ only valid in field-less enums +LL | Blue = 0x0000ff, + | ^^^^^^^^ only valid in field-less enums +LL | Black = 0x000000, + | ^^^^^^^^ only valid in field-less enums +LL | White = 0xffffff, + | ^^^^^^^^ only valid in field-less enums -error: aborting due to previous error +error[E0277]: the size for values of type `str` cannot be known at compilation time + --> $DIR/tag-variant-disr-non-nullary.rs:9:12 + | +LL | Other (str), + | ^^^ doesn't have a size known at compile-time + | + = help: the trait `std::marker::Sized` is not implemented for `str` + = note: to learn more, visit + = note: no field of an enum variant may have a dynamically sized type + +error: aborting due to 2 previous errors +For more information about this error, try `rustc --explain E0277`.