Skip to content

Commit 3514737

Browse files
author
Jakub Wieczorek
committed
Fix the span for discriminators in non-C-like enums
Fixes #17383.
1 parent 5d335c9 commit 3514737

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/libsyntax/parse/parser.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -5052,7 +5052,7 @@ impl<'a> Parser<'a> {
50525052
fn parse_enum_def(&mut self, _generics: &ast::Generics) -> EnumDef {
50535053
let mut variants = Vec::new();
50545054
let mut all_nullary = true;
5055-
let mut have_disr = false;
5055+
let mut any_disr = None;
50565056
while self.token != token::RBRACE {
50575057
let variant_attrs = self.parse_outer_attributes();
50585058
let vlo = self.span.lo;
@@ -5084,8 +5084,8 @@ impl<'a> Parser<'a> {
50845084
}
50855085
kind = TupleVariantKind(args);
50865086
} else if self.eat(&token::EQ) {
5087-
have_disr = true;
50885087
disr_expr = Some(self.parse_expr());
5088+
any_disr = disr_expr.as_ref().map(|expr| expr.span);
50895089
kind = TupleVariantKind(args);
50905090
} else {
50915091
kind = TupleVariantKind(Vec::new());
@@ -5104,9 +5104,11 @@ impl<'a> Parser<'a> {
51045104
if !self.eat(&token::COMMA) { break; }
51055105
}
51065106
self.expect(&token::RBRACE);
5107-
if have_disr && !all_nullary {
5108-
self.fatal("discriminator values can only be used with a c-like \
5109-
enum");
5107+
match any_disr {
5108+
Some(disr_span) if !all_nullary =>
5109+
self.span_err(disr_span,
5110+
"discriminator values can only be used with a c-like enum"),
5111+
_ => ()
51105112
}
51115113

51125114
ast::EnumDef { variants: variants }

src/test/compile-fail/issue-17383.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
enum X {
12+
A =
13+
b'a' //~ ERROR discriminator values can only be used with a c-like enum
14+
,
15+
B(int)
16+
}
17+
18+
fn main() {}

0 commit comments

Comments
 (0)