Skip to content

Commit 45eb0f8

Browse files
Reject const in inherent impls
1 parent 0c62c67 commit 45eb0f8

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/librustc_parse/parser/item.rs

+12-3
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::{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};
@@ -561,8 +561,9 @@ impl<'a> Parser<'a> {
561561
};
562562

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

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

0 commit comments

Comments
 (0)