Skip to content

Commit 74cb315

Browse files
committed
rustc_typeck: enforce argument type is sized
1 parent 6db4838 commit 74cb315

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,15 @@ fn check_fn<'a, 'gcx, 'tcx>(inherited: &'a Inherited<'a, 'gcx, 'tcx>,
994994
for (arg_ty, arg) in fn_sig.inputs().iter().zip(&body.arguments) {
995995
// Check the pattern.
996996
fcx.check_pat_arg(&arg.pat, arg_ty, true);
997+
998+
// Check that argument is Sized.
999+
// The check for a non-trivial pattern is a hack to avoid duplicate warnings
1000+
// for simple cases like `fn foo(x: Trait)`,
1001+
// where we would error once on the parameter as a whole, and once on the binding `x`.
1002+
if arg.pat.simple_name().is_none() {
1003+
fcx.require_type_is_sized(arg_ty, decl.output.span(), traits::MiscObligation);
1004+
}
1005+
9971006
fcx.write_ty(arg.id, arg_ty);
9981007
}
9991008

src/test/compile-fail/issue-38954.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(rustc_attrs)]
12-
1311
fn _test(ref _p: str) {}
12+
//~^ ERROR the trait bound `str: std::marker::Sized` is not satisfied [E0277]
1413

15-
#[rustc_error]
16-
fn main() { } //~ ERROR compilation successful
14+
fn main() { }

src/test/compile-fail/issue-42312.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::ops::Deref;
12+
13+
pub trait Foo {
14+
fn baz(_: Self::Target) where Self: Deref {}
15+
//~^ ERROR `<Self as std::ops::Deref>::Target: std::marker::Sized` is not satisfied
16+
}
17+
18+
pub fn f(_: ToString) {}
19+
//~^ ERROR the trait bound `std::string::ToString + 'static: std::marker::Sized` is not satisfied
20+
21+
fn main() { }

src/test/run-pass/associated-types-sugar-path.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ use std::ops::Deref;
1515
pub trait Foo {
1616
type A;
1717
fn boo(&self) -> Self::A;
18-
19-
fn baz(_: Self::Target) where Self: Deref {}
2018
}
2119

2220
impl Foo for isize {

0 commit comments

Comments
 (0)