Skip to content

Use defaults from formal type parameters in type inference, as fallbacks. #13702

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 3 commits 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
2 changes: 1 addition & 1 deletion src/librustc/middle/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ impl<'a> AstConv for Context<'a>{
}

fn ty_infer(&self, _span: Span) -> ty::t {
infer::new_infer_ctxt(self.tcx).next_ty_var()
infer::new_infer_ctxt(self.tcx).next_ty_var(None)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn check_match(fcx: &FnCtxt,
arms: &[ast::Arm]) {
let tcx = fcx.ccx.tcx;

let discrim_ty = fcx.infcx().next_ty_var();
let discrim_ty = fcx.infcx().next_ty_var(None);
check_expr_has_type(fcx, discrim, discrim_ty);

// Typecheck the patterns first, so that we get types for all the
Expand Down
34 changes: 16 additions & 18 deletions src/librustc/middle/typeck/check/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1095,24 +1095,22 @@ impl<'a> LookupContext<'a> {
// If they were not explicitly supplied, just construct fresh
// variables.
let num_supplied_tps = self.supplied_tps.len();
let num_method_tps = candidate.method_ty.generics.type_param_defs().len();
let m_substs = {
if num_supplied_tps == 0u {
self.fcx.infcx().next_ty_vars(num_method_tps)
} else if num_method_tps == 0u {
tcx.sess.span_err(
self.span,
"this method does not take type parameters");
self.fcx.infcx().next_ty_vars(num_method_tps)
} else if num_supplied_tps != num_method_tps {
tcx.sess.span_err(
self.span,
"incorrect number of type \
parameters given for this method");
self.fcx.infcx().next_ty_vars(num_method_tps)
} else {
Vec::from_slice(self.supplied_tps)
}
let method_tps = candidate.method_ty.generics.type_param_defs();
let m_substs = if num_supplied_tps == 0u {
self.fcx.infcx().next_ty_vars(method_tps)
} else if method_tps.len() == 0u {
tcx.sess.span_err(
self.span,
"this method does not take type parameters");
self.fcx.infcx().next_ty_vars(method_tps)
} else if num_supplied_tps != method_tps.len() {
tcx.sess.span_err(
self.span,
"incorrect number of type \
parameters given for this method");
self.fcx.infcx().next_ty_vars(method_tps)
} else {
Vec::from_slice(self.supplied_tps)
};

// Determine values for the early-bound lifetime parameters.
Expand Down
65 changes: 28 additions & 37 deletions src/librustc/middle/typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,18 +367,17 @@ struct GatherLocalsVisitor<'a> {

impl<'a> GatherLocalsVisitor<'a> {
fn assign(&mut self, nid: ast::NodeId, ty_opt: Option<ty::t>) {
match ty_opt {
None => {
// infer the variable's type
let var_id = self.fcx.infcx().next_ty_var_id();
let var_ty = ty::mk_var(self.fcx.tcx(), var_id);
self.fcx.inh.locals.borrow_mut().insert(nid, var_ty);
}
Some(typ) => {
// take type that the user specified
self.fcx.inh.locals.borrow_mut().insert(nid, typ);
}
match ty_opt {
None => {
// infer the variable's type
let var_ty = self.fcx.infcx().next_ty_var(None);
self.fcx.inh.locals.borrow_mut().insert(nid, var_ty);
}
Some(typ) => {
// take type that the user specified
self.fcx.inh.locals.borrow_mut().insert(nid, typ);
}
}
}
}

Expand Down Expand Up @@ -1051,7 +1050,7 @@ impl<'a> AstConv for FnCtxt<'a> {
}

fn ty_infer(&self, _span: Span) -> ty::t {
self.infcx().next_ty_var()
self.infcx().next_ty_var(None)
}
}

Expand Down Expand Up @@ -1479,20 +1478,16 @@ pub fn impl_self_ty(vcx: &VtableContext,
let tcx = vcx.tcx();

let ity = ty::lookup_item_type(tcx, did);
let (n_tps, rps, raw_ty) =
(ity.generics.type_param_defs().len(),
ity.generics.region_param_defs(),
ity.ty);

let rps = ity.generics.region_param_defs();
let rps = vcx.infcx.region_vars_for_defs(span, rps);
let tps = vcx.infcx.next_ty_vars(n_tps);

let substs = substs {
regions: ty::NonerasedRegions(rps),
self_ty: None,
tps: tps,
tps: vcx.infcx.next_ty_vars(ity.generics.type_param_defs()),
};
let substd_ty = ty::subst(tcx, &substs, raw_ty);
let substd_ty = ty::subst(tcx, &substs, ity.ty);

ty_param_substs_and_ty { substs: substs, ty: substd_ty }
}
Expand Down Expand Up @@ -2097,7 +2092,7 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
}

if ty::is_binopable(tcx, lhs_t, op) {
let tvar = fcx.infcx().next_ty_var();
let tvar = fcx.infcx().next_ty_var(None);
demand::suptype(fcx, expr.span, tvar, lhs_t);
check_expr_has_type(fcx, rhs, tvar);

Expand Down Expand Up @@ -2477,17 +2472,15 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
// Look up the number of type parameters and the raw type, and
// determine whether the class is region-parameterized.
let item_type = ty::lookup_item_type(tcx, class_id);
let type_parameter_count = item_type.generics.type_param_defs().len();
let region_param_defs = item_type.generics.region_param_defs();
let raw_type = item_type.ty;

// Generate the struct type.
let regions = fcx.infcx().region_vars_for_defs(span, region_param_defs);
let type_parameters = fcx.infcx().next_ty_vars(type_parameter_count);
let substitutions = substs {
regions: ty::NonerasedRegions(regions),
self_ty: None,
tps: type_parameters
tps: fcx.infcx().next_ty_vars(item_type.generics.type_param_defs())
};

let mut struct_type = ty::subst(tcx, &substitutions, raw_type);
Expand Down Expand Up @@ -2533,17 +2526,15 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
// Look up the number of type parameters and the raw type, and
// determine whether the enum is region-parameterized.
let item_type = ty::lookup_item_type(tcx, enum_id);
let type_parameter_count = item_type.generics.type_param_defs().len();
let region_param_defs = item_type.generics.region_param_defs();
let raw_type = item_type.ty;

// Generate the enum type.
let regions = fcx.infcx().region_vars_for_defs(span, region_param_defs);
let type_parameters = fcx.infcx().next_ty_vars(type_parameter_count);
let substitutions = substs {
regions: ty::NonerasedRegions(regions),
self_ty: None,
tps: type_parameters
tps: fcx.infcx().next_ty_vars(item_type.generics.type_param_defs())
};

let enum_type = ty::subst(tcx, &substitutions, raw_type);
Expand Down Expand Up @@ -2579,7 +2570,7 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
let v = ast_expr_vstore_to_vstore(fcx, ev, vst);
let mut any_error = false;
let mut any_bot = false;
let t: ty::t = fcx.infcx().next_ty_var();
let t = fcx.infcx().next_ty_var(None);
for e in args.iter() {
check_expr_has_type(fcx, *e, t);
let arg_t = fcx.expr_ty(*e);
Expand Down Expand Up @@ -2622,7 +2613,7 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
_ => ast::MutImmutable,
};
let tt = ast_expr_vstore_to_vstore(fcx, ev, vst);
let t = fcx.infcx().next_ty_var();
let t = fcx.infcx().next_ty_var(None);
check_expr_has_type(fcx, element, t);
let arg_t = fcx.expr_ty(element);
if ty::type_is_error(arg_t) {
Expand Down Expand Up @@ -3142,7 +3133,7 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
}
}
ast::ExprVec(ref args) => {
let t: ty::t = fcx.infcx().next_ty_var();
let t: ty::t = fcx.infcx().next_ty_var(None);
for e in args.iter() {
check_expr_has_type(fcx, *e, t);
}
Expand All @@ -3153,7 +3144,7 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
ast::ExprRepeat(element, count_expr) => {
check_expr_with_hint(fcx, count_expr, ty::mk_uint());
let count = ty::eval_repeat_count(fcx, count_expr);
let t: ty::t = fcx.infcx().next_ty_var();
let t: ty::t = fcx.infcx().next_ty_var(None);
check_expr_has_type(fcx, element, t);
let element_ty = fcx.expr_ty(element);
if ty::type_is_error(element_ty) {
Expand Down Expand Up @@ -3845,11 +3836,11 @@ pub fn instantiate_path(fcx: &FnCtxt,
// determine values for type parameters, using the values given by
// the user (if any) and otherwise using fresh type variables
let (tps, regions) = if ty_substs_len == 0 {
(fcx.infcx().next_ty_vars(ty_param_count), regions)
(fcx.infcx().next_ty_vars(tpt.generics.type_param_defs()), regions)
} else if ty_param_count == 0 {
fcx.ccx.tcx.sess.span_err
(span, "this item does not take type parameters");
(fcx.infcx().next_ty_vars(ty_param_count), regions)
(fcx.infcx().next_ty_vars(tpt.generics.type_param_defs()), regions)
} else if ty_substs_len > user_ty_param_count {
let expected = if user_ty_param_req < user_ty_param_count {
"expected at most"
Expand All @@ -3860,7 +3851,7 @@ pub fn instantiate_path(fcx: &FnCtxt,
(span,
format!("too many type parameters provided: {} {}, found {}",
expected, user_ty_param_count, ty_substs_len));
(fcx.infcx().next_ty_vars(ty_param_count), regions)
(fcx.infcx().next_ty_vars(tpt.generics.type_param_defs()), regions)
} else if ty_substs_len < user_ty_param_req {
let expected = if user_ty_param_req < user_ty_param_count {
"expected at least"
Expand All @@ -3871,7 +3862,7 @@ pub fn instantiate_path(fcx: &FnCtxt,
(span,
format!("not enough type parameters provided: {} {}, found {}",
expected, user_ty_param_req, ty_substs_len));
(fcx.infcx().next_ty_vars(ty_param_count), regions)
(fcx.infcx().next_ty_vars(tpt.generics.type_param_defs()), regions)
} else {
if ty_substs_len > user_ty_param_req
&& !fcx.tcx().sess.features.default_type_params.get() {
Expand All @@ -3891,7 +3882,7 @@ pub fn instantiate_path(fcx: &FnCtxt,
.enumerate() {
match self_parameter_index {
Some(index) if index == i => {
tps.push(*fcx.infcx().next_ty_vars(1).get(0));
tps.push(fcx.infcx().next_ty_var(None));
pushed = true;
}
_ => {}
Expand All @@ -3915,7 +3906,7 @@ pub fn instantiate_path(fcx: &FnCtxt,
for (i, default) in defaults.skip(ty_substs_len).enumerate() {
match self_parameter_index {
Some(index) if index == i + ty_substs_len => {
substs.tps.push(*fcx.infcx().next_ty_vars(1).get(0));
substs.tps.push(fcx.infcx().next_ty_var(None));
pushed = true;
}
_ => {}
Expand All @@ -3934,7 +3925,7 @@ pub fn instantiate_path(fcx: &FnCtxt,

// If the self parameter goes at the end, insert it there.
if !pushed && self_parameter_index.is_some() {
substs.tps.push(*fcx.infcx().next_ty_vars(1).get(0))
substs.tps.push(fcx.infcx().next_ty_var(None))
}

assert_eq!(substs.tps.len(), ty_param_count)
Expand Down
5 changes: 1 addition & 4 deletions src/librustc/middle/typeck/coherence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,13 +480,10 @@ impl<'a> CoherenceChecker<'a> {
infer::BoundRegionInCoherence(d.name)))
.collect();

let bounds_count = polytype.generics.type_param_defs().len();
let type_parameters = self.inference_context.next_ty_vars(bounds_count);

let substitutions = substs {
regions: ty::NonerasedRegions(region_parameters),
self_ty: None,
tps: type_parameters
tps: self.inference_context.next_ty_vars(polytype.generics.type_param_defs())
};
let monotype = subst(self.crate_context.tcx,
&substitutions,
Expand Down
48 changes: 26 additions & 22 deletions src/librustc/middle/typeck/infer/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,17 @@ use syntax::ast;
use syntax::owned_slice::OwnedSlice;
use syntax::abi;

pub trait Combine {
fn infcx<'a>(&'a self) -> &'a InferCtxt<'a>;
fn tag(&self) -> ~str;
fn a_is_expected(&self) -> bool;
fn trace(&self) -> TypeTrace;
pub trait Combine<'f> {
fn get_ref<'a>(&'a self) -> &'a CombineFields<'f>;
fn tag(&self) -> &'static str;

fn sub<'a>(&'a self) -> Sub<'a>;
fn lub<'a>(&'a self) -> Lub<'a>;
fn glb<'a>(&'a self) -> Glb<'a>;
fn infcx(&self) -> &'f InferCtxt<'f> { self.get_ref().infcx }
fn a_is_expected(&self) -> bool { self.get_ref().a_is_expected }
fn trace(&self) -> TypeTrace { self.get_ref().trace.clone() }

fn sub(&self) -> Sub<'f> { Sub(self.get_ref().clone()) }
fn lub(&self) -> Lub<'f> { Lub(self.get_ref().clone()) }
fn glb(&self) -> Glb<'f> { Glb(self.get_ref().clone()) }

fn mts(&self, a: &ty::mt, b: &ty::mt) -> cres<ty::mt>;
fn contratys(&self, a: ty::t, b: ty::t) -> cres<ty::t>;
Expand Down Expand Up @@ -131,11 +133,11 @@ pub trait Combine {
as_: &ty::substs,
bs: &ty::substs) -> cres<ty::substs> {

fn relate_region_params<C:Combine>(this: &C,
item_def_id: ast::DefId,
a: &ty::RegionSubsts,
b: &ty::RegionSubsts)
-> cres<ty::RegionSubsts> {
fn relate_region_params<'f, C: Combine<'f>>(this: &C,
item_def_id: ast::DefId,
a: &ty::RegionSubsts,
b: &ty::RegionSubsts)
-> cres<ty::RegionSubsts> {
let tcx = this.infcx().tcx;
match (a, b) {
(&ty::ErasedRegions, _) | (_, &ty::ErasedRegions) => {
Expand Down Expand Up @@ -331,7 +333,7 @@ pub struct CombineFields<'a> {
pub trace: TypeTrace,
}

pub fn expected_found<C:Combine,T>(
pub fn expected_found<'f, C: Combine<'f>, T>(
this: &C, a: T, b: T) -> ty::expected_found<T> {
if this.a_is_expected() {
ty::expected_found {expected: a, found: b}
Expand All @@ -340,15 +342,15 @@ pub fn expected_found<C:Combine,T>(
}
}

pub fn eq_tys<C:Combine>(this: &C, a: ty::t, b: ty::t) -> ures {
pub fn eq_tys<'f, C: Combine<'f>>(this: &C, a: ty::t, b: ty::t) -> ures {
let suber = this.sub();
this.infcx().try(|| {
suber.tys(a, b).and_then(|_ok| suber.contratys(a, b)).to_ures()
})
}

pub fn eq_regions<C:Combine>(this: &C, a: ty::Region, b: ty::Region)
-> ures {
pub fn eq_regions<'f, C: Combine<'f>>(this: &C, a: ty::Region, b: ty::Region)
-> ures {
debug!("eq_regions({}, {})",
a.repr(this.infcx().tcx),
b.repr(this.infcx().tcx));
Expand All @@ -368,9 +370,11 @@ pub fn eq_regions<C:Combine>(this: &C, a: ty::Region, b: ty::Region)
})
}

pub fn super_fn_sigs<C:Combine>(this: &C, a: &ty::FnSig, b: &ty::FnSig) -> cres<ty::FnSig> {
pub fn super_fn_sigs<'f, C: Combine<'f>>(this: &C, a: &ty::FnSig, b: &ty::FnSig)
-> cres<ty::FnSig> {

fn argvecs<C:Combine>(this: &C, a_args: &[ty::t], b_args: &[ty::t]) -> cres<Vec<ty::t> > {
fn argvecs<'f, C: Combine<'f>>(this: &C, a_args: &[ty::t], b_args: &[ty::t])
-> cres<Vec<ty::t> > {
if a_args.len() == b_args.len() {
result::collect(a_args.iter().zip(b_args.iter())
.map(|(a, b)| this.args(*a, *b)))
Expand All @@ -393,7 +397,7 @@ pub fn super_fn_sigs<C:Combine>(this: &C, a: &ty::FnSig, b: &ty::FnSig) -> cres<
variadic: a.variadic})
}

pub fn super_tys<C:Combine>(this: &C, a: ty::t, b: ty::t) -> cres<ty::t> {
pub fn super_tys<'f, C: Combine<'f>>(this: &C, a: ty::t, b: ty::t) -> cres<ty::t> {
let tcx = this.infcx().tcx;
let a_sty = &ty::get(a).sty;
let b_sty = &ty::get(b).sty;
Expand Down Expand Up @@ -575,7 +579,7 @@ pub fn super_tys<C:Combine>(this: &C, a: ty::t, b: ty::t) -> cres<ty::t> {
_ => Err(ty::terr_sorts(expected_found(this, a, b)))
};

fn unify_integral_variable<C:Combine>(
fn unify_integral_variable<'f, C: Combine<'f>>(
this: &C,
vid_is_expected: bool,
vid: ty::IntVid,
Expand All @@ -588,7 +592,7 @@ pub fn super_tys<C:Combine>(this: &C, a: ty::t, b: ty::t) -> cres<ty::t> {
}
}

fn unify_float_variable<C:Combine>(
fn unify_float_variable<'f, C: Combine<'f>>(
this: &C,
vid_is_expected: bool,
vid: ty::FloatVid,
Expand Down
Loading