]> git.lizzy.rs Git - rust.git/commitdiff
add separate errors for out-of-place associated consts and types
authorAndrew Paseltiner <apaseltiner@gmail.com>
Sat, 18 Jul 2015 23:56:15 +0000 (19:56 -0400)
committerAndrew Paseltiner <apaseltiner@gmail.com>
Sun, 19 Jul 2015 19:32:24 +0000 (15:32 -0400)
Previously, these would both be labeled as methods.

src/librustc_resolve/diagnostics.rs
src/librustc_resolve/lib.rs
src/test/compile-fail/trait-impl-can-not-have-untraitful-items.rs [new file with mode: 0644]
src/test/compile-fail/trait-impl-can-not-have-untraitful-methods.rs [deleted file]

index c7125c38aa9707cbbd512bd90ebbc9a42c4f9f1c..20084fa75b14fd992998379e72ffc5695408c7d6 100644 (file)
@@ -305,5 +305,7 @@ pub mod foo {
     E0432, // unresolved import
     E0433, // failed to resolve
     E0434, // can't capture dynamic environment in a fn item
-    E0435  // attempt to use a non-constant value in a constant
+    E0435, // attempt to use a non-constant value in a constant
+    E0437, // type is not a member of trait
+    E0438, // const is not a member of trait
 }
index e1afc33684ce65ea7b595d3b546feeace5405295..a3a0398e5ce1d58f5735d66066a70c2076f7580c 100644 (file)
@@ -123,6 +123,10 @@ pub enum ResolutionError<'a> {
     UndeclaredAssociatedType,
     /// error E0407: method is not a member of trait
     MethodNotMemberOfTrait(Name, &'a str),
+    /// error E0437: type is not a member of trait
+    TypeNotMemberOfTrait(Name, &'a str),
+    /// error E0438: const is not a member of trait
+    ConstNotMemberOfTrait(Name, &'a str),
     /// error E0408: variable `{}` from pattern #1 is not bound in pattern
     VariableNotBoundInPattern(Name, usize),
     /// error E0409: variable is bound with different mode in pattern #{} than in pattern #1
@@ -220,6 +224,18 @@ fn resolve_error<'b, 'a:'b, 'tcx:'a>(resolver: &'b Resolver<'a, 'tcx>, span: syn
                          method,
                          trait_);
         },
+        ResolutionError::TypeNotMemberOfTrait(type_, trait_) => {
+            span_err!(resolver.session, span, E0437,
+                         "type `{}` is not a member of trait `{}`",
+                         type_,
+                         trait_);
+        },
+        ResolutionError::ConstNotMemberOfTrait(const_, trait_) => {
+            span_err!(resolver.session, span, E0438,
+                         "const `{}` is not a member of trait `{}`",
+                         const_,
+                         trait_);
+        },
         ResolutionError::VariableNotBoundInPattern(variable_name, pattern_number) => {
             span_err!(resolver.session, span, E0408,
                          "variable `{}` from pattern #1 is not bound in pattern #{}",
@@ -2385,10 +2401,11 @@ fn resolve_implementation(&mut self,
                         for impl_item in impl_items {
                             match impl_item.node {
                                 ConstImplItem(..) => {
-                                    // If this is a trait impl, ensure the method
+                                    // If this is a trait impl, ensure the const
                                     // exists in trait
                                     this.check_trait_item(impl_item.ident.name,
-                                                          impl_item.span);
+                                                          impl_item.span,
+                                        |n, s| ResolutionError::ConstNotMemberOfTrait(n, s));
                                     this.with_constant_rib(|this| {
                                         visit::walk_impl_item(this, impl_item);
                                     });
@@ -2397,7 +2414,8 @@ fn resolve_implementation(&mut self,
                                     // If this is a trait impl, ensure the method
                                     // exists in trait
                                     this.check_trait_item(impl_item.ident.name,
-                                                          impl_item.span);
+                                                          impl_item.span,
+                                        |n, s| ResolutionError::MethodNotMemberOfTrait(n, s));
 
                                     // We also need a new scope for the method-
                                     // specific type parameters.
@@ -2410,10 +2428,11 @@ fn resolve_implementation(&mut self,
                                     });
                                 }
                                 TypeImplItem(ref ty) => {
-                                    // If this is a trait impl, ensure the method
+                                    // If this is a trait impl, ensure the type
                                     // exists in trait
                                     this.check_trait_item(impl_item.ident.name,
-                                                          impl_item.span);
+                                                          impl_item.span,
+                                        |n, s| ResolutionError::TypeNotMemberOfTrait(n, s));
 
                                     this.visit_ty(ty);
                                 }
@@ -2426,15 +2445,15 @@ fn resolve_implementation(&mut self,
         });
     }
 
-    fn check_trait_item(&self, name: Name, span: Span) {
+    fn check_trait_item<F>(&self, name: Name, span: Span, err: F)
+        where F: FnOnce(Name, &str) -> ResolutionError {
         // If there is a TraitRef in scope for an impl, then the method must be in the trait.
         if let Some((did, ref trait_ref)) = self.current_trait_ref {
             if !self.trait_item_map.contains_key(&(name, did)) {
                 let path_str = path_names_to_string(&trait_ref.path, 0);
                 resolve_error(self,
                               span,
-                              ResolutionError::MethodNotMemberOfTrait(name,
-                                                                       &*path_str));
+                              err(name, &*path_str));
             }
         }
     }
diff --git a/src/test/compile-fail/trait-impl-can-not-have-untraitful-items.rs b/src/test/compile-fail/trait-impl-can-not-have-untraitful-items.rs
new file mode 100644 (file)
index 0000000..0ed4e62
--- /dev/null
@@ -0,0 +1,21 @@
+// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(associated_consts)]
+
+trait A { }
+
+impl A for isize {
+    const BAR: () = (); //~ ERROR const `BAR` is not a member of trait `A`
+    type Baz = (); //~ ERROR type `Baz` is not a member of trait `A`
+    fn foo(&self) { } //~ ERROR method `foo` is not a member of trait `A`
+}
+
+fn main() { }
diff --git a/src/test/compile-fail/trait-impl-can-not-have-untraitful-methods.rs b/src/test/compile-fail/trait-impl-can-not-have-untraitful-methods.rs
deleted file mode 100644 (file)
index 3538c7f..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-trait A { }
-
-impl A for isize {
-    fn foo(&self) { } //~ ERROR method `foo` is not a member of trait `A`
-}
-
-fn main() { }