Skip to content

Commit 958b0bc

Browse files
Store impl const in ItemKind::Impl
1 parent a790f9b commit 958b0bc

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

src/librustc_ast_passes/ast_validation.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
616616
unsafety,
617617
polarity,
618618
defaultness: _,
619-
constness: _, // TODO
619+
constness: _,
620620
generics: _,
621621
of_trait: Some(_),
622622
ref self_ty,
@@ -650,7 +650,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
650650
unsafety,
651651
polarity,
652652
defaultness,
653-
constness: _, // TODO
653+
constness,
654654
generics: _,
655655
of_trait: None,
656656
self_ty: _,
@@ -678,6 +678,12 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
678678
.note("only trait implementations may be annotated with default")
679679
.emit();
680680
}
681+
if constness == Constness::Const {
682+
self.err_handler()
683+
.struct_span_err(item.span, "inherent impls cannot be `const`")
684+
.note("only trait implementations may be annotated with `const`")
685+
.emit();
686+
}
681687
}
682688
ItemKind::Fn(ref sig, ref generics, _) => {
683689
self.visit_fn_header(&sig.header);

src/librustc_parse/parser/item.rs

Lines changed: 4 additions & 12 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, Spanned};
8+
use rustc_span::source_map::{self, respan, Span};
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};
@@ -566,9 +566,9 @@ impl<'a> Parser<'a> {
566566
let constness = if self.eat_keyword(kw::Const) {
567567
let span = self.prev_span;
568568
self.sess.gated_spans.gate(sym::const_trait_impl, span);
569-
Some(respan(span, Constness::Const))
569+
Constness::Const
570570
} else {
571-
None
571+
Constness::NotConst
572572
};
573573

574574
// Disambiguate `impl !Trait for Type { ... }` and `impl ! { ... }` for the never type.
@@ -631,8 +631,7 @@ impl<'a> Parser<'a> {
631631
err_path(ty_first.span)
632632
}
633633
};
634-
let constness = constness.map(|c| c.node);
635-
let trait_ref = TraitRef { path, constness, ref_id: ty_first.id };
634+
let trait_ref = TraitRef { path, ref_id: ty_first.id };
636635

637636
ItemKind::Impl {
638637
unsafety,
@@ -646,13 +645,6 @@ impl<'a> Parser<'a> {
646645
}
647646
}
648647
None => {
649-
// Reject `impl const Type {}` here
650-
if let Some(Spanned { node: Constness::Const, span }) = constness {
651-
self.struct_span_err(span, "`const` cannot modify an inherent impl")
652-
.help("only a trait impl can be `const`")
653-
.emit();
654-
}
655-
656648
// impl Type
657649
ItemKind::Impl {
658650
unsafety,

0 commit comments

Comments
 (0)