1
Fork 0

librustc: Check built-in trait bounds on implementations when direct

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]
This commit is contained in:
Patrick Walton 2014-07-24 13:52:47 -07:00
parent b9035c26e2
commit f1520ea0cf
3 changed files with 34 additions and 2 deletions

View file

@ -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
//! }

View file

@ -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();
@ -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(

View file

@ -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
}