Skip to content

Commit 470dbef

Browse files
committed
auto merge of #15957 : pcwalton/rust/builtin-bound-impl-checking, r=huonw,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
2 parents e598464 + f1520ea commit 470dbef

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/libcore/any.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
//! }
5656
//!
5757
//! // This function wants to log its parameter out prior to doing work with it.
58-
//! fn do_work<T: Show>(value: &T) {
58+
//! fn do_work<T: Show+'static>(value: &T) {
5959
//! log(value);
6060
//! // ...do some other work
6161
//! }

src/librustc/middle/kind.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,8 @@ pub fn check_expr(cx: &mut Context, e: &Expr) {
324324

325325
fn check_bounds_on_type_parameters(cx: &mut Context, e: &Expr) {
326326
let method_map = cx.tcx.method_map.borrow();
327-
let method = method_map.find(&typeck::MethodCall::expr(e.id));
327+
let method_call = typeck::MethodCall::expr(e.id);
328+
let method = method_map.find(&method_call);
328329

329330
// Find the values that were provided (if any)
330331
let item_substs = cx.tcx.item_substs.borrow();
@@ -393,6 +394,14 @@ fn check_bounds_on_type_parameters(cx: &mut Context, e: &Expr) {
393394
type_param_def.space, type_param_def.index, ty.repr(cx.tcx));
394395
check_typaram_bounds(cx, e.span, ty, type_param_def)
395396
}
397+
398+
// Check the vtable.
399+
let vtable_map = cx.tcx.vtable_map.borrow();
400+
let vtable_res = match vtable_map.find(&method_call) {
401+
None => return,
402+
Some(vtable_res) => vtable_res,
403+
};
404+
check_type_parameter_bounds_in_vtable_result(cx, e.span, vtable_res);
396405
}
397406

398407
fn check_type_parameter_bounds_in_vtable_result(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2014 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+
trait Foo {
12+
}
13+
14+
impl<T:Copy> Foo for T {
15+
}
16+
17+
fn take_param<T:Foo>(foo: &T) { }
18+
19+
fn main() {
20+
let x = box 3i;
21+
take_param(&x);
22+
//~^ ERROR instantiating a type parameter with an incompatible type
23+
}

0 commit comments

Comments
 (0)