Move E0379 check from typeck to ast validation

This commit is contained in:
Keith Yeung 2016-08-07 14:33:35 -07:00
parent e17d6db80a
commit 1e9e798cce
6 changed files with 37 additions and 34 deletions

View File

@ -69,6 +69,17 @@ impl<'a> AstValidator<'a> {
}
}
}
fn check_trait_fn_not_const(&self, span: Span, constness: Constness) {
match constness {
Constness::Const => {
struct_span_err!(self.session, span, E0379, "trait fns cannot be declared const")
.span_label(span, &format!("trait fns cannot be const"))
.emit();
}
_ => {}
}
}
}
impl<'a> Visitor for AstValidator<'a> {
@ -146,6 +157,9 @@ impl<'a> Visitor for AstValidator<'a> {
self.invalid_visibility(&item.vis, item.span, None);
for impl_item in impl_items {
self.invalid_visibility(&impl_item.vis, impl_item.span, None);
if let ImplItemKind::Method(ref sig, _) = impl_item.node {
self.check_trait_fn_not_const(impl_item.span, sig.constness);
}
}
}
ItemKind::Impl(_, _, _, None, _, _) => {
@ -169,6 +183,13 @@ impl<'a> Visitor for AstValidator<'a> {
}
}
}
ItemKind::Trait(_, _, _, ref trait_items) => {
for trait_item in trait_items {
if let TraitItemKind::Method(ref sig, _) = trait_item.node {
self.check_trait_fn_not_const(trait_item.span, sig.constness);
}
}
}
ItemKind::Mod(_) => {
// Ensure that `path` attributes on modules are recorded as used (c.f. #35584).
attr::first_attr_value_str_by_name(&item.attrs, "path");

View File

@ -176,6 +176,13 @@ fn some_func() {
```
"##,
E0379: r##"
Trait methods cannot be declared `const` by design. For more information, see
[RFC 911].
[RFC 911]: https://github.com/rust-lang/rfcs/pull/911
"##,
E0449: r##"
A visibility qualifier was used when it was unnecessary. Erroneous code
examples:

View File

@ -836,13 +836,9 @@ pub fn check_item_body<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>, it: &'tcx hir::Item) {
check_const(ccx, &expr, trait_item.id)
}
hir::MethodTraitItem(ref sig, Some(ref body)) => {
check_trait_fn_not_const(ccx, trait_item.span, sig.constness);
check_bare_fn(ccx, &sig.decl, body, trait_item.id);
}
hir::MethodTraitItem(ref sig, None) => {
check_trait_fn_not_const(ccx, trait_item.span, sig.constness);
}
hir::MethodTraitItem(_, None) |
hir::ConstTraitItem(_, None) |
hir::TypeTraitItem(..) => {
// Nothing to do.
@ -854,22 +850,6 @@ pub fn check_item_body<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>, it: &'tcx hir::Item) {
}
}
fn check_trait_fn_not_const<'a,'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
span: Span,
constness: hir::Constness)
{
match constness {
hir::Constness::NotConst => {
// good
}
hir::Constness::Const => {
struct_span_err!(ccx.tcx.sess, span, E0379, "trait fns cannot be declared const")
.span_label(span, &format!("trait fns cannot be const"))
.emit()
}
}
}
fn check_on_unimplemented<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
def_id: DefId,
item: &hir::Item) {
@ -1027,9 +1007,7 @@ fn check_impl_items_against_trait<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
err.emit()
}
}
hir::ImplItemKind::Method(ref sig, ref body) => {
check_trait_fn_not_const(ccx, impl_item.span, sig.constness);
hir::ImplItemKind::Method(_, ref body) => {
let impl_method = match ty_impl_item {
ty::MethodTraitItem(ref mti) => mti,
_ => span_bug!(impl_item.span, "non-method impl-item for method")

View File

@ -3422,13 +3422,6 @@ containing the unsized type is the last and only unsized type field in the
struct.
"##,
E0379: r##"
Trait methods cannot be declared `const` by design. For more information, see
[RFC 911].
[RFC 911]: https://github.com/rust-lang/rfcs/pull/911
"##,
E0380: r##"
Default impls are only allowed for traits with no methods or associated items.
For more information see the [opt-in builtin traits RFC](https://github.com/rust

View File

@ -21,7 +21,7 @@ trait Foo {
impl Foo for u32 {
const fn f() -> u32 { 22 }
//~^ ERROR E0379
//~^ ERROR trait fns cannot be declared const
//~| NOTE trait fns cannot be const
}

View File

@ -14,8 +14,12 @@
#![feature(const_fn)]
trait Foo {
const fn f() -> u32; //~ ERROR trait fns cannot be declared const
const fn g() -> u32 { 0 } //~ ERROR trait fns cannot be declared const
const fn f() -> u32;
//~^ ERROR trait fns cannot be declared const
//~| NOTE trait fns cannot be const
const fn g() -> u32 { 0 }
//~^ ERROR trait fns cannot be declared const
//~| NOTE trait fns cannot be const
}
fn main() { }