Skip to content

Commit

Permalink
auto merge of #15957 : pcwalton/rust/builtin-bound-impl-checking, r=h…
Browse files Browse the repository at this point in the history
…uonw,pnkfelix

method calls are involved.

This breaks code like:

    impl<T:Copy> Foo for T { ... }

    fn take_param<T:Foo>(foo: &T) { ... }

    fn main() {
        let x = box 3i; // note no `Copy` bound
        take_param(&x);
    }

Change this code to not contain a type error. For example:

    impl<T:Copy> Foo for T { ... }

    fn take_param<T:Foo>(foo: &T) { ... }

    fn main() {
        let x = 3i; // satisfies `Copy` bound
        take_param(&x);
    }

Closes #15860.

[breaking-change]

r? @alexcrichton
  • Loading branch information
bors committed Jul 25, 2014
2 parents e598464 + f1520ea commit 470dbef
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/libcore/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
//! }
//!
//! // This function wants to log its parameter out prior to doing work with it.
//! fn do_work<T: Show>(value: &T) {
//! fn do_work<T: Show+'static>(value: &T) {
//! log(value);
//! // ...do some other work
//! }
Expand Down
11 changes: 10 additions & 1 deletion src/librustc/middle/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@ pub fn check_expr(cx: &mut Context, e: &Expr) {

fn check_bounds_on_type_parameters(cx: &mut Context, e: &Expr) {
let method_map = cx.tcx.method_map.borrow();
let method = method_map.find(&typeck::MethodCall::expr(e.id));
let method_call = typeck::MethodCall::expr(e.id);
let method = method_map.find(&method_call);

// Find the values that were provided (if any)
let item_substs = cx.tcx.item_substs.borrow();
Expand Down Expand Up @@ -393,6 +394,14 @@ fn check_bounds_on_type_parameters(cx: &mut Context, e: &Expr) {
type_param_def.space, type_param_def.index, ty.repr(cx.tcx));
check_typaram_bounds(cx, e.span, ty, type_param_def)
}

// Check the vtable.
let vtable_map = cx.tcx.vtable_map.borrow();
let vtable_res = match vtable_map.find(&method_call) {
None => return,
Some(vtable_res) => vtable_res,
};
check_type_parameter_bounds_in_vtable_result(cx, e.span, vtable_res);
}

fn check_type_parameter_bounds_in_vtable_result(
Expand Down
23 changes: 23 additions & 0 deletions src/test/compile-fail/kindck-impl-type-params-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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.

trait Foo {
}

impl<T:Copy> Foo for T {
}

fn take_param<T:Foo>(foo: &T) { }

fn main() {
let x = box 3i;
take_param(&x);
//~^ ERROR instantiating a type parameter with an incompatible type
}

0 comments on commit 470dbef

Please sign in to comment.