diff options
| author | Dylan MacKenzie <ecstaticmorse@gmail.com> | 2020-01-13 20:30:25 -0800 |
|---|---|---|
| committer | Dylan MacKenzie <ecstaticmorse@gmail.com> | 2020-01-19 23:41:44 -0800 |
| commit | 958b0bc8d22633927796502b13a7ce944100dec5 (patch) | |
| tree | ec2426f5d32721335d35c211728616f3b6c6e5ff | |
| parent | a790f9bb2d277b5c7c267b15b812db12cc20b7a1 (diff) | |
| download | rust-958b0bc8d22633927796502b13a7ce944100dec5.tar.gz rust-958b0bc8d22633927796502b13a7ce944100dec5.zip | |
Store `impl const` in `ItemKind::Impl`
| -rw-r--r-- | src/librustc_ast_passes/ast_validation.rs | 10 | ||||
| -rw-r--r-- | src/librustc_parse/parser/item.rs | 16 |
2 files changed, 12 insertions, 14 deletions
diff --git a/src/librustc_ast_passes/ast_validation.rs b/src/librustc_ast_passes/ast_validation.rs index 23d2858a499..7dbd2362f04 100644 --- a/src/librustc_ast_passes/ast_validation.rs +++ b/src/librustc_ast_passes/ast_validation.rs @@ -616,7 +616,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { unsafety, polarity, defaultness: _, - constness: _, // TODO + constness: _, generics: _, of_trait: Some(_), ref self_ty, @@ -650,7 +650,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { unsafety, polarity, defaultness, - constness: _, // TODO + constness, generics: _, of_trait: None, self_ty: _, @@ -678,6 +678,12 @@ impl<'a> Visitor<'a> for AstValidator<'a> { .note("only trait implementations may be annotated with default") .emit(); } + if constness == Constness::Const { + self.err_handler() + .struct_span_err(item.span, "inherent impls cannot be `const`") + .note("only trait implementations may be annotated with `const`") + .emit(); + } } ItemKind::Fn(ref sig, ref generics, _) => { self.visit_fn_header(&sig.header); diff --git a/src/librustc_parse/parser/item.rs b/src/librustc_parse/parser/item.rs index e4982896376..43495da1928 100644 --- a/src/librustc_parse/parser/item.rs +++ b/src/librustc_parse/parser/item.rs @@ -5,7 +5,7 @@ use crate::maybe_whole; use rustc_error_codes::*; use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, PResult, StashKey}; -use rustc_span::source_map::{self, respan, Span, Spanned}; +use rustc_span::source_map::{self, respan, Span}; use rustc_span::symbol::{kw, sym, Symbol}; use rustc_span::BytePos; use syntax::ast::{self, AttrKind, AttrStyle, AttrVec, Attribute, Ident, DUMMY_NODE_ID}; @@ -566,9 +566,9 @@ impl<'a> Parser<'a> { let constness = if self.eat_keyword(kw::Const) { let span = self.prev_span; self.sess.gated_spans.gate(sym::const_trait_impl, span); - Some(respan(span, Constness::Const)) + Constness::Const } else { - None + Constness::NotConst }; // Disambiguate `impl !Trait for Type { ... }` and `impl ! { ... }` for the never type. @@ -631,8 +631,7 @@ impl<'a> Parser<'a> { err_path(ty_first.span) } }; - let constness = constness.map(|c| c.node); - let trait_ref = TraitRef { path, constness, ref_id: ty_first.id }; + let trait_ref = TraitRef { path, ref_id: ty_first.id }; ItemKind::Impl { unsafety, @@ -646,13 +645,6 @@ impl<'a> Parser<'a> { } } None => { - // Reject `impl const Type {}` here - if let Some(Spanned { node: Constness::Const, span }) = constness { - self.struct_span_err(span, "`const` cannot modify an inherent impl") - .help("only a trait impl can be `const`") - .emit(); - } - // impl Type ItemKind::Impl { unsafety, |
