Skip to content

Avoid forcing int and float type variables in early vtable resolution #10486

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
3 changes: 3 additions & 0 deletions src/librustc/middle/astencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,9 @@ pub fn encode_vtable_origin(ecx: &e::EncodeContext,
})
})
}
typeck::vtable_none => {
fail!("unexpected vtable_none in astencode")
}
}
})
}
Expand Down
3 changes: 3 additions & 0 deletions src/librustc/middle/trans/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,9 @@ pub fn resolve_vtable_under_param_substs(tcx: ty::ctxt,
}
}
}
typeck::vtable_none => {
tcx.sess.bug("unexpected vtable_none in trans");
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/librustc/middle/trans/meth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ pub fn trans_monomorphized_callee(bcx: @mut Block,
typeck::vtable_param(..) => {
fail!("vtable_param left in monomorphized function's vtable substs");
}
typeck::vtable_none => {
fail!("unexpected vtable_none in trans");
}
};

}
Expand Down
29 changes: 29 additions & 0 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,12 @@ struct ctxt_ {
// Maps a trait onto a list of impls of that trait.
trait_impls: @mut HashMap<ast::DefId, @mut ~[@Impl]>,

// Traits implemented by all integral types.
int_traits: @mut HashSet<ast::DefId>,

// Traits implemented by all floating point types.
float_traits: @mut HashSet<ast::DefId>,

// Maps a def_id of a type to a list of its inherent impls.
// Contains implementations of methods that are inherent to a type.
// Methods in these implementations don't need to be exported.
Expand Down Expand Up @@ -1009,6 +1015,8 @@ pub fn mk_ctxt(s: session::Session,
destructor_for_type: @mut HashMap::new(),
destructors: @mut HashSet::new(),
trait_impls: @mut HashMap::new(),
int_traits: @mut HashSet::new(),
float_traits: @mut HashSet::new(),
inherent_impls: @mut HashMap::new(),
impls: @mut HashMap::new(),
used_unsafe: @mut HashSet::new(),
Expand Down Expand Up @@ -4515,8 +4523,22 @@ pub fn populate_implementations_for_trait_if_necessary(
return
}

let mut int_count = 0;
let mut float_count = 0;

csearch::each_implementation_for_trait(tcx.sess.cstore, trait_id,
|implementation_def_id| {
let ty = lookup_item_type(tcx, implementation_def_id).ty;
match get(ty).sty {
ty_int(..) | ty_uint(..) => {
int_count += 1;
}
ty_float(..) => {
float_count += 1;
}
_ => ()
}

let implementation = @csearch::get_impl(tcx, implementation_def_id);

// Record the trait->implementation mapping.
Expand All @@ -4534,6 +4556,13 @@ pub fn populate_implementations_for_trait_if_necessary(
tcx.impls.insert(implementation_def_id, implementation);
});

if int_count == 10 {
tcx.int_traits.insert(trait_id);
}
if float_count == 2 {
tcx.float_traits.insert(trait_id);
}

tcx.populated_external_traits.insert(trait_id);
}

Expand Down
21 changes: 20 additions & 1 deletion 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::ty::{FloatVar, IntVar};
use middle::ty::param_ty;
use middle::ty;
use middle::ty_fold::TypeFolder;
Expand All @@ -18,7 +19,7 @@ use middle::typeck::infer::fixup_err_to_str;
use middle::typeck::infer::{resolve_and_force_all_but_regions, resolve_type};
use middle::typeck::infer;
use middle::typeck::{CrateCtxt, vtable_origin, vtable_res, vtable_param_res};
use middle::typeck::{vtable_static, vtable_param, impl_res};
use middle::typeck::{vtable_static, vtable_param, vtable_none, impl_res};
use middle::typeck::{param_numbered, param_self, param_index};
use middle::subst::Subst;
use util::common::indenter;
Expand Down Expand Up @@ -233,6 +234,24 @@ fn lookup_vtable(vcx: &VtableContext,
vcx.infcx.trait_ref_to_str(trait_ref));
let _i = indenter();

if is_early {
let tcx = vcx.tcx();
let ty = vcx.infcx.resolve_type_vars_if_possible(ty);
match ty::get(ty).sty {
ty::ty_infer(IntVar(..)) => {
if tcx.int_traits.contains(&trait_ref.def_id) {
return Some(vtable_none);
}
}
ty::ty_infer(FloatVar(..)) => {
if tcx.float_traits.contains(&trait_ref.def_id) {
return Some(vtable_none);
}
}
_ => ()
}
}

let ty = match fixup_ty(vcx, location_info, ty, is_early) {
Some(ty) => ty,
None => {
Expand Down
5 changes: 4 additions & 1 deletion src/librustc/middle/typeck/check/writeback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use middle::typeck::infer::{force_all, resolve_all, resolve_region};
use middle::typeck::infer::resolve_type;
use middle::typeck::infer;
use middle::typeck::{vtable_res, vtable_origin};
use middle::typeck::{vtable_static, vtable_param};
use middle::typeck::{vtable_static, vtable_param, vtable_none};
use middle::typeck::method_map_entry;
use middle::typeck::write_substs_to_tcx;
use middle::typeck::write_ty_to_tcx;
Expand Down Expand Up @@ -110,6 +110,9 @@ fn resolve_vtable_map_entry(fcx: @mut FnCtxt, sp: Span, id: ast::NodeId) {
&vtable_param(n, b) => {
vtable_param(n, b)
}
&vtable_none => {
vtable_none
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/librustc/middle/typeck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ pub enum vtable_origin {
and the second is the bound number (identifying baz)
*/
vtable_param(param_index, uint),

// For early vtable resolution
vtable_none,
}

impl Repr for vtable_origin {
Expand All @@ -199,6 +202,9 @@ impl Repr for vtable_origin {
vtable_param(x, y) => {
format!("vtable_param({:?}, {:?})", x, y)
}
vtable_none => {
~"vtable_none"
}
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/run-pass/issue-10436.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2013 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.

fn f<T: Clone>(x: T) -> T {
x
}

fn main() {
let x: uint = f(0);
assert_eq!(x, 0);
}