- hir::ExprRange(ref start, ref end) => {
- let t_start = start.as_ref().map(|e| {
- check_expr(fcx, &e);
- fcx.expr_ty(&e)
- });
- let t_end = end.as_ref().map(|e| {
- check_expr(fcx, &e);
- fcx.expr_ty(&e)
- });
-
- let idx_type = match (t_start, t_end) {
- (Some(ty), None) | (None, Some(ty)) => {
- Some(ty)
- }
- (Some(t_start), Some(t_end)) if (t_start.references_error() ||
- t_end.references_error()) => {
- Some(fcx.tcx().types.err)
- }
- (Some(t_start), Some(t_end)) => {
- Some(infer::common_supertype(fcx.infcx(),
- TypeOrigin::RangeExpression(expr.span),
- true,
- t_start,
- t_end))
- }
- _ => None
- };
-
- // Note that we don't check the type of start/end satisfy any
- // bounds because right now the range structs do not have any. If we add
- // some bounds, then we'll need to check `t_start` against them here.
-
- let range_type = match idx_type {
- Some(idx_type) if idx_type.references_error() => {
- fcx.tcx().types.err
- }
- Some(idx_type) => {
- // Find the did from the appropriate lang item.
- let did = match (start, end) {
- (&Some(_), &Some(_)) => tcx.lang_items.range_struct(),
- (&Some(_), &None) => tcx.lang_items.range_from_struct(),
- (&None, &Some(_)) => tcx.lang_items.range_to_struct(),
- (&None, &None) => {
- tcx.sess.span_bug(expr.span, "full range should be dealt with above")
- }
- };
-
- if let Some(did) = did {
- let def = tcx.lookup_adt_def(did);
- let predicates = tcx.lookup_predicates(did);
- let substs = Substs::new_type(vec![idx_type], vec![]);
- let bounds = fcx.instantiate_bounds(expr.span, &substs, &predicates);
- fcx.add_obligations_for_parameters(
- traits::ObligationCause::new(expr.span,
- fcx.body_id,
- traits::ItemObligation(did)),
- &bounds);
-
- tcx.mk_struct(def, tcx.mk_substs(substs))
- } else {
- span_err!(tcx.sess, expr.span, E0236, "no lang item for range syntax");
- fcx.tcx().types.err
- }
- }
- None => {
- // Neither start nor end => RangeFull
- if let Some(did) = tcx.lang_items.range_full_struct() {
- tcx.mk_struct(
- tcx.lookup_adt_def(did),
- tcx.mk_substs(Substs::empty())
- )
- } else {
- span_err!(tcx.sess, expr.span, E0237, "no lang item for range syntax");
- fcx.tcx().types.err
- }
- }
- };
-
- fcx.write_ty(id, range_type);
- }
-