Skip to content

Commit

Permalink
rustc_typeck: enforce argument type is sized
Browse files Browse the repository at this point in the history
  • Loading branch information
venkatagiri committed Jun 28, 2017
1 parent 6db4838 commit 74cb315
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -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);
}

Expand Down
6 changes: 2 additions & 4 deletions src/test/compile-fail/issue-38954.rs
Expand Up @@ -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() { }
21 changes: 21 additions & 0 deletions 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() { }
2 changes: 0 additions & 2 deletions src/test/run-pass/associated-types-sugar-path.rs
Expand Up @@ -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 {
Expand Down

0 comments on commit 74cb315

Please sign in to comment.