Skip to content

Commit 1e9e798

Browse files
committed
Move E0379 check from typeck to ast validation
1 parent e17d6db commit 1e9e798

File tree

6 files changed

+37
-34
lines changed

6 files changed

+37
-34
lines changed

src/librustc_passes/ast_validation.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ impl<'a> AstValidator<'a> {
6969
}
7070
}
7171
}
72+
73+
fn check_trait_fn_not_const(&self, span: Span, constness: Constness) {
74+
match constness {
75+
Constness::Const => {
76+
struct_span_err!(self.session, span, E0379, "trait fns cannot be declared const")
77+
.span_label(span, &format!("trait fns cannot be const"))
78+
.emit();
79+
}
80+
_ => {}
81+
}
82+
}
7283
}
7384

7485
impl<'a> Visitor for AstValidator<'a> {
@@ -146,6 +157,9 @@ impl<'a> Visitor for AstValidator<'a> {
146157
self.invalid_visibility(&item.vis, item.span, None);
147158
for impl_item in impl_items {
148159
self.invalid_visibility(&impl_item.vis, impl_item.span, None);
160+
if let ImplItemKind::Method(ref sig, _) = impl_item.node {
161+
self.check_trait_fn_not_const(impl_item.span, sig.constness);
162+
}
149163
}
150164
}
151165
ItemKind::Impl(_, _, _, None, _, _) => {
@@ -169,6 +183,13 @@ impl<'a> Visitor for AstValidator<'a> {
169183
}
170184
}
171185
}
186+
ItemKind::Trait(_, _, _, ref trait_items) => {
187+
for trait_item in trait_items {
188+
if let TraitItemKind::Method(ref sig, _) = trait_item.node {
189+
self.check_trait_fn_not_const(trait_item.span, sig.constness);
190+
}
191+
}
192+
}
172193
ItemKind::Mod(_) => {
173194
// Ensure that `path` attributes on modules are recorded as used (c.f. #35584).
174195
attr::first_attr_value_str_by_name(&item.attrs, "path");

src/librustc_passes/diagnostics.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ fn some_func() {
176176
```
177177
"##,
178178

179+
E0379: r##"
180+
Trait methods cannot be declared `const` by design. For more information, see
181+
[RFC 911].
182+
183+
[RFC 911]: https://github.com/rust-lang/rfcs/pull/911
184+
"##,
185+
179186
E0449: r##"
180187
A visibility qualifier was used when it was unnecessary. Erroneous code
181188
examples:

src/librustc_typeck/check/mod.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -836,13 +836,9 @@ pub fn check_item_body<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>, it: &'tcx hir::Item) {
836836
check_const(ccx, &expr, trait_item.id)
837837
}
838838
hir::MethodTraitItem(ref sig, Some(ref body)) => {
839-
check_trait_fn_not_const(ccx, trait_item.span, sig.constness);
840-
841839
check_bare_fn(ccx, &sig.decl, body, trait_item.id);
842840
}
843-
hir::MethodTraitItem(ref sig, None) => {
844-
check_trait_fn_not_const(ccx, trait_item.span, sig.constness);
845-
}
841+
hir::MethodTraitItem(_, None) |
846842
hir::ConstTraitItem(_, None) |
847843
hir::TypeTraitItem(..) => {
848844
// Nothing to do.
@@ -854,22 +850,6 @@ pub fn check_item_body<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>, it: &'tcx hir::Item) {
854850
}
855851
}
856852

857-
fn check_trait_fn_not_const<'a,'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
858-
span: Span,
859-
constness: hir::Constness)
860-
{
861-
match constness {
862-
hir::Constness::NotConst => {
863-
// good
864-
}
865-
hir::Constness::Const => {
866-
struct_span_err!(ccx.tcx.sess, span, E0379, "trait fns cannot be declared const")
867-
.span_label(span, &format!("trait fns cannot be const"))
868-
.emit()
869-
}
870-
}
871-
}
872-
873853
fn check_on_unimplemented<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
874854
def_id: DefId,
875855
item: &hir::Item) {
@@ -1027,9 +1007,7 @@ fn check_impl_items_against_trait<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
10271007
err.emit()
10281008
}
10291009
}
1030-
hir::ImplItemKind::Method(ref sig, ref body) => {
1031-
check_trait_fn_not_const(ccx, impl_item.span, sig.constness);
1032-
1010+
hir::ImplItemKind::Method(_, ref body) => {
10331011
let impl_method = match ty_impl_item {
10341012
ty::MethodTraitItem(ref mti) => mti,
10351013
_ => span_bug!(impl_item.span, "non-method impl-item for method")

src/librustc_typeck/diagnostics.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3422,13 +3422,6 @@ containing the unsized type is the last and only unsized type field in the
34223422
struct.
34233423
"##,
34243424

3425-
E0379: r##"
3426-
Trait methods cannot be declared `const` by design. For more information, see
3427-
[RFC 911].
3428-
3429-
[RFC 911]: https://github.com/rust-lang/rfcs/pull/911
3430-
"##,
3431-
34323425
E0380: r##"
34333426
Default impls are only allowed for traits with no methods or associated items.
34343427
For more information see the [opt-in builtin traits RFC](https://github.com/rust

src/test/compile-fail/const-fn-mismatch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ trait Foo {
2121

2222
impl Foo for u32 {
2323
const fn f() -> u32 { 22 }
24-
//~^ ERROR E0379
24+
//~^ ERROR trait fns cannot be declared const
2525
//~| NOTE trait fns cannot be const
2626
}
2727

src/test/compile-fail/const-fn-not-in-trait.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414
#![feature(const_fn)]
1515

1616
trait Foo {
17-
const fn f() -> u32; //~ ERROR trait fns cannot be declared const
18-
const fn g() -> u32 { 0 } //~ ERROR trait fns cannot be declared const
17+
const fn f() -> u32;
18+
//~^ ERROR trait fns cannot be declared const
19+
//~| NOTE trait fns cannot be const
20+
const fn g() -> u32 { 0 }
21+
//~^ ERROR trait fns cannot be declared const
22+
//~| NOTE trait fns cannot be const
1923
}
2024

2125
fn main() { }

0 commit comments

Comments
 (0)