Skip to content

Commit 31edbe9

Browse files
Reject const in inherent impls
1 parent b390fc4 commit 31edbe9

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/librustc_parse/parser/item.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::maybe_whole;
55

66
use rustc_error_codes::*;
77
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, PResult, StashKey};
8-
use rustc_span::source_map::{self, respan, Span};
8+
use rustc_span::source_map::{self, respan, Span, Spanned};
99
use rustc_span::symbol::{kw, sym, Symbol};
1010
use rustc_span::BytePos;
1111
use syntax::ast::{self, AttrKind, AttrStyle, AttrVec, Attribute, Ident, DUMMY_NODE_ID};
@@ -560,8 +560,9 @@ impl<'a> Parser<'a> {
560560
};
561561

562562
let constness = if self.eat_keyword(kw::Const) {
563-
self.sess.gated_spans.gate(sym::const_trait_impl, self.prev_span);
564-
Some(Constness::Const)
563+
let span = self.prev_span;
564+
self.sess.gated_spans.gate(sym::const_trait_impl, span);
565+
Some(respan(span, Constness::Const))
565566
} else {
566567
None
567568
};
@@ -626,6 +627,7 @@ impl<'a> Parser<'a> {
626627
err_path(ty_first.span)
627628
}
628629
};
630+
let constness = constness.map(|c| c.node);
629631
let trait_ref = TraitRef { path, constness, ref_id: ty_first.id };
630632

631633
ItemKind::Impl(
@@ -639,6 +641,13 @@ impl<'a> Parser<'a> {
639641
)
640642
}
641643
None => {
644+
// Reject `impl const Type {}` here
645+
if let Some(Spanned { node: Constness::Const, span }) = constness {
646+
self.struct_span_err(span, "`const` cannot modify an inherent impl")
647+
.help("only a trait impl can be `const`")
648+
.emit();
649+
}
650+
642651
// impl Type
643652
ItemKind::Impl(
644653
unsafety,

0 commit comments

Comments
 (0)