]> git.lizzy.rs Git - rust.git/commitdiff
rollup merge of #18413 : bkoropoff/issue-18412
authorAlex Crichton <alex@alexcrichton.com>
Thu, 30 Oct 2014 15:57:40 +0000 (08:57 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Thu, 30 Oct 2014 16:29:24 +0000 (09:29 -0700)
src/librustc/middle/trans/expr.rs
src/librustc/middle/ty.rs
src/test/run-pass/issue-18412.rs [new file with mode: 0644]

index 4d004c85f6e5f1ce47b3c93e23bef00d0a8426cd..2fcc4f6af151700a88e9292c0a49f08fddea2642 100644 (file)
@@ -833,7 +833,7 @@ fn trans_def<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
 
     let _icx = push_ctxt("trans_def_lvalue");
     match def {
-        def::DefFn(..) | def::DefStaticMethod(..) |
+        def::DefFn(..) | def::DefStaticMethod(..) | def::DefMethod(..) |
         def::DefStruct(_) | def::DefVariant(..) => {
             trans_def_fn_unadjusted(bcx, ref_expr, def)
         }
@@ -1191,10 +1191,12 @@ fn trans_def_fn_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
     let llfn = match def {
         def::DefFn(did, _, _) |
         def::DefStruct(did) | def::DefVariant(_, did, _) |
-        def::DefStaticMethod(did, def::FromImpl(_), _) => {
+        def::DefStaticMethod(did, def::FromImpl(_), _) |
+        def::DefMethod(did, _, def::FromImpl(_)) => {
             callee::trans_fn_ref(bcx, did, ExprId(ref_expr.id))
         }
-        def::DefStaticMethod(impl_did, def::FromTrait(trait_did), _) => {
+        def::DefStaticMethod(impl_did, def::FromTrait(trait_did), _) |
+        def::DefMethod(impl_did, _, def::FromTrait(trait_did)) => {
             meth::trans_static_method_callee(bcx, impl_did,
                                              trait_did, ref_expr.id)
         }
index edb67f7fddf8b3f971dd7538964dbf0f11b27204..3a1dbb887be67d40c0571071a80651096a405527 100644 (file)
@@ -3631,7 +3631,7 @@ pub fn expr_kind(tcx: &ctxt, expr: &ast::Expr) -> ExprKind {
                 def::DefFn(_, _, true) => RvalueDpsExpr,
 
                 // Fn pointers are just scalar values.
-                def::DefFn(..) | def::DefStaticMethod(..) => RvalueDatumExpr,
+                def::DefFn(..) | def::DefStaticMethod(..) | def::DefMethod(..) => RvalueDatumExpr,
 
                 // Note: there is actually a good case to be made that
                 // DefArg's, particularly those of immediate type, ought to
diff --git a/src/test/run-pass/issue-18412.rs b/src/test/run-pass/issue-18412.rs
new file mode 100644 (file)
index 0000000..c03301f
--- /dev/null
@@ -0,0 +1,36 @@
+// Copyright 2014 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(tuple_indexing)]
+
+// Test that non-static methods can be assigned to local variables as
+// function pointers.
+
+trait Foo {
+    fn foo(&self) -> uint;
+}
+
+struct A(uint);
+
+impl A {
+    fn bar(&self) -> uint { self.0 }
+}
+
+impl Foo for A {
+    fn foo(&self) -> uint { self.bar() }
+}
+
+fn main() {
+    let f = A::bar;
+    let g = Foo::foo;
+    let a = A(42);
+
+    assert_eq!(f(&a), g(&a));
+}