Skip to content

Commit 46d3ef5

Browse files
committed
parser: extract recover_const_mut.
1 parent 15e07a6 commit 46d3ef5

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/librustc_parse/parser/item.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -138,19 +138,7 @@ impl<'a> Parser<'a> {
138138
self.parse_item_const(Some(m))?
139139
} else if let Const::Yes(const_span) = self.parse_constness() {
140140
// CONST ITEM
141-
if self.eat_keyword(kw::Mut) {
142-
let prev_span = self.prev_span;
143-
self.struct_span_err(prev_span, "const globals cannot be mutable")
144-
.span_label(prev_span, "cannot be mutable")
145-
.span_suggestion(
146-
const_span,
147-
"you might want to declare a static instead",
148-
"static".to_owned(),
149-
Applicability::MaybeIncorrect,
150-
)
151-
.emit();
152-
}
153-
141+
self.recover_const_mut(const_span);
154142
self.parse_item_const(None)?
155143
} else if self.check_keyword(kw::Trait) || self.check_auto_or_unsafe_trait_item() {
156144
// TRAIT ITEM
@@ -987,6 +975,22 @@ impl<'a> Parser<'a> {
987975
}
988976
}
989977

978+
/// Recover on `const mut` with `const` already eaten.
979+
fn recover_const_mut(&mut self, const_span: Span) {
980+
if self.eat_keyword(kw::Mut) {
981+
let span = self.prev_span;
982+
self.struct_span_err(span, "const globals cannot be mutable")
983+
.span_label(span, "cannot be mutable")
984+
.span_suggestion(
985+
const_span,
986+
"you might want to declare a static instead",
987+
"static".to_owned(),
988+
Applicability::MaybeIncorrect,
989+
)
990+
.emit();
991+
}
992+
}
993+
990994
/// Parse `["const" | ("static" "mut"?)] $ident ":" $ty = $expr` with
991995
/// `["const" | ("static" "mut"?)]` already parsed and stored in `m`.
992996
///

0 commit comments

Comments
 (0)