Skip to content

rustc: added a check for builtin bounds to vtable search #13228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/librustc/middle/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ fn check_impl_of_trait(cx: &mut Context, it: &Item, trait_ref: &TraitRef, self_t
// If this trait has builtin-kind supertraits, meet them.
let self_ty: ty::t = ty::node_id_to_type(cx.tcx, it.id);
debug!("checking impl with self type {:?}", ty::get(self_ty).sty);
check_builtin_bounds(cx, self_ty, trait_def.bounds, |missing| {
check_builtin_bounds(cx.tcx, self_ty, trait_def.bounds, |missing| {
cx.tcx.sess.span_err(self_type.span,
format!("the type `{}', which does not fulfill `{}`, cannot implement this \
trait", ty_to_str(cx.tcx, self_ty), missing.user_string(cx.tcx)));
Expand Down Expand Up @@ -354,14 +354,14 @@ fn check_ty(cx: &mut Context, aty: &Ty) {
}

// Calls "any_missing" if any bounds were missing.
pub fn check_builtin_bounds(cx: &Context,
pub fn check_builtin_bounds(tcx: &ty::ctxt,
ty: ty::t,
bounds: ty::BuiltinBounds,
any_missing: |ty::BuiltinBounds|) {
let kind = ty::type_contents(cx.tcx, ty);
let kind = ty::type_contents(tcx, ty);
let mut missing = ty::EmptyBuiltinBounds();
for bound in bounds.iter() {
if !kind.meets_bound(cx.tcx, bound) {
if !kind.meets_bound(tcx, bound) {
missing.add(bound);
}
}
Expand All @@ -374,7 +374,7 @@ pub fn check_typaram_bounds(cx: &Context,
sp: Span,
ty: ty::t,
type_param_def: &ty::TypeParameterDef) {
check_builtin_bounds(cx,
check_builtin_bounds(cx.tcx,
ty,
type_param_def.bounds.builtin_bounds,
|missing| {
Expand All @@ -390,7 +390,7 @@ pub fn check_typaram_bounds(cx: &Context,
pub fn check_freevar_bounds(cx: &Context, sp: Span, ty: ty::t,
bounds: ty::BuiltinBounds, referenced_ty: Option<ty::t>)
{
check_builtin_bounds(cx, ty, bounds, |missing| {
check_builtin_bounds(cx.tcx, ty, bounds, |missing| {
// Will be Some if the freevar is implicitly borrowed (stack closure).
// Emit a less mysterious error message in this case.
match referenced_ty {
Expand All @@ -412,7 +412,7 @@ pub fn check_freevar_bounds(cx: &Context, sp: Span, ty: ty::t,

pub fn check_trait_cast_bounds(cx: &Context, sp: Span, ty: ty::t,
bounds: ty::BuiltinBounds) {
check_builtin_bounds(cx, ty, bounds, |missing| {
check_builtin_bounds(cx.tcx, ty, bounds, |missing| {
cx.tcx.sess.span_err(sp,
format!("cannot pack type `{}`, which does not fulfill \
`{}`, as a trait bounded by {}",
Expand Down
43 changes: 43 additions & 0 deletions src/librustc/middle/typeck/check/vtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.


use middle::kind::check_builtin_bounds;
use middle::ty;
use middle::ty::{AutoAddEnv, AutoDerefRef, AutoObject, param_ty};
use middle::ty_fold::TypeFolder;
Expand All @@ -26,6 +27,7 @@ use middle::subst::Subst;
use util::common::indenter;
use util::ppaux;
use util::ppaux::Repr;
use util::ppaux::UserString;

use std::rc::Rc;
use collections::HashSet;
Expand Down Expand Up @@ -121,8 +123,49 @@ fn lookup_vtables_for_param(vcx: &VtableContext,
type_param_bounds: &ty::ParamBounds,
ty: ty::t,
is_early: bool) -> vtable_param_res {

fn should_check_builtin_bounds(vcx: &VtableContext, ty: ty::t) -> bool {
match ty::get(ty).sty {
ty::ty_param(param_ty {idx: n, ..}) => {
if vcx.param_env.type_param_bounds.len() > n {
true
} else {
// more type parameters are being instantiated then were declared,
// so don't check builtin bounds to avoid trying to look-up nonexistent
// type param delaration
false
}
}

_ => true
}
}

let tcx = vcx.tcx();

// FIXME(#13231): this will likely break once opt-in builtin kinds are implemented
if !type_param_bounds.builtin_bounds.is_empty() {
match fixup_ty(vcx, span, ty, is_early) {
Some(fix_ty) => {
if should_check_builtin_bounds(vcx, fix_ty) {
check_builtin_bounds(
vcx.tcx(),
fix_ty,
type_param_bounds.builtin_bounds,
|missing: ty::BuiltinBounds| {
vcx.tcx().sess.span_err(
span,
format!("instantiating a type parameter with an incompatible type \
`{}`, which does not fulfill `{}`",
vcx.infcx.ty_to_str(ty),
missing.user_string(tcx)));
});
}
}
None => {}
}
}

// ty is the value supplied for the type parameter A...
let mut param_result = Vec::new();

Expand Down
47 changes: 47 additions & 0 deletions src/test/compile-fail/issue-10751.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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.

// Test that builtin bounds are checked when searching for an
// implementation of a trait
use std::cell::Cell;

trait A {}
trait B {}
trait C {}
trait D {}
impl<T: 'static> A for T {}
impl<T: Send> B for T {}
impl<T: Copy> C for T {}
impl<T: Share> D for T {}

fn main() {
let a = 3;
let b = &a;
let c = &b as &A; //~ ERROR instantiating a type parameter with an incompatible type `&int`
let d: &A = &b; //~ ERROR instantiating a type parameter with an incompatible type `&int`
let e = &b as &B; //~ ERROR instantiating a type parameter with an incompatible type `&int`
let f: &B = &b; //~ ERROR instantiating a type parameter with an incompatible type `&int`
let g = &~b as &C; //~ ERROR instantiating a type parameter with an incompatible type `~&int`
let h: &C = &~b; //~ ERROR instantiating a type parameter with an incompatible type `~&int`
let i = &Cell::new(b) as &D;
//~^ ERROR instantiating a type parameter with an incompatible type `std::cell::Cell<&int>`
let j: &D = &Cell::new(b);
//~^ ERROR instantiating a type parameter with an incompatible type `std::cell::Cell<&int>`

// These are all ok: int is 'static + Send + Copy + Share
let k: &A = b;
let l: &B = b;
let m: &C = b;
let n: &D = b;
let o = b as &A;
let p = b as &B;
let q = b as &C;
let r = b as &D;
}