about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVenkata Giri Reddy <venkatagirireddy@gmail.com>2017-06-13 20:22:28 +0000
committerVenkata Giri Reddy <venkatagirireddy@gmail.com>2017-06-28 17:54:18 +0000
commit74cb315a109f9b7a4a73cc6bf3ac481e92ec926d (patch)
tree647fc01f3c8e0b3512c4d544554f7eef07dcb257
parent6db48380ce437bcb21da450db1696ede0fcba158 (diff)
downloadrust-74cb315a109f9b7a4a73cc6bf3ac481e92ec926d.tar.gz
rust-74cb315a109f9b7a4a73cc6bf3ac481e92ec926d.zip
rustc_typeck: enforce argument type is sized
-rw-r--r--src/librustc_typeck/check/mod.rs9
-rw-r--r--src/test/compile-fail/issue-38954.rs6
-rw-r--r--src/test/compile-fail/issue-42312.rs21
-rw-r--r--src/test/run-pass/associated-types-sugar-path.rs2
4 files changed, 32 insertions, 6 deletions
diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs
index c08eeb740ec..a0afd58e586 100644
--- a/src/librustc_typeck/check/mod.rs
+++ b/src/librustc_typeck/check/mod.rs
@@ -994,6 +994,15 @@ fn check_fn<'a, 'gcx, 'tcx>(inherited: &'a Inherited<'a, 'gcx, 'tcx>,
     for (arg_ty, arg) in fn_sig.inputs().iter().zip(&body.arguments) {
         // Check the pattern.
         fcx.check_pat_arg(&arg.pat, arg_ty, true);
+
+        // Check that argument is Sized.
+        // The check for a non-trivial pattern is a hack to avoid duplicate warnings
+        // for simple cases like `fn foo(x: Trait)`,
+        // where we would error once on the parameter as a whole, and once on the binding `x`.
+        if arg.pat.simple_name().is_none() {
+            fcx.require_type_is_sized(arg_ty, decl.output.span(), traits::MiscObligation);
+        }
+
         fcx.write_ty(arg.id, arg_ty);
     }
 
diff --git a/src/test/compile-fail/issue-38954.rs b/src/test/compile-fail/issue-38954.rs
index 65b17a3db0b..896728b6da0 100644
--- a/src/test/compile-fail/issue-38954.rs
+++ b/src/test/compile-fail/issue-38954.rs
@@ -8,9 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-#![feature(rustc_attrs)]
-
 fn _test(ref _p: str) {}
+//~^ ERROR the trait bound `str: std::marker::Sized` is not satisfied [E0277]
 
-#[rustc_error]
-fn main() { } //~ ERROR compilation successful
+fn main() { }
diff --git a/src/test/compile-fail/issue-42312.rs b/src/test/compile-fail/issue-42312.rs
new file mode 100644
index 00000000000..06573b42b59
--- /dev/null
+++ b/src/test/compile-fail/issue-42312.rs
@@ -0,0 +1,21 @@
+// Copyright 2017 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.
+
+use std::ops::Deref;
+
+pub trait Foo {
+    fn baz(_: Self::Target) where Self: Deref {}
+    //~^ ERROR `<Self as std::ops::Deref>::Target: std::marker::Sized` is not satisfied
+}
+
+pub fn f(_: ToString) {}
+//~^ ERROR the trait bound `std::string::ToString + 'static: std::marker::Sized` is not satisfied
+
+fn main() { }
diff --git a/src/test/run-pass/associated-types-sugar-path.rs b/src/test/run-pass/associated-types-sugar-path.rs
index 587fb3f80d6..d8d42f2cba2 100644
--- a/src/test/run-pass/associated-types-sugar-path.rs
+++ b/src/test/run-pass/associated-types-sugar-path.rs
@@ -15,8 +15,6 @@ use std::ops::Deref;
 pub trait Foo {
     type A;
     fn boo(&self) -> Self::A;
-
-    fn baz(_: Self::Target) where Self: Deref {}
 }
 
 impl Foo for isize {